Files
nvim/lua/plugins/lsp.lua
2024-08-01 15:36:38 +02:00

165 lines
4.6 KiB
Lua

return {
{
'neovim/nvim-lspconfig',
dependencies = {
'folke/neodev.nvim',
'williamboman/mason.nvim',
'williamboman/mason-lspconfig.nvim',
'WhoIsSethDaniel/mason-tool-installer.nvim',
{ 'j-hui/fidget.nvim', opts = {} },
-- Autoformatting
'stevearc/conform.nvim',
-- Schema information
'b0o/SchemaStore.nvim',
},
config = function()
require('neodev').setup {
-- library = {
-- plugins = { "nvim-dap-ui" },
-- types = true,
-- },
}
local capabilities = nil
if pcall(require, 'cmp_nvim_lsp') then
capabilities = require('cmp_nvim_lsp').default_capabilities()
end
local lspconfig = require 'lspconfig'
local servers = {
bashls = true,
lua_ls = true,
rust_analyzer = true,
svelte = true,
templ = true,
cssls = true,
eslint = true,
tsserver = {
settings = {
diagnostics = {
ignoredCodes = {
7016, -- Could not find a declaration file for module
80001, -- May be converted to ES module
80002, -- May be converted to class declaration
},
},
},
},
jsonls = {
settings = {
json = {
schemas = require('schemastore').json.schemas(),
validate = { enable = true },
},
},
},
yamlls = {
settings = {
yaml = {
schemaStore = {
enable = false,
url = '',
},
schemas = require('schemastore').yaml.schemas(),
},
},
},
}
local servers_to_install = vim.tbl_filter(function(key)
local t = servers[key]
if type(t) == 'table' then
return not t.manual_install
else
return t
end
end, vim.tbl_keys(servers))
require('mason').setup()
local ensure_installed = {
'stylua',
'lua_ls',
'eslint',
'eslint_d',
'prettier',
-- "tailwind-language-server",
}
vim.list_extend(ensure_installed, servers_to_install)
require('mason-tool-installer').setup { ensure_installed = ensure_installed }
for name, config in pairs(servers) do
if config == true then
config = {}
end
config = vim.tbl_deep_extend('force', {}, {
capabilities = capabilities,
}, config)
lspconfig[name].setup(config)
end
local disable_semantic_tokens = {
lua = true,
}
vim.api.nvim_create_autocmd('LspAttach', {
callback = function(args)
local bufnr = args.buf
local client = assert(vim.lsp.get_client_by_id(args.data.client_id), 'must have valid client')
vim.opt_local.omnifunc = 'v:lua.vim.lsp.omnifunc'
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, { buffer = 0 })
vim.keymap.set('n', 'gr', vim.lsp.buf.references, { buffer = 0 })
vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, { buffer = 0 })
vim.keymap.set('n', 'gT', vim.lsp.buf.type_definition, { buffer = 0 })
vim.keymap.set('n', 'K', vim.lsp.buf.hover, { buffer = 0 })
vim.keymap.set('n', '<space>cr', vim.lsp.buf.rename, { buffer = 0 })
vim.keymap.set('n', '<space>ca', vim.lsp.buf.code_action, { buffer = 0 })
local filetype = vim.bo[bufnr].filetype
if disable_semantic_tokens[filetype] then
client.server_capabilities.semanticTokensProvider = nil
end
end,
})
-- Autoformatting Setup
require('conform').setup {
formatters = {
eslint_d = {
cwd = require('conform.util').root_file { '.eslintrc', '.eslintrc.js' },
},
},
formatters_by_ft = {
lua = { 'stylua' },
javascript = { { 'eslint_d', 'eslint', 'prettier' } },
javascriptreact = { { 'eslint_d', 'eslint', 'prettier' } },
json = { { 'eslint_d', 'eslint', 'prettier' } },
typescript = { { 'eslint_d', 'eslint', 'prettier' } },
typescriptreact = { { 'eslint_d', 'eslint', 'prettier' } },
['_'] = { 'trim_whitespace' },
},
}
vim.api.nvim_create_autocmd('BufWritePre', {
callback = function(args)
require('conform').format {
bufnr = args.buf,
lsp_fallback = true,
quiet = false,
}
end,
})
end,
},
}