add luasnip and nvim-cmp

This commit is contained in:
niuzhaolong 2025-05-20 23:59:53 +08:00
parent 4de50097c8
commit 3d87579b90

View File

@ -0,0 +1,101 @@
-- complete-functions etc
-- and snippets
--
return {
{
"L3MON4D3/LuaSnip",
-- follow latest release.
version = "v2.*", -- Replace <CurrentMajor> by the latest released major (first number of latest release)
-- install jsregexp (optional!).
build = "make install_jsregexp"
},
{
"hrsh7th/nvim-cmp",
dependencies = {
"hrsh7th/cmp-buffer", -- 缓冲区补全
"hrsh7th/cmp-path", -- 文件路径补全
"hrsh7th/cmp-cmdline", -- 命令行补全
"saadparwaiz1/cmp_luasnip", -- LuaSnip 集成
"L3MON4D3/LuaSnip", -- 代码片段引擎
"rafamadriz/friendly-snippets", -- 预定义片段库
"onsails/lspkind.nvim", -- 美化补全菜单图标
},
event = "InsertEnter", -- 插入模式时加载
config = function()
local cmp = require("cmp")
local luasnip = require("luasnip")
local lspkind = require("lspkind")
-- 加载友好代码片段库
require("luasnip.loaders.from_vscode").lazy_load()
cmp.setup({
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body) -- 启用 LuaSnip
end,
},
mapping = cmp.mapping.preset.insert({
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(), -- 触发补全
["<C-e>"] = cmp.mapping.abort(), -- 关闭补全
['<CR>'] = cmp.mapping(function(fallback)
if cmp.visible() then
if luasnip.expandable() then
luasnip.expand()
else
cmp.confirm({
select = true,
})
end
else
fallback()
end
end),
-- 超级Tab (片段跳转支持)
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
}),
sources = cmp.config.sources({
{ name = "luasnip" }, -- LuaSnip 片段
{ name = "nvim_lsp" }, -- LSP 补全
{ name = "path" }, -- 文件路径
{ name = "buffer" }, -- 缓冲区内容
}),
-- 美化补全菜单(图标 + 类型提示)
formatting = {
format = lspkind.cmp_format({
mode = "symbol_text",
maxwidth = 50,
ellipsis_char = "...",
}),
},
})
-- 命令行补全
cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = "cmdline" },
}),
})
end,
},
}