From e1ac175333d8b8410b34ca6f32dde8b82e75b449 Mon Sep 17 00:00:00 2001 From: Noah Masur <7386960+nmasur@users.noreply.github.com> Date: Mon, 11 Jul 2022 02:47:54 +0000 Subject: [PATCH] lsp and completion tweaks --- modules/editor/neovim/default.nix | 2 +- modules/editor/neovim/lua/keybinds.lua | 2 +- .../editor/neovim/lua/packer/completion.lua | 34 +++++++++++++++++-- modules/editor/neovim/lua/packer/lsp.lua | 25 +++++++++++--- modules/programming/lua.nix | 8 ++--- 5 files changed, 58 insertions(+), 13 deletions(-) diff --git a/modules/editor/neovim/default.nix b/modules/editor/neovim/default.nix index 794d1f6..b3b0447 100644 --- a/modules/editor/neovim/default.nix +++ b/modules/editor/neovim/default.nix @@ -35,7 +35,7 @@ # Always run packer.nvim sync home.activation.nvimPackerSync = config.home-manager.users.${config.user}.lib.dag.entryAfter - [ "writeBoundary" ] '' + [ "onFilesChange" ] '' $DRY_RUN_CMD nvim +PackerSync +qa ''; diff --git a/modules/editor/neovim/lua/keybinds.lua b/modules/editor/neovim/lua/keybinds.lua index 4e1a8c1..35606d8 100644 --- a/modules/editor/neovim/lua/keybinds.lua +++ b/modules/editor/neovim/lua/keybinds.lua @@ -74,7 +74,7 @@ key("n", "gr", telescope.lsp_references, { silent = true }) key("n", "R", vim.lsp.buf.rename, { silent = true }) key("n", "]e", vim.diagnostic.goto_next, { silent = true }) key("n", "[e", vim.diagnostic.goto_prev, { silent = true }) -key("n", "e", vim.lsp.diagnostic.show_line_diagnostics, { silent = true }) +key("n", "e", vim.diagnostic.open_float, { silent = true }) key("n", "E", vim.lsp.buf.code_action, { silent = true }) -- File commands diff --git a/modules/editor/neovim/lua/packer/completion.lua b/modules/editor/neovim/lua/packer/completion.lua index 7461988..c21e69b 100644 --- a/modules/editor/neovim/lua/packer/completion.lua +++ b/modules/editor/neovim/lua/packer/completion.lua @@ -21,11 +21,15 @@ M.packer = function(use) config = function() local cmp = require("cmp") cmp.setup({ + + -- Setup snippet completion snippet = { expand = function(args) require("luasnip").lsp_expand(args.body) end, }, + + -- Setup completion keybinds mapping = { [""] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }), [""] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }), @@ -36,8 +40,6 @@ M.packer = function(use) }) vim.cmd("stopinsert") --- Abort and leave insert mode end, - -- [''] = cmp.mapping(cmp.mapping.select_next_item(), { 'i', 's' }), - -- [''] = cmp.mapping(cmp.mapping.select_prev_item(), { 'i', 's' }), [""] = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Insert, select = true, @@ -52,11 +54,13 @@ M.packer = function(use) end end, { "i", "s" }), }, + + -- Setup completion engines sources = { { name = "nvim_lua" }, { name = "nvim_lsp" }, - { name = "path" }, { name = "luasnip" }, + { name = "path" }, { name = "buffer", keyword_length = 3, max_item_count = 10 }, { name = "rg", @@ -65,6 +69,30 @@ M.packer = function(use) option = { additional_arguments = "--ignore-case" }, }, }, + + -- Visual presentation + formatting = { + fields = { "abbr", "menu" }, + format = function(entry, vim_item) + vim_item.menu = ({ + luasnip = "[Snippet]", + buffer = "[Buffer]", + path = "[Path]", + rg = "[Grep]", + nvim_lsp = "[LSP]", + nvim_lua = "[Lua]", + })[entry.source.name] + return vim_item + end, + }, + + -- Docs + window = { + completion = cmp.config.window.bordered(), + documentation = cmp.config.window.bordered(), + }, + + -- Extra features experimental = { native_menu = false, --- Use cmp menu instead of Vim menu ghost_text = true, --- Show preview auto-completion diff --git a/modules/editor/neovim/lua/packer/lsp.lua b/modules/editor/neovim/lua/packer/lsp.lua index 2ab913a..ffd06b5 100644 --- a/modules/editor/neovim/lua/packer/lsp.lua +++ b/modules/editor/neovim/lua/packer/lsp.lua @@ -4,19 +4,32 @@ local M = {} -function on_path(program) - return vim.fn.executable(program) == 1 -end - M.packer = function(use) -- Language server engine use({ "neovim/nvim-lspconfig", requires = { "hrsh7th/cmp-nvim-lsp" }, config = function() + local function on_path(program) + return vim.fn.executable(program) == 1 + end + local capabilities = require("cmp_nvim_lsp").update_capabilities( vim.lsp.protocol.make_client_capabilities() ) + if on_path("lua-language-server") then + require("lspconfig").sumneko_lua.setup({ + capabilities = capabilities, + -- Turn off errors for vim global variable + settings = { + Lua = { + diagnostics = { + globals = { "vim" }, + }, + }, + }, + }) + end if on_path("rust-analyzer") then require("lspconfig").rust_analyzer.setup({ capabilities = capabilities }) end @@ -50,6 +63,10 @@ M.packer = function(use) "neovim/nvim-lspconfig", }, config = function() + local function on_path(program) + return vim.fn.executable(program) == 1 + end + require("null-ls").setup({ sources = { require("null-ls").builtins.formatting.stylua.with({ diff --git a/modules/programming/lua.nix b/modules/programming/lua.nix index dd4317c..e4de60f 100644 --- a/modules/programming/lua.nix +++ b/modules/programming/lua.nix @@ -1,8 +1,8 @@ { config, pkgs, ... }: { - home-manager.users.${config.user}.home.packages = with pkgs; - [ - stylua # Lua formatter - ]; + home-manager.users.${config.user}.home.packages = with pkgs; [ + stylua # Lua formatter + sumneko-lua-language-server # Lua LSP + ]; }