feat(lsp): add more LSP defaults (#28500)

- crn for rename
- crr for code actions
- gr for references
- <C-S> (in Insert mode) for signature help
This commit is contained in:
Gregory Anders 2024-04-26 11:12:49 -05:00 committed by GitHub
parent 9b028bd64f
commit 6888607415
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 274 additions and 239 deletions

View File

@ -281,6 +281,10 @@ gr{char} Replace the virtual characters under the cursor with
that have a special meaning in Insert mode, such as that have a special meaning in Insert mode, such as
most CTRL-keys, cannot be used. most CTRL-keys, cannot be used.
*gr-default*
Mapped to |vim.lsp.buf.references()| by default.
|default-mappings|
*digraph-arg* *digraph-arg*
The argument for Normal mode commands like |r| and |t| is a single character. The argument for Normal mode commands like |r| and |t| is a single character.
When 'cpo' doesn't contain the 'D' flag, this character can also be entered When 'cpo' doesn't contain the 'D' flag, this character can also be entered

View File

@ -61,47 +61,41 @@ options are not restored when the LSP client is stopped or detached.
- |K| is mapped to |vim.lsp.buf.hover()| unless |'keywordprg'| is customized or - |K| is mapped to |vim.lsp.buf.hover()| unless |'keywordprg'| is customized or
a custom keymap for `K` exists. a custom keymap for `K` exists.
*crr* *v_crr* *crn* *i_CTRL-S*
Some keymaps are created unconditionally when Nvim starts:
- "crn" is mapped in Normal mode to |vim.lsp.buf.rename()|
- "crr" is mapped in Normal and Visual mode to |vim.lsp.buf.code_action()|
- "gr" is mapped in Normal mode to |vim.lsp.buf.references()| |gr-default|
- CTRL-S is mapped in Insert mode to |vim.lsp.buf.signature_help()|
If not wanted, these keymaps can be removed at any time using
|vim.keymap.del()| or |:unmap|.
*lsp-defaults-disable* *lsp-defaults-disable*
To override the above defaults, set or unset the options on |LspAttach|: >lua To override the above defaults, set or unset the options on |LspAttach|: >lua
vim.api.nvim_create_autocmd('LspAttach', { vim.api.nvim_create_autocmd('LspAttach', {
callback = function(ev) callback = function(ev)
vim.bo[ev.buf].formatexpr = nil vim.bo[ev.buf].formatexpr = nil
vim.bo[ev.buf].omnifunc = nil vim.bo[ev.buf].omnifunc = nil
vim.keymap.del("n", "K", { buffer = ev.buf }) vim.keymap.del('n', 'K', { buffer = ev.buf })
end, end,
}) })
To use other LSP features like hover, rename, etc. you can set other keymaps To use other LSP features, set keymaps on |LspAttach|. Not all language
on |LspAttach|. Example: >lua servers provide the same capabilities. To ensure you only set keymaps if the
vim.api.nvim_create_autocmd('LspAttach', { language server supports a feature, guard keymaps behind capability checks.
callback = function(args) Example: >lua
vim.keymap.set('n', 'K', vim.lsp.buf.hover, { buffer = args.buf })
end,
})
The most common functions are:
- |vim.lsp.buf.hover()|
- |vim.lsp.buf.format()|
- |vim.lsp.buf.references()|
- |vim.lsp.buf.implementation()|
- |vim.lsp.buf.code_action()|
Not all language servers provide the same capabilities. To ensure you only set
keymaps if the language server supports a feature, you can guard the keymap
calls behind capability checks:
>lua
vim.api.nvim_create_autocmd('LspAttach', { vim.api.nvim_create_autocmd('LspAttach', {
callback = function(args) callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id) local client = vim.lsp.get_client_by_id(args.data.client_id)
if client.server_capabilities.hoverProvider then if client.supports_method('textDocument/implementation') then
vim.keymap.set('n', 'K', vim.lsp.buf.hover, { buffer = args.buf }) vim.keymap.set('n', 'g<C-I>', vim.lsp.buf.implementation, { buffer = args.buf })
end end
end, end,
}) })
< <
To learn what capabilities are available you can run the following command in To learn what capabilities are available you can run the following command in
a buffer with a started LSP client: >vim a buffer with a started LSP client: >vim

View File

@ -408,6 +408,10 @@ The following changes to existing APIs or features add new behavior.
• 'comments' includes "fb:•". • 'comments' includes "fb:•".
• 'shortmess' includes the "C" flag. • 'shortmess' includes the "C" flag.
• 'grepprg' defaults to using ripgrep if available. • 'grepprg' defaults to using ripgrep if available.
• |crn| in Normal mode maps to |vim.lsp.buf.rename()|.
• |crr| in Normal and Visual mode maps to |vim.lsp.buf.code_action()|.
• "gr" in Normal mode maps to |vim.lsp.buf.references()| |gr-default|
• |i_CTRL-S| in Insert mode maps to |vim.lsp.buf.signature_help()|
• Automatic linting of treesitter query files (see |ft-query-plugin|). • Automatic linting of treesitter query files (see |ft-query-plugin|).
Can be disabled via: >lua Can be disabled via: >lua
vim.g.query_lint_on = {} vim.g.query_lint_on = {}
@ -438,9 +442,10 @@ The following changes to existing APIs or features add new behavior.
:call netrw#BrowseX(expand(exists("g:netrw_gx")? g:netrw_gx : '<cfile>'), netrw#CheckIfRemote())<CR> :call netrw#BrowseX(expand(exists("g:netrw_gx")? g:netrw_gx : '<cfile>'), netrw#CheckIfRemote())<CR>
• |vim.lsp.start()| now maps |K| to use |vim.lsp.buf.hover()| if the server • |vim.lsp.start()| now creates the following default keymaps (assuming the
supports it, unless |'keywordprg'| was customized before calling server supports the feature):
|vim.lsp.start()|. - |K| in Normal mode maps to |vim.lsp.buf.hover()|, unless |'keywordprg'|
was customized before calling |vim.lsp.start()|.
• Terminal buffers started with no arguments (and use 'shell') close • Terminal buffers started with no arguments (and use 'shell') close
automatically if the job exited without error, eliminating the (often automatically if the job exited without error, eliminating the (often

View File

@ -137,6 +137,10 @@ of these in your config by simply removing the mapping, e.g. ":unmap Y".
- * |v_star-default| - * |v_star-default|
- gc |gc-default| |v_gc-default| |o_gc-default| - gc |gc-default| |v_gc-default| |o_gc-default|
- gcc |gcc-default| - gcc |gcc-default|
- |crn|
- |crr|
- gr |gr-default|
- <C-S> |i_CTRL-S|
- Nvim LSP client defaults |lsp-defaults| - Nvim LSP client defaults |lsp-defaults|
- K |K-lsp-default| - K |K-lsp-default|

View File

@ -144,6 +144,31 @@ do
end end
vim.keymap.set({ 'o' }, 'gc', textobject_rhs, { desc = 'Comment textobject' }) vim.keymap.set({ 'o' }, 'gc', textobject_rhs, { desc = 'Comment textobject' })
end end
--- Default maps for LSP functions.
---
--- These are mapped unconditionally to avoid confusion. If no server is attached, or if a server
--- does not support a capability, an error message is displayed rather than exhibiting different
--- behavior.
---
--- See |gr-default|, |crn|, |crr|, |i_CTRL-S|.
do
vim.keymap.set('n', 'crn', function()
vim.lsp.buf.rename()
end, { desc = 'vim.lsp.buf.rename()' })
vim.keymap.set({ 'n', 'v' }, 'crr', function()
vim.lsp.buf.code_action()
end, { desc = 'vim.lsp.buf.code_action()' })
vim.keymap.set('n', 'gr', function()
vim.lsp.buf.references()
end, { desc = 'vim.lsp.buf.references()' })
vim.keymap.set('i', '<C-S>', function()
vim.lsp.buf.signature_help()
end, { desc = 'vim.lsp.buf.signature_help()' })
end
end end
--- Default menus --- Default menus
@ -243,7 +268,6 @@ do
vim.notify(('W325: Ignoring swapfile from Nvim process %d'):format(info.pid)) vim.notify(('W325: Ignoring swapfile from Nvim process %d'):format(info.pid))
end, end,
}) })
end
-- Only do the following when the TUI is attached -- Only do the following when the TUI is attached
local tty = nil local tty = nil
@ -499,7 +523,10 @@ if tty then
end end
end end
end end
end
--- Default options
do
--- Default 'grepprg' to ripgrep if available. --- Default 'grepprg' to ripgrep if available.
if vim.fn.executable('rg') == 1 then if vim.fn.executable('rg') == 1 then
-- Match :grep default, otherwise rg searches cwd by default -- Match :grep default, otherwise rg searches cwd by default
@ -507,3 +534,4 @@ if vim.fn.executable('rg') == 1 then
vim.o.grepprg = 'rg --vimgrep -uuu $* ' .. (vim.fn.has('unix') == 1 and '/dev/null' or 'nul') vim.o.grepprg = 'rg --vimgrep -uuu $* ' .. (vim.fn.has('unix') == 1 and '/dev/null' or 'nul')
vim.o.grepformat = '%f:%l:%c:%m' vim.o.grepformat = '%f:%l:%c:%m'
end end
end

View File

@ -348,7 +348,7 @@ function lsp._set_defaults(client, bufnr)
and is_empty_or_default(bufnr, 'keywordprg') and is_empty_or_default(bufnr, 'keywordprg')
and vim.fn.maparg('K', 'n', false, false) == '' and vim.fn.maparg('K', 'n', false, false) == ''
then then
vim.keymap.set('n', 'K', vim.lsp.buf.hover, { buffer = bufnr }) vim.keymap.set('n', 'K', vim.lsp.buf.hover, { buffer = bufnr, desc = 'vim.lsp.buf.hover()' })
end end
end) end)
if client.supports_method(ms.textDocument_diagnostic) then if client.supports_method(ms.textDocument_diagnostic) then