From 7a1c3989d1859f3342d769a707027167d83f9429 Mon Sep 17 00:00:00 2001 From: Laily Date: Wed, 28 Sep 2022 11:47:03 +0800 Subject: [PATCH] fix: modify nvim-cmp --- .golangci.yml | 5 + .zshrc | 2 +- nvim/lua/keymappings.lua | 48 ++++----- nvim/lua/pluginconfig/init.lua | 2 +- nvim/lua/pluginconfig/nvim-cmp.lua | 92 ++++++++++++++++ nvim/lua/pluginconfig/nvim-compe.lua | 150 --------------------------- nvim/lua/plugins.lua | 15 ++- 7 files changed, 135 insertions(+), 179 deletions(-) create mode 100644 nvim/lua/pluginconfig/nvim-cmp.lua delete mode 100644 nvim/lua/pluginconfig/nvim-compe.lua diff --git a/.golangci.yml b/.golangci.yml index c08238f..d8df062 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,5 +1,6 @@ run: tests: false + timeout: 1m linters: enable: - govet @@ -12,8 +13,12 @@ linters: - gofmt - stylecheck - revive + - errorlint # finds code that will cause problems with the error wrapping scheme introduced in Go 1.13 disable-all: true linters-settings: + staticcheck: + # SAxxxx checks in https://staticcheck.io/docs/configuration/options/#checks + checks: ["all"] revive: # Maximum number of open files at the same time. # See https://github.com/mgechev/revive#command-line-flags diff --git a/.zshrc b/.zshrc index acdc169..5a06c51 100644 --- a/.zshrc +++ b/.zshrc @@ -134,7 +134,7 @@ alias alert='terminal-notifier -title "Terminal" -message "Done with task! Exit # git alias gitb="git branch --show-current | tr -d '\n' | pbcopy" alias ct="go test -gcflags=all=-l -cover ./..." -alias ci="golangci-lint run ./..." +alias ci="golangci-lint -c=~/.dotfile/.golangci.yml run ./..." alias gt="go test -gcflags=all=-l " alias swg="swag init -o docs -g " alias sws="swagger serve -F=swagger" diff --git a/nvim/lua/keymappings.lua b/nvim/lua/keymappings.lua index b2b0000..cf71e18 100644 --- a/nvim/lua/keymappings.lua +++ b/nvim/lua/keymappings.lua @@ -1,10 +1,10 @@ -vim.api.nvim_set_keymap('n', '-', ':RnvimrToggle', {noremap = true, silent = true}) +vim.api.nvim_set_keymap('n', '-', ':RnvimrToggle', { noremap = true, silent = true }) -- better window movement -vim.api.nvim_set_keymap('n', '', 'h', {silent = true}) -vim.api.nvim_set_keymap('n', '', 'j', {silent = true}) -vim.api.nvim_set_keymap('n', '', 'k', {silent = true}) -vim.api.nvim_set_keymap('n', '', 'l', {silent = true}) +vim.api.nvim_set_keymap('n', '', 'h', { silent = true }) +vim.api.nvim_set_keymap('n', '', 'j', { silent = true }) +vim.api.nvim_set_keymap('n', '', 'k', { silent = true }) +vim.api.nvim_set_keymap('n', '', 'l', { silent = true }) -- better edit vim.api.nvim_set_keymap('n', 'Y', 'y$', {}) @@ -25,27 +25,27 @@ vim.api.nvim_set_keymap('n', 'Y', 'y$', {}) -- TODO fix this -- resize with arrows -vim.api.nvim_set_keymap('n', '', ':resize -2', {silent = true}) -vim.api.nvim_set_keymap('n', '', ':resize +2', {silent = true}) -vim.api.nvim_set_keymap('n', '', ':vertical resize -2', {silent = true}) -vim.api.nvim_set_keymap('n', '', ':vertical resize +2', {silent = true}) +vim.api.nvim_set_keymap('n', '', ':resize -2', { silent = true }) +vim.api.nvim_set_keymap('n', '', ':resize +2', { silent = true }) +vim.api.nvim_set_keymap('n', '', ':vertical resize -2', { silent = true }) +vim.api.nvim_set_keymap('n', '', ':vertical resize +2', { silent = true }) -- better indenting -vim.api.nvim_set_keymap('v', '<', '', '>gv', {noremap = true, silent = true}) +vim.api.nvim_set_keymap('v', '<', '', '>gv', { noremap = true, silent = true }) -- I hate escape -vim.api.nvim_set_keymap('i', 'jk', '', {noremap = true, silent = true}) -vim.api.nvim_set_keymap('i', 'kj', '', {noremap = true, silent = true}) -vim.api.nvim_set_keymap('i', 'jj', '', {noremap = true, silent = true}) +vim.api.nvim_set_keymap('i', 'jk', '', { noremap = true, silent = true }) +vim.api.nvim_set_keymap('i', 'kj', '', { noremap = true, silent = true }) +vim.api.nvim_set_keymap('i', 'jj', '', { noremap = true, silent = true }) -- Tab switch buffer -vim.api.nvim_set_keymap('n', '', ':bnext', {noremap = true, silent = true}) -vim.api.nvim_set_keymap('n', '', ':bprevious', {noremap = true, silent = true}) +vim.api.nvim_set_keymap('n', '', ':bnext', { noremap = true, silent = true }) +vim.api.nvim_set_keymap('n', '', ':bprevious', { noremap = true, silent = true }) -- Move selected line / block of text in visual mode -vim.api.nvim_set_keymap('x', 'K', ':move \'<-2gv-gv', {noremap = true, silent = true}) -vim.api.nvim_set_keymap('x', 'J', ':move \'>+1gv-gv', {noremap = true, silent = true}) +vim.api.nvim_set_keymap('x', 'K', ':move \'<-2gv-gv', { noremap = true, silent = true }) +vim.api.nvim_set_keymap('x', 'J', ':move \'>+1gv-gv', { noremap = true, silent = true }) -- Better nav for omnicomplete vim.cmd('inoremap (\"\\\")') @@ -69,13 +69,13 @@ vim.cmd('vnoremap P "0P') -- ]]) -- Toggle the QuickFix window -vim.api.nvim_set_keymap('', '', ':call QuickFixToggle()', {noremap = true, silent = true}) +vim.api.nvim_set_keymap('', '', ':call QuickFixToggle()', { noremap = true, silent = true }) -- nvim-compe key map -vim.cmd('inoremap compe#complete()') -vim.cmd("inoremap compe#confirm('')") -vim.cmd("inoremap compe#close('')") -vim.cmd("inoremap compe#scroll({ 'delta': +4 })") -vim.cmd("inoremap compe#scroll({ 'delta': -4 })") +-- vim.cmd('inoremap compe#complete()') +-- vim.cmd("inoremap compe#confirm('')") +-- vim.cmd("inoremap compe#close('')") +-- vim.cmd("inoremap compe#scroll({ 'delta': +4 })") +-- vim.cmd("inoremap compe#scroll({ 'delta': -4 })") diff --git a/nvim/lua/pluginconfig/init.lua b/nvim/lua/pluginconfig/init.lua index 55dd92b..1b87db1 100644 --- a/nvim/lua/pluginconfig/init.lua +++ b/nvim/lua/pluginconfig/init.lua @@ -12,7 +12,7 @@ require('pluginconfig/neotest') require('pluginconfig/numb') require('pluginconfig/nvim-autopairs') require('pluginconfig/nvim-colorizer') -require('pluginconfig/nvim-compe') +require('pluginconfig/nvim-cmp') require('pluginconfig/fzf-lua') require('pluginconfig/nvim-tree') -- This plugin must be required somewhere before colorscheme. Placing it after will break navigation keymappings require('pluginconfig/nvim-treesitter') diff --git a/nvim/lua/pluginconfig/nvim-cmp.lua b/nvim/lua/pluginconfig/nvim-cmp.lua new file mode 100644 index 0000000..57ba65c --- /dev/null +++ b/nvim/lua/pluginconfig/nvim-cmp.lua @@ -0,0 +1,92 @@ +local lspkind = require('lspkind') +local cmp = require 'cmp' + +local function keymapping(cmp) + return { + -- 上一个 + [''] = cmp.mapping.select_prev_item(), + -- 下一个 + [''] = cmp.mapping.select_next_item(), + -- 出现补全 + [''] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }), + -- 取消 + [''] = cmp.mapping({ + i = cmp.mapping.abort(), + c = cmp.mapping.close(), + }), + -- 确认 + -- Accept currently selected item. If none selected, `select` first item. + -- Set `select` to `false` to only confirm explicitly selected items. + [''] = cmp.mapping.confirm({ + select = true, + behavior = cmp.ConfirmBehavior.Replace + }), + -- [''] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `` mapping. + [''] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }), + [''] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }), + } +end + +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 = keymapping(cmp), + -- 使用lspkind-nvim显示类型图标 + formatting = { + format = lspkind.cmp_format({ + with_text = true, -- do not show text alongside icons + maxwidth = 50, -- prevent the popup from showing more than provided characters (e.g 50 will not show more than 50 characters) + before = function(entry, vim_item) + -- Source 显示提示来源 + vim_item.menu = "[" .. string.upper(entry.source.name) .. "]" + return vim_item + end + }) + }, +} + +-- Use buffer source for `/`. +cmp.setup.cmdline('/', { + sources = { + { name = 'buffer' } + } +}) + +-- Use cmdline & path source for ':'. +cmp.setup.cmdline(':', { + sources = cmp.config.sources({ + { name = 'path' } + }, { + { name = 'cmdline' } + }) +}) diff --git a/nvim/lua/pluginconfig/nvim-compe.lua b/nvim/lua/pluginconfig/nvim-compe.lua deleted file mode 100644 index 2070e14..0000000 --- a/nvim/lua/pluginconfig/nvim-compe.lua +++ /dev/null @@ -1,150 +0,0 @@ -vim.o.completeopt = "menuone,noselect" - -require'compe'.setup { - enabled = true, - autocomplete = true, - debug = false, - min_length = 1, - preselect = 'enable', - throttle_time = 80, - source_timeout = 200, - incomplete_delay = 400, - max_abbr_width = 100, - max_kind_width = 100, - max_menu_width = 100, - documentation = true, - - source = { - path = {kind = "  (Path)"}, - buffer = {kind = "  (Buffer)"}, - calc = {kind = "  (Calc)"}, - vsnip = {kind = "  (Snippet)"}, - nvim_lsp = {kind = "  (LSP)"}, - -- nvim_lua = {kind = "  "}, - nvim_lua = false, - spell = {kind = "  (Spell)"}, - tags = false, - vim_dadbod_completion = true, - -- snippets_nvim = {kind = "  "}, - -- ultisnips = {kind = "  "}, - -- treesitter = {kind = "  "}, - emoji = {kind = " ﲃ (Emoji)", filetypes={"markdown", "text"}} - -- for emoji press : (idk if that in compe tho) - } -} - ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  --- ﬘ ---  ---  ---  --- m ---  ---  ---  ---  - --- local t = function(str) --- return vim.api.nvim_replace_termcodes(str, true, true, true) --- end - --- local check_back_space = function() --- local col = vim.fn.col('.') - 1 --- if col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then --- return true --- else --- return false --- end --- end - --- -- Use (s-)tab to: --- --- move to prev/next item in completion menuone --- --- jump to prev/next snippet's placeholder --- _G.tab_complete = function() --- if vim.fn.pumvisible() == 1 then --- return t "" --- elseif vim.fn.call("vsnip#available", {1}) == 1 then --- return t "(vsnip-expand-or-jump)" --- elseif check_back_space() then --- return t "" --- else --- return vim.fn['compe#complete']() --- end --- end --- _G.s_tab_complete = function() --- if vim.fn.pumvisible() == 1 then --- return t "" --- elseif vim.fn.call("vsnip#jumpable", {-1}) == 1 then --- return t "(vsnip-jump-prev)" --- else --- return t "" --- end --- end - --- vim.api.nvim_set_keymap("i", "", "v:lua.tab_complete()", {expr = true}) --- vim.api.nvim_set_keymap("s", "", "v:lua.tab_complete()", {expr = true}) --- vim.api.nvim_set_keymap("i", "", "v:lua.s_tab_complete()", {expr = true}) --- vim.api.nvim_set_keymap("s", "", "v:lua.s_tab_complete()", {expr = true}) - - - - - - - -local t = function(str) - return vim.api.nvim_replace_termcodes(str, true, true, true) -end - -local check_back_space = function() - local col = vim.fn.col('.') - 1 - if col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then - return true - else - return false - end -end - --- Use (s-)tab to: ---- move to prev/next item in completion menuone ---- jump to prev/next snippet's placeholder -_G.tab_complete = function() - if vim.fn.pumvisible() == 1 then - return t "" - elseif vim.fn.call("vsnip#available", {1}) == 1 then - return t "(vsnip-expand-or-jump)" - elseif check_back_space() then - return t "" - else - return vim.fn['compe#complete']() - end -end -_G.s_tab_complete = function() - if vim.fn.pumvisible() == 1 then - return t "" - elseif vim.fn.call("vsnip#jumpable", {-1}) == 1 then - return t "(vsnip-jump-prev)" - else - return t "" - end -end - -vim.api.nvim_set_keymap("i", "", "v:lua.tab_complete()", {expr = true}) -vim.api.nvim_set_keymap("s", "", "v:lua.tab_complete()", {expr = true}) -vim.api.nvim_set_keymap("i", "", "v:lua.s_tab_complete()", {expr = true}) -vim.api.nvim_set_keymap("s", "", "v:lua.s_tab_complete()", {expr = true}) diff --git a/nvim/lua/plugins.lua b/nvim/lua/plugins.lua index 39ea6ca..5b1df58 100644 --- a/nvim/lua/plugins.lua +++ b/nvim/lua/plugins.lua @@ -60,8 +60,18 @@ return require("packer").startup(function(use) -- Debugging use { "mfussenegger/nvim-dap", opt = true } - -- Autocomplete - use { "hrsh7th/nvim-compe", opt = true } + -- nvim-cmp + 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 'hrsh7th/nvim-cmp' + -- vsnip + use 'hrsh7th/cmp-vsnip' -- { name = 'vsnip' } + use 'hrsh7th/vim-vsnip' + use 'rafamadriz/friendly-snippets' + -- lspkind + use 'onsails/lspkind-nvim' -- Treesitter use { "nvim-treesitter/nvim-treesitter", run = ":TSUpdate" } @@ -155,7 +165,6 @@ return require("packer").startup(function(use) require_plugin("telescope.nvim") require_plugin('telescope-project.nvim') require_plugin("nvim-dap") - require_plugin("nvim-compe") require_plugin("nvim-ts-autotag") require_plugin('vim-matchup') require_plugin("nvim-tree.lua")