From 9628ff30b92b0e7ce24c4c5909f99ea32240f988 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Tue, 3 May 2022 10:11:00 +0200 Subject: [PATCH] nvim: Move cmp configuration to its own file While it is lsp adjacent, this muddles the waters too much and the configuration file was way too long. --- nvim/.config/nvim/lua/plug/_cmp.lua | 99 +++++++++++++++++++++++++++++ nvim/.config/nvim/lua/plug/_lsp.lua | 89 -------------------------- nvim/.config/nvim/lua/plugins.lua | 2 +- 3 files changed, 100 insertions(+), 90 deletions(-) create mode 100644 nvim/.config/nvim/lua/plug/_cmp.lua diff --git a/nvim/.config/nvim/lua/plug/_cmp.lua b/nvim/.config/nvim/lua/plug/_cmp.lua new file mode 100644 index 0000000..244cb49 --- /dev/null +++ b/nvim/.config/nvim/lua/plug/_cmp.lua @@ -0,0 +1,99 @@ +local cmp = require 'cmp' +local lspkind = require 'lspkind' + +local has_words_before = function() + local line, col = unpack(vim.api.nvim_win_get_cursor(0)) + return col ~= 0 and + vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, + col) + :match("%s") == nil +end + +local feedkey = function(key, mode) + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), + mode, true) +end + +vim.o.completeopt = "menu,menuone,noselect" + +-- completion items +require('cmp').register_source('vCard', require('completion_vcard').setup_cmp( + '~/documents/contacts/myconts')) +vim.g.vsnip_snippet_dir = (vim.env.XDG_DATA_HOME or "~/.local/share") .. + "/nvim/snippets" +cmp.setup({ + snippet = {expand = function(args) vim.fn["vsnip#anonymous"](args.body) end}, + mapping = { + [''] = cmp.mapping(cmp.mapping.scroll_docs(-4), {'i', 'c'}), + [''] = cmp.mapping(cmp.mapping.scroll_docs(4), {'i', 'c'}), + [''] = cmp.mapping(cmp.mapping.complete(), {'i', 'c'}), + [''] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `` mapping. + [''] = 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 = false}), + [""] = 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() + 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"}) + }, + formatting = { + format = lspkind.cmp_format({ + with_text = false, + menu = ({ + buffer = "B", + nvim_lua = "NLua", + tmux = "τ", + vCard = "VCARD" + }) + }) + }, + sources = cmp.config.sources({ + {name = 'path'}, {name = 'nvim_lsp'}, {name = 'treesitter'}, + {name = 'tmux'}, {name = 'vsnip'}, {name = 'latex_symbols'}, + {name = 'pandoc_references'}, {name = 'vCard'} + }, {{name = 'buffer'}, {name = 'spell'}}) +}) +if vim.o.ft == 'clap_input' and vim.o.ft == 'guihua' and vim.o.ft == + 'guihua_rust' then require'cmp'.setup.buffer {completion = {enable = false}} end + +-- Use buffer source for `/` search +cmp.setup.cmdline('/', { + mapping = cmp.mapping.preset.cmdline(), + sources = {{name = 'buffer'}}} +) + +-- Use cmdline & path source for ':' in vim +cmp.setup.cmdline(':', { + mapping = cmp.mapping.preset.cmdline(), + sources = cmp.config.sources({{name = 'path'}}, {{name = 'cmdline'}}) +}) + +cmp.setup.filetype('gitcommit', { +sources = cmp.config.sources({ + { name = 'cmp_git' }, -- You can specify the `cmp_git` source if you were installed it. +}, { + { name = 'buffer' }, +}) +}) + +require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol + .make_client_capabilities()) diff --git a/nvim/.config/nvim/lua/plug/_lsp.lua b/nvim/.config/nvim/lua/plug/_lsp.lua index ccbbd85..9503e62 100644 --- a/nvim/.config/nvim/lua/plug/_lsp.lua +++ b/nvim/.config/nvim/lua/plug/_lsp.lua @@ -1,92 +1,3 @@ -local cmp = require 'cmp' -local lspkind = require 'lspkind' - -local has_words_before = function() - local line, col = unpack(vim.api.nvim_win_get_cursor(0)) - return col ~= 0 and - vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, - col) - :match("%s") == nil -end - -local feedkey = function(key, mode) - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), - mode, true) -end - -vim.o.completeopt = "menu,menuone,noselect" - --- completion items -require('cmp').register_source('vCard', require('completion_vcard').setup_cmp( - '~/documents/contacts/myconts')) -vim.g.vsnip_snippet_dir = (vim.env.XDG_DATA_HOME or "~/.local/share") .. - "/nvim/snippets" -cmp.setup({ - snippet = {expand = function(args) vim.fn["vsnip#anonymous"](args.body) end}, - mapping = { - [''] = cmp.mapping(cmp.mapping.scroll_docs(-4), {'i', 'c'}), - [''] = cmp.mapping(cmp.mapping.scroll_docs(4), {'i', 'c'}), - [''] = cmp.mapping(cmp.mapping.complete(), {'i', 'c'}), - [''] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `` mapping. - [''] = 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 = false}), - [""] = 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() - 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"}) - }, - formatting = { - format = lspkind.cmp_format({ - with_text = false, - menu = ({ - buffer = "B", - nvim_lua = "NLua", - tmux = "τ", - vCard = "VCARD" - }) - }) - }, - sources = cmp.config.sources({ - {name = 'path'}, {name = 'nvim_lsp'}, {name = 'treesitter'}, - {name = 'tmux'}, {name = 'vsnip'}, {name = 'latex_symbols'}, - {name = 'pandoc_references'}, {name = 'vCard'} - }, {{name = 'buffer'}, {name = 'spell'}}) -}) -if vim.o.ft == 'clap_input' and vim.o.ft == 'guihua' and vim.o.ft == - 'guihua_rust' then require'cmp'.setup.buffer {completion = {enable = false}} end - --- Use buffer source for `/` search -cmp.setup.cmdline('/', {sources = {{name = 'buffer'}}}) - --- Use cmdline & path source for ':' in vim -cmp.setup.cmdline(':', { - sources = cmp.config.sources({{name = 'path'}}, {{name = 'cmdline'}}) -}) - -require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol - .make_client_capabilities()) - --- requires the lua-language-server package to be installed -- The arch package defaults to the following directory local sumneko_root_path = "/usr/share/lua-language-server" require'navigator'.setup({ diff --git a/nvim/.config/nvim/lua/plugins.lua b/nvim/.config/nvim/lua/plugins.lua index a9ad28b..42ff713 100644 --- a/nvim/.config/nvim/lua/plugins.lua +++ b/nvim/.config/nvim/lua/plugins.lua @@ -224,6 +224,6 @@ require("packer").startup(function() 'cbarrete/completion-vcard' } } - require('plug._lsp') + require('plug._cmp') end)