From 1cfe4b03152f18e830444805121088beef7c9158 Mon Sep 17 00:00:00 2001
From: Oliver Davies <oliver@oliverdavies.dev>
Date: Fri, 3 Jan 2025 18:00:00 +0000
Subject: [PATCH] Refactor snippet configuration

---
 nvim/lua/opdavies/snippets/ft/javascript.lua |  10 --
 nvim/lua/opdavies/snippets/ft/lua.lua        |  16 ---
 nvim/lua/opdavies/snippets/ft/php.lua        | 125 -------------------
 nvim/lua/opdavies/snippets/ft/rst.lua        |  49 --------
 nvim/lua/opdavies/snippets/ft/scss.lua       |  10 --
 nvim/lua/opdavies/snippets/ft/yaml.lua       |  39 ------
 nvim/lua/opdavies/snippets/javascript.lua    |  12 ++
 nvim/lua/opdavies/snippets/lua.lua           |  23 ++++
 nvim/lua/opdavies/snippets/php.lua           | 124 ++++++++++++++++++
 nvim/lua/opdavies/snippets/rst.lua           |  63 ++++++++++
 nvim/lua/opdavies/snippets/scss.lua          |  12 ++
 nvim/lua/opdavies/snippets/yaml.lua          |  47 +++++++
 nvim/plugin/completion.lua                   |  78 ------------
 nvim/plugin/snippets.lua                     |  32 +++++
 14 files changed, 313 insertions(+), 327 deletions(-)
 delete mode 100644 nvim/lua/opdavies/snippets/ft/javascript.lua
 delete mode 100644 nvim/lua/opdavies/snippets/ft/lua.lua
 delete mode 100644 nvim/lua/opdavies/snippets/ft/php.lua
 delete mode 100644 nvim/lua/opdavies/snippets/ft/rst.lua
 delete mode 100644 nvim/lua/opdavies/snippets/ft/scss.lua
 delete mode 100644 nvim/lua/opdavies/snippets/ft/yaml.lua
 create mode 100644 nvim/lua/opdavies/snippets/javascript.lua
 create mode 100644 nvim/lua/opdavies/snippets/lua.lua
 create mode 100644 nvim/lua/opdavies/snippets/php.lua
 create mode 100644 nvim/lua/opdavies/snippets/rst.lua
 create mode 100644 nvim/lua/opdavies/snippets/scss.lua
 create mode 100644 nvim/lua/opdavies/snippets/yaml.lua
 create mode 100644 nvim/plugin/snippets.lua

diff --git a/nvim/lua/opdavies/snippets/ft/javascript.lua b/nvim/lua/opdavies/snippets/ft/javascript.lua
deleted file mode 100644
index d3e795f8..00000000
--- a/nvim/lua/opdavies/snippets/ft/javascript.lua
+++ /dev/null
@@ -1,10 +0,0 @@
-local fmta = require("luasnip.extras.fmt").fmta
-local ls = require "luasnip"
-
-local i = ls.insert_node
-
-local M = {
-  log = fmta("console.log(<>);", { i(1, "value") }),
-}
-
-return M
diff --git a/nvim/lua/opdavies/snippets/ft/lua.lua b/nvim/lua/opdavies/snippets/ft/lua.lua
deleted file mode 100644
index 18c28e5b..00000000
--- a/nvim/lua/opdavies/snippets/ft/lua.lua
+++ /dev/null
@@ -1,16 +0,0 @@
-local ls = require "luasnip"
-
-local fmt = require("luasnip.extras.fmt").fmt
-
-local f, i = ls.function_node, ls.insert_node
-
-return {
-  req = fmt([[local {} = require "{}"]], {
-    f(function(import_name)
-      local parts = vim.split(import_name[1][1], ".", true)
-
-      return parts[#parts] or ""
-    end, { 1 }),
-    i(1),
-  }),
-}
diff --git a/nvim/lua/opdavies/snippets/ft/php.lua b/nvim/lua/opdavies/snippets/ft/php.lua
deleted file mode 100644
index 5d43d7d1..00000000
--- a/nvim/lua/opdavies/snippets/ft/php.lua
+++ /dev/null
@@ -1,125 +0,0 @@
-local fmta = require("luasnip.extras.fmt").fmta
-local ls = require "luasnip"
-
-local c = ls.choice_node
-local f = ls.function_node
-local i = ls.insert_node
-local t = ls.text_node
-
-local M = {
-  __construct = fmta(
-    [[
-    public function __construct(<>) {
-      <>
-    }
-  ]],
-    { i(1), i(0) }
-  ),
-
-  __invoke = fmta(
-    [[
-    public function __invoke(<>) {
-      <>
-    }
-  ]],
-    { i(1), i(0) }
-  ),
-
-  drupalclass = fmta(
-    [[
-    <<?php
-
-    declare(strict_types=1);
-
-    namespace <>;
-
-    final class <> {
-
-      <>
-
-    }]],
-    {
-      f(function()
-        local filepath = vim.fn.expand "%:h"
-        local filepath_parts = vim.fn.split(filepath, "/")
-
-        if not vim.tbl_contains(filepath_parts, "src") then
-          return ""
-        end
-
-        local namespace_parts = { "Drupal" }
-
-        local is_test_file = vim.tbl_contains(filepath_parts, "tests")
-        if is_test_file then
-          table.insert(namespace_parts, "Tests")
-        end
-
-        -- Find and add the module name.
-        for k, v in ipairs(filepath_parts) do
-          if v == "src" then
-            if is_test_file then
-              table.insert(namespace_parts, filepath_parts[k - 2])
-            else
-              table.insert(namespace_parts, filepath_parts[k - 1])
-            end
-          end
-        end
-
-        -- Add the rest of the namespace.
-        local namespace = vim.split(filepath, "src/")
-        local final_part = (namespace[2] or ""):gsub("/", "\\")
-        table.insert(namespace_parts, final_part)
-
-        return table.concat(namespace_parts, "\\")
-      end),
-      f(function()
-        return vim.fn.expand "%:t:r"
-      end),
-      i(0),
-    }
-  ),
-
-  func = fmta("function <>(<>)<> {\n  <>\n}<>", { i(1), i(2), i(3), i(4), i(0) }),
-
-  met = fmta(
-    [[
-    <> function <>(<>)<> {
-      <>
-    }<>
-    ]],
-    { c(1, { t "public", t "protected", t "private" }), i(2), i(3), i(4), i(5), i(0) }
-  ),
-
-  pest = fmta("<>('<>', function() {\n  <>\n});", { c(1, { t "it", t "test" }), i(2), i(0) }),
-
-  test = fmta(
-    [[
-    public function test<>(): void {
-      <>
-    }<>
-    ]],
-    { i(1), i(2), i(0) }
-  ),
-
-  testa = fmta(
-    [[
-    /** @test */
-    public function <>(): void {
-      <>
-    }<>
-    ]],
-    { i(1), i(2), i(0) }
-  ),
-
-  testat = fmta(
-    [[
-    [#Test]
-    public function <>(): void {
-      <>
-    }<>
-    ]],
-    { i(1), i(2), i(0) }
-  ),
-}
-
-return M
diff --git a/nvim/lua/opdavies/snippets/ft/rst.lua b/nvim/lua/opdavies/snippets/ft/rst.lua
deleted file mode 100644
index 206bbf1c..00000000
--- a/nvim/lua/opdavies/snippets/ft/rst.lua
+++ /dev/null
@@ -1,49 +0,0 @@
-local fmta = require("luasnip.extras.fmt").fmta
-local ls = require "luasnip"
-
-local i = ls.insert_node
-local f = ls.function_node
-
-local fill_line = function(char)
-  return function()
-    local row = vim.api.nvim_win_get_cursor(0)[1]
-    local lines = vim.api.nvim_buf_get_lines(0, row - 2, row, false)
-    return string.rep(char, #lines[1])
-  end
-end
-
-local M = {
-  class = { ".. class:: ", i(1) },
-  footer = { ".. footer:: ", i(1) },
-  link = { ".. _", i(1), ":" },
-  raw = { ".. raw:: ", i(1) },
-
-  -- TODO: add an optional new line and ":width" property.
-  image = { ".. image:: ", i(1) },
-
-  head = f(fill_line "=", {}),
-  sub = f(fill_line "-", {}),
-  subsub = f(fill_line "^", {}),
-
-  -- Add a page break with an optional page template.
-  pb = fmta(
-    [[
-    .. raw:: pdf
-
-       PageBreak<>
-    ]],
-    { i(0) }
-  ),
-
-  -- Add a new speaker note.
-  ta = fmta(
-    [[
-    .. raw:: pdf
-
-       TextAnnotation "<>"
-    ]],
-    { i(0) }
-  ),
-}
-
-return M
diff --git a/nvim/lua/opdavies/snippets/ft/scss.lua b/nvim/lua/opdavies/snippets/ft/scss.lua
deleted file mode 100644
index 846cbcb2..00000000
--- a/nvim/lua/opdavies/snippets/ft/scss.lua
+++ /dev/null
@@ -1,10 +0,0 @@
-local fmta = require("luasnip.extras.fmt").fmta
-local ls = require "luasnip"
-
-local i = ls.insert_node
-
-local M = {
-  bp = fmta("@include breakpoint(<>) {\n  <>\n}", { i(1), i(0) }),
-}
-
-return M
diff --git a/nvim/lua/opdavies/snippets/ft/yaml.lua b/nvim/lua/opdavies/snippets/ft/yaml.lua
deleted file mode 100644
index 4c5b3ce2..00000000
--- a/nvim/lua/opdavies/snippets/ft/yaml.lua
+++ /dev/null
@@ -1,39 +0,0 @@
-local fmta = require("luasnip.extras.fmt").fmta
-local ls = require "luasnip"
-local rep = require("luasnip.extras").rep
-
-local c = ls.choice_node
-local i = ls.insert_node
-local t = ls.text_node
-
-local M = {
-  drupal_info = fmta(
-    [[
-    name: <module_name>
-    description: <description>
-    core_version_requirement: ^10 || ^11
-    type: <type>
-    package: <package>
-    ]],
-    { module_name = i(1), description = i(2), type = c(3, { t "module", t "theme" }), package = i(0) }
-  ),
-
-  drupal_route = fmta(
-    [[
-    <module>.<route>:
-      path: /<path>
-      defaults:
-        _controller: Drupal\<module_same>\Controller\<class>
-        # _form:
-        # _title:
-        # _title_callback:
-      methods: [GET]
-      requirements:
-        _permission: access content
-        # _access: TRUE<finish>
-    ]],
-    { module = i(1), route = i(2), path = i(3), module_same = rep(1), class = i(4), finish = i(0) }
-  ),
-}
-
-return M
diff --git a/nvim/lua/opdavies/snippets/javascript.lua b/nvim/lua/opdavies/snippets/javascript.lua
new file mode 100644
index 00000000..c5af9696
--- /dev/null
+++ b/nvim/lua/opdavies/snippets/javascript.lua
@@ -0,0 +1,12 @@
+require("luasnip.session.snippet_collection").clear_snippets "javascript"
+
+local ls = require "luasnip"
+
+local i = ls.insert_node
+local s = ls.snippet
+
+local fmta = require("luasnip.extras.fmt").fmta
+
+ls.add_snippets("javascript", {
+  s("log", fmta("console.log(<>);", { i(1, "value") })),
+})
diff --git a/nvim/lua/opdavies/snippets/lua.lua b/nvim/lua/opdavies/snippets/lua.lua
new file mode 100644
index 00000000..e7eb8cd1
--- /dev/null
+++ b/nvim/lua/opdavies/snippets/lua.lua
@@ -0,0 +1,23 @@
+require("luasnip.session.snippet_collection").clear_snippets "lua"
+
+local ls = require "luasnip"
+
+local f = ls.function_node
+local i = ls.insert_node
+local s = ls.snippet
+
+local fmt = require("luasnip.extras.fmt").fmt
+
+ls.add_snippets("lua", {
+  s(
+    "req",
+    fmt([[local {} = require "{}"]], {
+      f(function(import_name)
+        local parts = vim.split(import_name[1][1], ".", true)
+
+        return parts[#parts] or ""
+      end, { 1 }),
+      i(1),
+    })
+  ),
+})
diff --git a/nvim/lua/opdavies/snippets/php.lua b/nvim/lua/opdavies/snippets/php.lua
new file mode 100644
index 00000000..ee1b898e
--- /dev/null
+++ b/nvim/lua/opdavies/snippets/php.lua
@@ -0,0 +1,124 @@
+require("luasnip.session.snippet_collection").clear_snippets "php"
+
+local ls = require "luasnip"
+
+local c = ls.choice_node
+local f = ls.function_node
+local i = ls.insert_node
+local s = ls.snippet
+local t = ls.text_node
+
+local fmta = require("luasnip.extras.fmt").fmta
+
+ls.add_snippets("php", {
+  s(
+    "drupalclass",
+    fmta(
+      [[
+      <<?php
+
+      declare(strict_types=1);
+
+      namespace <>;
+
+      final class <> {
+
+        <>
+
+      }]],
+      {
+        f(function()
+          local filepath = vim.fn.expand "%:h"
+          local filepath_parts = vim.fn.split(filepath, "/")
+
+          if not vim.tbl_contains(filepath_parts, "src") then
+            return ""
+          end
+
+          local namespace_parts = { "Drupal" }
+
+          local is_test_file = vim.tbl_contains(filepath_parts, "tests")
+          if is_test_file then
+            table.insert(namespace_parts, "Tests")
+          end
+
+          -- Find and add the module name.
+          for k, v in ipairs(filepath_parts) do
+            if v == "src" then
+              if is_test_file then
+                table.insert(namespace_parts, filepath_parts[k - 2])
+              else
+                table.insert(namespace_parts, filepath_parts[k - 1])
+              end
+            end
+          end
+
+          -- Add the rest of the namespace.
+          local namespace = vim.split(filepath, "src/")
+          local final_part = (namespace[2] or ""):gsub("/", "\\")
+          table.insert(namespace_parts, final_part)
+
+          return table.concat(namespace_parts, "\\")
+        end),
+        f(function()
+          return vim.fn.expand "%:t:r"
+        end),
+        i(0),
+      }
+    )
+  ),
+
+  s("func", fmta("function <>(<>)<> {\n  <>\n}<>", { i(1), i(2), i(3), i(4), i(0) })),
+
+  s(
+    "met",
+    fmta(
+      [[
+      <> function <>(<>)<> {
+        <>
+      }<>
+      ]],
+      { c(1, { t "public", t "protected", t "private" }), i(2), i(3), i(4), i(5), i(0) }
+    )
+  ),
+
+  s("pest", fmta("<>('<>', function() {\n  <>\n});", { c(1, { t "it", t "test" }), i(2), i(0) })),
+
+  s(
+    "test",
+    fmta(
+      [[
+      public function test<>(): void {
+        <>
+      }<>
+      ]],
+      { i(1), i(2), i(0) }
+    )
+  ),
+
+  s(
+    "testan",
+    fmta(
+      [[
+      /** @test */
+      public function <>(): void {
+        <>
+      }<>
+      ]],
+      { i(1), i(2), i(0) }
+    )
+  ),
+
+  s(
+    "testat",
+    fmta(
+      [[
+      [#Test]
+      public function <>(): void {
+        <>
+      }<>
+      ]],
+      { i(1), i(2), i(0) }
+    )
+  ),
+})
diff --git a/nvim/lua/opdavies/snippets/rst.lua b/nvim/lua/opdavies/snippets/rst.lua
new file mode 100644
index 00000000..954dfc8f
--- /dev/null
+++ b/nvim/lua/opdavies/snippets/rst.lua
@@ -0,0 +1,63 @@
+require("luasnip.session.snippet_collection").clear_snippets "rst"
+
+local ls = require "luasnip"
+
+local i = ls.insert_node
+local f = ls.function_node
+local s = ls.snippet
+local t = ls.text_node
+
+local fmta = require("luasnip.extras.fmt").fmta
+
+local fill_line = function(char)
+  return function()
+    local row = vim.api.nvim_win_get_cursor(0)[1]
+    local lines = vim.api.nvim_buf_get_lines(0, row - 2, row, false)
+    return string.rep(char, #lines[1])
+  end
+end
+
+ls.add_snippets("rst", {
+  s("class", t(".. class:: ", i(1))),
+
+  s("footer", t(".. footer:: ", i(1))),
+
+  s("link", t(".. _", i(1), ":")),
+
+  s("raw", t(".. raw:: ", i(1))),
+
+  -- TODO: add an optional new line and ":width" property.
+  s("image", t(".. image:: ", i(1))),
+
+  s("head", f(fill_line "=", {})),
+
+  s("sub", f(fill_line "-", {})),
+
+  s("subsub", f(fill_line "^", {})),
+
+  -- Add a page break with an optional page template.
+  s(
+    "pb",
+    fmta(
+      [[
+      .. raw:: pdf
+
+         PageBreak<>
+      ]],
+      { i(0) }
+    )
+  ),
+
+  -- Add a new speaker note.
+  s(
+    "ta",
+    fmta(
+      [[
+      .. raw:: pdf
+
+         TextAnnotation "<>"
+      ]],
+      { i(0) }
+    )
+  ),
+})
diff --git a/nvim/lua/opdavies/snippets/scss.lua b/nvim/lua/opdavies/snippets/scss.lua
new file mode 100644
index 00000000..35520859
--- /dev/null
+++ b/nvim/lua/opdavies/snippets/scss.lua
@@ -0,0 +1,12 @@
+require("luasnip.session.snippet_collection").clear_snippets "scss"
+
+local ls = require "luasnip"
+
+local i = ls.insert_node
+local s = ls.snippet
+
+local fmta = require("luasnip.extras.fmt").fmta
+
+ls.add_snippets("scss", {
+  s("bp", fmta("@include breakpoint(<>) {\n  <>\n}", { i(1), i(0) })),
+})
diff --git a/nvim/lua/opdavies/snippets/yaml.lua b/nvim/lua/opdavies/snippets/yaml.lua
new file mode 100644
index 00000000..399a3072
--- /dev/null
+++ b/nvim/lua/opdavies/snippets/yaml.lua
@@ -0,0 +1,47 @@
+require("luasnip.session.snippet_collection").clear_snippets "yaml"
+
+local ls = require "luasnip"
+
+local c = ls.choice_node
+local i = ls.insert_node
+local s = ls.snippet
+local t = ls.text_node
+
+local fmta = require("luasnip.extras.fmt").fmta
+local rep = require("luasnip.extras").rep
+
+ls.add_snippets("yaml", {
+  s(
+    "drupal_info",
+    fmta(
+      [[
+      name: <module_name>
+      description: <description>
+      core_version_requirement: ^10 || ^11
+      type: <type>
+      package: <package>
+      ]],
+      { module_name = i(1), description = i(2), type = c(3, { t "module", t "theme" }), package = i(0) }
+    )
+  ),
+
+  s(
+    "drupal_route",
+    fmta(
+      [[
+      <module>.<route>:
+        path: /<path>
+        defaults:
+          _controller: Drupal\<module_same>\Controller\<class>
+          # _form:
+          # _title:
+          # _title_callback:
+        methods: [GET]
+        requirements:
+          _permission: access content
+          # _access: TRUE<finish>
+      ]],
+      { module = i(1), route = i(2), path = i(3), module_same = rep(1), class = i(4), finish = i(0) }
+    )
+  ),
+})
diff --git a/nvim/plugin/completion.lua b/nvim/plugin/completion.lua
index ab21c534..dd12d8d4 100644
--- a/nvim/plugin/completion.lua
+++ b/nvim/plugin/completion.lua
@@ -75,81 +75,3 @@ cmp.setup.filetype({ "mysql", "sql" }, {
     { name = "buffer" },
   },
 })
-
-local snippet = ls.snippet
-local i = ls.insert_node
-local t = ls.text_node
-
-local shortcut = function(val)
-  if type(val) == "string" then
-    return { t { val }, i(0) }
-  end
-
-  if type(val) == "table" then
-    for k, v in ipairs(val) do
-      if type(v) == "string" then
-        val[k] = t { v }
-      end
-    end
-  end
-
-  return val
-end
-
-local make = function(tbl)
-  local result = {}
-  for k, v in pairs(tbl) do
-    table.insert(result, (snippet({ trig = k, desc = v.desc }, shortcut(v))))
-  end
-
-  return result
-end
-
-local snippets = {}
-
-for _, ft_path in ipairs(vim.api.nvim_get_runtime_file("lua/opdavies/snippets/ft/*.lua", true)) do
-  local ft = vim.fn.fnamemodify(ft_path, ":t:r")
-  snippets[ft] = make(loadfile(ft_path)())
-
-  ls.add_snippets(ft, snippets[ft])
-end
-
-ls.add_snippets("js", snippets.javascript)
-ls.add_snippets("typescript", snippets.javascript)
-ls.add_snippets("vue", snippets.javascript)
-
--- Include any snippets to use in presentations.
-for _, ft_path in ipairs(vim.api.nvim_get_runtime_file("lua/opdavies/snippets/talks/*.lua", true)) do
-  loadfile(ft_path)()
-end
-
-require("luasnip.loaders.from_vscode").lazy_load()
-
-ls.config.set_config {
-  enable_autosnippets = true,
-  history = true,
-  updateevents = "TextChanged,TextChangedI",
-}
-
--- Expand the current item or just to the next item within the snippet.
-vim.keymap.set({ "i", "s" }, "<c-k>", function()
-  if ls.expand_or_jumpable() then
-    ls.expand_or_jump()
-  end
-end, { silent = true })
-
--- Jump backwards.
-vim.keymap.set({ "i", "s" }, "<c-j>", function()
-  if ls.jumpable(-1) then
-    ls.jump(-1)
-  end
-end, { silent = true })
-
--- Select within a list of options.
-vim.keymap.set("i", "<c-l>", function()
-  if ls.choice_active() then
-    ls.change_choice(1)
-  end
-end)
-
-vim.keymap.set("n", "<leader><leader>s", "<cmd>source ~/Code/opdavies.nvim/after/plugin/luasnip.lua<CR>")
diff --git a/nvim/plugin/snippets.lua b/nvim/plugin/snippets.lua
new file mode 100644
index 00000000..c483d35a
--- /dev/null
+++ b/nvim/plugin/snippets.lua
@@ -0,0 +1,32 @@
+local ls = require "luasnip"
+
+ls.config.set_config {
+  enable_autosnippets = true,
+  history = true,
+  updateevents = "TextChanged,TextChangedI",
+}
+
+for _, ft_path in ipairs(vim.api.nvim_get_runtime_file("lua/opdavies/snippets/*.lua", true)) do
+  loadfile(ft_path)()
+end
+
+-- Expand the current item or just to the next item within the snippet.
+vim.keymap.set({ "i", "s" }, "<c-k>", function()
+  if ls.expand_or_jumpable() then
+    ls.expand_or_jump()
+  end
+end, { silent = true })
+
+-- Jump backwards.
+vim.keymap.set({ "i", "s" }, "<c-j>", function()
+  if ls.jumpable(-1) then
+    ls.jump(-1)
+  end
+end, { silent = true })
+
+-- Select within a list of options.
+vim.keymap.set("i", "<c-l>", function()
+  if ls.choice_active() then
+    ls.change_choice(1)
+  end
+end)