From 0f8b79f10a2f6ce416d0a69f85c6875d7ceede35 Mon Sep 17 00:00:00 2001 From: Nick Zana Date: Mon, 2 May 2022 13:58:27 -0400 Subject: [PATCH] Simplify LSP and remap a few LSP keybindings --- init.lua | 1 + lua/keybindings.lua | 31 +++++-------- lua/lsp.lua | 17 ++++++-- lua/nvim-cmp-cfg.lua | 101 ++++++++++++++++++++----------------------- lua/plugins.lua | 22 ++++------ lua/ts-context.lua | 22 ++++++++++ 6 files changed, 102 insertions(+), 92 deletions(-) create mode 100644 lua/ts-context.lua diff --git a/init.lua b/init.lua index c9a4c0a..4fe16db 100644 --- a/init.lua +++ b/init.lua @@ -3,3 +3,4 @@ require('nvim_opts') -- Basic vim options require('autocmd') -- Convenient Autocommands require('lsp') require('keybindings') +require('nvim-cmp-cfg') diff --git a/lua/keybindings.lua b/lua/keybindings.lua index 92a9e5e..b4044f8 100644 --- a/lua/keybindings.lua +++ b/lua/keybindings.lua @@ -3,28 +3,20 @@ local map = vim.api.nvim_set_keymap -- Avoid infinitely recursive definitions options = { noremap = true } --- Copy/paste -map('n', 'y', '"+y', options) -map('v', 'y', '"+y', options) - -- WINDOW MANAGEMENT --- Move windows TODO - -- Terminal map('t', '', '', options) -- Exit Terminal mode enter Normal -- FILE NAVIGATION --- NerdTree -map('n', 'nt', 'NERDTreeToggle', options) -map('n', '', 'Telescope git_files', options) -map('n', '', 'Telescope find_files', options) +map('n', 'f', 'Telescope git_files', options) +map('n', 'af', 'Telescope find_files', options) -- Grep project file contents with live results, respecting .gitignore -map('n', 'ps', "lua require('telescope.builtin').live_grep()", options) --- Grep for prompted str project wide -map('n', 'fs', "lua require('telescope.builtin').grep_string({ search = vim.fn.input(\"Grep For > \")})", options) +map('n', 'g', "lua require('telescope.builtin').live_grep()", options) -- Telescope fuzzy search for buffers -map('n', 'pb', "lua require('telescope.builtin').buffers()", options) +map('n', 'b', "lua require('telescope.builtin').buffers()", options) + +map('n', 'ga', '', options) -- ga to switch to last used buffer -- CODE NAVIGATOIN @@ -66,17 +58,16 @@ local on_attach = function(client, bufnr) buf_set_keymap('n', 'gD', 'lua vim.lsp.buf.declaration()', options) buf_set_keymap('n', 'gd', 'lua vim.lsp.buf.definition()', options) - buf_set_keymap('n', '', 'lua vim.lsp.buf.hover()', options) - buf_set_keymap('n', 'gi', 'lua vim.lsp.buf.implementation()', options) + buf_set_keymap('n', 'k', 'lua vim.lsp.buf.hover()', options) buf_set_keymap('n', 's', 'lua vim.lsp.buf.signature_help()', options) - buf_set_keymap('n', 'rn', 'lua vim.lsp.buf.rename()', options) - buf_set_keymap('n', 'ga', 'lua vim.lsp.buf.code_action()', { noremap = true }) + buf_set_keymap('n', 'r', 'lua vim.lsp.buf.rename()', options) + buf_set_keymap('n', 'a', 'lua vim.lsp.buf.code_action()', { noremap = true }) buf_set_keymap('n', 'gr', 'lua vim.lsp.buf.references()', options) buf_set_keymap('n', 'd', 'lua vim.lsp.diagnostic.show_line_diagnostics()', options) buf_set_keymap('n', '[d', 'lua vim.lsp.diagnostic.goto_prev()', options) buf_set_keymap('n', ']d', 'lua vim.lsp.diagnostic.goto_next()', options) - buf_set_keymap('n', 'q', 'lua vim.diagnostic.set_qflist()', options) - buf_set_keymap('n', 'f', 'lua vim.lsp.buf.formatting()', options) + buf_set_keymap('n', 'Q', 'lua vim.diagnostic.set_qflist()', options) + buf_set_keymap('n', 'F', 'lua vim.lsp.buf.formatting()', options) end diff --git a/lua/lsp.lua b/lua/lsp.lua index abb1051..06e2d98 100644 --- a/lua/lsp.lua +++ b/lua/lsp.lua @@ -1,12 +1,15 @@ local on_attach = require'keybindings'.on_attach -local capabilities = require'cmp_nvim_lsp'.update_capabilities(vim.lsp.protocol.make_client_capabilities()) -local cmp = require'nvim-cmp-cfg'.cmp +-- Add additional capabilities supported by nvim-cmp +local capabilities = vim.lsp.protocol.make_client_capabilities() +capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities) + +local lspconfig = require('lspconfig') -- SERVERS -- rust_analyzer vim.g.rustfmt_autosave = 1 -require'lspconfig'.rust_analyzer.setup{ - on_attach = on_attach, +lspconfig.rust_analyzer.setup{ + on_attach = on_attach, capabilities = capabilities, settings = { ["rust-analyzer"] = { @@ -20,3 +23,9 @@ require'lspconfig'.rust_analyzer.setup{ } } } + +-- clangd +lspconfig.clangd.setup{ + on_attach = on_attach, + capabilities = capabilities, +} diff --git a/lua/nvim-cmp-cfg.lua b/lua/nvim-cmp-cfg.lua index ff984a5..d2eadec 100644 --- a/lua/nvim-cmp-cfg.lua +++ b/lua/nvim-cmp-cfg.lua @@ -1,56 +1,47 @@ --- TODO: Improve docs --- Setup nvim-cmp. -local cmp = require'cmp' +-- luasnip setup +local luasnip = require 'luasnip' --- Ensure vsnip is installed -require'cmp_vsnip' - -cmp.setup({ - snippet = { - expand = function(args) - vim.fn["vsnip#anonymous"](args.body) - end, - }, - mapping = { - [''] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }), - [''] = cmp.mapping.confirm({ 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 = cmp.config.sources({ - { name = 'nvim_lsp' }, - { name = 'vsnip' }, - }, { - { name = 'buffer' }, - }), -}) - --- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore). -cmp.setup.cmdline('/', { - sources = { - { name = 'buffer' } - } -}) - --- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore). -cmp.setup.cmdline(':', { - sources = cmp.config.sources({ - { name = 'path' } - }, { - { name = 'cmdline' } - }) -}) - -return { cmp = cmp } +-- nvim-cmp setup +local cmp = require 'cmp' +cmp.setup { + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + window = { + completion = cmp.config.window.bordered(), + documentation = cmp.config.window.bordered(), + }, + mapping = cmp.mapping.preset.insert({ + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.scroll_docs(-4), + [''] = 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' }, + }, +} diff --git a/lua/plugins.lua b/lua/plugins.lua index d4d8bab..1cf8893 100644 --- a/lua/plugins.lua +++ b/lua/plugins.lua @@ -11,31 +11,27 @@ local plugins = require('packer').startup(function(use) -- file management use { 'nvim-telescope/telescope.nvim', -- search and select tool - commit = '80cdb00b221f69348afc4fb4b701f51eb8dd3120', -- for neovim 0.5.0 compatibility + commit = '80cdb00b221f69348afc4fb4b701f51eb8dd3120', -- for neovim 0.5.0 compatibility requires = { {'nvim-lua/plenary.nvim'} } } - use 'preservim/nerdtree' -- git use 'tpope/vim-fugitive' -- LSP use 'neovim/nvim-lspconfig' -- Common lsp server configurations - use 'tjdevries/lsp_extensions.nvim' -- Provides type inlay hint support - use 'hrsh7th/cmp-nvim-lsp' -- Completion LSP integeration - use 'hrsh7th/cmp-buffer' - use 'hrsh7th/cmp-path' - use 'hrsh7th/cmp-cmdline' - use 'hrsh7th/nvim-cmp' -- Autocompletion + use 'hrsh7th/nvim-cmp' -- Autocompletion plugin + use 'hrsh7th/cmp-nvim-lsp' -- LSP source for nvim-cmp - -- Snippets (Required for nvim-cmp) - use 'hrsh7th/cmp-vsnip' - use 'hrsh7th/vim-vsnip' + -- LSP Extensions + use 'nvim-lua/lsp_extensions.nvim' + + -- Snippets (required for nvim-cmp) + use 'L3MON4D3/LuaSnip' + use 'saadparwaiz1/cmp_luasnip' -- Language Specfic use 'rust-lang/rust.vim' - use 'cespare/vim-toml' - use 'stephpy/vim-yaml' if packer_bootstrap then require('packer').sync() diff --git a/lua/ts-context.lua b/lua/ts-context.lua new file mode 100644 index 0000000..9dbae6d --- /dev/null +++ b/lua/ts-context.lua @@ -0,0 +1,22 @@ +require('treesitter-context').setup{ + enable = true, -- Enable this plugin (Can be enabled/disabled later via commands) + throttle = true, -- Throttles plugin updates (may improve performance) + max_lines = 0, -- How many lines the window should span. Values <= 0 mean no limit. + patterns = { -- Match patterns for TS nodes. These get wrapped to match at word boundaries. + -- For all filetypes + default = { + 'class', + 'function', + 'method', + 'for', + 'while', + -- 'if', + -- 'switch', + -- 'case', + }, + rust = { + 'loop_expression', + 'impl_item', + }, + }, +}