Browse Source

[nvim] Migrate LSP configuration to vim.lsp

master
Fredrik Ekre 6 months ago
parent
commit
6eee91c773
No known key found for this signature in database
GPG Key ID: DE82E6D5E364C0A2
  1. 118
      .config/nvim/lua/plugins/lsp.lua

118
.config/nvim/lua/plugins/lsp.lua

@ -1,78 +1,68 @@
-- nvim-lspconfig, https://github.com/neovim/nvim-lspconfig -- nvim-lspconfig, https://github.com/neovim/nvim-lspconfig
-- Set LSP keymappings in on_attach (i.e. only in buffers with LSP active) -- Note: The server configuration in this file is done in the
-- TODO: lspconfig recommend doing this in an LspAttach autocommand instead -- nvim-lspconfig.config() setup function. This is a bit strange but at least
local on_attach = function(client, bufnr) -- it ensures the config is run *after* configuring the plugin...
local opts = { buffer = bufnr, silent = true }
vim.keymap.set("n", "K", vim.lsp.buf.hover, opts)
vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts)
vim.keymap.set("n", "<leader>lrn", vim.lsp.buf.rename, opts)
vim.keymap.set("n", "<leader>lrr", vim.lsp.buf.references, opts)
vim.keymap.set("n", "<leader>lca", function() vim.lsp.buf.code_action({apply = true}) end, opts)
vim.keymap.set("n", "<leader>lqf", function() vim.lsp.buf.code_action({apply = true}) end, opts)
vim.keymap.set("i", "<C-h>", vim.lsp.buf.signature_help, opts)
vim.keymap.set("v", "<leader>lfmt", function() vim.lsp.buf.format({timeout_ms = 1000000}) end, opts)
end
-- Setup lspconfig: capabilities is passed to lspconfig.$server.setup
-- TODO: Why don't I have to make_client_capabilities and extend?
local capabilities = require("cmp_nvim_lsp").default_capabilities()
local default_opts = { -- TODO: Unclear whether this is needed or not. Completions seem to work anyway?
on_attach = on_attach, -- local capabilities = require("cmp_nvim_lsp").default_capabilities()
capabilities = capabilities,
}
local servers = { local function config()
-- Rust LSP (rust_analyzer) -- Configure an autocommand which fires when a server attaches to a buffer
rust_analyzer = default_opts, vim.api.nvim_create_autocmd('LspAttach', {
-- C/C++ LSP (clangd) callback = function(args)
clangd = default_opts, local opts = { buffer = args.buf, silent = true }
-- Go LSP (gopls) vim.keymap.set("n", "K", vim.lsp.buf.hover, opts)
gopls = default_opts, vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts)
-- YAML LSP (yaml-language-server) vim.keymap.set("n", "<leader>lrn", vim.lsp.buf.rename, opts)
yamlls = default_opts, vim.keymap.set("n", "<leader>lrr", vim.lsp.buf.references, opts)
-- Ansible LSP (ansible-language-server) vim.keymap.set("n", "<leader>lca", function() vim.lsp.buf.code_action({apply = true}) end, opts)
ansiblels = default_opts, vim.keymap.set("n", "<leader>lqf", function() vim.lsp.buf.code_action({apply = true}) end, opts)
-- Terraform LSP (terraform-ls) vim.keymap.set("i", "<C-h>", vim.lsp.buf.signature_help, opts)
terraformls = default_opts, vim.keymap.set("v", "<leader>lfmt", function() vim.lsp.buf.format({timeout_ms = 1000000}) end, opts)
end,
})
-- Julia LSP (LanguageServer.jl) -- Julia LSP (LanguageServer.jl)
julials = vim.tbl_extend( do
"force", -- Modify the julia binary to out custom built one if it exists
default_opts, local cfg = vim.lsp.config.julials
{ local cmd = cfg.cmd
on_new_config = function(new_config, _) local julia = vim.fn.expand("~/.julia/environments/nvim-lspconfig/bin/julia")
local julia = vim.fn.expand("~/.julia/environments/nvim-lspconfig/bin/julia") local REVISE_LANGUAGESERVER = false
local REVISE_LANGUAGESERVER = false if REVISE_LANGUAGESERVER then
if REVISE_LANGUAGESERVER then cmd[5] = (cmd[5]):gsub("using LanguageServer", "using Revise; using LanguageServer; LanguageServer.USE_REVISE[] = true")
new_config.cmd[5] = (new_config.cmd[5]):gsub("using LanguageServer", "using Revise; using LanguageServer; LanguageServer.USE_REVISE[] = true") elseif require("lspconfig").util.path.is_file(julia) then
elseif require("lspconfig").util.path.is_file(julia) then cmd[1] = julia
new_config.cmd[1] = julia end
end vim.lsp.config('julials', {
end, cmd = cmd,
-- This just adds dirname(fname) as a fallback (see nvim-lspconfig#1768).
root_dir = function(fname)
local util = require("lspconfig.util")
return util.root_pattern "Project.toml"(fname) or util.find_git_ancestor(fname) or
util.path.dirname(fname)
end,
on_attach = function(client, bufnr) on_attach = function(client, bufnr)
on_attach(client, bufnr) -- Disable automatic formatexpr since the LS.jl formatter isn't very nice.
-- Disable automatic formatexpr since the LS.jl formatter isn't so nice.
vim.bo[bufnr].formatexpr = "" vim.bo[bufnr].formatexpr = ""
end, end,
} -- -- This just adds dirname(fname) as a fallback (see nvim-lspconfig#1768).
), -- root_dir = function(fname)
} -- local util = require("lspconfig.util")
-- return util.root_pattern "Project.toml"(fname) or util.find_git_ancestor(fname) or
local function configure_lsp() -- util.path.dirname(fname)
lspconfig = require("lspconfig") -- end,
for name, opts in pairs(servers) do })
lspconfig[name].setup(opts) vim.lsp.enable("julials")
end end
-- Enable some more servers. These don't need special handling, yey!
vim.lsp.enable({
"ansiblels", -- Ansible LSP (ansible-language-server)
"clangd", -- C/C++ LSP (clangd)
"clangd", -- C/C++ LSP (clangd)
"gopls", -- Go LSP (gopls)
"pylsp", -- Python LSP (python-lsp-server)
"rust_analyzer", -- Rust LSP (rust_analyzer)
"terraformls", -- Terraform LSP (terraform-ls)
"yamlls", -- YAML LSP (yaml-language-server)
})
end end
return { return {
"neovim/nvim-lspconfig", "neovim/nvim-lspconfig",
config = configure_lsp, config = config,
} }

Loading…
Cancel
Save