Refactor snippet configuration

This commit is contained in:
Oliver Davies 2025-01-03 18:00:00 +00:00
parent c5833a9cdc
commit 1cfe4b0315
14 changed files with 313 additions and 327 deletions

View file

@ -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

View file

@ -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),
}),
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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") })),
})

View file

@ -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),
})
),
})

View file

@ -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) }
)
),
})

View file

@ -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) }
)
),
})

View file

@ -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) })),
})

View file

@ -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) }
)
),
})

View file

@ -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>")

32
nvim/plugin/snippets.lua Normal file
View file

@ -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)