feat(lsp): map K to hover by default #24331

Related: https://github.com/neovim/neovim/issues/24252
This commit is contained in:
Mathias Fußenegger 2023-07-14 18:47:18 +02:00 committed by GitHub
parent fd9ac5aa8e
commit 33e1a8cd70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 2 deletions

View File

@ -58,6 +58,8 @@ options are not restored when the LSP client is stopped or detached.
- 'formatexpr' is set to |vim.lsp.formatexpr()|, so you can format lines via
|gq| if the language server supports it.
- To opt out of this use |gw| instead of gq, or set 'formatexpr' on LspAttach.
- |K| is mapped to |vim.lsp.buf.hover()| unless |'keywordprg'| is customized or
a custom keymap for `K` exists.
*lsp-defaults-disable*
To override the above defaults, set or unset the options on |LspAttach|: >lua
@ -65,6 +67,7 @@ To override the above defaults, set or unset the options on |LspAttach|: >lua
callback = function(ev)
vim.bo[ev.buf].formatexpr = nil
vim.bo[ev.buf].omnifunc = nil
vim.keymap.del("n", "K", { bufnr = ev.buf })
end,
})

View File

@ -156,6 +156,10 @@ The following changes to existing APIs or features add new behavior.
`vim.ui.open` or remap `gx`. To continue using netrw (deprecated): >vim
: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
supports it, unless |'keywordprg'| was customized before calling
|vim.lsp.start()|.
==============================================================================
REMOVED FEATURES *news-removed*

View File

@ -963,6 +963,15 @@ function lsp._set_defaults(client, bufnr)
then
vim.bo[bufnr].formatexpr = 'v:lua.vim.lsp.formatexpr()'
end
api.nvim_buf_call(bufnr, function()
if
client.supports_method('textDocument/hover')
and is_empty_or_default(bufnr, 'keywordprg')
and vim.fn.maparg('K', 'n', false, false) == ''
then
vim.keymap.set('n', 'K', vim.lsp.buf.hover, { buffer = bufnr })
end
end)
end
--- @class lsp.ClientConfig
@ -1202,7 +1211,7 @@ function lsp.start_client(config)
---@private
--- Reset defaults set by `set_defaults`.
--- Must only be called if the last client attached to a buffer exits.
local function unset_defaults(bufnr)
local function reset_defaults(bufnr)
if vim.bo[bufnr].tagfunc == 'v:lua.vim.lsp.tagfunc' then
vim.bo[bufnr].tagfunc = nil
end
@ -1212,6 +1221,12 @@ function lsp.start_client(config)
if vim.bo[bufnr].formatexpr == 'v:lua.vim.lsp.formatexpr()' then
vim.bo[bufnr].formatexpr = nil
end
api.nvim_buf_call(bufnr, function()
local keymap = vim.fn.maparg('K', 'n', false, true)
if keymap and keymap.callback == vim.lsp.buf.hover then
vim.keymap.del('n', 'K', { buffer = bufnr })
end
end)
end
---@private
@ -1243,7 +1258,7 @@ function lsp.start_client(config)
client_ids[client_id] = nil
if vim.tbl_isempty(client_ids) then
unset_defaults(bufnr)
reset_defaults(bufnr)
end
end)
end

View File

@ -939,6 +939,7 @@ function tests.set_defaults_all_capabilities()
definitionProvider = true,
completionProvider = true,
documentRangeFormattingProvider = true,
hoverProvider = true,
}
}
end;

View File

@ -365,6 +365,14 @@ describe('LSP', function()
eq('v:lua.vim.lsp.tagfunc', get_buf_option("tagfunc"))
eq('v:lua.vim.lsp.omnifunc', get_buf_option("omnifunc"))
eq('v:lua.vim.lsp.formatexpr()', get_buf_option("formatexpr"))
eq('', get_buf_option("keywordprg"))
eq(true, exec_lua[[
local keymap
vim.api.nvim_buf_call(BUFFER, function()
keymap = vim.fn.maparg("K", "n", false, true)
end)
return keymap.callback == vim.lsp.buf.hover
]])
client.stop()
end
end;
@ -372,6 +380,13 @@ describe('LSP', function()
eq('', get_buf_option("tagfunc"))
eq('', get_buf_option("omnifunc"))
eq('', get_buf_option("formatexpr"))
eq('', exec_lua[[
local keymap
vim.api.nvim_buf_call(BUFFER, function()
keymap = vim.fn.maparg("K", "n", false, false)
end)
return keymap
]])
end;
}
end)