Neovim: switch lsp to use mason automatic setup

This commit is contained in:
Marko Korhonen 2023-04-26 21:41:28 +03:00
parent 972043dcae
commit 8804b5a2bf
Signed by: FunctionalHacker
GPG key ID: A7F78BCB859CD890
4 changed files with 123 additions and 128 deletions

View file

@ -1,21 +1,28 @@
local nvim_local_dir = vim.fn.expand('~/.local/share/nvim')
local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t')
local workspace_dir = nvim_local_dir .. '/jdtls-workspaces/' .. project_name
local nvim_local_dir = vim.fn.expand("~/.local/share/nvim")
local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
local workspace_dir = nvim_local_dir .. "/jdtls-workspaces/" .. project_name
require('jdtls').start_or_attach({
cmd = {
'/usr/lib/jvm/java-17-openjdk-amd64/bin/java', '-Declipse.application=org.eclipse.jdt.ls.core.id1',
'-Dosgi.bundles.defaultStartLevel=4',
'-Declipse.product=org.eclipse.jdt.ls.core.product',
'-Dlog.protocol=true', '-Dlog.level=ALL', '-Xms1g',
'--add-modules=ALL-SYSTEM', '--add-opens',
'java.base/java.util=ALL-UNNAMED', '--add-opens',
'java.base/java.lang=ALL-UNNAMED', '-jar', nvim_local_dir ..
'/mason/packages/jdtls/plugins/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar',
'-configuration',
nvim_local_dir .. '/mason/packages/jdtls/config_linux', '-data',
workspace_dir
}
require("jdtls").start_or_attach({
cmd = {
"/usr/lib/jvm/java-17-openjdk-amd64/bin/java",
"-Declipse.application=org.eclipse.jdt.ls.core.id1",
"-Dosgi.bundles.defaultStartLevel=4",
"-Declipse.product=org.eclipse.jdt.ls.core.product",
"-Dlog.protocol=true",
"-Dlog.level=ALL",
"-Xms1g",
"--add-modules=ALL-SYSTEM",
"--add-opens",
"java.base/java.util=ALL-UNNAMED",
"--add-opens",
"java.base/java.lang=ALL-UNNAMED",
"-jar",
nvim_local_dir .. "/mason/packages/jdtls/plugins/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar",
"-configuration",
nvim_local_dir .. "/mason/packages/jdtls/config_linux",
"-data",
workspace_dir,
},
})
require('plugins.lspconfig').map_keys()
require("plugins.mason").map_keys()

View file

@ -82,22 +82,16 @@ local plugins = {
{ "folke/which-key.nvim", config = true },
-- Package manager for LSP servers, DAP adapters etc.
{ "williamboman/mason.nvim", config = true },
-- Install LSP server executables with Mason
{
"williamboman/mason-lspconfig.nvim",
config = true,
"williamboman/mason.nvim",
config = require("plugins.mason").setup,
dependencies = {
"neovim/nvim-lspconfig",
"williamboman/mason-lspconfig.nvim",
"WhoIsSethDaniel/mason-tool-installer.nvim",
},
},
{
"WhoIsSethDaniel/mason-tool-installer.nvim",
config = true,
},
-- Configs for built-in LSP
{ "neovim/nvim-lspconfig", config = require("plugins.lspconfig").setup },
-- Additional LSP features for Java
"mfussenegger/nvim-jdtls",

View file

@ -1,97 +0,0 @@
-- Pairs of server name and settings.
-- This is iterated through and every
-- server is setup with lspconfig
local servers = {
bashls = {},
html = {},
jsonls = {},
lemminx = {},
marksman = {},
yamlls = {},
taplo = {},
tsserver = {},
eslint = {},
rust_analyzer = {},
lua_ls = {
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),
},
-- Do not send telemetry data containing a randomized but unique identifier
telemetry = { enable = false },
},
},
}
local m = {}
function m.map_keys()
-- Register keybindings via which-key
-- to get documentation in which-key
local wk = require("which-key")
wk.register({
g = {
name = "Go to",
d = { vim.lsp.buf.definition, "Definition" },
D = { vim.lsp.buf.declaration, "Declaration" },
i = { vim.lsp.buf.implementation, "Implementation" },
r = { vim.lsp.buf.references, "References" },
},
["<leader>"] = {
name = "Leader",
w = {
name = "Workspace",
a = { vim.lsp.buf.add_workspace_folder, "Add folder" },
r = { vim.lsp.buf.remove_workspace_folder, "Remove folder" },
l = {
function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end,
"List folders",
},
},
D = { vim.lsp.buf.type_definition, "Type definition" },
rn = { vim.lsp.buf.rename, "Rename symbol" },
ca = { vim.lsp.buf.code_action, "Code action" },
e = { vim.diagnostic.open_float, "Open diagnostics" },
f = { vim.lsp.buf.format, "Format" },
},
K = { vim.lsp.buf.hover, "Hover" },
["["] = { d = { vim.diagnostic.goto_prev, "Previous diagnostic" } },
["]"] = { d = { vim.diagnostic.goto_next, "Next diagnostic" } },
})
end
function m.setup()
local function on_attach()
-- Setup lsp signature plugin
require("lsp_signature").setup({})
-- Setup keybinds
m.map_keys()
end
local capabilities = require("cmp_nvim_lsp").default_capabilities()
-- Setup every defined server
for server, settings in pairs(servers) do
require("lspconfig")[server].setup({
on_attach = on_attach,
settings = settings,
-- Inform lsp server about client
-- capabilities
capabilities = capabilities,
})
end
end
return m

View file

@ -0,0 +1,91 @@
local m = {}
function m.setup()
local function on_attach()
-- Setup lsp signature plugin
require("lsp_signature").setup({})
-- Setup keybinds
m.map_keys()
end
-- Inform lsp about completion capabilities from cmp
local capabilities = require("cmp_nvim_lsp").default_capabilities()
require("mason").setup()
local mason_lsp = require("mason-lspconfig")
mason_lsp.setup()
mason_lsp.setup_handlers({
-- Default handler
function(server_name)
require("lspconfig")[server_name].setup({
on_attach = on_attach,
capabilities = capabilities,
})
end,
-- Override lua_ls settings
["lua_ls"] = function()
require("lspconfig").lua_ls.setup({
on_attach = on_attach,
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),
},
-- Do not send telemetry data containing a randomized but unique identifier
telemetry = { enable = false },
},
},
})
end,
})
end
function m.map_keys()
local wk = require("which-key")
wk.register({
g = {
name = "Go to",
d = { vim.lsp.buf.definition, "Definition" },
D = { vim.lsp.buf.declaration, "Declaration" },
i = { vim.lsp.buf.implementation, "Implementation" },
r = { vim.lsp.buf.references, "References" },
},
["<leader>"] = {
name = "Leader",
w = {
name = "Workspace",
a = { vim.lsp.buf.add_workspace_folder, "Add folder" },
r = { vim.lsp.buf.remove_workspace_folder, "Remove folder" },
l = {
function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end,
"List folders",
},
},
D = { vim.lsp.buf.type_definition, "Type definition" },
rn = { vim.lsp.buf.rename, "Rename symbol" },
ca = { vim.lsp.buf.code_action, "Code action" },
e = { vim.diagnostic.open_float, "Open diagnostics" },
f = { vim.lsp.buf.format, "Format" },
},
K = { vim.lsp.buf.hover, "Hover" },
["["] = { d = { vim.diagnostic.goto_prev, "Previous diagnostic" } },
["]"] = { d = { vim.diagnostic.goto_next, "Next diagnostic" } },
})
end
return m