neovim: update completion bindings & add iferr snippet
This commit is contained in:
@@ -10,18 +10,18 @@ cmp.setup({
|
|||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
mapping = {
|
mapping = {
|
||||||
["<C-j>"] = cmp.mapping(function()
|
["<C-j>"] = cmp.mapping(function(fallback)
|
||||||
if cmp.visible() then
|
if cmp.visible() then
|
||||||
cmp.select_next_item({ behavior = cmp.SelectBehavior.Select })
|
cmp.select_next_item({ behavior = cmp.SelectBehavior.Select })
|
||||||
else
|
else
|
||||||
ls.jump(1)
|
fallback()
|
||||||
end
|
end
|
||||||
end),
|
end),
|
||||||
["<C-k>"] = cmp.mapping(function()
|
["<C-k>"] = cmp.mapping(function(fallback)
|
||||||
if cmp.visible() then
|
if cmp.visible() then
|
||||||
cmp.select_prev_item({ behavior = cmp.SelectBehavior.Select })
|
cmp.select_prev_item({ behavior = cmp.SelectBehavior.Select })
|
||||||
else
|
else
|
||||||
ls.jump(-1)
|
fallback()
|
||||||
end
|
end
|
||||||
end),
|
end),
|
||||||
["<C-space>"] = cmp.mapping(function()
|
["<C-space>"] = cmp.mapping(function()
|
||||||
@@ -31,10 +31,27 @@ cmp.setup({
|
|||||||
cmp.complete()
|
cmp.complete()
|
||||||
end
|
end
|
||||||
end),
|
end),
|
||||||
["<TAB>"] = cmp.mapping.confirm({
|
["<TAB>"] = cmp.mapping(function(fallback)
|
||||||
select = true,
|
if ls.jumpable(1) then
|
||||||
behavior = cmp.SelectBehavior.Insert,
|
ls.jump(1)
|
||||||
}),
|
else
|
||||||
|
if cmp.visible() then
|
||||||
|
cmp.confirm({
|
||||||
|
select = true,
|
||||||
|
behavior = cmp.SelectBehavior.Insert,
|
||||||
|
})
|
||||||
|
else
|
||||||
|
fallback()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end),
|
||||||
|
["<S-TAB>"] = cmp.mapping(function(fallback)
|
||||||
|
if ls.jumpable(-1) then
|
||||||
|
ls.jump(-1)
|
||||||
|
else
|
||||||
|
fallback()
|
||||||
|
end
|
||||||
|
end),
|
||||||
["<C-S-j>"] = cmp.mapping.scroll_docs(4),
|
["<C-S-j>"] = cmp.mapping.scroll_docs(4),
|
||||||
["<C-S-k>"] = cmp.mapping.scroll_docs(-4),
|
["<C-S-k>"] = cmp.mapping.scroll_docs(-4),
|
||||||
["<C-f>"] = cmp.mapping(function(fallback)
|
["<C-f>"] = cmp.mapping(function(fallback)
|
||||||
@@ -89,3 +106,88 @@ cmp.setup.filetype("gitcommit", {
|
|||||||
})
|
})
|
||||||
|
|
||||||
require("luasnip.loaders.from_vscode").lazy_load()
|
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 = "<>",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
),
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user