nvim/cmp: use better default mappings

This commit is contained in:
Primrose 2025-07-09 11:12:39 +02:00
parent 760875108c
commit b6522ffbba
Signed by: primrose
GPG key ID: 4E887A4CA9714ADA

View file

@ -1,15 +1,15 @@
local cmp = require("cmp") local cmp = require("cmp")
local ls = require("luasnip") local luasnip = require("luasnip")
local s = ls.snippet local s = luasnip.snippet
local sn = ls.snippet_node local sn = luasnip.snippet_node
local t = ls.text_node local t = luasnip.text_node
local cr = function() return t { "", "" } end -- linebreak local cr = function() return t { "", "" } end -- linebreak
local i = ls.insert_node local i = luasnip.insert_node
local f = ls.function_node local f = luasnip.function_node
local c = ls.choice_node local c = luasnip.choice_node
local d = ls.dynamic_node local d = luasnip.dynamic_node
local r = ls.restore_node local r = luasnip.restore_node
local l = require("luasnip.extras").lambda local l = require("luasnip.extras").lambda
local rep = require("luasnip.extras").rep local rep = require("luasnip.extras").rep
local p = require("luasnip.extras").partial local p = require("luasnip.extras").partial
@ -36,13 +36,13 @@ require("luasnip.loaders.from_vscode").lazy_load { paths = { "./snippets" } }
---------- ----------
-- Init -- -- Init --
---------- ----------
ls.setup { update_events = { "TextChanged", "TextChangedI" } } luasnip.setup { update_events = { "TextChanged", "TextChangedI" } }
----------- -----------
-- Typst -- -- Typst --
----------- -----------
local function show_date_typst_entry() return os.date('"%Y-%m-%d %H:%M:%S"') end local function show_date_typst_entry() return os.date('"%Y-%m-%d %H:%M:%S"') end
ls.add_snippets("typst", { luasnip.add_snippets("typst", {
s("entry", { s("entry", {
t("#entry("), t("#entry("),
f(show_date_typst_entry), f(show_date_typst_entry),
@ -91,7 +91,7 @@ local function right()
end end
-- stylua: ignore start -- stylua: ignore start
ls.add_snippets("all", { luasnip.add_snippets("all", {
s("banner", { s("banner", {
f(horizon, { 1 }), cr(), f(horizon, { 1 }), cr(),
f(left), i(1), f(right), cr(), f(left), i(1), f(right), cr(),
@ -104,13 +104,13 @@ ls.add_snippets("all", {
-- Haskell -- -- Haskell --
------------- -------------
local haskell_snippets = require("haskell-snippets").all local haskell_snippets = require("haskell-snippets").all
ls.add_snippets("haskell", haskell_snippets, { key = "haskell" }) luasnip.add_snippets("haskell", haskell_snippets, { key = "haskell" })
------------ ------------
-- Golang -- -- Golang --
------------ ------------
-- stylua: ignore start -- stylua: ignore start
ls.add_snippets("go", { luasnip.add_snippets("go", {
s("ie", { s("ie", {
t("if err != nil {"), cr(), t("if err != nil {"), cr(),
t("\t"), i(0), cr(), t("\t"), i(0), cr(),
@ -134,57 +134,9 @@ end
cmp.setup { cmp.setup {
snippet = { snippet = {
expand = function(args) ls.lsp_expand(args.body) end, expand = function(args) luasnip.lsp_expand(args.body) end,
},
mapping = cmp.mapping.preset.insert {
["<Tab>"] = cmp.mapping(function(fallback) -- Next or jump
if cmp.visible() then
cmp.select_next_item()
elseif ls.expand_or_locally_jumpable() then
ls.expand_or_jump()
elseif has_words_before() and (not of_filetype { "ledger" }) then
cmp.complete()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback) -- Previous
if cmp.visible() then
cmp.select_prev_item()
elseif ls.jumpable(-1) then
ls.jump(-1)
else
fallback()
end
end, { "i", "s" }),
["<A-Tab>"] = cmp.mapping(function(fallback) -- Force jump
if ls.expand_or_locally_jumpable() then
ls.expand_or_jump()
else
fallback()
end
end, { "i", "s" }),
["<CR>"] = cmp.mapping(function(fallback) -- Confirm
if cmp.visible() then
cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Insert,
select = true,
}()
else
fallback()
end
end, { "i", "s" }),
["<S-CR>"] = cmp.mapping(function(fallback) -- Confirm and replace
if cmp.visible() then
cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Replace,
select = true,
}()
else
fallback()
end
end, { "i", "s" }),
}, },
mapping = cmp.mapping.preset.insert(),
sources = { sources = {
{ name = "luasnip" }, { name = "luasnip" },
{ name = "nvim_lsp" }, { name = "nvim_lsp" },