feat: add snippet support

Add go snippets (finally)
This commit is contained in:
Simon Lasbrugnas 2024-11-11 02:51:54 +01:00
parent 85a92af060
commit 1cf776744d
Signed by untrusted user who does not match committer: simon
GPG key ID: 86039876BA6ED8DE
3 changed files with 120 additions and 1 deletions

View file

@ -1,7 +1,7 @@
local cmp = require("cmp") local cmp = require("cmp")
local defaults = require("cmp.config.default")() local defaults = require("cmp.config.default")()
local lspkind = require "lspkind" local lspkind = require "lspkind"
lspkind.init {} require("custom.snippets")
cmp.setup({ cmp.setup({
completion = { completion = {
@ -31,4 +31,22 @@ cmp.setup({
{ name = "buffer" }, { name = "buffer" },
}), }),
sorting = defaults.sorting, sorting = defaults.sorting,
format = lspkind.cmp_format({
mode = 'text_symbol', -- show only symbol annotations
maxwidth = {
-- prevent the popup from showing more than provided characters (e.g 50 will not show more than 50 characters)
-- can also be a function to dynamically calculate max width such as
-- menu = function() return math.floor(0.45 * vim.o.columns) end,
menu = 50, -- leading text (labelDetails)
abbr = 50, -- actual suggestion item
},
ellipsis_char = '...', -- when popup menu exceed maxwidth, the truncated part would show ellipsis_char instead (must define maxwidth first)
show_labelDetails = true, -- show labelDetails in menu. Disabled by default
-- The function below will be called before any actual modifications from lspkind
-- so that you can provide more controls on popup customization. (See [#30](https://github.com/onsails/lspkind-nvim/pull/30))
before = function(_, vim_item)
return vim_item
end
})
}) })

48
lua/custom/snippets.lua Normal file
View file

@ -0,0 +1,48 @@
local ls = require "luasnip"
vim.snippet.expand = ls.lsp_expand
---@diagnostic disable-next-line: duplicate-set-field
vim.snippet.active = function(filter)
filter = filter or {}
filter.direction = filter.direction or 1
if filter.direction == 1 then
return ls.expand_or_jumpable()
else
return ls.jumpable(filter.direction)
end
end
---@diagnostic disable-next-line: duplicate-set-field
vim.snippet.jump = function(direction)
if direction == 1 then
if ls.expandable() then
return ls.expand_or_jump()
else
return ls.jumpable(1) and ls.jump(1)
end
else
return ls.jumpable(-1) and ls.jump(-1)
end
end
vim.snippet.stop = ls.unlink_current
ls.config.set_config {
history = true,
updateevents = "TextChanged,TextChangedI",
override_builtin = true,
}
for _, ft_path in ipairs(vim.api.nvim_get_runtime_file("lua/custom/snippets/*.lua", true)) do
loadfile(ft_path)()
end
vim.keymap.set({ "i", "s" }, "<c-j>", function()
return vim.snippet.active { direction = 1 } and vim.snippet.jump(1)
end, { silent = true })
vim.keymap.set({ "i", "s" }, "<c-k>", function()
return vim.snippet.active { direction = -1 } and vim.snippet.jump(-1)
end, { silent = true })

View file

@ -0,0 +1,53 @@
require("luasnip.session.snippet_collection").clear_snippets "go"
local ls = require "luasnip"
local s = ls.snippet
local i = ls.insert_node
local fmt = require("luasnip.extras.fmt").fmt
ls.add_snippets("go", {
s("fmt debug", fmt('fmt.Println("[DEBUG] {}", {})', { i(1), i(2) })),
s("fmt info", fmt('fmt.Println("[INFO] {}", {})', { i(1), i(2) })),
s("fmt error", fmt('fmt.Println("[ERROR] {}", {})', { i(1), i(2) })),
s("fmt warn", fmt('fmt.Println("[WARN] {}", {})', { i(1), i(2) })),
s("if err !=", fmt([[
if err != nil {{
return err
}}
]], {})),
s("if err :=", fmt([[
if err := {}; err != nil {{
return err
}}
]], { i(1) })),
s("json unmarshal", fmt([[
var {var_name} {}
if err := json.Unmarshal({}, &{var_name}); err != nil {{
return err
}}
]], { var_name = i(1), i(2), i(3) }, { repeat_duplicates = true })),
s("json decode", fmt([[
var {var_name} {}
if err := json.NewDecoder({}).Decode(&{var_name}); err != nil {{
return err
}}
]], { var_name = i(1), i(2), i(3) }, { repeat_duplicates = true })),
s("func", fmt([[
func {}({}) {} {{
{}
}}
]], { i(1), i(2), i(3), i(4) })),
})