Moved from LSP to Mason

This commit is contained in:
Kristofers Solo
2022-11-07 17:21:49 +02:00
parent 7cf129370e
commit dca22b880b
11 changed files with 171 additions and 92 deletions

131
lua/user/mason/handlers.lua Normal file
View File

@@ -0,0 +1,131 @@
local M = {}
local status_cmp_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
if not status_cmp_ok then
return
end
M.capabilities = vim.lsp.protocol.make_client_capabilities()
M.capabilities.textDocument.completion.completionItem.snippetSupport = true
-- M.capabilities = cmp_nvim_lsp.update_capabilities(M.capabilities)
M.capabilities = cmp_nvim_lsp.default_capabilities(M.capabilities)
M.setup = function()
local signs = {
{ name = "DiagnosticSignError", text = "" },
{ name = "DiagnosticSignWarn", text = "" },
{ name = "DiagnosticSignHint", text = "" },
{ name = "DiagnosticSignInfo", text = "" },
}
for _, sign in ipairs(signs) do
vim.fn.sign_define(sign.name, { texthl = sign.name, text = sign.text, numhl = "" })
end
local config = {
virtual_text = false, -- disable virtual text
signs = {
active = signs, -- show signs
},
update_in_insert = true,
underline = true,
severity_sort = true,
float = {
focusable = true,
style = "minimal",
border = "rounded",
source = "always",
header = "",
prefix = "",
},
}
vim.diagnostic.config(config)
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
border = "rounded",
})
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, {
border = "rounded",
})
end
local function lsp_keymaps(bufnr)
local opts = { noremap = true, silent = true }
local keymap = vim.api.nvim_buf_set_keymap
keymap(bufnr, "n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opts)
keymap(bufnr, "n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opts)
keymap(bufnr, "n", "K", "<cmd>lua vim.lsp.buf.hover()<CR>", opts)
keymap(bufnr, "n", "gI", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
keymap(bufnr, "n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
keymap(bufnr, "n", "gl", "<cmd>lua vim.diagnostic.open_float()<CR>", opts)
vim.cmd([[command! Format execute "lua vim.lsp.buf.format()"]])
keymap(bufnr, "n", "<leader>li", "<cmd>LspInfo<cr>", opts)
keymap(bufnr, "n", "<leader>lI", "<cmd>LspInstallInfo<cr>", opts)
keymap(bufnr, "n", "<leader>la", "<cmd>lua vim.lsp.buf.code_action()<cr>", opts)
keymap(bufnr, "n", "<leader>lj", "<cmd>lua vim.diagnostic.goto_next({buffer=0})<cr>", opts)
keymap(bufnr, "n", "<leader>lk", "<cmd>lua vim.diagnostic.goto_prev({buffer=0})<cr>", opts)
keymap(bufnr, "n", "<leader>lr", "<cmd>lua vim.lsp.buf.rename()<cr>", opts)
keymap(bufnr, "n", "<leader>ls", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
keymap(bufnr, "n", "<leader>lq", "<cmd>lua vim.diagnostic.setloclist()<CR>", opts)
end
M.on_attach = function(client, bufnr)
-- if client.name == "tsserver" then
-- client.resolved_capabilities.document_formatting = false
-- end
--
-- if client.name == "sumneko_lua" then
-- client.resolved_capabilities.document_formatting = false
-- end
lsp_keymaps(bufnr)
local status_ok, illuminate = pcall(require, "illuminate")
if not status_ok then
return
end
illuminate.configure({
-- providers: provider used to get references in the buffer, ordered by priority
providers = {
"lsp",
"treesitter",
"regex",
},
-- delay: delay in milliseconds
delay = 100,
-- filetype_overrides: filetype specific overrides.
-- The keys are strings to represent the filetype while the values are tables that
-- supports the same keys passed to .configure except for filetypes_denylist and filetypes_allowlist
filetype_overrides = {},
-- filetypes_denylist: filetypes to not illuminate, this overrides filetypes_allowlist
filetypes_denylist = {
"dirvish",
"fugitive",
"alpha",
"NvimTree",
},
-- filetypes_allowlist: filetypes to illuminate, this is overriden by filetypes_denylist
filetypes_allowlist = {},
-- modes_denylist: modes to not illuminate, this overrides modes_allowlist
modes_denylist = {},
-- modes_allowlist: modes to illuminate, this is overriden by modes_denylist
modes_allowlist = {},
-- providers_regex_syntax_denylist: syntax to not illuminate, this overrides providers_regex_syntax_allowlist
-- Only applies to the 'regex' provider
-- Use :echom synIDattr(synIDtrans(synID(line('.'), col('.'), 1)), 'name')
providers_regex_syntax_denylist = {},
-- providers_regex_syntax_allowlist: syntax to illuminate, this is overriden by providers_regex_syntax_denylist
-- Only applies to the 'regex' provider
-- Use :echom synIDattr(synIDtrans(synID(line('.'), col('.'), 1)), 'name')
providers_regex_syntax_allowlist = {},
-- under_cursor: whether or not to illuminate under the cursor
under_cursor = true,
-- max_file_lines: max number of lines in a file to illuminate
max_file_lines = nil,
})
illuminate.on_attach(client)
end
return M

8
lua/user/mason/init.lua Normal file
View File

@@ -0,0 +1,8 @@
local status_ok, _ = pcall(require, "mason-lspconfig")
if not status_ok then
return
end
require("user.mason.mason-installer")
require("user.mason.handlers").setup()
require("user.mason.null-ls")

View File

@@ -0,0 +1,243 @@
local status_ok, mason = pcall(require, "mason")
if not status_ok then
return
end
local status_mason_lspconfig_ok, mason_lspconfig = pcall(require, "mason-lspconfig")
if not status_mason_lspconfig_ok then
return
end
local status_lspconfig_ok, lspconfig = pcall(require, "lspconfig")
if not status_lspconfig_ok then
return
end
local status_update_all_ok, mason_update_all = pcall(require, "mason-update-all")
if not status_update_all_ok then
return
end
local status_tool_ok, mason_tool = pcall(require, "mason-tool-installer")
if not status_tool_ok then
return
end
mason.setup()
mason_lspconfig.setup()
mason_update_all.setup()
local servers = {
"bashls",
"cssls",
"clangd",
"emmet_ls",
"html",
"jsonls",
"pyright",
"rust_analyzer",
"sumneko_lua",
"taplo",
"texlab",
"tsserver",
"vimls",
"yamlls",
}
mason_tool.setup({
ensure_installed = {
"bash-language-server",
"clangd",
"css-lsp",
"diagnostic-languageserver",
"emmet-ls",
"html-lsp",
"json-lsp",
"lua-language-server",
"marksman",
"pyright",
"rust-analyzer",
"taplo",
"texlab",
-- "typescript-language-server",
"vim-language-server",
"yaml-language-server",
},
auto_update = true,
run_on_start = true,
start_delay = 0,
})
local opts = {}
for _, server in pairs(servers) do
opts = {
on_attach = require("user.mason.handlers").on_attach,
capabilities = require("user.mason.handlers").capabilities,
}
if server == "sumneko_lua" then
local sumneko_opts = require("user.mason.settings.sumneko_lua")
opts = vim.tbl_deep_extend("force", sumneko_opts, opts)
end
if server == "pyright" then
local pyright_opts = require("user.mason.settings.pyright")
opts = vim.tbl_deep_extend("force", pyright_opts, opts)
end
-- if server == "clangd" then
-- local clangd_opts = require("user.mason.settings.clangd")
-- opts = vim.tbl_deep_extend("force", clangd_opts)
-- end
--
-- if server == "html" then
-- local html_opts = require("user.mason.settings.html")
-- opts = vim.tbl_deep_extend("force", html_opts)
-- end
if server == "rust_analyzer" then
local keymap = vim.keymap.set
local key_opts = { silent = true }
keymap("n", "<leader>rh", "<cmd>RustSetInlayHints<Cr>", key_opts)
keymap("n", "<leader>rhd", "<cmd>RustDisableInlayHints<Cr>", key_opts)
keymap("n", "<leader>th", "<cmd>RustToggleInlayHints<Cr>", key_opts)
keymap("n", "<leader>rr", "<cmd>RustRunnables<Cr>", key_opts)
keymap("n", "<leader>rem", "<cmd>RustExpandMacro<Cr>", key_opts)
keymap("n", "<leader>roc", "<cmd>RustOpenCargo<Cr>", key_opts)
keymap("n", "<leader>rpm", "<cmd>RustParentModule<Cr>", key_opts)
keymap("n", "<leader>rjl", "<cmd>RustJoinLines<Cr>", key_opts)
keymap("n", "<leader>rha", "<cmd>RustHoverActions<Cr>", key_opts)
keymap("n", "<leader>rhr", "<cmd>RustHoverRange<Cr>", key_opts)
keymap("n", "<leader>rmd", "<cmd>RustMoveItemDown<Cr>", key_opts)
keymap("n", "<leader>rmu", "<cmd>RustMoveItemUp<Cr>", key_opts)
keymap("n", "<leader>rsb", "<cmd>RustStartStandaloneServerForBuffer<Cr>", key_opts)
keymap("n", "<leader>rd", "<cmd>RustDebuggables<Cr>", key_opts)
keymap("n", "<leader>rv", "<cmd>RustViewCrateGraph<Cr>", key_opts)
keymap("n", "<leader>rw", "<cmd>RustReloadWorkspace<Cr>", key_opts)
keymap("n", "<leader>rss", "<cmd>RustSSR<Cr>", key_opts)
keymap("n", "<leader>rxd", "<cmd>RustOpenExternalDocs<Cr>", key_opts)
require("rust-tools").setup({
tools = {
on_initialized = function()
vim.cmd([[
autocmd BufEnter,CursorHold,InsertLeave,BufWritePost *.rs silent! lua vim.lsp.codelens.refresh()
]])
end,
},
server = {
on_attach = require("user.mason.handlers").on_attach,
capabilities = require("user.mason.handlers").capabilities,
settings = {
["rust-analyzer"] = {
lens = {
enable = true,
},
checkOnSave = {
command = "clippy",
},
},
},
},
})
goto continue
end
if server == "clangd" then
require("clangd_extensions").setup({
server = {
-- options to pass to nvim-lspconfig
-- i.e. the arguments to require("lspconfig").clangd.setup({})
},
extensions = {
-- defaults:
-- Automatically set inlay hints (type hints)
autoSetHints = true,
-- These apply to the default ClangdSetInlayHints command
inlay_hints = {
-- Only show inlay hints for the current line
only_current_line = false,
-- Event which triggers a refersh of the inlay hints.
-- You can make this "CursorMoved" or "CursorMoved,CursorMovedI" but
-- not that this may cause higher CPU usage.
-- This option is only respected when only_current_line and
-- autoSetHints both are true.
only_current_line_autocmd = "CursorHold",
-- whether to show parameter hints with the inlay hints or not
show_parameter_hints = true,
-- prefix for parameter hints
parameter_hints_prefix = "<- ",
-- prefix for all the other hints (type, chaining)
other_hints_prefix = "=> ",
-- whether to align to the length of the longest line in the file
max_len_align = false,
-- padding from the left if max_len_align is true
max_len_align_padding = 1,
-- whether to align to the extreme right or not
right_align = false,
-- padding from the right if right_align is true
right_align_padding = 7,
-- The color of the hints
highlight = "Comment",
-- The highlight group priority for extmark
priority = 100,
},
ast = {
-- -- These are unicode, should be available in any font
-- role_icons = {
-- type = "🄣",
-- declaration = "🄓",
-- expression = "🄔",
-- statement = ";",
-- specifier = "🄢",
-- ["template argument"] = "🆃",
-- },
-- kind_icons = {
-- Compound = "🄲",
-- Recovery = "🅁",
-- TranslationUnit = "🅄",
-- PackExpansion = "🄿",
-- TemplateTypeParm = "🅃",
-- TemplateTemplateParm = "🅃",
-- TemplateParamObject = "🅃",
-- },
-- These require codicons (https://github.com/microsoft/vscode-codicons)
role_icons = {
type = "",
declaration = "",
expression = "",
specifier = "",
statement = "",
["template argument"] = "",
},
kind_icons = {
Compound = "",
Recovery = "",
TranslationUnit = "",
PackExpansion = "",
TemplateTypeParm = "",
TemplateTemplateParm = "",
TemplateParamObject = "",
},
highlights = {
detail = "Comment",
},
},
memory_usage = {
border = "none",
},
symbol_info = {
border = "none",
},
},
})
goto continue
end
lspconfig[server].setup(opts)
::continue::
end

View File

@@ -0,0 +1,55 @@
local null_ls_status_ok, null_ls = pcall(require, "null-ls")
if not null_ls_status_ok then
return
end
local mason_null_ls_status_ok, mason_null_ls = pcall(require, "mason-null-ls")
if not mason_null_ls_status_ok then
return
end
-- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/formatting
local formatting = null_ls.builtins.formatting
-- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/diagnostics
local diagnostics = null_ls.builtins.diagnostics
-- https://github.com/prettier-solidity/prettier-plugin-solidity
null_ls.setup({
debug = false,
sources = {
formatting.prettier.with({
extra_filetypes = { "toml" },
extra_args = { "--no-semi", "--single-quote", "--jsx-single-quote" },
}),
formatting.autopep8,
formatting.stylua,
formatting.beautysh,
formatting.djlint,
formatting.tidy,
diagnostics.cpplint,
diagnostics.luacheck,
diagnostics.pylint,
-- diagnostics.mypy,
diagnostics.zsh,
},
})
mason_null_ls.setup({
ensure_installed = {
"autopep8",
"beautysh",
"djlint",
"shfmt",
"yamlfmt",
"codespell",
"cpplint",
"luacheck",
"misspell",
"pylint",
"mypy",
"html_lint",
"gitlint",
},
automatic_installation = true,
automatic_setup = true,
})

View File

@@ -0,0 +1,9 @@
return {
settings = {
python = {
analysis = {
typeCheckingMode = "true",
},
},
},
}

View File

@@ -0,0 +1,18 @@
return {
settings = {
Lua = {
diagnostics = {
globals = { "vim" },
},
workspace = {
library = {
[vim.fn.expand("$VIMRUNTIME/lua")] = true,
[vim.fn.stdpath("config") .. "/lua"] = true,
},
},
telemetry = {
enable = false,
},
},
},
}