Dan Anglin
f2aa4651fa
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
139 lines
4.3 KiB
Lua
139 lines
4.3 KiB
Lua
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
|
|
|
|
vim.api.nvim_create_autocmd('LspAttach', {
|
|
group = vim.api.nvim_create_augroup('UserLspConfig', {}),
|
|
callback = function(ev)
|
|
-- Enable completion triggered by <c-x><c-o>
|
|
vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'
|
|
|
|
-- 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 }
|
|
|
|
vim.keymap.set('n', '<leader>h', vim.lsp.buf.hover, opts)
|
|
vim.keymap.set('n', '<leader>gd', vim.lsp.buf.definition, opts)
|
|
vim.keymap.set('n', '<leader>gr', vim.lsp.buf.references, opts)
|
|
vim.keymap.set('n', '<leader>gi', vim.lsp.buf.implementation, opts)
|
|
vim.keymap.set('n', '<leader>dl', vim.diagnostic.setloclist, opts)
|
|
vim.keymap.set('n', '<leader>dn', vim.diagnostic.goto_next, opts)
|
|
vim.keymap.set('n', '<leader>dp', vim.diagnostic.goto_prev, opts)
|
|
vim.keymap.set('n', '<leader>gt', vim.lsp.buf.type_definition, opts)
|
|
vim.keymap.set('n', '<leader>rn', vim.lsp.buf.rename, opts)
|
|
vim.keymap.set("n", '<leader>ff', function() vim.lsp.buf.format{ async = true } end, opts)
|
|
vim.keymap.set('n', '<leader>ca', vim.lsp.buf.code_action, opts)
|
|
vim.keymap.set('n', '<leader>dec', vim.lsp.buf.declaration, opts)
|
|
|
|
if vim.lsp.inlay_hint then
|
|
vim.keymap.set('n', '<leader>ih', function() vim.lsp.inlay_hint(0, nil) end, opts)
|
|
end
|
|
|
|
--vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, opts)
|
|
--vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, opts)
|
|
--vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, opts)
|
|
--vim.keymap.set('n', '<space>wl', function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end', opts)
|
|
end,
|
|
})
|
|
|
|
local lsp_flags = {
|
|
debounce_text_changes = 150,
|
|
}
|
|
|
|
-- Go
|
|
lspconfig.gopls.setup{
|
|
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",
|
|
},
|
|
gofumpt = true,
|
|
hints = {
|
|
parameterNames = true,
|
|
constantValues = true,
|
|
compositeLiteralFields = true,
|
|
assignVariableTypes = true,
|
|
},
|
|
staticcheck = true,
|
|
vulncheck = "Imports",
|
|
usePlaceholders = true,
|
|
},
|
|
},
|
|
flags = lsp_flags,
|
|
}
|
|
|
|
-- Jsonnet
|
|
lspconfig.jsonnet_ls.setup{
|
|
cmd = {"jsonnet-language-server", "--lint"},
|
|
flags = lsp_flags,
|
|
}
|
|
|
|
-- Terraform
|
|
lspconfig.terraformls.setup{
|
|
cmd = {"terraform-ls", "serve", "-log-file="..log_directory.."/terraformls.log"},
|
|
filetypes = {"terraform", "hcl", "tf"},
|
|
root_dir = lspconfig.util.root_pattern{".terraform", ".git", "main.tf"},
|
|
flags = lsp_flags,
|
|
}
|
|
|
|
-- Python
|
|
lspconfig.pylsp.setup{
|
|
cmd = { "pylsp" },
|
|
filetypes = { "python" },
|
|
flags = lsp_flags,
|
|
}
|
|
|
|
-- Lua
|
|
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,
|
|
}
|