feat(lsp): defaults: tagfunc, omnifunc (#19003)

set `tagfunc` to `vim.lsp.tagfunc` and `omnifunc` to `vim.lsp.omnifunc` if empty when attaching a server
This commit is contained in:
Mathias Fußenegger 2022-07-10 17:26:43 +02:00 committed by GitHub
parent 67b26a39f0
commit 95c65a6b22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 9 deletions

View File

@ -51,15 +51,14 @@ Starting a LSP client will automatically report diagnostics via
|vim.diagnostic|. Read |vim.diagnostic.config| to learn how to customize the |vim.diagnostic|. Read |vim.diagnostic.config| to learn how to customize the
display. display.
To get completion from the LSP server you can enable the |vim.lsp.omnifunc|: It also sets some buffer options if the options are otherwise empty and if the
> language server supports the functionality.
vim.bo.omnifunc = 'v:lua.vim.lsp.omnifunc'
<
To trigger completion, use |i_CTRL-X_CTRL-O|
To get features like go-to-definition you can enable the |vim.lsp.tagfunc| - |omnifunc| is set to |vim.lsp.omnifunc|. This allows to trigger completion
which changes commands like |:tjump| to utilize the language server and also using |i_CTRL-X_CTRL-o|
enables keymaps like |CTLR-]|, |CTRL-W_]|, |CTRL-W_}| and many more. - |tagfunc| is set to |vim.lsp.tagfunc|. This enables features like
go-to-definition, |:tjump|, and keymaps like |CTRL-]|, |CTRL-W_]|,
|CTRL-W_}| to utilize the language server.
To use other LSP features like hover, rename, etc. you can setup some To use other LSP features like hover, rename, etc. you can setup some
additional keymaps. It's recommended to setup them in a |LspAttach| autocmd to additional keymaps. It's recommended to setup them in a |LspAttach| autocmd to

View File

@ -562,7 +562,7 @@ end
---@private ---@private
--- Default handler for the 'textDocument/didOpen' LSP notification. --- Default handler for the 'textDocument/didOpen' LSP notification.
--- ---
---@param bufnr (Number) Number of the buffer, or 0 for current ---@param bufnr number Number of the buffer, or 0 for current
---@param client Client object ---@param client Client object
local function text_document_did_open_handler(bufnr, client) local function text_document_did_open_handler(bufnr, client)
changetracking.init(client, bufnr) changetracking.init(client, bufnr)
@ -947,6 +947,28 @@ function lsp.start_client(config)
end end
end end
---@private
local function set_defaults(client, bufnr)
if client.server_capabilities.definitionProvider and vim.bo[bufnr].tagfunc == '' then
vim.bo[bufnr].tagfunc = 'v:lua.vim.lsp.tagfunc'
end
if client.server_capabilities.completionProvider and vim.bo[bufnr].omnifunc == '' then
vim.bo[bufnr].omnifunc = 'v:lua.vim.lsp.omnifunc'
end
end
---@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)
if vim.bo[bufnr].tagfunc == 'v:lua.vim.lsp.tagfunc' then
vim.bo[bufnr].tagfunc = nil
end
if vim.bo[bufnr].omnifunc == 'v:lua.vim.lsp.omnifunc' then
vim.bo[bufnr].omnifunc = nil
end
end
---@private ---@private
--- Invoked on client exit. --- Invoked on client exit.
--- ---
@ -972,6 +994,11 @@ function lsp.start_client(config)
client_ids[client_id] = nil client_ids[client_id] = nil
end end
if vim.tbl_isempty(client_ids) then
vim.schedule(function()
unset_defaults(bufnr)
end)
end
end end
active_clients[client_id] = nil active_clients[client_id] = nil
@ -1325,6 +1352,8 @@ function lsp.start_client(config)
function client._on_attach(bufnr) function client._on_attach(bufnr)
text_document_did_open_handler(bufnr, client) text_document_did_open_handler(bufnr, client)
set_defaults(client, bufnr)
nvim_exec_autocmds('LspAttach', { nvim_exec_autocmds('LspAttach', {
buffer = bufnr, buffer = bufnr,
modeline = false, modeline = false,