From 14ced6b533d2ba9fef79711400eeffdb8983ff17 Mon Sep 17 00:00:00 2001 From: Cody Hiar Date: Tue, 16 Nov 2021 21:31:30 -0700 Subject: lsp tab completion --- init.lua | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/init.lua b/init.lua index a3bb5ed..e7ee22f 100644 --- a/init.lua +++ b/init.lua @@ -194,6 +194,10 @@ return require('packer').startup(function() } use { "neovim/nvim-lspconfig", + requires = { + {'hrsh7th/nvim-cmp'}, + {'hrsh7th/cmp-nvim-lsp'}, + }, config = function() nvim_lsp = require('lspconfig') -- Use an on_attach function to only map the following keys @@ -223,21 +227,60 @@ return require('packer').startup(function() buf_set_keymap('n', ']d', 'lua vim.lsp.diagnostic.goto_next()', opts) buf_set_keymap('n', 'q', 'lua vim.lsp.diagnostic.set_loclist()', opts) buf_set_keymap('n', 'f', 'lua vim.lsp.buf.formatting()', opts) - end -- Use a loop to conveniently call 'setup' on multiple servers and -- map buffer local keybindings when the language server attaches -- local servers = { 'pyls', 'rust_analyzer', 'tsserver' } local servers = { 'pyright', 'bashls' } + -- Add additional capabilities supported by nvim-cmp + local capabilities = vim.lsp.protocol.make_client_capabilities() + capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities) + -- Setup language servers for _, lsp in ipairs(servers) do nvim_lsp[lsp].setup { on_attach = on_attach, flags = { debounce_text_changes = 150, }, + capabilities = capabilities, } end + -- Set completeopt to have a better completion experience + vim.o.completeopt = 'menuone,noselect' + -- nvim-cmp setup + local cmp = require 'cmp' + cmp.setup { + mapping = { + [''] = cmp.mapping.select_prev_item(), + [''] = cmp.mapping.select_next_item(), + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.close(), + [''] = cmp.mapping.confirm { + behavior = cmp.ConfirmBehavior.Replace, + select = true, + }, + [''] = function(fallback) + if cmp.visible() then + cmp.select_next_item() + else + fallback() + end + end, + [''] = function(fallback) + if cmp.visible() then + cmp.select_prev_item() + else + fallback() + end + end, + }, + sources = { + { name = 'nvim_lsp' }, + }, + } end, } use 'nvim-treesitter/playground' @@ -377,13 +420,6 @@ return require('packer').startup(function() require'hop'.setup { keys = 'etovxqpdygfblzhckisuran' } end } - use { - 'ervandew/supertab', - config = function() - -- Default to scrolling down the list - vim.g.SuperTabDefaultCompletionType = "" - end - } -- The treesitter for nix doesn't do indents use 'LnL7/vim-nix' use { -- cgit v1.2.3