feat: Move 'LspAttach' autocmd boilerplate to dedicated library function

This commit is contained in:
punkfairie 2025-01-02 21:04:51 -08:00
parent 45b1cb99ee
commit 432f7e0595
Signed by: punkfairie
GPG key ID: A509E8F77FB9D696
4 changed files with 121 additions and 120 deletions

View file

@ -1,6 +1,8 @@
---@class lib
local M = {}
M.lsp = require('lib.lsp')
---Find the root of a project based on `vim.g.root_spec`. Defaults to
---`{ '.git' }` if unset.
---@return string?

20
nvim/lua/lib/lsp.lua Normal file
View file

@ -0,0 +1,20 @@
---@class lib.lsp
local M = {}
---Boilerplate to create an autocommand to do something on LspAttach.
---@param callback fun(client:vim.lsp.Client, buffer:number)
---@param name? string
function M.on_attach(callback, name)
return vim.api.nvim_create_autocmd('LspAttach', {
callback = function(args)
local buffer = args.buf ---@type number
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client and (not name or client.name == name) then
return callback(client, buffer)
end
end,
})
end
return M

View file

@ -82,102 +82,89 @@ vim.lsp.config('*', {
vim.lsp.enable({ 'lua-language-server' })
-- Keymaps -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
vim.api.nvim_create_autocmd('LspAttach', {
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
MarleyVim.lsp.on_attach(function(client)
local set = vim.keymap.set
if not client then
return
end
set('n', 'gD', vim.lsp.buf.declaration, { desc = 'declaration' })
set('n', 'gI', vim.lsp.buf.implementation, { desc = 'implementation' })
local set = vim.keymap.set
set('n', 'gr', vim.lsp.buf.references, { desc = 'references', nowait = true })
set('n', 'gD', vim.lsp.buf.declaration, { desc = 'declaration' })
set('n', 'gI', vim.lsp.buf.implementation, { desc = 'implementation' })
set('n', 'gy', vim.lsp.buf.type_definition, { desc = 'type definition' })
set('n', 'K', function()
return vim.lsp.buf.hover()
end, { desc = 'hover' })
if client.supports_method('textDocument/codeAction') then
set(
{ 'n', 'v' },
'<LEADER>ca',
vim.lsp.buf.code_action,
{ desc = 'code action' }
)
end
if client.supports_method('textDocument/codeLens') then
set(
{ 'n', 'v' },
'<LEADER>cc',
vim.lsp.codelens.run,
{ desc = 'run codelens' }
)
set(
'n',
'gr',
vim.lsp.buf.references,
{ desc = 'references', nowait = true }
'<LEADER>cC',
vim.lsp.codelens.refresh,
{ desc = 'refresh & display codelens' }
)
end
set('n', 'gy', vim.lsp.buf.type_definition, { desc = 'type definition' })
if client.supports_method('textDocument/definition') then
set('n', 'gd', vim.lsp.buf.definition, { desc = 'definition' })
end
set('n', 'K', function()
return vim.lsp.buf.hover()
end, { desc = 'hover' })
if client.supports_method('textDocument/documentHighlight') then
if Snacks.words.is_enabled() then
set('n', '<A-n>', function()
Snacks.words.jump(vim.v.count1, true)
end, { desc = 'next reference' })
if client.supports_method('textDocument/codeAction') then
set(
{ 'n', 'v' },
'<LEADER>ca',
vim.lsp.buf.code_action,
{ desc = 'code action' }
)
set('n', '<A-p>', function()
Snacks.words.jump(-vim.v.count1, true)
end, { desc = 'prev reference' })
set('n', ']]', function()
Snacks.words.jump(vim.v.count1)
end, { desc = 'next reference' })
set('n', '[[', function()
Snacks.words.jump(-vim.v.count1)
end, { desc = 'prev reference' })
end
end
if client.supports_method('textDocument/codeLens') then
set(
{ 'n', 'v' },
'<LEADER>cc',
vim.lsp.codelens.run,
{ desc = 'run codelens' }
)
if client.supports_method('textDocument/rename') then
set('n', '<LEADER>cr', vim.lsp.buf.rename, { desc = 'rename' })
end
set(
'n',
'<LEADER>cC',
vim.lsp.codelens.refresh,
{ desc = 'refresh & display codelens' }
)
end
if client.supports_method('textDocument/signatureHelp') then
set('i', '<C-k>', function()
return vim.lsp.buf.signature_help()
end, { desc = 'signature help' })
if client.supports_method('textDocument/definition') then
set('n', 'gd', vim.lsp.buf.definition, { desc = 'definition' })
end
set('n', 'gK', function()
vim.lsp.buf.signature_help()
end, { desc = 'signature help' })
end
if client.supports_method('textDocument/documentHighlight') then
if Snacks.words.is_enabled() then
set('n', '<A-n>', function()
Snacks.words.jump(vim.v.count1, true)
end, { desc = 'next reference' })
set('n', '<A-p>', function()
Snacks.words.jump(-vim.v.count1, true)
end, { desc = 'prev reference' })
set('n', ']]', function()
Snacks.words.jump(vim.v.count1)
end, { desc = 'next reference' })
set('n', '[[', function()
Snacks.words.jump(-vim.v.count1)
end, { desc = 'prev reference' })
end
end
if client.supports_method('textDocument/rename') then
set('n', '<LEADER>cr', vim.lsp.buf.rename, { desc = 'rename' })
end
if client.supports_method('textDocument/signatureHelp') then
set('i', '<C-k>', function()
return vim.lsp.buf.signature_help()
end, { desc = 'signature help' })
set('n', 'gK', function()
vim.lsp.buf.signature_help()
end, { desc = 'signature help' })
end
if
client.supports_method('workspace/didRenameFiles')
or client.supports_method('workspace/willRenameFiles')
then
set('n', '<LEADER>cR', function()
Snacks.rename.rename_file()
end, { desc = 'rename file' })
end
end,
})
if
client.supports_method('workspace/didRenameFiles')
or client.supports_method('workspace/willRenameFiles')
then
set('n', '<LEADER>cR', function()
Snacks.rename.rename_file()
end, { desc = 'rename file' })
end
end)

View file

@ -248,47 +248,39 @@ return {
require('fzf-lua').setup(opts)
-- LSP keybinds.
vim.api.nvim_create_autocmd('LspAttach', {
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
MarleyVim.lsp.on_attach(function(client)
local set = vim.keymap.set
if not client then
return
end
local pickOpts =
{ jump_to_single_result = true, ignore_current_line = true }
local set = vim.keymap.set
local pickOpts =
{ jump_to_single_result = true, ignore_current_line = true }
set(
'n',
'gI',
pick('lsp_implementations', pickOpts),
{ desc = 'implementation' }
)
set(
'n',
'gr',
pick('lsp_references', pickOpts),
{ desc = 'references', nowait = true }
)
set(
'n',
'gy',
pick('lsp_typedefs', pickOpts),
{ desc = 't[y]pe definition' }
)
if client.supports_method('textDocument/definition') then
set(
'n',
'gI',
pick('lsp_implementations', pickOpts),
{ desc = 'implementation' }
'gd',
pick('lsp_definitions', pickOpts),
{ desc = 'definition' }
)
set(
'n',
'gr',
pick('lsp_references', pickOpts),
{ desc = 'references', nowait = true }
)
set(
'n',
'gy',
pick('lsp_typedefs', pickOpts),
{ desc = 't[y]pe definition' }
)
if client.supports_method('textDocument/definition') then
set(
'n',
'gd',
pick('lsp_definitions', pickOpts),
{ desc = 'definition' }
)
end
end,
})
end
end)
end,
}