From fbdcb2f6ac461d58d63e3bd06fac7489af573c50 Mon Sep 17 00:00:00 2001 From: Patrick Michl Date: Mon, 11 Oct 2021 11:38:44 +0200 Subject: [PATCH] Add cmp for completion support, luasnip for snippets and better syntax highlighting using treesitter --- init.lua | 3 +++ lua/completion.lua | 44 ++++++++++++++++++++++++++++++++++++++++++++ lua/keys.lua | 12 ++++++++++++ lua/lsp.lua | 13 ++++++++++++- lua/plugins.lua | 5 +++++ lua/treesitter.lua | 17 +++++++++++++++++ 6 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 lua/completion.lua create mode 100644 lua/treesitter.lua diff --git a/init.lua b/init.lua index 30854a0..bd81d57 100644 --- a/init.lua +++ b/init.lua @@ -3,7 +3,10 @@ require 'plugins' vim.cmd('colorscheme space-vim-dark') require 'lsp' +require 'completion' +require 'treesitter' require 'debugger' require 'term' require 'settings' require 'keys' + diff --git a/lua/completion.lua b/lua/completion.lua new file mode 100644 index 0000000..f8bbbd1 --- /dev/null +++ b/lua/completion.lua @@ -0,0 +1,44 @@ +local luasnip = require 'luasnip' +local cmp = require 'cmp' + +cmp.setup { + snippet = { + expand = function(args) + require('luasnip').lsp_expand(args.body) + end, + }, + 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 vim.fn.pumvisible() == 1 then + vim.fn.feedkeys(vim.api.nvim_replace_termcodes('', true, true, true), 'n') + elseif luasnip.expand_or_jumpable() then + vim.fn.feedkeys(vim.api.nvim_replace_termcodes('luasnip-expand-or-jump', true, true, true), '') + else + fallback() + end + end, + [''] = function(fallback) + if vim.fn.pumvisible() == 1 then + vim.fn.feedkeys(vim.api.nvim_replace_termcodes('', true, true, true), 'n') + elseif luasnip.jumpable(-1) then + vim.fn.feedkeys(vim.api.nvim_replace_termcodes('luasnip-jump-prev', true, true, true), '') + else + fallback() + end + end, + }, + sources = { + { name = 'nvim_lsp' }, + { name = 'luasnip' }, + }, +} diff --git a/lua/keys.lua b/lua/keys.lua index b52d0df..a6909f1 100644 --- a/lua/keys.lua +++ b/lua/keys.lua @@ -9,3 +9,15 @@ key('', '', ':NERDTreeToggle', {}) key('i', '', 'pumvisible() ? "" : ""', {expr = true, silent = true}) key('i', '', 'pumvisible() ? "" : ""', {expr = true, silent = true}) + +-- Highlight on yank +vim.api.nvim_exec( + [[ + augroup YankHighlight + autocmd! + autocmd TextYankPost * silent! lua vim.highlight.on_yank() + augroup end +]], + false +) + diff --git a/lua/lsp.lua b/lua/lsp.lua index a542740..b3199b1 100644 --- a/lua/lsp.lua +++ b/lua/lsp.lua @@ -23,7 +23,7 @@ local on_attach = function(client, bufnr) buf_set_keymap('n', 'ff', ':lua vim.lsp.buf.formatting()', opts) buf_set_keymap('n', 'gn', 'lua vim.lsp.buf.rename()', opts) - require'completion'.on_attach(client) + --require'completion'.on_attach(client) -- Set highlight colors local highlights = { @@ -49,11 +49,16 @@ local on_attach = function(client, bufnr) ) end +-- nvim-cmp supports additional completion capabilities +local capabilities = vim.lsp.protocol.make_client_capabilities() +capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities) + local servers = require'lspinstall'.installed_servers() for _, lsp in ipairs(servers) do local config = { on_attach = on_attach, + capabilities = capabilities, flags = {debounce_text_changes = 150} } if lsp == "lua" then @@ -61,3 +66,9 @@ for _, lsp in ipairs(servers) do end nvim_lsp[lsp].setup(config) end + + +-- Set completeopt to have a better completion experience +vim.o.completeopt = 'menuone,noselect' + + diff --git a/lua/plugins.lua b/lua/plugins.lua index af18b4a..55f6a9d 100644 --- a/lua/plugins.lua +++ b/lua/plugins.lua @@ -31,6 +31,11 @@ return require('packer').startup({function() use 'nvim-lua/completion-nvim' use 'nvim-lua/popup.nvim' use 'nvim-lua/plenary.nvim' + use 'hrsh7th/nvim-cmp' -- Autocompletion plugin + use 'hrsh7th/cmp-nvim-lsp' + use 'saadparwaiz1/cmp_luasnip' + use 'L3MON4D3/LuaSnip' -- Snippets plugin + use 'nvim-treesitter/nvim-treesitter' -- Debugger -- use 'mfussenegger/nvim-dap' end, diff --git a/lua/treesitter.lua b/lua/treesitter.lua new file mode 100644 index 0000000..6ad0708 --- /dev/null +++ b/lua/treesitter.lua @@ -0,0 +1,17 @@ +require('nvim-treesitter.configs').setup { + highlight = { + enable = true, -- false will disable the whole extension + }, + incremental_selection = { + enable = false, + keymaps = { + init_selection = 'gnn', + node_incremental = 'grn', + scope_incremental = 'grc', + node_decremental = 'grm', + }, + }, + indent = { + enable = true, + } +}