From f2aa4651fa27db09f7d54536021d13f4c7358894 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sat, 23 Sep 2023 13:10:38 +0100 Subject: [PATCH] fix: update LSP configuration Changes: - fix: replace on_attach and custom_attach with autocommand for LspAttach as recommended by the official documentation. - fix: enable inlay hints for gopls. - fix: add a keymap to toggle inlay hints for supported versions of neovim. - fix: customise the NormalFloat background colour. - fix: add LSP configuration for the Lua Language Server. - refactor: move the lint autocommand to lint.lua --- nvim/lua/autocommands.lua | 12 -- nvim/lua/plugins/config/lint.lua | 12 ++ nvim/lua/plugins/config/lsp.lua | 147 ++++++++++++++----------- nvim/lua/plugins/config/tokyonight.lua | 7 +- 4 files changed, 101 insertions(+), 77 deletions(-) diff --git a/nvim/lua/autocommands.lua b/nvim/lua/autocommands.lua index 6358786..9c5c9fa 100644 --- a/nvim/lua/autocommands.lua +++ b/nvim/lua/autocommands.lua @@ -4,15 +4,3 @@ vim.api.nvim_create_autocmd('BufWritePre', { vim.lsp.buf.code_action({ context = { only = { 'source.organizeImports' } }, apply = true }) end }) - -vim.api.nvim_create_autocmd('BufWritePost', { - callback = function() - local ok, lint = pcall(require, "lint") - - if not ok then - return - end - - lint.try_lint() - end -}) diff --git a/nvim/lua/plugins/config/lint.lua b/nvim/lua/plugins/config/lint.lua index 0aa19e9..e56e23c 100644 --- a/nvim/lua/plugins/config/lint.lua +++ b/nvim/lua/plugins/config/lint.lua @@ -9,3 +9,15 @@ lint.linters_by_ft = { sh = {'shellcheck'}, python = {'pylint'}, } + +vim.api.nvim_create_autocmd('BufWritePost', { + callback = function() + local ok, lint = pcall(require, "lint") + + if not ok then + return + end + + lint.try_lint() + end +}) diff --git a/nvim/lua/plugins/config/lsp.lua b/nvim/lua/plugins/config/lsp.lua index 5242a46..d718dec 100644 --- a/nvim/lua/plugins/config/lsp.lua +++ b/nvim/lua/plugins/config/lsp.lua @@ -1,39 +1,55 @@ local ok, lspconfig = pcall(require, "lspconfig") +if not ok then + return +end + +local ok, util = pcall(require, "lspconfig/util") + +if not ok then + return +end + local log_directory = vim.env.LOG_HOME if log_directory == nil then log_directory = "/tmp" end -if not ok then - return -end +vim.api.nvim_create_autocmd('LspAttach', { + group = vim.api.nvim_create_augroup('UserLspConfig', {}), + callback = function(ev) + -- Enable completion triggered by + vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc' -local custom_attach = function(_, bufnr) - local opts = { noremap=true, silent=true, buffer=bufnr } + -- Buffer local mappings. + -- See `:help vim.lsp.*` for documentation on any of the below functions + -- See `:help vim.diagnostic.*` for documentation on any of the diagnostics functions + local opts = { noremap=true, silent=true, buffer=ev.buf } - -- Enable completion triggered by - vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') + vim.keymap.set('n', 'h', vim.lsp.buf.hover, opts) + vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts) + vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts) + vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts) + vim.keymap.set('n', 'dl', vim.diagnostic.setloclist, opts) + vim.keymap.set('n', 'dn', vim.diagnostic.goto_next, opts) + vim.keymap.set('n', 'dp', vim.diagnostic.goto_prev, opts) + vim.keymap.set('n', 'gt', vim.lsp.buf.type_definition, opts) + vim.keymap.set('n', 'rn', vim.lsp.buf.rename, opts) + vim.keymap.set("n", 'ff', function() vim.lsp.buf.format{ async = true } end, opts) + vim.keymap.set('n', 'ca', vim.lsp.buf.code_action, opts) + vim.keymap.set('n', 'dec', vim.lsp.buf.declaration, opts) - -- See `:help vim.diagnostic.*` for documentation on any of the diagnostics functions - vim.keymap.set('n', 'h', vim.lsp.buf.hover, opts) - vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts) - vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts) - vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts) - vim.keymap.set('n', 'dl', vim.diagnostic.setloclist, opts) - vim.keymap.set('n', 'dn', vim.diagnostic.goto_next, opts) - vim.keymap.set('n', 'dp', vim.diagnostic.goto_prev, opts) - vim.keymap.set('n', 'gt', vim.lsp.buf.type_definition, opts) - vim.keymap.set('n', 'rn', vim.lsp.buf.rename, opts) - vim.keymap.set("n", 'ff', function() vim.lsp.buf.format{ async = true } end, opts) - vim.keymap.set('n', 'ca', vim.lsp.buf.code_action, opts) - vim.keymap.set('n', 'dec', vim.lsp.buf.declaration, opts) - --buf_set_keymap('n', '', vim.lsp.buf.signature_help, opts) - --buf_set_keymap('n', 'wa', vim.lsp.buf.add_workspace_folder, opts) - --buf_set_keymap('n', 'wr', vim.lsp.buf.remove_workspace_folder, opts) - --buf_set_keymap('n', 'wl', function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end', opts) -end + if vim.lsp.inlay_hint then + vim.keymap.set('n', 'ih', function() vim.lsp.inlay_hint(0, nil) end, opts) + end + + --vim.keymap.set('n', '', vim.lsp.buf.signature_help, opts) + --vim.keymap.set('n', 'wa', vim.lsp.buf.add_workspace_folder, opts) + --vim.keymap.set('n', 'wr', vim.lsp.buf.remove_workspace_folder, opts) + --vim.keymap.set('n', 'wl', function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end', opts) + end, +}) local lsp_flags = { debounce_text_changes = 150, @@ -41,23 +57,28 @@ local lsp_flags = { -- Go lspconfig.gopls.setup{ - on_attach = custom_attach, cmd = {"gopls", "serve", "-logfile="..log_directory.."/gopls.log"}, + filetypes = {"go", "gomod"}, + root_dir = util.root_pattern("go.work", "go.mod", ".git"), settings = { gopls = { analyses = { unusedparams = true, + useany = true, }, env = { - GOFLAGS = "-tags=mage" + GOFLAGS = "-tags=mage", }, gofumpt = true, + hints = { + parameterNames = true, + constantValues = true, + compositeLiteralFields = true, + assignVariableTypes = true, + }, staticcheck = true, - }, - init_options = { + vulncheck = "Imports", usePlaceholders = true, - completeUnimported = true, - hoverKind = "FullDocumentation", }, }, flags = lsp_flags, @@ -65,14 +86,12 @@ lspconfig.gopls.setup{ -- Jsonnet lspconfig.jsonnet_ls.setup{ - on_attach = custom_attach, cmd = {"jsonnet-language-server", "--lint"}, flags = lsp_flags, } -- Terraform lspconfig.terraformls.setup{ - on_attach = custom_attach, cmd = {"terraform-ls", "serve", "-log-file="..log_directory.."/terraformls.log"}, filetypes = {"terraform", "hcl", "tf"}, root_dir = lspconfig.util.root_pattern{".terraform", ".git", "main.tf"}, @@ -81,40 +100,40 @@ lspconfig.terraformls.setup{ -- Python lspconfig.pylsp.setup{ - on_attach = custom_attach, cmd = { "pylsp" }, filetypes = { "python" }, flags = lsp_flags, } -- Lua ---local sumneko_root_path = vim.fn.expand('$HOME') .. "/Git/github.com/sumneko/lua-language-server" ---local sumneko_binary = sumneko_root_path .. "/bin/Linux/lua-language-server" --- ---lspconfig.sumneko_lua.setup { --- on_attach = custom_attach, --- cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"}, --- settings = { --- Lua = { --- runtime = { --- -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim) --- version = 'LuaJIT', --- -- Setup your lua path --- path = vim.split(package.path, ';') --- }, --- diagnostics = { --- -- Get the language server to recognize the vim and awesome globals --- globals = {'vim', 'awesome'} --- }, --- workspace = { --- -- Make the server aware of Neovim runtime files --- library = vim.api.nvim_get_runtime_file("", true), --- }, --- telemetry = { --- -- Do not send telemetry data containing a randomized but unique identifier --- enable = false, --- }, --- } --- }, --- flags = lsp_flags, ---} +lspconfig.lua_ls.setup { + on_init = function(client) + local path = client.workspace_folders[1].name + if not vim.loop.fs_stat(path..'/.luarc.json') and not vim.loop.fs_stat(path..'/.luarc.jsonc') then + client.config.settings = vim.tbl_deep_extend('force', client.config.settings, { + Lua = { + runtime = { + -- Tell the language server which version of Lua you're using + -- (most likely LuaJIT in the case of Neovim) + version = 'LuaJIT' + }, + -- Make the server aware of Neovim runtime files + workspace = { + checkThirdParty = false, + library = { + vim.env.VIMRUNTIME + -- "${3rd}/luv/library" + -- "${3rd}/busted/library", + } + -- or pull in all of 'runtimepath'. NOTE: this is a lot slower + -- library = vim.api.nvim_get_runtime_file("", true) + } + } + }) + + client.notify("workspace/didChangeConfiguration", { settings = client.config.settings }) + end + return true + end, + flags = lsp_flags, +} diff --git a/nvim/lua/plugins/config/tokyonight.lua b/nvim/lua/plugins/config/tokyonight.lua index 50dfe6c..c9983ee 100644 --- a/nvim/lua/plugins/config/tokyonight.lua +++ b/nvim/lua/plugins/config/tokyonight.lua @@ -6,6 +6,11 @@ end tokyonight.setup { style = "night", - sidebars = {"packer", "terminal"}, + sidebars = {"terminal"}, dim_inactive = true, + on_highlights = function(hl, c) + hl.NormalFloat = { + bg = "#394b70", + } + end, }