diff --git a/lua/custom/completion.lua b/lua/custom/completion.lua index 82d41e6..f8b476c 100644 --- a/lua/custom/completion.lua +++ b/lua/custom/completion.lua @@ -1,7 +1,7 @@ local cmp = require("cmp") local defaults = require("cmp.config.default")() local lspkind = require "lspkind" -lspkind.init {} +require("custom.snippets") cmp.setup({ completion = { @@ -31,4 +31,22 @@ cmp.setup({ { name = "buffer" }, }), 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 + }) }) diff --git a/lua/custom/snippets.lua b/lua/custom/snippets.lua new file mode 100644 index 0000000..44ed10e --- /dev/null +++ b/lua/custom/snippets.lua @@ -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" }, "", function() + return vim.snippet.active { direction = 1 } and vim.snippet.jump(1) +end, { silent = true }) + +vim.keymap.set({ "i", "s" }, "", function() + return vim.snippet.active { direction = -1 } and vim.snippet.jump(-1) +end, { silent = true }) diff --git a/lua/custom/snippets/go.lua b/lua/custom/snippets/go.lua new file mode 100644 index 0000000..b18fd00 --- /dev/null +++ b/lua/custom/snippets/go.lua @@ -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) })), +}) +