From d63cee0e9828a031941ed19f6ba4640ce43f1c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Fri, 17 Feb 2023 12:38:44 +0100 Subject: [PATCH] add lsp --- modules/programs/nvim/default.nix | 10 +++ modules/programs/nvim/init.lua | 124 ++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) diff --git a/modules/programs/nvim/default.nix b/modules/programs/nvim/default.nix index 553def7..5549f2b 100644 --- a/modules/programs/nvim/default.nix +++ b/modules/programs/nvim/default.nix @@ -26,17 +26,27 @@ in withNodeJs = true; withPython3 = true; extraLuaConfig = builtins.readFile ./init.lua; + extraPackages = with pkgs; [ + sumneko-lua-language-server + nil + ]; plugins = with pkgs.vimPlugins; [ catppuccin-nvim + cmp-nvim-lsp dashboard-nvim neogit noice-nvim nui-nvim # for noice-nvim + nvim-cmp + nvim-lspconfig nvim-treesitter.withAllGrammars nvim-web-devicons # for dashboard-nvim plenary-nvim # for telescope, neogit telescope-nvim which-key-nvim + cmp_luasnip + luasnip + lsp_lines-nvim ]; }; }; diff --git a/modules/programs/nvim/init.lua b/modules/programs/nvim/init.lua index ea00f2c..18cd345 100644 --- a/modules/programs/nvim/init.lua +++ b/modules/programs/nvim/init.lua @@ -88,3 +88,127 @@ require("nvim-treesitter.configs").setup({ additional_vim_regex_highlighting = true, }, }) + +local cmp = require("cmp") +local luasnip = require("luasnip") +cmp.setup({ + snippet = { + -- REQUIRED - you must specify a snippet engine + expand = function(args) + require("luasnip").lsp_expand(args.body) + end, + }, + mapping = cmp.mapping.preset.insert({ + [""] = cmp.mapping.scroll_docs(-4), + [""] = cmp.mapping.scroll_docs(4), + [""] = cmp.mapping.complete(), + [""] = cmp.mapping.abort(), + [""] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. + [""] = 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" }, + { name = "buffer" }, + }, +}) + +-- The nvim-cmp almost supports LSP's capabilities so You should advertise it to LSP servers.. +local capabilities = require("cmp_nvim_lsp").default_capabilities() + +local lspconfig = require("lspconfig") +local on_attach_def = function(_, bufnr) + wk.register({ + K = { vim.lsp.buf.hover, "show info" }, + ["l"] = { + name = "lsp", + d = { vim.diagnostic.open_float, "open diagnostic window" }, + n = { vim.diagnostic.goto_next, "next error" }, + p = { vim.diagnostic.goto_prev, "prev error" }, + c = { vim.lsp.buf.code_action, "code action" }, + r = { vim.lsp.buf.rename, "rename" }, + f = { + function() + vim.lsp.buf.format({ async = true }) + end, + "format", + }, + }, + g = { + name = "goto", + r = { vim.lsp.buf.references, "references" }, + d = { vim.lsp.buf.definition, "definition" }, + D = { vim.lsp.buf.declaration, "declaration" }, + i = { vim.lsp.buf.implementation, "implementation" }, + t = { vim.lsp.buf.type_definition, "type defininition" }, + }, + }, { noremap = true, silent = true, buffer = bufnr }) +end + +local servers = { "nil_ls", "pylsp" } +for _, lsp in ipairs(servers) do + lspconfig[lsp].setup({ + on_attach = on_attach_def, + capabilities = capabilities, + flags = { + debounce_text_changes = 100, + }, + }) +end + +lspconfig.sumneko_lua.setup({ + on_attach = on_attach_def, + capabilities = capabilities, + settings = { + Lua = { + runtime = { + -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) + version = "LuaJIT", + }, + diagnostics = { + -- Get the language server to recognize the `vim` global + globals = { "vim" }, + }, + workspace = { + -- Make the server aware of Neovim runtime files + library = vim.api.nvim_get_runtime_file("", true), + checkThirdParty = false, + }, + -- Do not send telemetry data containing a randomized but unique identifier + telemetry = { + enable = false, + }, + }, + }, +}) + +local lsp_lines = require("lsp_lines") +lsp_lines.setup() +-- Disable virtual_text since it's redundant due to lsp_lines. +vim.diagnostic.config({ + virtual_text = false, +}) +wk.register({ + t = { + name = "toggle", + l = { lsp_lines.toggle, "lsp lines" }, + }, + { prefix = "" }, +})