LspAttach autocmd instead of on_attach fn

This commit is contained in:
Amanda Graven 2023-11-12 09:54:58 +01:00
parent 5a8f49e2ca
commit 542b72024f
Signed by: amanda
GPG key ID: F747582C5608F4CB

View file

@ -351,6 +351,12 @@ require'packer'.startup(function(use)
use 'vim-airline/vim-airline' use 'vim-airline/vim-airline'
use 'vim-airline/vim-airline-themes' use 'vim-airline/vim-airline-themes'
use 'romgrk/barbar.nvim' use 'romgrk/barbar.nvim'
use {'luckasRanarison/clear-action.nvim', config = function()
require'clear-action'.setup {
signs = { enable = false },
popup = { enable = true },
}
end}
use {'MattesGroeger/vim-bookmarks', config = function() use {'MattesGroeger/vim-bookmarks', config = function()
vim.g.bookmark_no_default_key_mappings = 1 vim.g.bookmark_no_default_key_mappings = 1
vim.g.bookmark_save_per_working_dir = 1 vim.g.bookmark_save_per_working_dir = 1
@ -923,16 +929,24 @@ capabilities.textDocument.foldingRange = {
} }
---- lsp: language servers ---- ---- lsp: language servers ----
--- Performs keymaps and other setup specific to buffers with LSP enabled au('LspAttach', {
--- @param bufnr number callback = function (event)
--- @diagnostic disable-next-line: unused-local local bufnr = event.buf
local function on_attach(client, bufnr) local client = vim.lsp.get_client_by_id(event.data.client_id)
local function code_action()
local ok, clear_action = pcall(require, 'clear-action')
if ok then
clear_action.code_action()
else
vim.lsp.buf.code_action()
end
end
require'which-key'.register({ require'which-key'.register({
['<Leader>l'] = { ['<Leader>l'] = {
D = {vim.lsp.buf.declaration, 'Declaration'}, D = {vim.lsp.buf.declaration, 'Declaration'},
d = {'<Cmd>Telescope lsp_definitions<CR>', 'Definition'}, d = {'<Cmd>Telescope lsp_definitions<CR>', 'Definition'},
i = {'<Cmd>Telescope lsp_implementations<CR>', 'Implementation'}, i = {'<Cmd>Telescope lsp_implementations<CR>', 'Implementation'},
a = {vim.lsp.buf.code_action, 'Code action'}, a = {code_action, 'Code action'},
r = {vim.lsp.buf.rename, 'Rename'}, r = {vim.lsp.buf.rename, 'Rename'},
h = {function() vim.lsp.inlay_hint(0, nil) end, 'Inlay hints'}, h = {function() vim.lsp.inlay_hint(0, nil) end, 'Inlay hints'},
l = {vim.lsp.codelens.refresh, 'Show codelenses'}, l = {vim.lsp.codelens.refresh, 'Show codelenses'},
@ -943,7 +957,7 @@ local function on_attach(client, bufnr)
g = { g = {
d = {vim.lsp.buf.definition, 'Goto definition'}, d = {vim.lsp.buf.definition, 'Goto definition'},
D = {vim.lsp.buf.implementation, 'Goto implementation'}, D = {vim.lsp.buf.implementation, 'Goto implementation'},
a = {vim.lsp.buf.code_action, 'Code action'}, a = {code_action, 'Code action'},
R = {vim.lsp.buf.rename, 'Rename'}, R = {vim.lsp.buf.rename, 'Rename'},
y = {vim.lsp.buf.type_definition, 'Type definition'}, y = {vim.lsp.buf.type_definition, 'Type definition'},
}, },
@ -973,16 +987,15 @@ local function on_attach(client, bufnr)
require'nvim-navic'.attach(client, bufnr) require'nvim-navic'.attach(client, bufnr)
end end
})
-- Enable language servers -- Enable language servers
local default = { local default = {
capabilities = capabilities, capabilities = capabilities,
on_attach = on_attach,
} }
require'lspconfig'.lua_ls.setup { require'lspconfig'.lua_ls.setup {
capabilities = capabilities, capabilities = capabilities,
on_attach = on_attach,
settings = { settings = {
Lua = { Lua = {
runtime = { runtime = {
@ -1054,7 +1067,6 @@ au('FileType', {
capabilities = capabilities, capabilities = capabilities,
on_attach = function (client, bufnr) on_attach = function (client, bufnr)
on_attach(client, bufnr)
jdtls.setup_dap() jdtls.setup_dap()
end, end,
} }
@ -1071,7 +1083,6 @@ au('FileType', {
require'lspconfig'.sqls.setup { require'lspconfig'.sqls.setup {
capabilities = capabilities, capabilities = capabilities,
on_attach = function (client, bufnr) on_attach = function (client, bufnr)
on_attach(client, bufnr)
local ok, sqls = pcall(require, 'sqls') local ok, sqls = pcall(require, 'sqls')
if not ok then return end if not ok then return end
sqls.on_attach(client, bufnr) sqls.on_attach(client, bufnr)
@ -1123,7 +1134,6 @@ require'rust-tools'.setup {
-- Enable standalone file (i.e. no cargo) support -- Enable standalone file (i.e. no cargo) support
standalone = true, standalone = true,
capabilities = capabilities, capabilities = capabilities,
on_attach = on_attach,
settings = { settings = {
['rust-analyzer'] = { ['rust-analyzer'] = {
cargo = { cargo = {
@ -1267,7 +1277,6 @@ vim.keymap.set('n', '<Leader>t', '<Cmd>NvimTreeFindFile<CR>', {desc = 'Nvim Tree
---- treesitter ---- ---- treesitter ----
---@diagnostic disable-next-line: missing-fields
require'nvim-treesitter.configs'.setup { require'nvim-treesitter.configs'.setup {
-- Configure the nvim-ts-autotag plugin -- Configure the nvim-ts-autotag plugin
autotag = { autotag = {