From d1a1b650ae5c148e4d695438ad3059f7b2ef43f4 Mon Sep 17 00:00:00 2001 From: YuhangQ Date: Fri, 25 Nov 2022 15:42:27 +0800 Subject: [PATCH] lsp cmp --- init.lua | 2 + lua/basic.lua | 12 +++--- lua/keybindings.lua | 47 +++++++++++++++++++++ lua/lsp/cmp.lua | 57 +++++++++++++++++++++++++ lua/plugins.lua | 14 +++++++ plugin/packer_compiled.lua | 86 ++++++++++++++++++++++++++++---------- test.cpp | 15 +++++++ 7 files changed, 204 insertions(+), 29 deletions(-) create mode 100644 lua/lsp/cmp.lua create mode 100644 test.cpp diff --git a/init.lua b/init.lua index 6f0591a..7e87c8f 100644 --- a/init.lua +++ b/init.lua @@ -27,3 +27,5 @@ require("plugin-config.nvim-treesitter") -- (新增) -- 内置LSP (新增) require("lsp.setup") + +require("lsp.cmp") -- (新增) diff --git a/lua/basic.lua b/lua/basic.lua index 9f929f3..7e6c344 100644 --- a/lua/basic.lua +++ b/lua/basic.lua @@ -9,18 +9,18 @@ vim.wo.number = true vim.wo.relativenumber = true -- 高亮所在行 vim.wo.cursorline = true --- 显示左侧图标指示列 +-- 显示左侧图标指示4 vim.wo.signcolumn = "yes" -- 右侧参考线,超过表示代码太长了,考虑换行 vim.wo.colorcolumn = "80" -- 缩进2个空格等于一个Tab -vim.o.tabstop = 2 -vim.bo.tabstop = 2 -vim.o.softtabstop = 2 +vim.o.tabstop = 4 +vim.bo.tabstop = 4 +vim.o.softtabstop = 4 vim.o.shiftround = true -- >> << 时移动长度 -vim.o.shiftwidth = 2 -vim.bo.shiftwidth = 2 +vim.o.shiftwidth = 4 +vim.bo.shiftwidth = 4 -- 空格替代tab vim.o.expandtab = true vim.bo.expandtab = true diff --git a/lua/keybindings.lua b/lua/keybindings.lua index 297f121..6d372c0 100644 --- a/lua/keybindings.lua +++ b/lua/keybindings.lua @@ -166,4 +166,51 @@ pluginKeys.mapLSP = function(mapbuf) -- mapbuf('n', 'D', 'lua vim.lsp.buf.type_definition()', opt) end + +pluginKeys.cmp = function(cmp) + return { + -- 出现补全 + [""] = cmp.mapping(cmp.mapping.complete(), {"i", "c"}), + -- 取消 + [""] = cmp.mapping({ + i = cmp.mapping.abort(), + c = cmp.mapping.close() + }), + -- 上一个 + [""] = cmp.mapping.select_prev_item(), + -- 下一个 + [""] = cmp.mapping.select_next_item(), + -- 确认 + [""] = cmp.mapping.confirm({ + select = true, + behavior = cmp.ConfirmBehavior.Replace + }), + -- 如果窗口内容太多,可以滚动 + [""] = cmp.mapping(cmp.mapping.scroll_docs(-4), {"i", "c"}), + [""] = cmp.mapping(cmp.mapping.scroll_docs(4), {"i", "c"}), + -- Super Tab + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif vim.fn["vsnip#available"](1) == 1 then + feedkey("(vsnip-expand-or-jump)", "") + elseif has_words_before() then + cmp.complete() + else + fallback() -- The fallback function sends a already mapped key. In this case, it's probably ``. + end + end, {"i", "s"}), + + [""] = cmp.mapping(function() + if cmp.visible() then + cmp.select_prev_item() + elseif vim.fn["vsnip#jumpable"](-1) == 1 then + feedkey("(vsnip-jump-prev)", "") + end + end, {"i", "s"}) + -- end of super Tab + } +end + + return pluginKeys diff --git a/lua/lsp/cmp.lua b/lua/lsp/cmp.lua new file mode 100644 index 0000000..ebf4d60 --- /dev/null +++ b/lua/lsp/cmp.lua @@ -0,0 +1,57 @@ +local cmp = require("cmp") + +cmp.setup({ + -- 指定 snippet 引擎 + snippet = { + expand = function(args) + -- For `vsnip` users. + vim.fn["vsnip#anonymous"](args.body) + + -- For `luasnip` users. + -- require('luasnip').lsp_expand(args.body) + + -- For `ultisnips` users. + -- vim.fn["UltiSnips#Anon"](args.body) + + -- For `snippy` users. + -- require'snippy'.expand_snippet(args.body) + end, + }, + -- 补全源 + sources = cmp.config.sources({ + { name = "nvim_lsp" }, + -- For vsnip users. + { name = "vsnip" }, + + -- For luasnip users. + -- { name = 'luasnip' }, + + --For ultisnips users. + -- { name = 'ultisnips' }, + + -- -- For snippy users. + -- { name = 'snippy' }, + }, { { name = "buffer" }, { name = "path" } }), + + -- 快捷键设置 + mapping = require("keybindings").cmp(cmp), +}) + +-- / 查找模式使用 buffer 源 +cmp.setup.cmdline("/", { + mapping = cmp.mapping.preset.cmdline(), + sources = { + { name = "buffer" }, + }, +}) + +-- : 命令行模式中使用 path 和 cmdline 源. +cmp.setup.cmdline(":", { + mapping = cmp.mapping.preset.cmdline(), + sources = cmp.config.sources({ + { name = "path" }, + }, { + { name = "cmdline" }, + }), +}) + diff --git a/lua/plugins.lua b/lua/plugins.lua index 6306ef9..560f858 100644 --- a/lua/plugins.lua +++ b/lua/plugins.lua @@ -31,6 +31,20 @@ packer.startup({ -- Lspconfig use({ "neovim/nvim-lspconfig" }) + -- 补全引擎 + use("hrsh7th/nvim-cmp") + -- snippet 引擎 + use("hrsh7th/vim-vsnip") + -- 补全源 + use("hrsh7th/cmp-vsnip") + use("hrsh7th/cmp-nvim-lsp") -- { name = nvim_lsp } + use("hrsh7th/cmp-buffer") -- { name = 'buffer' }, + use("hrsh7th/cmp-path") -- { name = 'path' } + use("hrsh7th/cmp-cmdline") -- { name = 'cmdline' } + + -- 常见编程语言代码段 + use("rafamadriz/friendly-snippets") + --------------------- colorschemes -------------------- -- tokyonight use("folke/tokyonight.nvim") diff --git a/plugin/packer_compiled.lua b/plugin/packer_compiled.lua index aaf5534..1f498a2 100644 --- a/plugin/packer_compiled.lua +++ b/plugin/packer_compiled.lua @@ -49,8 +49,8 @@ local function save_profiles(threshold) end time([[Luarocks path setup]], true) -local package_path_str = "/Users/zxd/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/?.lua;/Users/zxd/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/?/init.lua;/Users/zxd/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/?.lua;/Users/zxd/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/?/init.lua" -local install_cpath_pattern = "/Users/zxd/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/lua/5.1/?.so" +local package_path_str = "/Users/yuhangq/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/?.lua;/Users/yuhangq/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/?/init.lua;/Users/yuhangq/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/?.lua;/Users/yuhangq/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/?/init.lua" +local install_cpath_pattern = "/Users/yuhangq/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/lua/5.1/?.so" if not string.find(package.path, package_path_str, 1, true) then package.path = package.path .. ';' .. package_path_str end @@ -76,108 +76,148 @@ time([[Defining packer_plugins]], true) _G.packer_plugins = { ["bufferline.nvim"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/bufferline.nvim", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/bufferline.nvim", url = "https://github.com/akinsho/bufferline.nvim" }, + ["cmp-buffer"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/cmp-buffer", + url = "https://github.com/hrsh7th/cmp-buffer" + }, + ["cmp-cmdline"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/cmp-cmdline", + url = "https://github.com/hrsh7th/cmp-cmdline" + }, + ["cmp-nvim-lsp"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/cmp-nvim-lsp", + url = "https://github.com/hrsh7th/cmp-nvim-lsp" + }, + ["cmp-path"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/cmp-path", + url = "https://github.com/hrsh7th/cmp-path" + }, + ["cmp-vsnip"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/cmp-vsnip", + url = "https://github.com/hrsh7th/cmp-vsnip" + }, ["dashboard-nvim"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/dashboard-nvim", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/dashboard-nvim", url = "https://github.com/glepnir/dashboard-nvim" }, + ["friendly-snippets"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/friendly-snippets", + url = "https://github.com/rafamadriz/friendly-snippets" + }, ["gruvbox.nvim"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/gruvbox.nvim", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/gruvbox.nvim", url = "https://github.com/ellisonleao/gruvbox.nvim" }, ["lualine-lsp-progress"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/lualine-lsp-progress", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/lualine-lsp-progress", url = "https://github.com/arkav/lualine-lsp-progress" }, ["lualine.nvim"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/lualine.nvim", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/lualine.nvim", url = "https://github.com/nvim-lualine/lualine.nvim" }, ["lush.nvim"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/lush.nvim", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/lush.nvim", url = "https://github.com/rktjmp/lush.nvim" }, ["nightfox.nvim"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/nightfox.nvim", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/nightfox.nvim", url = "https://github.com/EdenEast/nightfox.nvim" }, ["nord.nvim"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/nord.nvim", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/nord.nvim", url = "https://github.com/shaunsingh/nord.nvim" }, + ["nvim-cmp"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/nvim-cmp", + url = "https://github.com/hrsh7th/nvim-cmp" + }, ["nvim-lsp-installer"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/nvim-lsp-installer", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/nvim-lsp-installer", url = "https://github.com/williamboman/nvim-lsp-installer" }, ["nvim-lspconfig"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/nvim-lspconfig", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/nvim-lspconfig", url = "https://github.com/neovim/nvim-lspconfig" }, ["nvim-tree.lua"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/nvim-tree.lua", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/nvim-tree.lua", url = "https://github.com/kyazdani42/nvim-tree.lua" }, ["nvim-treesitter"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/nvim-treesitter", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/nvim-treesitter", url = "https://github.com/nvim-treesitter/nvim-treesitter" }, ["nvim-web-devicons"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/nvim-web-devicons", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/nvim-web-devicons", url = "https://github.com/kyazdani42/nvim-web-devicons" }, ["oceanic-next"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/oceanic-next", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/oceanic-next", url = "https://github.com/mhartington/oceanic-next" }, ["onedark.nvim"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/onedark.nvim", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/onedark.nvim", url = "https://github.com/ful1e5/onedark.nvim" }, ["packer.nvim"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/packer.nvim", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/packer.nvim", url = "https://github.com/wbthomason/packer.nvim" }, ["plenary.nvim"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/plenary.nvim", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/plenary.nvim", url = "https://github.com/nvim-lua/plenary.nvim" }, ["project.nvim"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/project.nvim", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/project.nvim", url = "https://github.com/ahmedkhalf/project.nvim" }, ["telescope.nvim"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/telescope.nvim", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/telescope.nvim", url = "https://github.com/nvim-telescope/telescope.nvim" }, ["tokyonight.nvim"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/tokyonight.nvim", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/tokyonight.nvim", url = "https://github.com/folke/tokyonight.nvim" }, ["vim-bbye"] = { loaded = true, - path = "/Users/zxd/.local/share/nvim/site/pack/packer/start/vim-bbye", + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/vim-bbye", url = "https://github.com/moll/vim-bbye" + }, + ["vim-vsnip"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/vim-vsnip", + url = "https://github.com/hrsh7th/vim-vsnip" } } diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..45d61ce --- /dev/null +++ b/test.cpp @@ -0,0 +1,15 @@ +#include +#include +#include +using namespace std; + + + +int main (int argc, char *argv[]) +{ + map mp; + mp.size(); + mp.size(); + + return 0; +}