dotfiles/dot_config/nvim/lua/lsp.lua

290 lines
8.2 KiB
Lua

require("mason").setup({
ui = {
border = "none",
icons = {
package_installed = "",
package_pending = "",
package_uninstalled = "",
},
},
})
require("mason-lspconfig").setup({ automatic_installation = true })
vim.opt.completeopt = "menu,menuone,noselect"
local present, cmp = pcall(require, "cmp")
if not present or not cmp then
return
end
local has_words_before = function()
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0
and vim.api
.nvim_buf_get_lines(0, line - 1, line, true)[1]
:sub(col, col)
:match("%s")
== nil
end
local feedkey = function(key, mode)
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes(key, true, true, true),
mode,
true
)
end
cmp.setup({
snippet = {
expand = function(args)
vim.fn["vsnip#anonymous"](args.body)
end,
},
window = {
completion = {
border = { "", "", "", "", "", "", "", "" },
winhighlight = "Normal:CmpPmenu,FloatBorder:CmpBorder,CursorLine:PmenuSel,Search:None",
},
documentation = {
border = { "", "", "", "", "", "", "", "" },
winhighlight = "Normal:CmpPmenu,FloatBorder:CmpBorder,CursorLine:PmenuSel,Search:None",
},
},
mapping = cmp.mapping.preset.insert({
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(),
["<C-e>"] = cmp.mapping.abort(),
["<CR>"] = cmp.mapping.confirm({ select = true }),
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif vim.fn["vsnip#available"](1) == 1 then
feedkey("<Plug>(vsnip-expand-or-jump)", "")
elseif has_words_before() then
cmp.complete()
else
fallback() -- The fallback function sends a already mapped key. In this case, it's probably `<Tab>`.
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function()
if cmp.visible() then
cmp.select_prev_item()
elseif vim.fn["vsnip#jumpable"](-1) == 1 then
feedkey("<Plug>(vsnip-jump-prev)", "")
end
end, { "i", "s" }),
}),
sources = cmp.config.sources({
{ name = "nvim_lsp" },
{ name = "vsnip" },
{ name = "vim-dadbod-completion" },
}, {
{ name = "buffer" },
}),
formatting = {
format = require("lspkind").cmp_format({
mode = "symbol_text",
maxwidth = 50,
ellipsis_char = "...",
}),
},
})
cmp.setup.filetype("gitcommit", {
sources = cmp.config.sources({
{ name = "cmp_git" },
}, {
{ name = "buffer" },
}),
})
cmp.setup.cmdline({ "/", "?" }, {
mapping = cmp.mapping.preset.cmdline(),
sources = {
{ name = "buffer" },
},
})
cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = "path", option = { trailing_slash = true } },
}, {
{ name = "cmdline" },
}),
})
local capabilities = require("cmp_nvim_lsp").default_capabilities(
vim.lsp.protocol.make_client_capabilities()
)
---@diagnostic disable-next-line: unused-local
local on_attach = function(client, bufnr)
-- Enable completion triggered by <c-x><c-o>
vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")
-- Mappings.
-- See `:help vim.lsp.*` for documentation on any of the below functions
local bufopts = { noremap = true, silent = true, buffer = bufnr }
vim.keymap.set("n", "gD", vim.lsp.buf.declaration, bufopts)
vim.keymap.set("n", "gd", vim.lsp.buf.definition, bufopts)
vim.keymap.set("n", "K", vim.lsp.buf.hover, bufopts)
vim.keymap.set("n", "gi", vim.lsp.buf.implementation, bufopts)
vim.keymap.set("n", "<C-k>", vim.lsp.buf.signature_help, bufopts)
vim.keymap.set("n", "<leader>wa", vim.lsp.buf.add_workspace_folder, bufopts)
vim.keymap.set(
"n",
"<leader>wr",
vim.lsp.buf.remove_workspace_folder,
bufopts
)
vim.keymap.set("n", "<leader>wl", function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, bufopts)
vim.keymap.set("n", "<leader>D", vim.lsp.buf.type_definition, bufopts)
vim.keymap.set("n", "<leader>rn", vim.lsp.buf.rename, bufopts)
vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, bufopts)
vim.keymap.set("n", "gr", vim.lsp.buf.references, bufopts)
vim.keymap.set("n", "<leader>lr", "<cmd>LspRestart<CR>", bufopts)
vim.keymap.set("n", "]d", vim.diagnostic.goto_next, bufopts)
vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, bufopts)
end
local lspconfig = require("lspconfig")
lspconfig.gopls.setup({
capabilities = capabilities,
on_attach = on_attach,
})
require("go").setup({
disable_defaults = true,
lsp_inlay_hints = {
enable = true,
-- 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 variable name before type hints with the inlay hints or not
-- default: false
show_variable_name = true,
-- prefix for parameter hints
parameter_hints_prefix = "",
show_parameter_hints = true,
-- prefix for all the other hints (type, chaining)
other_hints_prefix = "=> ",
-- whether to align to the lenght 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 = 6,
-- The color of the hints
highlight = "Comment",
},
})
lspconfig.sumneko_lua.setup({
capabilities = capabilities,
on_attach = on_attach,
settings = {
Lua = {
workspace = {
-- Make the server aware of Neovim runtime files
library = vim.api.nvim_get_runtime_file("", true),
checkThirdParty = false,
maxPreload = 100000,
preloadFileSize = 10000,
},
diagnostics = {
globals = { "vim" }, -- Get the server to recognize the `vim` global
},
telemetry = {
enable = false,
},
},
},
})
lspconfig["emmet_ls"].setup({
capabilities = capabilities,
on_attach = on_attach,
filetypes = {
"javascriptreact",
"typescriptreact",
"html",
"svelte",
"css",
"less",
"sass",
"scss",
},
})
-- attach tsserver only when there's a 'package.json' file in the CWD
lspconfig.tsserver.setup({
capabilities = capabilities,
on_attach = on_attach,
root_dir = lspconfig.util.root_pattern("package.json"),
})
-- attach deno only when there's a 'deps.ts' file in the CWD
lspconfig.denols.setup({
capabilities = capabilities,
on_attach = on_attach,
root_dir = lspconfig.util.root_pattern("deps.ts"),
single_file_support = false,
})
lspconfig.ltex.setup({
capabilities = capabilities,
on_attach = function(client, bufnr)
require("ltex_extra").setup({
load_langs = { "en-US", "de-AT" },
init_check = true,
path = vim.fn.stdpath("data") .. "/dictionary",
})
on_attach(client, bufnr)
end,
settings = {
ltex = {},
},
})
lspconfig.yamlls.setup({
capabilities = capabilities,
on_attach = on_attach,
})
local null = require("null-ls")
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
null.setup({
sources = {
null.builtins.formatting.gofmt,
null.builtins.formatting.stylua,
null.builtins.formatting.stylua,
null.builtins.formatting.deno_fmt,
},
on_attach = function(client, bufnr)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
buffer = bufnr,
callback = function()
vim.lsp.buf.format({ bufnr = bufnr })
end,
})
end
end,
})
local toggle_formatters = function()
null.toggle({ methods = null.methods.FORMATTING })
end
vim.api.nvim_create_user_command("ToggleFormatters", toggle_formatters, {})