diff --git a/neovim/.config/nvim/after/plugin/comp.lua b/neovim/.config/nvim/after/plugin/comp.lua index 7adc9d6..ec960ea 100644 --- a/neovim/.config/nvim/after/plugin/comp.lua +++ b/neovim/.config/nvim/after/plugin/comp.lua @@ -10,18 +10,18 @@ cmp.setup({ end, }, mapping = { - [""] = cmp.mapping(function() + [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_next_item({ behavior = cmp.SelectBehavior.Select }) else - ls.jump(1) + fallback() end end), - [""] = cmp.mapping(function() + [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_prev_item({ behavior = cmp.SelectBehavior.Select }) else - ls.jump(-1) + fallback() end end), [""] = cmp.mapping(function() @@ -31,10 +31,27 @@ cmp.setup({ cmp.complete() end end), - [""] = cmp.mapping.confirm({ - select = true, - behavior = cmp.SelectBehavior.Insert, - }), + [""] = cmp.mapping(function(fallback) + if ls.jumpable(1) then + ls.jump(1) + else + if cmp.visible() then + cmp.confirm({ + select = true, + behavior = cmp.SelectBehavior.Insert, + }) + else + fallback() + end + end + end), + [""] = cmp.mapping(function(fallback) + if ls.jumpable(-1) then + ls.jump(-1) + else + fallback() + end + end), [""] = cmp.mapping.scroll_docs(4), [""] = cmp.mapping.scroll_docs(-4), [""] = cmp.mapping(function(fallback) @@ -89,3 +106,88 @@ cmp.setup.filetype("gitcommit", { }) require("luasnip.loaders.from_vscode").lazy_load() + +local s = ls.snippet +local i = ls.insert_node +local t = ls.text_node +local d = ls.dynamic_node +local sn = ls.snippet_node +local fmt = require("luasnip.extras.fmt").fmt + +local ts_utils = require("nvim-treesitter.ts_utils") + +ls.add_snippets("go", { + s( + "iferr", + fmt( + [[ + if err != nil { + return <> + } + ]], + { + d(1, function() + local current_node = ts_utils.get_node_at_cursor(0, true) + if not current_node then + return "" + end + + local func = current_node + while func do + if func:type() == "function_declaration" then + break + end + func = func:parent() + end + + if not func then + return "" + end + + local return_type_node = func:child(3) + if not return_type_node then + return "" + end + + local params = {} + for index = 0, return_type_node:child_count(), 1 do + local param = return_type_node:child(index) + if param then + if param:type() == "parameter_declaration" then + local text = ts_utils.get_node_text(param) + table.insert(params, text[1]) + end + end + end + + local rec = { + ["string"] = '""', + ["int"] = "0", + ["uint"] = "0", + ["error"] = "err", + } + + local size = vim.tbl_count(params) + local x = {} + for index, param in ipairs(params) do + local replace = rec[param] + if replace ~= nil then + table.insert(x, i(index, replace)) + else + table.insert(x, i(index, param)) + end + + if index < size then + table.insert(x, t(", ")) + end + end + + return sn(nil, x) + end, {}), + }, + { + delimiters = "<>", + } + ) + ), +})