diff --git a/nvim/lua/lib/init.lua b/nvim/lua/lib/init.lua index a69fc53..c7b9bcf 100644 --- a/nvim/lua/lib/init.lua +++ b/nvim/lua/lib/init.lua @@ -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? diff --git a/nvim/lua/lib/lsp.lua b/nvim/lua/lib/lsp.lua new file mode 100644 index 0000000..ead14c1 --- /dev/null +++ b/nvim/lua/lib/lsp.lua @@ -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 diff --git a/nvim/lua/lsp.lua b/nvim/lua/lsp.lua index 8c7f8f2..fdc9b96 100644 --- a/nvim/lua/lsp.lua +++ b/nvim/lua/lsp.lua @@ -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' }, + 'ca', + vim.lsp.buf.code_action, + { desc = 'code action' } + ) + end + + if client.supports_method('textDocument/codeLens') then + set( + { 'n', 'v' }, + 'cc', + vim.lsp.codelens.run, + { desc = 'run codelens' } + ) set( 'n', - 'gr', - vim.lsp.buf.references, - { desc = 'references', nowait = true } + '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', '', function() + Snacks.words.jump(vim.v.count1, true) + end, { desc = 'next reference' }) - if client.supports_method('textDocument/codeAction') then - set( - { 'n', 'v' }, - 'ca', - vim.lsp.buf.code_action, - { desc = 'code action' } - ) + set('n', '', 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' }, - 'cc', - vim.lsp.codelens.run, - { desc = 'run codelens' } - ) + if client.supports_method('textDocument/rename') then + set('n', 'cr', vim.lsp.buf.rename, { desc = 'rename' }) + end - set( - 'n', - 'cC', - vim.lsp.codelens.refresh, - { desc = 'refresh & display codelens' } - ) - end + if client.supports_method('textDocument/signatureHelp') then + set('i', '', 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', '', function() - Snacks.words.jump(vim.v.count1, true) - end, { desc = 'next reference' }) - - set('n', '', 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', 'cr', vim.lsp.buf.rename, { desc = 'rename' }) - end - - if client.supports_method('textDocument/signatureHelp') then - set('i', '', 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', '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', 'cR', function() + Snacks.rename.rename_file() + end, { desc = 'rename file' }) + end +end) diff --git a/nvim/lua/plugins/editor/fzf-lua.lua b/nvim/lua/plugins/editor/fzf-lua.lua index aa03b49..c15a470 100644 --- a/nvim/lua/plugins/editor/fzf-lua.lua +++ b/nvim/lua/plugins/editor/fzf-lua.lua @@ -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, }