diff options
-rw-r--r-- | runtime/doc/lsp.txt | 17 | ||||
-rw-r--r-- | runtime/lua/vim/lsp.lua | 31 |
2 files changed, 38 insertions, 10 deletions
diff --git a/runtime/doc/lsp.txt b/runtime/doc/lsp.txt index c1d7f3a45a..78100d5277 100644 --- a/runtime/doc/lsp.txt +++ b/runtime/doc/lsp.txt @@ -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 display. -To get completion from the LSP server you can enable the |vim.lsp.omnifunc|: -> - 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| -which changes commands like |:tjump| to utilize the language server and also -enables keymaps like |CTLR-]|, |CTRL-W_]|, |CTRL-W_}| and many more. +It also sets some buffer options if the options are otherwise empty and if the +language server supports the functionality. + +- |omnifunc| is set to |vim.lsp.omnifunc|. This allows to trigger completion + using |i_CTRL-X_CTRL-o| +- |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 additional keymaps. It's recommended to setup them in a |LspAttach| autocmd to diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua index 75c3b63da7..e11f127f47 100644 --- a/runtime/lua/vim/lsp.lua +++ b/runtime/lua/vim/lsp.lua @@ -562,7 +562,7 @@ end ---@private --- 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 local function text_document_did_open_handler(bufnr, client) changetracking.init(client, bufnr) @@ -948,6 +948,28 @@ function lsp.start_client(config) 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 --- Invoked on client exit. --- ---@param code (number) exit code of the process @@ -972,6 +994,11 @@ function lsp.start_client(config) client_ids[client_id] = nil end + if vim.tbl_isempty(client_ids) then + vim.schedule(function() + unset_defaults(bufnr) + end) + end end active_clients[client_id] = nil @@ -1325,6 +1352,8 @@ function lsp.start_client(config) function client._on_attach(bufnr) text_document_did_open_handler(bufnr, client) + set_defaults(client, bufnr) + nvim_exec_autocmds('LspAttach', { buffer = bufnr, modeline = false, |