From 3d87579b90b986ece66bbe0cfd982839b2b86c08 Mon Sep 17 00:00:00 2001 From: niuzhaolong Date: Tue, 20 May 2025 23:59:53 +0800 Subject: [PATCH] add luasnip and nvim-cmp --- lua/plugins/complete-func.lua | 101 ++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 lua/plugins/complete-func.lua diff --git a/lua/plugins/complete-func.lua b/lua/plugins/complete-func.lua new file mode 100644 index 0000000..f27ef66 --- /dev/null +++ b/lua/plugins/complete-func.lua @@ -0,0 +1,101 @@ +-- complete-functions etc +-- and snippets +-- +return { + { + "L3MON4D3/LuaSnip", + -- follow latest release. + version = "v2.*", -- Replace 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({ + [""] = cmp.mapping.scroll_docs(-4), + [""] = cmp.mapping.scroll_docs(4), + [""] = cmp.mapping.complete(), -- 触发补全 + [""] = cmp.mapping.abort(), -- 关闭补全 + [''] = 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 (片段跳转支持) + [""] = 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" }), + [""] = 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, + }, +}