Files
nvim/lua/plugins/lsp.lua
2024-09-20 09:36:33 +02:00

205 lines
5.7 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()
local util = require 'lspconfig.util'
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,
ts_ls = {
root_dir = function(fname)
-- by default use parent .git folder
local git_root = util.find_git_ancestor(fname)
-- well, this is a bit of a hack for the frontend-apps monorepo
if fname:find 'frontend%-apps/bcr' then
return git_root .. '/bcr'
else
return git_root
end
end,
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_d',
'prettier',
}
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', 'gi', vim.lsp.buf.implementation, { 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 = {
require_cwd = true,
cwd = require('conform.util').root_file { '.eslintrc', '.eslintrc.js' },
},
prettier = {
require_cwd = true,
cwd = require('conform.util').root_file {
'.prettierrc',
'.prettierrc.json',
'.prettierrc.yml',
'.prettierrc.yaml',
'.prettierrc.json5',
'.prettierrc.js',
'.prettierrc.cjs',
'.prettierrc.mjs',
'.prettierrc.toml',
'prettier.config.js',
'prettier.config.cjs',
'prettier.config.mjs',
},
},
},
formatters_by_ft = {
lua = { 'stylua' },
javascript = { 'eslint_d', 'prettier' },
javascriptreact = { 'eslint_d', 'prettier' },
json = { 'prettier' },
typescript = { 'eslint_d', 'prettier' },
typescriptreact = { 'eslint_d', 'prettier' },
['_'] = { 'trim_whitespace' },
},
stop_after_first = true,
}
vim.api.nvim_create_autocmd('BufWritePre', {
callback = function(args)
require('conform').format {
bufnr = args.buf,
lsp_fallback = true,
quiet = false,
}
end,
})
end,
},
{
'hedyhli/outline.nvim',
lazy = true,
cmd = { 'Outline', 'OutlineOpen' },
keys = {
{ '<leader>co', '<cmd>Outline<CR>', desc = 'Toggle outline' },
},
opts = {
-- Your setup opts here
},
},
}