From afc0bcd1f153fcab769c160f7d025d1ce03af4df Mon Sep 17 00:00:00 2001 From: Donovan Glover Date: Sun, 3 Dec 2023 07:59:48 -0500 Subject: [PATCH] neovim: Add autocompletion support with nvim-cmp Did not expect this to work on the first try but it did. Taken directly from the nvim-lspconfig wiki. https://github.com/neovim/nvim-lspconfig/wiki/Autocompletion --- home/neovim.nix | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/home/neovim.nix b/home/neovim.nix index c1ca747..0dfa2d9 100644 --- a/home/neovim.nix +++ b/home/neovim.nix @@ -271,6 +271,73 @@ in }) ''; } + { + plugin = nvim-cmp; + type = "lua"; + config = /* lua */ '' + -- Add additional capabilities supported by nvim-cmp + local capabilities = require("cmp_nvim_lsp").default_capabilities() + + local lspconfig = require('lspconfig') + + -- Enable some language servers with the additional completion capabilities offered by nvim-cmp + local servers = { 'clangd', 'rust_analyzer', 'pyright', 'tsserver' } + for _, lsp in ipairs(servers) do + lspconfig[lsp].setup { + -- on_attach = my_custom_on_attach, + capabilities = capabilities, + } + end + + -- luasnip setup + local luasnip = require 'luasnip' + + -- nvim-cmp setup + local cmp = require 'cmp' + cmp.setup { + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + mapping = cmp.mapping.preset.insert({ + [''] = cmp.mapping.scroll_docs(-4), -- Up + [''] = cmp.mapping.scroll_docs(4), -- Down + -- C-b (back) C-f (forward) for snippet placeholder navigation. + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.confirm { + behavior = cmp.ConfirmBehavior.Replace, + select = true, + }, + [''] = 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 = { + { name = 'nvim_lsp' }, + { name = 'luasnip' }, + }, + } + ''; + } + cmp-nvim-lsp + cmp_luasnip + luasnip { plugin = nvim-base16; type = "lua";