From 6f1535f83f1ae05aa4159af9a3850605e93a734b Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Mon, 4 Apr 2022 17:30:26 +0200 Subject: [PATCH] feat: replace coq.nvim with nvim-cmp and luasnip --- nvim/lua/options.lua | 10 ++--- nvim/lua/plugins.lua | 80 +++++++++++++++++++++++++++++++--- nvim/lua/plugins/lspconfig.lua | 25 +++++------ 3 files changed, 88 insertions(+), 27 deletions(-) diff --git a/nvim/lua/options.lua b/nvim/lua/options.lua index 3650b14..87fde64 100644 --- a/nvim/lua/options.lua +++ b/nvim/lua/options.lua @@ -39,6 +39,9 @@ vim.opt.smartcase = true -- preview commands vim.opt.inccommand = "split" +-- completion +vim.opt.completeopt = "menu,menuone,noselect" + -- set cursorline in active window vim.cmd([[ augroup CursorLine @@ -57,10 +60,3 @@ vim.cmd([[ augroup END ]]) --- coq.nvim -vim.g.coq_settings = { - auto_start = "shut-up", - keymap = { - jump_to_mark = "" -- prevent remapping - } -} diff --git a/nvim/lua/plugins.lua b/nvim/lua/plugins.lua index 546a257..4b50156 100644 --- a/nvim/lua/plugins.lua +++ b/nvim/lua/plugins.lua @@ -44,10 +44,7 @@ return require('packer').startup(function(use) config = function() require'tabline'.setup { enable = true, - options = { - show_bufnr = true, - show_filename_only = true, - } + options = {show_bufnr = true, show_filename_only = true} } end, requires = { @@ -87,9 +84,78 @@ return require('packer').startup(function(use) -- autocompletion use({ - "ms-jpq/coq_nvim", - branch = "coq", - requires = {{'ms-jpq/coq.artifacts', branch = 'artifacts'}} + "L3MON4D3/LuaSnip", + requires = {"rafamadriz/friendly-snippets"}, + config = function() + require("luasnip.loaders.from_vscode").lazy_load() + end + }) + use({ + "hrsh7th/nvim-cmp", + requires = { + "hrsh7th/cmp-nvim-lsp", "hrsh7th/cmp-buffer", "hrsh7th/cmp-path", + "hrsh7th/cmp-cmdline", "hrsh7th/cmp-git", "hrsh7th/cmp-nvim-lua", + "saadparwaiz1/cmp_luasnip" + }, + config = function() + 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 cmp = require('cmp') + local luasnip = require("luasnip") + + cmp.setup({ + snippet = { + expand = function(args) + require("luasnip").lsp_expand(args.body) + end + }, + sources = require("cmp").config.sources({ + {name = "nvim_lsp"}, {name = "luasnip"}, {name = "path"}, + {name = "buffer"}, {name = "git"} + }), + mapping = { + [''] = cmp.mapping(cmp.mapping.complete(), + {'i', 'c'}), + [''] = cmp.mapping.confirm({select = true}), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + elseif has_words_before() then + cmp.complete() + 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"}) + } + }) + + -- `/` cmdline setup. + cmp.setup.cmdline('/', {sources = {{name = 'buffer'}}}) + + -- `:` cmdline setup. + cmp.setup.cmdline(':', { + sources = cmp.config + .sources({{name = 'path'}}, {{name = 'cmdline'}}) + }) + + end }) -- highlight current symbol diff --git a/nvim/lua/plugins/lspconfig.lua b/nvim/lua/plugins/lspconfig.lua index 5fe3d15..1e1f401 100644 --- a/nvim/lua/plugins/lspconfig.lua +++ b/nvim/lua/plugins/lspconfig.lua @@ -1,4 +1,3 @@ -local coq = require("coq") local lsp_installer = require("nvim-lsp-installer") local nvim_runtime_path = vim.split(package.path, ';') @@ -6,9 +5,9 @@ table.insert(nvim_runtime_path, "lua/?.lua") table.insert(nvim_runtime_path, "lua/?/init.lua") local language_servers = { - "ansiblels", "bashls", "clangd", "dockerls", "efm", "eslint", "html", "jsonls", - "pyright", "rust_analyzer", "sumneko_lua", "svelte", "taplo", "terraformls", - "tflint", "tsserver", "volar" + "ansiblels", "bashls", "clangd", "dockerls", "efm", "eslint", "html", + "jsonls", "pyright", "rust_analyzer", "sumneko_lua", "svelte", "taplo", + "terraformls", "tflint", "tsserver", "volar" } for _, server_name in pairs(language_servers) do @@ -22,7 +21,8 @@ end local extra_server_opts = { ["efm"] = function(opts) opts.filetypes = { - "lua", "html", "javascript", "markdown", "typescript", "typescriptreact" + "lua", "html", "javascript", "markdown", "typescript", + "typescriptreact" } opts.init_options = {documentFormatting = true} opts.settings = { @@ -67,11 +67,7 @@ local extra_server_opts = { end, ["rust_analyzer"] = function(opts) opts.settings = { - ["rust-analyzer"] = { - checkOnSave = { - command = "clippy" - } - } + ["rust-analyzer"] = {checkOnSave = {command = "clippy"}} } end, ["sumneko_lua"] = function(opts) @@ -134,10 +130,13 @@ local function custom_on_attach(client, buffer_nr) end lsp_installer.on_server_ready(function(server) - local opts = coq.lsp_ensure_capabilities({ + local cmp = require("cmp_nvim_lsp") + + local opts = { on_attach = custom_on_attach, - capabilities = vim.lsp.protocol.make_client_capabilities() - }) + capabilities = cmp.update_capabilities(vim.lsp.protocol + .make_client_capabilities()) + } if extra_server_opts[server.name] then extra_server_opts[server.name](opts)