2022-07-15 09:26:47 -07:00
|
|
|
local api = vim.api
|
2019-11-13 13:55:26 -07:00
|
|
|
local validate = vim.validate
|
|
|
|
|
2024-01-22 10:23:28 -07:00
|
|
|
local lsp = vim._defer_require('vim.lsp', {
|
|
|
|
_changetracking = ..., --- @module 'vim.lsp._changetracking'
|
2024-11-29 05:40:32 -07:00
|
|
|
_folding_range = ..., --- @module 'vim.lsp._folding_range'
|
2024-01-22 10:23:28 -07:00
|
|
|
_snippet_grammar = ..., --- @module 'vim.lsp._snippet_grammar'
|
2024-02-03 15:47:56 -07:00
|
|
|
_tagfunc = ..., --- @module 'vim.lsp._tagfunc'
|
2024-01-22 10:23:28 -07:00
|
|
|
_watchfiles = ..., --- @module 'vim.lsp._watchfiles'
|
|
|
|
buf = ..., --- @module 'vim.lsp.buf'
|
2024-02-07 10:22:03 -07:00
|
|
|
client = ..., --- @module 'vim.lsp.client'
|
2024-01-22 10:23:28 -07:00
|
|
|
codelens = ..., --- @module 'vim.lsp.codelens'
|
2024-02-04 15:13:23 -07:00
|
|
|
completion = ..., --- @module 'vim.lsp.completion'
|
2024-01-22 10:23:28 -07:00
|
|
|
diagnostic = ..., --- @module 'vim.lsp.diagnostic'
|
|
|
|
handlers = ..., --- @module 'vim.lsp.handlers'
|
|
|
|
inlay_hint = ..., --- @module 'vim.lsp.inlay_hint'
|
|
|
|
log = ..., --- @module 'vim.lsp.log'
|
|
|
|
protocol = ..., --- @module 'vim.lsp.protocol'
|
|
|
|
rpc = ..., --- @module 'vim.lsp.rpc'
|
|
|
|
semantic_tokens = ..., --- @module 'vim.lsp.semantic_tokens'
|
|
|
|
util = ..., --- @module 'vim.lsp.util'
|
|
|
|
})
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 20:21:34 -07:00
|
|
|
|
2024-01-22 10:23:28 -07:00
|
|
|
local log = lsp.log
|
|
|
|
local protocol = lsp.protocol
|
|
|
|
local ms = protocol.Methods
|
|
|
|
local util = lsp.util
|
|
|
|
local changetracking = lsp._changetracking
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 20:21:34 -07:00
|
|
|
|
2024-01-22 10:23:28 -07:00
|
|
|
-- Export these directly from rpc.
|
|
|
|
---@nodoc
|
|
|
|
lsp.rpc_response_error = lsp.rpc.rpc_response_error
|
2019-11-13 13:55:26 -07:00
|
|
|
|
2024-11-05 10:34:21 -07:00
|
|
|
lsp._resolve_to_request = {
|
|
|
|
[ms.codeAction_resolve] = ms.textDocument_codeAction,
|
|
|
|
[ms.codeLens_resolve] = ms.textDocument_codeLens,
|
|
|
|
[ms.documentLink_resolve] = ms.textDocument_documentLink,
|
|
|
|
[ms.inlayHint_resolve] = ms.textDocument_inlayHint,
|
|
|
|
}
|
|
|
|
|
2022-04-30 02:22:30 -07:00
|
|
|
-- maps request name to the required server_capability in the client.
|
2020-10-24 21:28:15 -07:00
|
|
|
lsp._request_name_to_capability = {
|
2023-08-03 04:03:48 -07:00
|
|
|
[ms.callHierarchy_incomingCalls] = { 'callHierarchyProvider' },
|
|
|
|
[ms.callHierarchy_outgoingCalls] = { 'callHierarchyProvider' },
|
2024-08-20 06:37:03 -07:00
|
|
|
[ms.codeAction_resolve] = { 'codeActionProvider', 'resolveProvider' },
|
|
|
|
[ms.codeLens_resolve] = { 'codeLensProvider', 'resolveProvider' },
|
|
|
|
[ms.documentLink_resolve] = { 'documentLinkProvider', 'resolveProvider' },
|
|
|
|
[ms.inlayHint_resolve] = { 'inlayHintProvider', 'resolveProvider' },
|
2023-08-03 04:03:48 -07:00
|
|
|
[ms.textDocument_codeAction] = { 'codeActionProvider' },
|
|
|
|
[ms.textDocument_codeLens] = { 'codeLensProvider' },
|
|
|
|
[ms.textDocument_completion] = { 'completionProvider' },
|
2024-08-20 06:37:03 -07:00
|
|
|
[ms.textDocument_declaration] = { 'declarationProvider' },
|
|
|
|
[ms.textDocument_definition] = { 'definitionProvider' },
|
2023-08-03 04:03:48 -07:00
|
|
|
[ms.textDocument_diagnostic] = { 'diagnosticProvider' },
|
2024-05-19 10:03:06 -07:00
|
|
|
[ms.textDocument_didClose] = { 'textDocumentSync', 'openClose' },
|
|
|
|
[ms.textDocument_didOpen] = { 'textDocumentSync', 'openClose' },
|
2024-08-20 06:37:03 -07:00
|
|
|
[ms.textDocument_documentColor] = { 'colorProvider' },
|
|
|
|
[ms.textDocument_documentHighlight] = { 'documentHighlightProvider' },
|
|
|
|
[ms.textDocument_documentLink] = { 'documentLinkProvider' },
|
|
|
|
[ms.textDocument_documentSymbol] = { 'documentSymbolProvider' },
|
2024-11-29 05:40:32 -07:00
|
|
|
[ms.textDocument_foldingRange] = { 'foldingRangeProvider' },
|
2024-08-20 06:37:03 -07:00
|
|
|
[ms.textDocument_formatting] = { 'documentFormattingProvider' },
|
|
|
|
[ms.textDocument_hover] = { 'hoverProvider' },
|
|
|
|
[ms.textDocument_implementation] = { 'implementationProvider' },
|
|
|
|
[ms.textDocument_inlayHint] = { 'inlayHintProvider' },
|
|
|
|
[ms.textDocument_inlineValue] = { 'inlineValueProvider' },
|
|
|
|
[ms.textDocument_linkedEditingRange] = { 'linkedEditingRangeProvider' },
|
|
|
|
[ms.textDocument_moniker] = { 'monikerProvider' },
|
|
|
|
[ms.textDocument_onTypeFormatting] = { 'documentOnTypeFormattingProvider' },
|
|
|
|
[ms.textDocument_prepareCallHierarchy] = { 'callHierarchyProvider' },
|
|
|
|
[ms.textDocument_prepareRename] = { 'renameProvider', 'prepareProvider' },
|
|
|
|
[ms.textDocument_prepareTypeHierarchy] = { 'typeHierarchyProvider' },
|
|
|
|
[ms.textDocument_rangeFormatting] = { 'documentRangeFormattingProvider' },
|
|
|
|
[ms.textDocument_rangesFormatting] = { 'documentRangeFormattingProvider', 'rangesSupport' },
|
|
|
|
[ms.textDocument_references] = { 'referencesProvider' },
|
|
|
|
[ms.textDocument_rename] = { 'renameProvider' },
|
|
|
|
[ms.textDocument_selectionRange] = { 'selectionRangeProvider' },
|
|
|
|
[ms.textDocument_semanticTokens_full] = { 'semanticTokensProvider' },
|
|
|
|
[ms.textDocument_semanticTokens_full_delta] = { 'semanticTokensProvider' },
|
|
|
|
[ms.textDocument_signatureHelp] = { 'signatureHelpProvider' },
|
|
|
|
[ms.textDocument_typeDefinition] = { 'typeDefinitionProvider' },
|
2024-05-19 10:03:06 -07:00
|
|
|
[ms.textDocument_willSaveWaitUntil] = { 'textDocumentSync', 'willSaveWaitUntil' },
|
2024-08-20 06:37:03 -07:00
|
|
|
[ms.textDocument_willSave] = { 'textDocumentSync', 'willSave' },
|
|
|
|
[ms.typeHierarchy_subtypes] = { 'typeHierarchyProvider' },
|
|
|
|
[ms.typeHierarchy_supertypes] = { 'typeHierarchyProvider' },
|
|
|
|
[ms.workspace_executeCommand] = { 'executeCommandProvider' },
|
|
|
|
[ms.workspace_symbol] = { 'workspaceSymbolProvider' },
|
2020-10-24 21:28:15 -07:00
|
|
|
}
|
|
|
|
|
2019-11-13 13:55:26 -07:00
|
|
|
-- TODO improve handling of scratch buffers with LSP attached.
|
|
|
|
|
2021-08-22 13:55:28 -07:00
|
|
|
---@private
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 20:21:34 -07:00
|
|
|
--- Called by the client when trying to call a method that's not
|
2020-10-24 21:28:15 -07:00
|
|
|
--- supported in any of the servers registered for the current buffer.
|
2021-08-22 13:55:28 -07:00
|
|
|
---@param method (string) name of the method
|
2020-10-24 21:28:15 -07:00
|
|
|
function lsp._unsupported_method(method)
|
|
|
|
local msg = string.format(
|
|
|
|
'method %s is not supported by any of the servers registered for the current buffer',
|
|
|
|
method
|
|
|
|
)
|
|
|
|
log.warn(msg)
|
2021-10-10 22:32:50 -07:00
|
|
|
return msg
|
2020-10-24 21:28:15 -07:00
|
|
|
end
|
|
|
|
|
2024-11-24 14:43:27 -07:00
|
|
|
---@private
|
|
|
|
---@param workspace_folders string|lsp.WorkspaceFolder[]?
|
|
|
|
---@return lsp.WorkspaceFolder[]?
|
|
|
|
function lsp._get_workspace_folders(workspace_folders)
|
|
|
|
if type(workspace_folders) == 'table' then
|
|
|
|
return workspace_folders
|
|
|
|
elseif type(workspace_folders) == 'string' then
|
|
|
|
return {
|
|
|
|
{
|
|
|
|
uri = vim.uri_from_fname(workspace_folders),
|
|
|
|
name = workspace_folders,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-13 13:55:26 -07:00
|
|
|
local wait_result_reason = { [-1] = 'timeout', [-2] = 'interrupted', [-3] = 'error' }
|
|
|
|
|
2021-11-09 15:37:48 -07:00
|
|
|
local format_line_ending = {
|
|
|
|
['unix'] = '\n',
|
|
|
|
['dos'] = '\r\n',
|
|
|
|
['mac'] = '\r',
|
|
|
|
}
|
|
|
|
|
2023-12-17 02:54:38 -07:00
|
|
|
---@private
|
2021-11-21 10:03:45 -07:00
|
|
|
---@param bufnr (number)
|
2023-02-25 10:47:05 -07:00
|
|
|
---@return string
|
2023-12-17 02:54:38 -07:00
|
|
|
function lsp._buf_get_line_ending(bufnr)
|
2022-12-19 09:37:45 -07:00
|
|
|
return format_line_ending[vim.bo[bufnr].fileformat] or '\n'
|
2021-11-21 10:03:45 -07:00
|
|
|
end
|
|
|
|
|
2019-11-13 13:55:26 -07:00
|
|
|
-- Tracks all clients created via lsp.start_client
|
2024-03-21 08:15:20 -07:00
|
|
|
local all_clients = {} --- @type table<integer,vim.lsp.Client>
|
2019-11-13 13:55:26 -07:00
|
|
|
|
2024-02-12 06:46:32 -07:00
|
|
|
local client_errors_base = table.maxn(lsp.rpc.client_errors)
|
|
|
|
local client_errors_offset = 0
|
|
|
|
|
2024-03-02 14:11:23 -07:00
|
|
|
local function client_error(name)
|
2024-02-12 06:46:32 -07:00
|
|
|
client_errors_offset = client_errors_offset + 1
|
2024-03-02 14:11:23 -07:00
|
|
|
local index = client_errors_base + client_errors_offset
|
|
|
|
return { [name] = index, [index] = name }
|
2024-02-12 06:46:32 -07:00
|
|
|
end
|
|
|
|
|
2023-07-18 07:42:30 -07:00
|
|
|
--- Error codes to be used with `on_error` from |vim.lsp.start_client|.
|
|
|
|
--- Can be used to look up the string from a the number or the number
|
|
|
|
--- from the string.
|
|
|
|
--- @nodoc
|
2024-03-21 08:15:20 -07:00
|
|
|
lsp.client_errors = vim.tbl_extend(
|
2019-11-13 13:55:26 -07:00
|
|
|
'error',
|
2024-01-22 10:23:28 -07:00
|
|
|
lsp.rpc.client_errors,
|
2024-03-02 14:11:23 -07:00
|
|
|
client_error('BEFORE_INIT_CALLBACK_ERROR'),
|
|
|
|
client_error('ON_INIT_CALLBACK_ERROR'),
|
|
|
|
client_error('ON_ATTACH_ERROR'),
|
|
|
|
client_error('ON_EXIT_CALLBACK_ERROR')
|
2022-05-09 02:23:51 -07:00
|
|
|
)
|
2019-11-13 13:55:26 -07:00
|
|
|
|
2023-12-17 02:54:38 -07:00
|
|
|
---@private
|
2020-08-19 09:17:08 -07:00
|
|
|
--- Returns full text of buffer {bufnr} as a string.
|
|
|
|
---
|
2021-08-22 13:55:28 -07:00
|
|
|
---@param bufnr (number) Buffer handle, or 0 for current.
|
2023-02-25 10:47:05 -07:00
|
|
|
---@return string # Buffer text as string.
|
2023-12-17 02:54:38 -07:00
|
|
|
function lsp._buf_get_full_text(bufnr)
|
|
|
|
local line_ending = lsp._buf_get_line_ending(bufnr)
|
2024-02-11 05:37:20 -07:00
|
|
|
local text = table.concat(api.nvim_buf_get_lines(bufnr, 0, -1, true), line_ending)
|
2022-12-19 09:37:45 -07:00
|
|
|
if vim.bo[bufnr].eol then
|
2021-11-21 10:03:45 -07:00
|
|
|
text = text .. line_ending
|
2019-11-21 02:29:54 -07:00
|
|
|
end
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
2021-03-12 07:01:41 -07:00
|
|
|
--- Memoizes a function. On first run, the function return value is saved and
|
2021-04-15 04:42:25 -07:00
|
|
|
--- immediately returned on subsequent runs. If the function returns a multival,
|
|
|
|
--- only the first returned value will be memoized and returned. The function will only be run once,
|
|
|
|
--- even if it has side effects.
|
2021-03-12 07:01:41 -07:00
|
|
|
---
|
2023-06-07 05:39:41 -07:00
|
|
|
---@generic T: function
|
|
|
|
---@param fn (T) Function to run
|
|
|
|
---@return T
|
2021-03-12 07:01:41 -07:00
|
|
|
local function once(fn)
|
2023-12-13 06:04:24 -07:00
|
|
|
local value --- @type function
|
2021-04-15 04:42:25 -07:00
|
|
|
local ran = false
|
2021-03-12 07:01:41 -07:00
|
|
|
return function(...)
|
2021-04-15 04:42:25 -07:00
|
|
|
if not ran then
|
2023-12-13 06:04:24 -07:00
|
|
|
value = fn(...) --- @type function
|
2021-04-15 04:42:25 -07:00
|
|
|
ran = true
|
|
|
|
end
|
2021-03-12 07:01:41 -07:00
|
|
|
return value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-04-02 03:56:29 -07:00
|
|
|
--- @param client vim.lsp.Client
|
|
|
|
--- @param config vim.lsp.ClientConfig
|
|
|
|
--- @return boolean
|
|
|
|
local function reuse_client_default(client, config)
|
|
|
|
if client.name ~= config.name then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2024-11-24 14:43:27 -07:00
|
|
|
local config_folders = lsp._get_workspace_folders(config.workspace_folders or config.root_dir)
|
|
|
|
or {}
|
|
|
|
local config_folders_included = 0
|
|
|
|
|
|
|
|
if not next(config_folders) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, config_folder in ipairs(config_folders) do
|
|
|
|
for _, client_folder in ipairs(client.workspace_folders) do
|
|
|
|
if config_folder.uri == client_folder.uri then
|
|
|
|
config_folders_included = config_folders_included + 1
|
|
|
|
break
|
2024-04-02 03:56:29 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-24 14:43:27 -07:00
|
|
|
return config_folders_included == #config_folders
|
2024-04-02 03:56:29 -07:00
|
|
|
end
|
|
|
|
|
2024-12-04 06:14:47 -07:00
|
|
|
--- Reset defaults set by `set_defaults`.
|
|
|
|
--- Must only be called if the last client attached to a buffer exits.
|
|
|
|
local function reset_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
|
|
|
|
if vim.bo[bufnr].formatexpr == 'v:lua.vim.lsp.formatexpr()' then
|
|
|
|
vim.bo[bufnr].formatexpr = nil
|
|
|
|
end
|
|
|
|
vim._with({ buf = bufnr }, function()
|
|
|
|
local keymap = vim.fn.maparg('K', 'n', false, true)
|
|
|
|
if keymap and keymap.callback == vim.lsp.buf.hover and keymap.buffer == 1 then
|
|
|
|
vim.keymap.del('n', 'K', { buffer = bufnr })
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- @param code integer
|
|
|
|
--- @param signal integer
|
|
|
|
--- @param client_id integer
|
|
|
|
local function on_client_exit(code, signal, client_id)
|
|
|
|
local client = all_clients[client_id]
|
|
|
|
|
|
|
|
vim.schedule(function()
|
|
|
|
for bufnr in pairs(client.attached_buffers) do
|
|
|
|
if client and client.attached_buffers[bufnr] and api.nvim_buf_is_valid(bufnr) then
|
|
|
|
api.nvim_exec_autocmds('LspDetach', {
|
|
|
|
buffer = bufnr,
|
|
|
|
modeline = false,
|
|
|
|
data = { client_id = client_id },
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
client.attached_buffers[bufnr] = nil
|
|
|
|
|
|
|
|
if #lsp.get_clients({ bufnr = bufnr, _uninitialized = true }) == 0 then
|
|
|
|
reset_defaults(bufnr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local namespace = vim.lsp.diagnostic.get_namespace(client_id)
|
|
|
|
vim.diagnostic.reset(namespace)
|
|
|
|
end)
|
|
|
|
|
|
|
|
local name = client.name or 'unknown'
|
|
|
|
|
|
|
|
-- Schedule the deletion of the client object so that it exists in the execution of LspDetach
|
|
|
|
-- autocommands
|
|
|
|
vim.schedule(function()
|
|
|
|
all_clients[client_id] = nil
|
|
|
|
|
|
|
|
-- Client can be absent if executable starts, but initialize fails
|
|
|
|
-- init/attach won't have happened
|
|
|
|
if client then
|
|
|
|
changetracking.reset(client)
|
|
|
|
end
|
|
|
|
if code ~= 0 or (signal ~= 0 and signal ~= 15) then
|
|
|
|
local msg = string.format(
|
|
|
|
'Client %s quit with exit code %s and signal %s. Check log for errors: %s',
|
|
|
|
name,
|
|
|
|
code,
|
|
|
|
signal,
|
|
|
|
lsp.get_log_path()
|
|
|
|
)
|
|
|
|
vim.notify(msg, vim.log.levels.WARN)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Creates and initializes a client with the given configuration.
|
|
|
|
--- @param config vim.lsp.ClientConfig Configuration for the server.
|
|
|
|
--- @return integer? client_id |vim.lsp.get_client_by_id()| Note: client may not be
|
|
|
|
--- fully initialized. Use `on_init` to do any actions once
|
|
|
|
--- the client has been initialized.
|
|
|
|
--- @return string? # Error message, if any
|
|
|
|
local function create_and_initialize_client(config)
|
|
|
|
local ok, res = pcall(require('vim.lsp.client').create, config)
|
|
|
|
if not ok then
|
|
|
|
return nil, res --[[@as string]]
|
|
|
|
end
|
|
|
|
|
|
|
|
local client = assert(res)
|
|
|
|
|
|
|
|
--- @diagnostic disable-next-line: invisible
|
|
|
|
table.insert(client._on_exit_cbs, on_client_exit)
|
|
|
|
|
|
|
|
all_clients[client.id] = client
|
|
|
|
|
|
|
|
client:initialize()
|
|
|
|
|
|
|
|
return client.id, nil
|
|
|
|
end
|
|
|
|
|
2024-11-01 09:31:51 -07:00
|
|
|
--- @class vim.lsp.Config : vim.lsp.ClientConfig
|
|
|
|
---
|
|
|
|
--- See `cmd` in [vim.lsp.ClientConfig].
|
|
|
|
--- @field cmd? string[]|fun(dispatchers: vim.lsp.rpc.Dispatchers): vim.lsp.rpc.PublicClient
|
|
|
|
---
|
|
|
|
--- Filetypes the client will attach to, if activated by `vim.lsp.enable()`.
|
|
|
|
--- If not provided, then the client will attach to all filetypes.
|
|
|
|
--- @field filetypes? string[]
|
|
|
|
---
|
|
|
|
--- Directory markers (.e.g. '.git/') where the LSP server will base its workspaceFolders,
|
|
|
|
--- rootUri, and rootPath on initialization. Unused if `root_dir` is provided.
|
|
|
|
--- @field root_markers? string[]
|
|
|
|
---
|
|
|
|
--- Predicate used to decide if a client should be re-used. Used on all
|
|
|
|
--- running clients. The default implementation re-uses a client if name and
|
|
|
|
--- root_dir matches.
|
|
|
|
--- @field reuse_client? fun(client: vim.lsp.Client, config: vim.lsp.ClientConfig): boolean
|
|
|
|
|
|
|
|
--- Update the configuration for an LSP client.
|
|
|
|
---
|
|
|
|
--- Use name '*' to set default configuration for all clients.
|
|
|
|
---
|
|
|
|
--- Can also be table-assigned to redefine the configuration for a client.
|
|
|
|
---
|
|
|
|
--- Examples:
|
|
|
|
---
|
|
|
|
--- - Add a root marker for all clients:
|
|
|
|
--- ```lua
|
|
|
|
--- vim.lsp.config('*', {
|
|
|
|
--- root_markers = { '.git' },
|
|
|
|
--- })
|
|
|
|
--- ```
|
|
|
|
--- - Add additional capabilities to all clients:
|
|
|
|
--- ```lua
|
|
|
|
--- vim.lsp.config('*', {
|
|
|
|
--- capabilities = {
|
|
|
|
--- textDocument = {
|
|
|
|
--- semanticTokens = {
|
|
|
|
--- multilineTokenSupport = true,
|
|
|
|
--- }
|
|
|
|
--- }
|
|
|
|
--- }
|
|
|
|
--- })
|
|
|
|
--- ```
|
|
|
|
--- - (Re-)define the configuration for clangd:
|
|
|
|
--- ```lua
|
|
|
|
--- vim.lsp.config.clangd = {
|
|
|
|
--- cmd = {
|
|
|
|
--- 'clangd',
|
|
|
|
--- '--clang-tidy',
|
|
|
|
--- '--background-index',
|
|
|
|
--- '--offset-encoding=utf-8',
|
|
|
|
--- },
|
|
|
|
--- root_markers = { '.clangd', 'compile_commands.json' },
|
|
|
|
--- filetypes = { 'c', 'cpp' },
|
|
|
|
--- }
|
|
|
|
--- ```
|
|
|
|
--- - Get configuration for luals:
|
|
|
|
--- ```lua
|
|
|
|
--- local cfg = vim.lsp.config.luals
|
|
|
|
--- ```
|
|
|
|
---
|
|
|
|
--- @param name string
|
|
|
|
--- @param cfg vim.lsp.Config
|
|
|
|
--- @diagnostic disable-next-line:assign-type-mismatch
|
|
|
|
function lsp.config(name, cfg)
|
|
|
|
local _, _ = name, cfg -- ignore unused
|
|
|
|
-- dummy proto for docs
|
|
|
|
end
|
|
|
|
|
|
|
|
lsp._enabled_configs = {} --- @type table<string,{resolved_config:vim.lsp.Config?}>
|
|
|
|
|
|
|
|
--- If a config in vim.lsp.config() is accessed then the resolved config becomes invalid.
|
|
|
|
--- @param name string
|
|
|
|
local function invalidate_enabled_config(name)
|
|
|
|
if name == '*' then
|
|
|
|
for _, v in pairs(lsp._enabled_configs) do
|
|
|
|
v.resolved_config = nil
|
|
|
|
end
|
|
|
|
elseif lsp._enabled_configs[name] then
|
|
|
|
lsp._enabled_configs[name].resolved_config = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- @nodoc
|
|
|
|
--- @class vim.lsp.config
|
|
|
|
--- @field [string] vim.lsp.Config
|
|
|
|
--- @field package _configs table<string,vim.lsp.Config>
|
|
|
|
lsp.config = setmetatable({ _configs = {} }, {
|
|
|
|
--- @param self vim.lsp.config
|
|
|
|
--- @param name string
|
|
|
|
--- @return vim.lsp.Config
|
|
|
|
__index = function(self, name)
|
|
|
|
validate('name', name, 'string')
|
|
|
|
invalidate_enabled_config(name)
|
|
|
|
self._configs[name] = self._configs[name] or {}
|
|
|
|
return self._configs[name]
|
|
|
|
end,
|
|
|
|
|
|
|
|
--- @param self vim.lsp.config
|
|
|
|
--- @param name string
|
|
|
|
--- @param cfg vim.lsp.Config
|
|
|
|
__newindex = function(self, name, cfg)
|
|
|
|
validate('name', name, 'string')
|
|
|
|
validate('cfg', cfg, 'table')
|
|
|
|
invalidate_enabled_config(name)
|
|
|
|
self._configs[name] = cfg
|
|
|
|
end,
|
|
|
|
|
|
|
|
--- @param self vim.lsp.config
|
|
|
|
--- @param name string
|
|
|
|
--- @param cfg vim.lsp.Config
|
|
|
|
__call = function(self, name, cfg)
|
|
|
|
validate('name', name, 'string')
|
|
|
|
validate('cfg', cfg, 'table')
|
|
|
|
invalidate_enabled_config(name)
|
|
|
|
self[name] = vim.tbl_deep_extend('force', self._configs[name] or {}, cfg)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
--- @private
|
|
|
|
--- @param name string
|
|
|
|
--- @return vim.lsp.Config
|
|
|
|
function lsp._resolve_config(name)
|
|
|
|
local econfig = lsp._enabled_configs[name] or {}
|
|
|
|
|
|
|
|
if not econfig.resolved_config then
|
|
|
|
-- Resolve configs from lsp/*.lua
|
|
|
|
-- Calls to vim.lsp.config in lsp/* have a lower precedence than calls from other sites.
|
|
|
|
local orig_configs = lsp.config._configs
|
|
|
|
lsp.config._configs = {}
|
|
|
|
pcall(vim.cmd.runtime, { ('lsp/%s.lua'):format(name), bang = true })
|
|
|
|
local rtp_configs = lsp.config._configs
|
|
|
|
lsp.config._configs = orig_configs
|
|
|
|
|
|
|
|
local config = vim.tbl_deep_extend(
|
|
|
|
'force',
|
|
|
|
lsp.config._configs['*'] or {},
|
|
|
|
rtp_configs[name] or {},
|
|
|
|
lsp.config._configs[name] or {}
|
|
|
|
)
|
|
|
|
|
|
|
|
config.name = name
|
|
|
|
|
|
|
|
validate('cmd', config.cmd, { 'function', 'table' })
|
|
|
|
validate('cmd', config.reuse_client, 'function', true)
|
|
|
|
-- All other fields are validated in client.create
|
|
|
|
|
|
|
|
econfig.resolved_config = config
|
|
|
|
end
|
|
|
|
|
|
|
|
return assert(econfig.resolved_config)
|
|
|
|
end
|
|
|
|
|
|
|
|
local lsp_enable_autocmd_id --- @type integer?
|
|
|
|
|
|
|
|
--- @param bufnr integer
|
|
|
|
local function lsp_enable_callback(bufnr)
|
|
|
|
-- Only ever attach to buffers that represent an actual file.
|
|
|
|
if vim.bo[bufnr].buftype ~= '' then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
--- @param config vim.lsp.Config
|
|
|
|
local function can_start(config)
|
|
|
|
if config.filetypes and not vim.tbl_contains(config.filetypes, vim.bo[bufnr].filetype) then
|
|
|
|
return false
|
|
|
|
elseif type(config.cmd) == 'table' and vim.fn.executable(config.cmd[1]) == 0 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
for name in vim.spairs(lsp._enabled_configs) do
|
|
|
|
local config = lsp._resolve_config(name)
|
|
|
|
|
|
|
|
if can_start(config) then
|
|
|
|
-- Deepcopy config so changes done in the client
|
|
|
|
-- do not propagate back to the enabled configs.
|
|
|
|
config = vim.deepcopy(config)
|
|
|
|
|
|
|
|
vim.lsp.start(config, {
|
|
|
|
bufnr = bufnr,
|
|
|
|
reuse_client = config.reuse_client,
|
|
|
|
_root_markers = config.root_markers,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Enable an LSP server to automatically start when opening a buffer.
|
|
|
|
---
|
|
|
|
--- Uses configuration defined with `vim.lsp.config`.
|
|
|
|
---
|
|
|
|
--- Examples:
|
|
|
|
---
|
|
|
|
--- ```lua
|
|
|
|
--- vim.lsp.enable('clangd')
|
|
|
|
---
|
|
|
|
--- vim.lsp.enable({'luals', 'pyright'})
|
|
|
|
--- ```
|
|
|
|
---
|
|
|
|
--- @param name string|string[] Name(s) of client(s) to enable.
|
|
|
|
--- @param enable? boolean `true|nil` to enable, `false` to disable.
|
|
|
|
function lsp.enable(name, enable)
|
|
|
|
validate('name', name, { 'string', 'table' })
|
|
|
|
|
|
|
|
local names = vim._ensure_list(name) --[[@as string[] ]]
|
|
|
|
for _, nm in ipairs(names) do
|
|
|
|
if nm == '*' then
|
|
|
|
error('Invalid name')
|
|
|
|
end
|
|
|
|
lsp._enabled_configs[nm] = enable == false and nil or {}
|
|
|
|
end
|
|
|
|
|
|
|
|
if not next(lsp._enabled_configs) then
|
|
|
|
if lsp_enable_autocmd_id then
|
|
|
|
api.nvim_del_autocmd(lsp_enable_autocmd_id)
|
|
|
|
lsp_enable_autocmd_id = nil
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Only ever create autocmd once to reuse computation of config merging.
|
|
|
|
lsp_enable_autocmd_id = lsp_enable_autocmd_id
|
|
|
|
or api.nvim_create_autocmd('FileType', {
|
|
|
|
group = api.nvim_create_augroup('nvim.lsp.enable', {}),
|
|
|
|
callback = function(args)
|
|
|
|
lsp_enable_callback(args.buf)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
--- @class vim.lsp.start.Opts
|
|
|
|
--- @inlinedoc
|
2019-12-31 07:52:14 -07:00
|
|
|
---
|
2024-02-27 08:20:32 -07:00
|
|
|
--- Predicate used to decide if a client should be re-used. Used on all
|
2024-11-24 14:43:27 -07:00
|
|
|
--- running clients. The default implementation re-uses a client if it has the
|
|
|
|
--- same name and if the given workspace folders (or root_dir) are all included
|
|
|
|
--- in the client's workspace folders.
|
|
|
|
--- @field reuse_client? fun(client: vim.lsp.Client, config: vim.lsp.ClientConfig): boolean
|
2024-02-15 10:16:04 -07:00
|
|
|
---
|
2024-02-27 08:20:32 -07:00
|
|
|
--- Buffer handle to attach to if starting or re-using a client (0 for current).
|
2024-06-06 19:55:14 -07:00
|
|
|
--- @field bufnr? integer
|
2024-04-26 06:15:44 -07:00
|
|
|
---
|
2024-12-04 06:14:47 -07:00
|
|
|
--- Whether to attach the client to a buffer (default true).
|
|
|
|
--- If set to `false`, `reuse_client` and `bufnr` will be ignored.
|
|
|
|
--- @field attach? boolean
|
|
|
|
---
|
2024-04-26 06:15:44 -07:00
|
|
|
--- Suppress error reporting if the LSP server fails to start (default false).
|
2024-04-26 11:26:21 -07:00
|
|
|
--- @field silent? boolean
|
2024-11-01 09:31:51 -07:00
|
|
|
---
|
|
|
|
--- @field package _root_markers? string[]
|
2023-06-07 05:39:41 -07:00
|
|
|
|
2022-06-03 05:59:19 -07:00
|
|
|
--- Create a new LSP client and start a language server or reuses an already
|
|
|
|
--- running client if one is found matching `name` and `root_dir`.
|
|
|
|
--- Attaches the current buffer to the client.
|
|
|
|
---
|
|
|
|
--- Example:
|
2023-09-14 06:23:01 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2022-06-03 05:59:19 -07:00
|
|
|
--- vim.lsp.start({
|
|
|
|
--- name = 'my-server-name',
|
|
|
|
--- cmd = {'name-of-language-server-executable'},
|
2024-04-24 19:43:46 -07:00
|
|
|
--- root_dir = vim.fs.root(0, {'pyproject.toml', 'setup.py'}),
|
2022-06-03 05:59:19 -07:00
|
|
|
--- })
|
2023-09-14 06:23:01 -07:00
|
|
|
--- ```
|
2022-06-03 05:59:19 -07:00
|
|
|
---
|
2024-12-04 06:14:47 -07:00
|
|
|
--- See |vim.lsp.ClientConfig| for all available options. The most important are:
|
2022-06-03 05:59:19 -07:00
|
|
|
---
|
2022-10-14 08:01:13 -07:00
|
|
|
--- - `name` arbitrary name for the LSP client. Should be unique per language server.
|
2024-12-04 06:14:47 -07:00
|
|
|
--- - `cmd` command string[] or function.
|
2022-10-14 08:01:13 -07:00
|
|
|
--- - `root_dir` path to the project root. By default this is used to decide if an existing client
|
2024-09-29 05:07:21 -07:00
|
|
|
--- should be re-used. The example above uses |vim.fs.root()| to detect the root by traversing
|
|
|
|
--- the file system upwards starting from the current directory until either a `pyproject.toml`
|
|
|
|
--- or `setup.py` file is found.
|
2022-10-14 08:01:13 -07:00
|
|
|
--- - `workspace_folders` list of `{ uri:string, name: string }` tables specifying the project root
|
|
|
|
--- folders used by the language server. If `nil` the property is derived from `root_dir` for
|
|
|
|
--- convenience.
|
2022-06-03 05:59:19 -07:00
|
|
|
---
|
|
|
|
--- Language servers use this information to discover metadata like the
|
|
|
|
--- dependencies of your project and they tend to index the contents within the
|
|
|
|
--- project folder.
|
|
|
|
---
|
|
|
|
---
|
|
|
|
--- To ensure a language server is only started for languages it can handle,
|
2022-09-25 16:58:27 -07:00
|
|
|
--- make sure to call |vim.lsp.start()| within a |FileType| autocmd.
|
2022-06-03 05:59:19 -07:00
|
|
|
--- Either use |:au|, |nvim_create_autocmd()| or put the call in a
|
|
|
|
--- `ftplugin/<filetype_name>.lua` (See |ftplugin-name|)
|
|
|
|
---
|
2024-02-27 08:20:32 -07:00
|
|
|
--- @param config vim.lsp.ClientConfig Configuration for the server.
|
2024-12-04 06:14:47 -07:00
|
|
|
--- @param opts vim.lsp.start.Opts? Optional keyword arguments.
|
2024-02-27 08:20:32 -07:00
|
|
|
--- @return integer? client_id
|
2022-06-03 05:59:19 -07:00
|
|
|
function lsp.start(config, opts)
|
|
|
|
opts = opts or {}
|
2024-04-02 03:56:29 -07:00
|
|
|
local reuse_client = opts.reuse_client or reuse_client_default
|
2024-12-07 06:05:05 -07:00
|
|
|
local bufnr = vim._resolve_bufnr(opts.bufnr)
|
2024-02-11 05:37:20 -07:00
|
|
|
|
2024-11-01 09:31:51 -07:00
|
|
|
if not config.root_dir and opts._root_markers then
|
|
|
|
config = vim.deepcopy(config)
|
|
|
|
config.root_dir = vim.fs.root(bufnr, opts._root_markers)
|
|
|
|
end
|
|
|
|
|
2024-03-21 08:15:20 -07:00
|
|
|
for _, client in pairs(all_clients) do
|
|
|
|
if reuse_client(client, config) then
|
2024-12-04 06:14:47 -07:00
|
|
|
if opts.attach == false then
|
|
|
|
return client.id
|
|
|
|
end
|
|
|
|
|
2024-04-26 06:15:44 -07:00
|
|
|
if lsp.buf_attach_client(bufnr, client.id) then
|
|
|
|
return client.id
|
|
|
|
end
|
2024-11-01 09:31:51 -07:00
|
|
|
return
|
2022-06-03 05:59:19 -07:00
|
|
|
end
|
|
|
|
end
|
2024-02-11 05:37:20 -07:00
|
|
|
|
2024-12-04 06:14:47 -07:00
|
|
|
local client_id, err = create_and_initialize_client(config)
|
2024-04-26 06:15:44 -07:00
|
|
|
if err then
|
|
|
|
if not opts.silent then
|
|
|
|
vim.notify(err, vim.log.levels.WARN)
|
|
|
|
end
|
2024-11-01 09:31:51 -07:00
|
|
|
return
|
2024-04-26 06:15:44 -07:00
|
|
|
end
|
2024-02-11 05:37:20 -07:00
|
|
|
|
2024-12-04 06:14:47 -07:00
|
|
|
if opts.attach == false then
|
|
|
|
return client_id
|
|
|
|
end
|
|
|
|
|
2024-04-26 06:15:44 -07:00
|
|
|
if client_id and lsp.buf_attach_client(bufnr, client_id) then
|
|
|
|
return client_id
|
2022-07-11 18:37:01 -07:00
|
|
|
end
|
2022-06-03 05:59:19 -07:00
|
|
|
end
|
|
|
|
|
2023-06-09 02:32:43 -07:00
|
|
|
--- Consumes the latest progress messages from all clients and formats them as a string.
|
|
|
|
--- Empty if there are no clients or if no new messages
|
|
|
|
---
|
|
|
|
---@return string
|
|
|
|
function lsp.status()
|
|
|
|
local percentage = nil
|
2023-12-13 05:00:11 -07:00
|
|
|
local messages = {} --- @type string[]
|
2023-07-17 09:27:16 -07:00
|
|
|
for _, client in ipairs(vim.lsp.get_clients()) do
|
2024-02-07 10:22:03 -07:00
|
|
|
--- @diagnostic disable-next-line:no-unknown
|
2023-06-09 02:32:43 -07:00
|
|
|
for progress in client.progress do
|
2024-02-07 10:22:03 -07:00
|
|
|
--- @cast progress {token: lsp.ProgressToken, value: lsp.LSPAny}
|
2023-06-09 02:32:43 -07:00
|
|
|
local value = progress.value
|
|
|
|
if type(value) == 'table' and value.kind then
|
2023-06-22 01:18:49 -07:00
|
|
|
local message = value.message and (value.title .. ': ' .. value.message) or value.title
|
|
|
|
messages[#messages + 1] = message
|
2023-06-09 02:32:43 -07:00
|
|
|
if value.percentage then
|
|
|
|
percentage = math.max(percentage or 0, value.percentage)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- else: Doesn't look like work done progress and can be in any format
|
|
|
|
-- Just ignore it as there is no sensible way to display it
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local message = table.concat(messages, ', ')
|
|
|
|
if percentage then
|
2023-06-10 11:32:41 -07:00
|
|
|
return string.format('%3d%%: %s', percentage, message)
|
2023-06-09 02:32:43 -07:00
|
|
|
end
|
|
|
|
return message
|
|
|
|
end
|
|
|
|
|
2023-05-27 22:51:28 -07:00
|
|
|
-- Determines whether the given option can be set by `set_defaults`.
|
2023-12-13 05:00:11 -07:00
|
|
|
---@param bufnr integer
|
|
|
|
---@param option string
|
|
|
|
---@return boolean
|
2023-05-27 22:51:28 -07:00
|
|
|
local function is_empty_or_default(bufnr, option)
|
|
|
|
if vim.bo[bufnr][option] == '' then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2023-12-13 05:00:11 -07:00
|
|
|
local info = api.nvim_get_option_info2(option, { buf = bufnr })
|
2024-05-04 15:08:17 -07:00
|
|
|
---@param e vim.fn.getscriptinfo.ret
|
2023-05-27 22:51:28 -07:00
|
|
|
local scriptinfo = vim.tbl_filter(function(e)
|
|
|
|
return e.sid == info.last_set_sid
|
|
|
|
end, vim.fn.getscriptinfo())
|
|
|
|
|
|
|
|
if #scriptinfo ~= 1 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return vim.startswith(scriptinfo[1].name, vim.fn.expand('$VIMRUNTIME'))
|
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
2024-02-27 08:20:32 -07:00
|
|
|
---@param client vim.lsp.Client
|
2023-12-13 05:00:11 -07:00
|
|
|
---@param bufnr integer
|
2023-05-27 22:51:28 -07:00
|
|
|
function lsp._set_defaults(client, bufnr)
|
|
|
|
if
|
2024-11-14 04:53:20 -07:00
|
|
|
client:supports_method(ms.textDocument_definition) and is_empty_or_default(bufnr, 'tagfunc')
|
2023-05-27 22:51:28 -07:00
|
|
|
then
|
|
|
|
vim.bo[bufnr].tagfunc = 'v:lua.vim.lsp.tagfunc'
|
|
|
|
end
|
|
|
|
if
|
2024-11-14 04:53:20 -07:00
|
|
|
client:supports_method(ms.textDocument_completion) and is_empty_or_default(bufnr, 'omnifunc')
|
2023-05-27 22:51:28 -07:00
|
|
|
then
|
|
|
|
vim.bo[bufnr].omnifunc = 'v:lua.vim.lsp.omnifunc'
|
|
|
|
end
|
|
|
|
if
|
2024-11-14 04:53:20 -07:00
|
|
|
client:supports_method(ms.textDocument_rangeFormatting)
|
2023-05-27 22:51:28 -07:00
|
|
|
and is_empty_or_default(bufnr, 'formatprg')
|
|
|
|
and is_empty_or_default(bufnr, 'formatexpr')
|
|
|
|
then
|
|
|
|
vim.bo[bufnr].formatexpr = 'v:lua.vim.lsp.formatexpr()'
|
|
|
|
end
|
2024-06-08 12:40:18 -07:00
|
|
|
vim._with({ buf = bufnr }, function()
|
2023-07-14 09:47:18 -07:00
|
|
|
if
|
2024-11-14 04:53:20 -07:00
|
|
|
client:supports_method(ms.textDocument_hover)
|
2023-07-14 09:47:18 -07:00
|
|
|
and is_empty_or_default(bufnr, 'keywordprg')
|
|
|
|
and vim.fn.maparg('K', 'n', false, false) == ''
|
|
|
|
then
|
2024-11-22 05:51:30 -07:00
|
|
|
vim.keymap.set('n', 'K', function()
|
|
|
|
vim.lsp.buf.hover()
|
|
|
|
end, { buffer = bufnr, desc = 'vim.lsp.buf.hover()' })
|
2023-07-14 09:47:18 -07:00
|
|
|
end
|
|
|
|
end)
|
2024-11-14 04:53:20 -07:00
|
|
|
if client:supports_method(ms.textDocument_diagnostic) then
|
2023-07-20 00:03:48 -07:00
|
|
|
lsp.diagnostic._enable(bufnr)
|
|
|
|
end
|
2023-05-27 22:51:28 -07:00
|
|
|
end
|
|
|
|
|
2024-12-04 06:14:47 -07:00
|
|
|
--- @deprecated
|
2019-12-31 08:51:54 -07:00
|
|
|
--- Starts and initializes a client with the given configuration.
|
2024-02-27 08:20:32 -07:00
|
|
|
--- @param config vim.lsp.ClientConfig Configuration for the server.
|
2024-04-26 06:15:44 -07:00
|
|
|
--- @return integer? client_id |vim.lsp.get_client_by_id()| Note: client may not be
|
|
|
|
--- fully initialized. Use `on_init` to do any actions once
|
|
|
|
--- the client has been initialized.
|
|
|
|
--- @return string? # Error message, if any
|
2019-11-13 13:55:26 -07:00
|
|
|
function lsp.start_client(config)
|
2024-12-04 06:14:47 -07:00
|
|
|
vim.deprecate('vim.lsp.start_client()', 'vim.lsp.start()', '0.13')
|
|
|
|
return create_and_initialize_client(config)
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
|
|
|
|
2022-07-17 10:13:33 -07:00
|
|
|
---Buffer lifecycle handler for textDocument/didSave
|
2023-12-13 05:00:11 -07:00
|
|
|
--- @param bufnr integer
|
2022-07-17 10:13:33 -07:00
|
|
|
local function text_document_did_save_handler(bufnr)
|
2024-12-07 06:05:05 -07:00
|
|
|
bufnr = vim._resolve_bufnr(bufnr)
|
2019-11-13 13:55:26 -07:00
|
|
|
local uri = vim.uri_from_bufnr(bufnr)
|
2023-12-17 02:54:38 -07:00
|
|
|
local text = once(lsp._buf_get_full_text)
|
2023-07-17 09:27:16 -07:00
|
|
|
for _, client in ipairs(lsp.get_clients({ bufnr = bufnr })) do
|
2022-08-01 13:32:53 -07:00
|
|
|
local name = api.nvim_buf_get_name(bufnr)
|
2023-03-10 23:35:23 -07:00
|
|
|
local old_name = changetracking._get_and_set_name(client, bufnr, name)
|
|
|
|
if old_name and name ~= old_name then
|
2024-11-14 04:53:20 -07:00
|
|
|
client:notify(ms.textDocument_didClose, {
|
2023-03-11 06:50:14 -07:00
|
|
|
textDocument = {
|
|
|
|
uri = vim.uri_from_fname(old_name),
|
|
|
|
},
|
|
|
|
})
|
2024-11-14 04:53:20 -07:00
|
|
|
client:notify(ms.textDocument_didOpen, {
|
2022-08-01 13:32:53 -07:00
|
|
|
textDocument = {
|
|
|
|
version = 0,
|
|
|
|
uri = uri,
|
2024-02-12 06:46:32 -07:00
|
|
|
languageId = client.get_language_id(bufnr, vim.bo[bufnr].filetype),
|
2023-12-17 02:54:38 -07:00
|
|
|
text = lsp._buf_get_full_text(bufnr),
|
2022-08-01 13:32:53 -07:00
|
|
|
},
|
|
|
|
})
|
2024-06-07 02:36:46 -07:00
|
|
|
util.buf_versions[bufnr] = 0
|
2022-08-01 13:32:53 -07:00
|
|
|
end
|
2022-04-30 13:13:26 -07:00
|
|
|
local save_capability = vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'save')
|
|
|
|
if save_capability then
|
2023-12-13 05:00:11 -07:00
|
|
|
local included_text --- @type string?
|
2022-04-30 13:13:26 -07:00
|
|
|
if type(save_capability) == 'table' and save_capability.includeText then
|
2022-01-03 19:03:16 -07:00
|
|
|
included_text = text(bufnr)
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
2024-11-14 04:53:20 -07:00
|
|
|
client:notify(ms.textDocument_didSave, {
|
2019-11-13 13:55:26 -07:00
|
|
|
textDocument = {
|
|
|
|
uri = uri,
|
2020-12-20 10:12:39 -07:00
|
|
|
},
|
|
|
|
text = included_text,
|
2019-11-13 13:55:26 -07:00
|
|
|
})
|
|
|
|
end
|
2023-06-22 04:54:35 -07:00
|
|
|
end
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
|
|
|
|
2024-05-23 06:17:03 -07:00
|
|
|
---@param bufnr integer resolved buffer
|
|
|
|
---@param client vim.lsp.Client
|
|
|
|
local function buf_detach_client(bufnr, client)
|
|
|
|
api.nvim_exec_autocmds('LspDetach', {
|
|
|
|
buffer = bufnr,
|
|
|
|
modeline = false,
|
|
|
|
data = { client_id = client.id },
|
|
|
|
})
|
|
|
|
|
|
|
|
changetracking.reset_buf(client, bufnr)
|
|
|
|
|
2024-11-14 04:53:20 -07:00
|
|
|
if client:supports_method(ms.textDocument_didClose) then
|
2024-05-23 06:17:03 -07:00
|
|
|
local uri = vim.uri_from_bufnr(bufnr)
|
|
|
|
local params = { textDocument = { uri = uri } }
|
2024-11-14 04:53:20 -07:00
|
|
|
client:notify(ms.textDocument_didClose, params)
|
2024-05-23 06:17:03 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
client.attached_buffers[bufnr] = nil
|
|
|
|
|
|
|
|
local namespace = lsp.diagnostic.get_namespace(client.id)
|
|
|
|
vim.diagnostic.reset(namespace, bufnr)
|
|
|
|
end
|
|
|
|
|
2024-04-26 06:58:17 -07:00
|
|
|
--- @type table<integer,true>
|
|
|
|
local attached_buffers = {}
|
|
|
|
|
2024-03-21 08:15:20 -07:00
|
|
|
--- @param bufnr integer
|
2024-04-26 06:58:17 -07:00
|
|
|
local function buf_attach(bufnr)
|
|
|
|
if attached_buffers[bufnr] then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
attached_buffers[bufnr] = true
|
|
|
|
|
2024-03-21 08:15:20 -07:00
|
|
|
local uri = vim.uri_from_bufnr(bufnr)
|
2024-04-26 06:58:17 -07:00
|
|
|
local augroup = ('lsp_b_%d_save'):format(bufnr)
|
2024-03-21 08:15:20 -07:00
|
|
|
local group = api.nvim_create_augroup(augroup, { clear = true })
|
|
|
|
api.nvim_create_autocmd('BufWritePre', {
|
|
|
|
group = group,
|
|
|
|
buffer = bufnr,
|
|
|
|
desc = 'vim.lsp: textDocument/willSave',
|
|
|
|
callback = function(ctx)
|
|
|
|
for _, client in ipairs(lsp.get_clients({ bufnr = ctx.buf })) do
|
|
|
|
local params = {
|
|
|
|
textDocument = {
|
|
|
|
uri = uri,
|
|
|
|
},
|
2024-05-04 15:08:17 -07:00
|
|
|
reason = protocol.TextDocumentSaveReason.Manual, ---@type integer
|
2024-03-21 08:15:20 -07:00
|
|
|
}
|
2024-11-14 04:53:20 -07:00
|
|
|
if client:supports_method(ms.textDocument_willSave) then
|
|
|
|
client:notify(ms.textDocument_willSave, params)
|
2024-03-21 08:15:20 -07:00
|
|
|
end
|
2024-11-14 04:53:20 -07:00
|
|
|
if client:supports_method(ms.textDocument_willSaveWaitUntil) then
|
2024-03-21 08:15:20 -07:00
|
|
|
local result, err =
|
2024-11-14 04:53:20 -07:00
|
|
|
client:request_sync(ms.textDocument_willSaveWaitUntil, params, 1000, ctx.buf)
|
2024-03-21 08:15:20 -07:00
|
|
|
if result and result.result then
|
|
|
|
util.apply_text_edits(result.result, ctx.buf, client.offset_encoding)
|
|
|
|
elseif err then
|
|
|
|
log.error(vim.inspect(err))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
api.nvim_create_autocmd('BufWritePost', {
|
|
|
|
group = group,
|
|
|
|
buffer = bufnr,
|
|
|
|
desc = 'vim.lsp: textDocument/didSave handler',
|
|
|
|
callback = function(ctx)
|
|
|
|
text_document_did_save_handler(ctx.buf)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
-- First time, so attach and set up stuff.
|
|
|
|
api.nvim_buf_attach(bufnr, false, {
|
2024-06-07 02:36:46 -07:00
|
|
|
on_lines = function(_, _, changedtick, firstline, lastline, new_lastline)
|
2024-04-26 06:58:17 -07:00
|
|
|
if #lsp.get_clients({ bufnr = bufnr }) == 0 then
|
2024-05-27 08:06:03 -07:00
|
|
|
-- detach if there are no clients
|
|
|
|
return #lsp.get_clients({ bufnr = bufnr, _uninitialized = true }) == 0
|
2024-04-26 06:58:17 -07:00
|
|
|
end
|
2024-06-07 02:36:46 -07:00
|
|
|
util.buf_versions[bufnr] = changedtick
|
2024-04-26 06:58:17 -07:00
|
|
|
changetracking.send_changes(bufnr, firstline, lastline, new_lastline)
|
|
|
|
end,
|
|
|
|
|
2024-03-21 08:15:20 -07:00
|
|
|
on_reload = function()
|
2024-05-21 11:02:48 -07:00
|
|
|
local clients = lsp.get_clients({ bufnr = bufnr })
|
2024-03-21 08:15:20 -07:00
|
|
|
local params = { textDocument = { uri = uri } }
|
2024-05-21 11:02:48 -07:00
|
|
|
for _, client in ipairs(clients) do
|
2024-03-21 08:15:20 -07:00
|
|
|
changetracking.reset_buf(client, bufnr)
|
2024-11-14 04:53:20 -07:00
|
|
|
if client:supports_method(ms.textDocument_didClose) then
|
|
|
|
client:notify(ms.textDocument_didClose, params)
|
2024-03-21 08:15:20 -07:00
|
|
|
end
|
2024-05-21 11:02:48 -07:00
|
|
|
end
|
|
|
|
for _, client in ipairs(clients) do
|
2024-03-21 08:15:20 -07:00
|
|
|
client:_text_document_did_open_handler(bufnr)
|
|
|
|
end
|
|
|
|
end,
|
2024-04-26 06:58:17 -07:00
|
|
|
|
2024-03-21 08:15:20 -07:00
|
|
|
on_detach = function()
|
2024-05-23 06:17:03 -07:00
|
|
|
local clients = lsp.get_clients({ bufnr = bufnr, _uninitialized = true })
|
|
|
|
for _, client in ipairs(clients) do
|
|
|
|
buf_detach_client(bufnr, client)
|
2024-03-21 08:15:20 -07:00
|
|
|
end
|
2024-04-26 06:58:17 -07:00
|
|
|
attached_buffers[bufnr] = nil
|
2024-06-10 09:53:08 -07:00
|
|
|
util.buf_versions[bufnr] = nil
|
2024-03-21 08:15:20 -07:00
|
|
|
end,
|
2024-04-26 06:58:17 -07:00
|
|
|
|
2024-03-21 08:15:20 -07:00
|
|
|
-- TODO if we know all of the potential clients ahead of time, then we
|
|
|
|
-- could conditionally set this.
|
|
|
|
-- utf_sizes = size_index > 1;
|
|
|
|
utf_sizes = true,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-12-31 07:52:14 -07:00
|
|
|
--- Implements the `textDocument/did…` notifications required to track a buffer
|
|
|
|
--- for any language server.
|
|
|
|
---
|
|
|
|
--- Without calling this, the server won't be notified of changes to a buffer.
|
|
|
|
---
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param bufnr (integer) Buffer handle, or 0 for current
|
|
|
|
---@param client_id (integer) Client id
|
2023-06-20 12:17:13 -07:00
|
|
|
---@return boolean success `true` if client was attached successfully; `false` otherwise
|
2019-11-13 13:55:26 -07:00
|
|
|
function lsp.buf_attach_client(bufnr, client_id)
|
2024-10-16 09:03:48 -07:00
|
|
|
validate('bufnr', bufnr, 'number', true)
|
|
|
|
validate('client_id', client_id, 'number')
|
2024-12-07 06:05:05 -07:00
|
|
|
bufnr = vim._resolve_bufnr(bufnr)
|
2022-07-15 09:26:47 -07:00
|
|
|
if not api.nvim_buf_is_loaded(bufnr) then
|
2024-02-08 02:24:47 -07:00
|
|
|
log.warn(string.format('buf_attach_client called on unloaded buffer (id: %d): ', bufnr))
|
2021-12-19 13:49:56 -07:00
|
|
|
return false
|
|
|
|
end
|
2019-11-13 13:55:26 -07:00
|
|
|
|
2024-03-21 08:15:20 -07:00
|
|
|
local client = lsp.get_client_by_id(client_id)
|
|
|
|
if not client then
|
|
|
|
return false
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
2021-01-23 09:43:06 -07:00
|
|
|
|
2024-04-26 06:58:17 -07:00
|
|
|
buf_attach(bufnr)
|
|
|
|
|
2024-03-21 08:15:20 -07:00
|
|
|
if client.attached_buffers[bufnr] then
|
2023-06-20 12:17:13 -07:00
|
|
|
return true
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
2024-03-25 13:16:42 -07:00
|
|
|
|
2024-03-21 08:15:20 -07:00
|
|
|
client.attached_buffers[bufnr] = true
|
|
|
|
|
|
|
|
-- This is our first time attaching this client to this buffer.
|
2019-11-13 13:55:26 -07:00
|
|
|
-- Send didOpen for the client if it is initialized. If it isn't initialized
|
|
|
|
-- then it will send didOpen on initialize.
|
2024-03-21 08:15:20 -07:00
|
|
|
if client.initialized then
|
2024-11-14 04:53:20 -07:00
|
|
|
client:on_attach(bufnr)
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2021-12-21 11:53:34 -07:00
|
|
|
--- Detaches client from the specified buffer.
|
|
|
|
--- Note: While the server is notified that the text document (buffer)
|
|
|
|
--- was closed, it is still able to send notifications should it ignore this notification.
|
|
|
|
---
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param bufnr integer Buffer handle, or 0 for current
|
|
|
|
---@param client_id integer Client id
|
2021-12-21 11:53:34 -07:00
|
|
|
function lsp.buf_detach_client(bufnr, client_id)
|
2024-10-16 09:03:48 -07:00
|
|
|
validate('bufnr', bufnr, 'number', true)
|
|
|
|
validate('client_id', client_id, 'number')
|
2024-12-07 06:05:05 -07:00
|
|
|
bufnr = vim._resolve_bufnr(bufnr)
|
2021-12-21 11:53:34 -07:00
|
|
|
|
2024-03-21 08:15:20 -07:00
|
|
|
local client = all_clients[client_id]
|
2021-12-21 11:53:34 -07:00
|
|
|
if not client or not client.attached_buffers[bufnr] then
|
|
|
|
vim.notify(
|
|
|
|
string.format(
|
|
|
|
'Buffer (id: %d) is not attached to client (id: %d). Cannot detach.',
|
2023-02-20 23:24:47 -07:00
|
|
|
bufnr,
|
|
|
|
client_id
|
2021-12-21 11:53:34 -07:00
|
|
|
)
|
2022-07-07 09:27:18 -07:00
|
|
|
)
|
2021-12-21 11:53:34 -07:00
|
|
|
return
|
2024-05-23 06:17:03 -07:00
|
|
|
else
|
|
|
|
buf_detach_client(bufnr, client)
|
2021-12-21 11:53:34 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-31 07:52:14 -07:00
|
|
|
--- Checks if a buffer is attached for a particular client.
|
|
|
|
---
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param bufnr (integer) Buffer handle, or 0 for current
|
|
|
|
---@param client_id (integer) the client id
|
2019-11-13 13:55:26 -07:00
|
|
|
function lsp.buf_is_attached(bufnr, client_id)
|
2024-03-21 08:15:20 -07:00
|
|
|
return lsp.get_clients({ bufnr = bufnr, id = client_id, _uninitialized = true })[1] ~= nil
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
|
|
|
|
2020-10-22 11:59:17 -07:00
|
|
|
--- Gets a client by id, or nil if the id is invalid.
|
|
|
|
--- The returned client may not yet be fully initialized.
|
|
|
|
---
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param client_id integer client id
|
2019-12-31 07:52:14 -07:00
|
|
|
---
|
2024-02-27 08:20:32 -07:00
|
|
|
---@return (nil|vim.lsp.Client) client rpc object
|
2019-11-13 13:55:26 -07:00
|
|
|
function lsp.get_client_by_id(client_id)
|
2024-03-21 08:15:20 -07:00
|
|
|
return all_clients[client_id]
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
|
|
|
|
2021-01-23 09:43:06 -07:00
|
|
|
--- Returns list of buffers attached to client_id.
|
|
|
|
---
|
2023-02-25 10:47:05 -07:00
|
|
|
---@param client_id integer client id
|
|
|
|
---@return integer[] buffers list of buffer ids
|
2021-01-23 09:43:06 -07:00
|
|
|
function lsp.get_buffers_by_client_id(client_id)
|
2024-03-21 08:15:20 -07:00
|
|
|
local client = all_clients[client_id]
|
2021-10-20 09:33:09 -07:00
|
|
|
return client and vim.tbl_keys(client.attached_buffers) or {}
|
2021-01-23 09:43:06 -07:00
|
|
|
end
|
|
|
|
|
2019-12-31 08:51:54 -07:00
|
|
|
--- Stops a client(s).
|
2019-12-31 07:52:14 -07:00
|
|
|
---
|
2024-02-27 08:20:32 -07:00
|
|
|
--- You can also use the `stop()` function on a |vim.lsp.Client| object.
|
2019-12-31 08:51:54 -07:00
|
|
|
--- To stop all clients:
|
2023-09-14 06:23:01 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
2023-07-17 09:27:16 -07:00
|
|
|
--- vim.lsp.stop_client(vim.lsp.get_clients())
|
2023-09-14 06:23:01 -07:00
|
|
|
--- ```
|
2019-12-31 07:52:14 -07:00
|
|
|
---
|
|
|
|
--- By default asks the server to shutdown, unless stop was requested
|
|
|
|
--- already for this client, then force-shutdown is attempted.
|
|
|
|
---
|
2024-03-21 08:15:20 -07:00
|
|
|
---@param client_id integer|integer[]|vim.lsp.Client[] id, list of id's, or list of |vim.lsp.Client| objects
|
|
|
|
---@param force? boolean shutdown forcefully
|
2019-11-13 13:55:26 -07:00
|
|
|
function lsp.stop_client(client_id, force)
|
2024-03-21 08:15:20 -07:00
|
|
|
--- @type integer[]|vim.lsp.Client[]
|
2019-12-31 08:51:54 -07:00
|
|
|
local ids = type(client_id) == 'table' and client_id or { client_id }
|
|
|
|
for _, id in ipairs(ids) do
|
2024-03-21 08:15:20 -07:00
|
|
|
if type(id) == 'table' then
|
|
|
|
if id.stop then
|
2024-11-14 04:53:20 -07:00
|
|
|
id:stop(force)
|
2024-03-21 08:15:20 -07:00
|
|
|
end
|
|
|
|
else
|
|
|
|
--- @cast id -vim.lsp.Client
|
|
|
|
local client = all_clients[id]
|
|
|
|
if client then
|
2024-11-14 04:53:20 -07:00
|
|
|
client:stop(force)
|
2024-03-21 08:15:20 -07:00
|
|
|
end
|
2019-12-31 08:51:54 -07:00
|
|
|
end
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
--- Key-value pairs used to filter the returned clients.
|
|
|
|
--- @class vim.lsp.get_clients.Filter
|
|
|
|
--- @inlinedoc
|
|
|
|
---
|
|
|
|
--- Only return clients with the given id
|
|
|
|
--- @field id? integer
|
|
|
|
---
|
|
|
|
--- Only return clients attached to this buffer
|
|
|
|
--- @field bufnr? integer
|
|
|
|
---
|
|
|
|
--- Only return clients with the given name
|
|
|
|
--- @field name? string
|
|
|
|
---
|
|
|
|
--- Only return clients supporting the given method
|
|
|
|
--- @field method? string
|
2024-03-21 08:15:20 -07:00
|
|
|
---
|
|
|
|
--- Also return uninitialized clients.
|
|
|
|
--- @field package _uninitialized? boolean
|
2022-12-09 11:18:31 -07:00
|
|
|
|
2022-05-16 15:44:55 -07:00
|
|
|
--- Get active clients.
|
2019-12-31 07:52:14 -07:00
|
|
|
---
|
2024-02-27 08:20:32 -07:00
|
|
|
---@param filter? vim.lsp.get_clients.Filter
|
|
|
|
---@return vim.lsp.Client[]: List of |vim.lsp.Client| objects
|
2023-07-17 09:27:16 -07:00
|
|
|
function lsp.get_clients(filter)
|
2024-10-16 09:03:48 -07:00
|
|
|
validate('filter', filter, 'table', true)
|
2022-05-16 15:44:55 -07:00
|
|
|
|
|
|
|
filter = filter or {}
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
local clients = {} --- @type vim.lsp.Client[]
|
2022-05-16 15:44:55 -07:00
|
|
|
|
2024-12-07 06:05:05 -07:00
|
|
|
local bufnr = filter.bufnr and vim._resolve_bufnr(filter.bufnr)
|
2024-03-21 08:15:20 -07:00
|
|
|
|
|
|
|
for _, client in pairs(all_clients) do
|
2022-05-16 15:44:55 -07:00
|
|
|
if
|
2022-12-30 08:42:18 -07:00
|
|
|
client
|
|
|
|
and (filter.id == nil or client.id == filter.id)
|
2024-03-21 08:15:20 -07:00
|
|
|
and (filter.bufnr == nil or client.attached_buffers[bufnr])
|
2022-05-16 15:44:55 -07:00
|
|
|
and (filter.name == nil or client.name == filter.name)
|
2024-11-14 04:53:20 -07:00
|
|
|
and (filter.method == nil or client:supports_method(filter.method, filter.bufnr))
|
2024-03-21 08:15:20 -07:00
|
|
|
and (filter._uninitialized or client.initialized)
|
2022-05-16 15:44:55 -07:00
|
|
|
then
|
|
|
|
clients[#clients + 1] = client
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return clients
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
|
|
|
|
2023-07-17 09:27:16 -07:00
|
|
|
---@private
|
|
|
|
---@deprecated
|
|
|
|
function lsp.get_active_clients(filter)
|
2023-12-25 13:28:28 -07:00
|
|
|
vim.deprecate('vim.lsp.get_active_clients()', 'vim.lsp.get_clients()', '0.12')
|
2023-07-17 09:27:16 -07:00
|
|
|
return lsp.get_clients(filter)
|
|
|
|
end
|
|
|
|
|
2022-07-17 10:13:33 -07:00
|
|
|
api.nvim_create_autocmd('VimLeavePre', {
|
|
|
|
desc = 'vim.lsp: exit handler',
|
|
|
|
callback = function()
|
2024-03-21 08:15:20 -07:00
|
|
|
local active_clients = lsp.get_clients()
|
2022-07-17 10:13:33 -07:00
|
|
|
log.info('exit_handler', active_clients)
|
2024-03-21 08:15:20 -07:00
|
|
|
for _, client in pairs(all_clients) do
|
2024-11-14 04:53:20 -07:00
|
|
|
client:stop()
|
2022-07-17 10:13:33 -07:00
|
|
|
end
|
2020-05-20 08:08:19 -07:00
|
|
|
|
2023-12-13 05:00:11 -07:00
|
|
|
local timeouts = {} --- @type table<integer,integer>
|
2022-07-17 10:13:33 -07:00
|
|
|
local max_timeout = 0
|
|
|
|
local send_kill = false
|
2021-10-21 09:36:01 -07:00
|
|
|
|
2022-07-17 10:13:33 -07:00
|
|
|
for client_id, client in pairs(active_clients) do
|
2024-03-21 08:15:20 -07:00
|
|
|
local timeout = client.flags.exit_timeout
|
2022-07-17 10:13:33 -07:00
|
|
|
if timeout then
|
|
|
|
send_kill = true
|
|
|
|
timeouts[client_id] = timeout
|
|
|
|
max_timeout = math.max(timeout, max_timeout)
|
|
|
|
end
|
2021-10-21 09:36:01 -07:00
|
|
|
end
|
|
|
|
|
2022-07-17 10:13:33 -07:00
|
|
|
local poll_time = 50
|
2021-10-21 09:36:01 -07:00
|
|
|
|
2022-07-17 10:13:33 -07:00
|
|
|
local function check_clients_closed()
|
|
|
|
for client_id, timeout in pairs(timeouts) do
|
|
|
|
timeouts[client_id] = timeout - poll_time
|
|
|
|
end
|
2021-10-31 12:05:57 -07:00
|
|
|
|
2022-07-17 10:13:33 -07:00
|
|
|
for client_id, _ in pairs(active_clients) do
|
|
|
|
if timeouts[client_id] ~= nil and timeouts[client_id] > 0 then
|
|
|
|
return false
|
|
|
|
end
|
2021-10-21 09:36:01 -07:00
|
|
|
end
|
2022-07-17 10:13:33 -07:00
|
|
|
return true
|
2021-10-21 09:36:01 -07:00
|
|
|
end
|
|
|
|
|
2022-07-17 10:13:33 -07:00
|
|
|
if send_kill then
|
|
|
|
if not vim.wait(max_timeout, check_clients_closed, poll_time) then
|
|
|
|
for client_id, client in pairs(active_clients) do
|
|
|
|
if timeouts[client_id] ~= nil then
|
2024-11-14 04:53:20 -07:00
|
|
|
client:stop(true)
|
2022-07-17 10:13:33 -07:00
|
|
|
end
|
2021-10-31 12:05:57 -07:00
|
|
|
end
|
2021-10-21 09:36:01 -07:00
|
|
|
end
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
2022-07-17 10:13:33 -07:00
|
|
|
end,
|
|
|
|
})
|
2019-11-13 13:55:26 -07:00
|
|
|
|
2022-08-18 01:57:17 -07:00
|
|
|
---@private
|
2019-12-31 07:52:14 -07:00
|
|
|
--- Sends an async request for all active clients attached to the
|
|
|
|
--- buffer.
|
|
|
|
---
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param bufnr (integer) Buffer handle, or 0 for current.
|
2021-08-22 13:55:28 -07:00
|
|
|
---@param method (string) LSP method name
|
2024-10-24 02:18:36 -07:00
|
|
|
---@param params? table|(fun(client: vim.lsp.Client, bufnr: integer): table?) Parameters to send to the server
|
2023-12-13 05:00:11 -07:00
|
|
|
---@param handler? lsp.Handler See |lsp-handler|
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 20:21:34 -07:00
|
|
|
--- If nil, follows resolution strategy defined in |lsp-handler-configuration|
|
2024-07-16 10:48:54 -07:00
|
|
|
---@param on_unsupported? fun()
|
|
|
|
--- The function to call when the buffer has no clients that support the given method.
|
|
|
|
--- Defaults to an `ERROR` level notification.
|
2023-07-10 04:38:15 -07:00
|
|
|
---@return table<integer, integer> client_request_ids Map of client-id:request-id pairs
|
|
|
|
---for all successful requests.
|
|
|
|
---@return function _cancel_all_requests Function which can be used to
|
|
|
|
---cancel all the requests. You could instead
|
|
|
|
---iterate all clients and call their `cancel_request()` methods.
|
2024-07-16 10:48:54 -07:00
|
|
|
function lsp.buf_request(bufnr, method, params, handler, on_unsupported)
|
2024-10-16 09:03:48 -07:00
|
|
|
validate('bufnr', bufnr, 'number', true)
|
|
|
|
validate('method', method, 'string')
|
|
|
|
validate('handler', handler, 'function', true)
|
|
|
|
validate('on_unsupported', on_unsupported, 'function', true)
|
2019-11-13 13:55:26 -07:00
|
|
|
|
2024-12-07 06:05:05 -07:00
|
|
|
bufnr = vim._resolve_bufnr(bufnr)
|
2020-10-24 21:28:15 -07:00
|
|
|
local method_supported = false
|
2023-07-17 09:27:16 -07:00
|
|
|
local clients = lsp.get_clients({ bufnr = bufnr })
|
2023-12-13 05:00:11 -07:00
|
|
|
local client_request_ids = {} --- @type table<integer,integer>
|
2023-06-22 04:54:35 -07:00
|
|
|
for _, client in ipairs(clients) do
|
2024-11-14 04:53:20 -07:00
|
|
|
if client:supports_method(method, bufnr) then
|
2020-10-24 21:28:15 -07:00
|
|
|
method_supported = true
|
2023-06-22 04:54:35 -07:00
|
|
|
|
2024-10-24 02:18:36 -07:00
|
|
|
local cparams = type(params) == 'function' and params(client, bufnr) or params --[[@as table?]]
|
2024-11-14 04:53:20 -07:00
|
|
|
local request_success, request_id = client:request(method, cparams, handler, bufnr)
|
2023-06-22 04:54:35 -07:00
|
|
|
-- This could only fail if the client shut down in the time since we looked
|
|
|
|
-- it up and we did the request, which should be rare.
|
|
|
|
if request_success then
|
|
|
|
client_request_ids[client.id] = request_id
|
|
|
|
end
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
2023-06-22 04:54:35 -07:00
|
|
|
end
|
2019-11-13 13:55:26 -07:00
|
|
|
|
2021-10-10 22:32:50 -07:00
|
|
|
-- if has client but no clients support the given method, notify the user
|
2023-06-22 04:54:35 -07:00
|
|
|
if next(clients) and not method_supported then
|
2024-07-16 10:48:54 -07:00
|
|
|
if on_unsupported == nil then
|
|
|
|
vim.notify(lsp._unsupported_method(method), vim.log.levels.ERROR)
|
|
|
|
else
|
|
|
|
on_unsupported()
|
|
|
|
end
|
2024-02-11 05:37:20 -07:00
|
|
|
vim.cmd.redraw()
|
2021-10-31 04:44:45 -07:00
|
|
|
return {}, function() end
|
2020-10-24 21:28:15 -07:00
|
|
|
end
|
|
|
|
|
2019-12-31 07:52:14 -07:00
|
|
|
local function _cancel_all_requests()
|
2019-11-13 13:55:26 -07:00
|
|
|
for client_id, request_id in pairs(client_request_ids) do
|
2024-03-21 08:15:20 -07:00
|
|
|
local client = all_clients[client_id]
|
2024-11-14 04:53:20 -07:00
|
|
|
client:cancel_request(request_id)
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-31 07:52:14 -07:00
|
|
|
return client_request_ids, _cancel_all_requests
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
|
|
|
|
2023-06-19 08:40:33 -07:00
|
|
|
--- Sends an async request for all active clients attached to the buffer and executes the `handler`
|
|
|
|
--- callback with the combined result.
|
2021-04-15 04:42:25 -07:00
|
|
|
---
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param bufnr (integer) Buffer handle, or 0 for current.
|
2021-08-22 13:55:28 -07:00
|
|
|
---@param method (string) LSP method name
|
2024-10-24 08:47:41 -07:00
|
|
|
---@param params? table|(fun(client: vim.lsp.Client, bufnr: integer): table?) Parameters to send to the server.
|
|
|
|
--- Can also be passed as a function that returns the params table for cases where
|
|
|
|
--- parameters are specific to the client.
|
2024-10-24 08:44:09 -07:00
|
|
|
---@param handler lsp.MultiHandler (function)
|
2023-06-19 08:40:33 -07:00
|
|
|
--- Handler called after all requests are completed. Server results are passed as
|
|
|
|
--- a `client_id:result` map.
|
2023-07-10 04:38:15 -07:00
|
|
|
---@return function cancel Function that cancels all requests.
|
2023-06-19 08:40:33 -07:00
|
|
|
function lsp.buf_request_all(bufnr, method, params, handler)
|
2024-10-24 08:44:09 -07:00
|
|
|
local results = {} --- @type table<integer,{err: lsp.ResponseError?, result: any}>
|
|
|
|
local remaining --- @type integer?
|
|
|
|
|
|
|
|
local _, cancel = lsp.buf_request(bufnr, method, params, function(err, result, ctx, config)
|
|
|
|
if not remaining then
|
|
|
|
-- Calculate as late as possible in case a client is removed during the request
|
|
|
|
remaining = #lsp.get_clients({ bufnr = bufnr, method = method })
|
2023-06-22 04:54:35 -07:00
|
|
|
end
|
2021-04-15 04:42:25 -07:00
|
|
|
|
2024-10-24 08:44:09 -07:00
|
|
|
-- The error key is deprecated and will be removed in 0.13
|
|
|
|
results[ctx.client_id] = { err = err, error = err, result = result }
|
|
|
|
remaining = remaining - 1
|
2021-04-15 04:42:25 -07:00
|
|
|
|
2024-10-24 08:44:09 -07:00
|
|
|
if remaining == 0 then
|
|
|
|
handler(results, ctx, config)
|
2021-04-15 04:42:25 -07:00
|
|
|
end
|
2024-10-24 08:44:09 -07:00
|
|
|
end)
|
2021-04-15 04:42:25 -07:00
|
|
|
|
|
|
|
return cancel
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Sends a request to all server and waits for the response of all of them.
|
2019-12-31 07:52:14 -07:00
|
|
|
---
|
2021-04-15 04:42:25 -07:00
|
|
|
--- Calls |vim.lsp.buf_request_all()| but blocks Nvim while awaiting the result.
|
2023-06-19 08:40:33 -07:00
|
|
|
--- Parameters are the same as |vim.lsp.buf_request_all()| but the result is
|
2024-03-05 05:06:15 -07:00
|
|
|
--- different. Waits a maximum of {timeout_ms}.
|
|
|
|
---
|
|
|
|
---@param bufnr integer Buffer handle, or 0 for current.
|
|
|
|
---@param method string LSP method name
|
|
|
|
---@param params table? Parameters to send to the server
|
|
|
|
---@param timeout_ms integer? Maximum time in milliseconds to wait for a result.
|
|
|
|
--- (default: `1000`)
|
2024-05-14 16:18:33 -07:00
|
|
|
---@return table<integer, {error: lsp.ResponseError?, result: any}>? result Map of client_id:request_result.
|
2024-03-05 05:06:15 -07:00
|
|
|
---@return string? err On timeout, cancel, or error, `err` is a string describing the failure reason, and `result` is nil.
|
2019-11-13 13:55:26 -07:00
|
|
|
function lsp.buf_request_sync(bufnr, method, params, timeout_ms)
|
2024-05-04 15:08:17 -07:00
|
|
|
local request_results ---@type table
|
2021-04-15 04:42:25 -07:00
|
|
|
|
|
|
|
local cancel = lsp.buf_request_all(bufnr, method, params, function(it)
|
|
|
|
request_results = it
|
|
|
|
end)
|
2020-05-20 08:08:19 -07:00
|
|
|
|
2021-05-02 08:08:57 -07:00
|
|
|
local wait_result, reason = vim.wait(timeout_ms or 1000, function()
|
2021-04-15 04:42:25 -07:00
|
|
|
return request_results ~= nil
|
2019-11-13 13:55:26 -07:00
|
|
|
end, 10)
|
2020-05-20 08:08:19 -07:00
|
|
|
|
|
|
|
if not wait_result then
|
2019-11-13 13:55:26 -07:00
|
|
|
cancel()
|
2020-05-20 08:08:19 -07:00
|
|
|
return nil, wait_result_reason[reason]
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
2021-04-15 04:42:25 -07:00
|
|
|
|
2019-11-13 13:55:26 -07:00
|
|
|
return request_results
|
|
|
|
end
|
|
|
|
|
2020-02-26 12:22:14 -07:00
|
|
|
--- Send a notification to a server
|
2023-03-09 14:17:08 -07:00
|
|
|
---@param bufnr (integer|nil) The number of the buffer
|
2022-12-09 11:18:31 -07:00
|
|
|
---@param method (string) Name of the request method
|
2023-01-11 12:17:10 -07:00
|
|
|
---@param params (any) Arguments to send to the server
|
2020-07-02 04:09:17 -07:00
|
|
|
---
|
2023-02-25 10:47:05 -07:00
|
|
|
---@return boolean success true if any client returns true; false otherwise
|
2019-11-13 13:55:26 -07:00
|
|
|
function lsp.buf_notify(bufnr, method, params)
|
2024-10-16 09:03:48 -07:00
|
|
|
validate('bufnr', bufnr, 'number', true)
|
|
|
|
validate('method', method, 'string')
|
2020-02-26 12:22:14 -07:00
|
|
|
local resp = false
|
2023-07-17 09:27:16 -07:00
|
|
|
for _, client in ipairs(lsp.get_clients({ bufnr = bufnr })) do
|
2020-02-26 12:22:14 -07:00
|
|
|
if client.rpc.notify(method, params) then
|
|
|
|
resp = true
|
|
|
|
end
|
2023-06-22 04:54:35 -07:00
|
|
|
end
|
2020-02-26 12:22:14 -07:00
|
|
|
return resp
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
|
|
|
|
2020-01-13 00:41:55 -07:00
|
|
|
--- Implements 'omnifunc' compatible LSP completion.
|
|
|
|
---
|
2021-08-22 13:55:28 -07:00
|
|
|
---@see |complete-functions|
|
|
|
|
---@see |complete-items|
|
|
|
|
---@see |CompleteDone|
|
2020-01-13 00:41:55 -07:00
|
|
|
---
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param findstart integer 0 or 1, decides behavior
|
|
|
|
---@param base integer findstart=0, text to match against
|
2020-01-13 00:41:55 -07:00
|
|
|
---
|
2023-07-10 04:38:15 -07:00
|
|
|
---@return integer|table Decided by {findstart}:
|
2020-01-13 00:41:55 -07:00
|
|
|
--- - findstart=0: column where the completion starts, or -2 or -3
|
|
|
|
--- - findstart=1: list of matches (actually just calls |complete()|)
|
2019-11-13 13:55:26 -07:00
|
|
|
function lsp.omnifunc(findstart, base)
|
2024-02-04 15:13:23 -07:00
|
|
|
return vim.lsp.completion._omnifunc(findstart, base)
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
|
|
|
|
2024-02-27 08:20:32 -07:00
|
|
|
--- @class vim.lsp.formatexpr.Opts
|
|
|
|
--- @inlinedoc
|
|
|
|
---
|
|
|
|
--- The timeout period for the formatting request.
|
|
|
|
--- (default: 500ms).
|
|
|
|
--- @field timeout_ms integer
|
|
|
|
|
2021-10-31 05:40:26 -07:00
|
|
|
--- Provides an interface between the built-in client and a `formatexpr` function.
|
|
|
|
---
|
|
|
|
--- Currently only supports a single client. This can be set via
|
2024-08-28 15:11:32 -07:00
|
|
|
--- `setlocal formatexpr=v:lua.vim.lsp.formatexpr()` or (more typically) in `on_attach`
|
2024-02-15 10:16:04 -07:00
|
|
|
--- via `vim.bo[bufnr].formatexpr = 'v:lua.vim.lsp.formatexpr(#{timeout_ms:250})'`.
|
2021-10-31 05:40:26 -07:00
|
|
|
---
|
2024-02-27 08:20:32 -07:00
|
|
|
---@param opts? vim.lsp.formatexpr.Opts
|
2021-10-31 05:40:26 -07:00
|
|
|
function lsp.formatexpr(opts)
|
|
|
|
opts = opts or {}
|
|
|
|
local timeout_ms = opts.timeout_ms or 500
|
|
|
|
|
2023-04-14 01:39:57 -07:00
|
|
|
if vim.list_contains({ 'i', 'R', 'ic', 'ix' }, vim.fn.mode()) then
|
2021-10-31 05:40:26 -07:00
|
|
|
-- `formatexpr` is also called when exceeding `textwidth` in insert mode
|
|
|
|
-- fall back to internal formatting
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2022-08-08 04:02:15 -07:00
|
|
|
local start_lnum = vim.v.lnum
|
|
|
|
local end_lnum = start_lnum + vim.v.count - 1
|
2021-10-31 05:40:26 -07:00
|
|
|
|
2022-08-08 04:02:15 -07:00
|
|
|
if start_lnum <= 0 or end_lnum <= 0 then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
local bufnr = api.nvim_get_current_buf()
|
2023-07-17 09:27:16 -07:00
|
|
|
for _, client in pairs(lsp.get_clients({ bufnr = bufnr })) do
|
2024-11-14 04:53:20 -07:00
|
|
|
if client:supports_method(ms.textDocument_rangeFormatting) then
|
2022-08-08 04:02:15 -07:00
|
|
|
local params = util.make_formatting_params()
|
2023-02-25 10:47:05 -07:00
|
|
|
local end_line = vim.fn.getline(end_lnum) --[[@as string]]
|
2024-10-26 07:38:25 -07:00
|
|
|
local end_col = vim.str_utfindex(end_line, client.offset_encoding)
|
2023-12-13 05:00:11 -07:00
|
|
|
--- @cast params +lsp.DocumentRangeFormattingParams
|
2022-08-08 04:02:15 -07:00
|
|
|
params.range = {
|
|
|
|
start = {
|
|
|
|
line = start_lnum - 1,
|
|
|
|
character = 0,
|
|
|
|
},
|
|
|
|
['end'] = {
|
|
|
|
line = end_lnum - 1,
|
|
|
|
character = end_col,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
local response =
|
2024-11-14 04:53:20 -07:00
|
|
|
client:request_sync(ms.textDocument_rangeFormatting, params, timeout_ms, bufnr)
|
2023-06-23 04:54:47 -07:00
|
|
|
if response and response.result then
|
2024-02-24 18:21:57 -07:00
|
|
|
lsp.util.apply_text_edits(response.result, bufnr, client.offset_encoding)
|
2021-10-31 05:40:26 -07:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- do not run builtin formatter.
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2021-11-18 11:29:31 -07:00
|
|
|
--- Provides an interface between the built-in client and 'tagfunc'.
|
|
|
|
---
|
|
|
|
--- When used with normal mode commands (e.g. |CTRL-]|) this will invoke
|
|
|
|
--- the "textDocument/definition" LSP method to find the tag under the cursor.
|
|
|
|
--- Otherwise, uses "workspace/symbol". If no results are returned from
|
|
|
|
--- any LSP servers, falls back to using built-in tags.
|
|
|
|
---
|
2022-12-09 11:18:31 -07:00
|
|
|
---@param pattern string Pattern used to find a workspace symbol
|
|
|
|
---@param flags string See |tag-function|
|
2021-11-18 11:29:31 -07:00
|
|
|
---
|
2023-02-25 10:47:05 -07:00
|
|
|
---@return table[] tags A list of matching tags
|
2023-06-07 05:39:41 -07:00
|
|
|
function lsp.tagfunc(pattern, flags)
|
2024-02-03 15:47:56 -07:00
|
|
|
return vim.lsp._tagfunc(pattern, flags)
|
2021-11-18 10:26:26 -07:00
|
|
|
end
|
|
|
|
|
2024-11-29 05:40:32 -07:00
|
|
|
--- Provides an interface between the built-in client and a `foldexpr` function.
|
2024-12-02 11:13:09 -07:00
|
|
|
---
|
|
|
|
--- To use, check for the "textDocument/foldingRange" capability in an
|
|
|
|
--- |LspAttach| autocommand. Example:
|
|
|
|
---
|
|
|
|
--- ```lua
|
|
|
|
--- vim.api.nvim_create_autocommand('LspAttach', {
|
|
|
|
--- callback = function(args)
|
|
|
|
--- local client = vim.lsp.get_client_by_id(args.data.client_id)
|
|
|
|
--- if client:supports_method('textDocument/foldingRange') then
|
|
|
|
--- vim.wo.foldmethod = 'expr'
|
|
|
|
--- vim.wo.foldexpr = 'v:lua.vim.lsp.foldexpr()'
|
|
|
|
--- end
|
|
|
|
--- end,
|
|
|
|
--- })
|
|
|
|
--- ```
|
|
|
|
---
|
2024-11-29 05:40:32 -07:00
|
|
|
---@param lnum integer line number
|
|
|
|
function lsp.foldexpr(lnum)
|
|
|
|
return vim.lsp._folding_range.foldexpr(lnum)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Close all {kind} of folds in the the window with {winid}.
|
|
|
|
---
|
|
|
|
--- To automatically fold imports when opening a file, you can use an autocmd:
|
|
|
|
---
|
|
|
|
--- ```lua
|
|
|
|
--- vim.api.nvim_create_autocmd('LspNotify', {
|
|
|
|
--- callback = function(args)
|
|
|
|
--- if args.data.method == 'textDocument/didOpen' then
|
|
|
|
--- vim.lsp.foldclose('imports', vim.fn.bufwinid(args.buf))
|
|
|
|
--- end
|
|
|
|
--- end,
|
|
|
|
--- })
|
|
|
|
--- ```
|
|
|
|
---
|
|
|
|
---@param kind lsp.FoldingRangeKind Kind to close, one of "comment", "imports" or "region".
|
|
|
|
---@param winid? integer Defaults to the current window.
|
|
|
|
function lsp.foldclose(kind, winid)
|
|
|
|
return vim.lsp._folding_range.foldclose(kind, winid)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Provides a `foldtext` function that shows the `collapsedText` retrieved,
|
|
|
|
--- defaults to the first folded line if `collapsedText` is not provided.
|
|
|
|
function lsp.foldtext()
|
|
|
|
return vim.lsp._folding_range.foldtext()
|
|
|
|
end
|
|
|
|
|
2020-08-19 09:17:08 -07:00
|
|
|
---Checks whether a client is stopped.
|
|
|
|
---
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param client_id (integer)
|
2023-02-25 10:47:05 -07:00
|
|
|
---@return boolean stopped true if client is stopped, false otherwise.
|
2019-11-20 17:03:32 -07:00
|
|
|
function lsp.client_is_stopped(client_id)
|
2023-05-31 23:38:38 -07:00
|
|
|
assert(client_id, 'missing client_id param')
|
2024-03-21 08:15:20 -07:00
|
|
|
return not all_clients[client_id]
|
2019-11-13 13:55:26 -07:00
|
|
|
end
|
|
|
|
|
2019-12-31 07:52:14 -07:00
|
|
|
--- Gets a map of client_id:client pairs for the given buffer, where each value
|
2024-02-27 08:20:32 -07:00
|
|
|
--- is a |vim.lsp.Client| object.
|
2019-11-13 13:55:26 -07:00
|
|
|
---
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param bufnr (integer|nil): Buffer handle, or 0 for current
|
2023-07-10 04:38:15 -07:00
|
|
|
---@return table result is table of (client_id, client) pairs
|
2023-07-17 09:27:16 -07:00
|
|
|
---@deprecated Use |vim.lsp.get_clients()| instead.
|
2019-11-13 13:55:26 -07:00
|
|
|
function lsp.buf_get_clients(bufnr)
|
2023-12-25 13:28:28 -07:00
|
|
|
vim.deprecate('vim.lsp.buf_get_clients()', 'vim.lsp.get_clients()', '0.12')
|
2024-02-27 08:20:32 -07:00
|
|
|
local result = {} --- @type table<integer,vim.lsp.Client>
|
2024-12-07 06:05:05 -07:00
|
|
|
for _, client in ipairs(lsp.get_clients({ bufnr = vim._resolve_bufnr(bufnr) })) do
|
2022-05-16 15:44:55 -07:00
|
|
|
result[client.id] = client
|
|
|
|
end
|
2019-11-13 13:55:26 -07:00
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2023-07-18 07:42:30 -07:00
|
|
|
--- Log level dictionary with reverse lookup as well.
|
|
|
|
---
|
|
|
|
--- Can be used to lookup the number from the name or the
|
|
|
|
--- name from the number.
|
|
|
|
--- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF"
|
|
|
|
--- Level numbers begin with "TRACE" at 0
|
|
|
|
--- @nodoc
|
2019-11-13 13:55:26 -07:00
|
|
|
lsp.log_levels = log.levels
|
|
|
|
|
2019-12-31 07:52:14 -07:00
|
|
|
--- Sets the global log level for LSP logging.
|
|
|
|
---
|
2022-05-03 07:49:23 -07:00
|
|
|
--- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF"
|
|
|
|
---
|
2019-12-31 07:52:14 -07:00
|
|
|
--- Level numbers begin with "TRACE" at 0
|
|
|
|
---
|
|
|
|
--- Use `lsp.log_levels` for reverse lookup.
|
|
|
|
---
|
2021-08-22 13:55:28 -07:00
|
|
|
---@see |vim.lsp.log_levels|
|
2019-12-31 07:52:14 -07:00
|
|
|
---
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param level (integer|string) the case insensitive level name or number
|
2019-11-13 13:55:26 -07:00
|
|
|
function lsp.set_log_level(level)
|
|
|
|
if type(level) == 'string' or type(level) == 'number' then
|
|
|
|
log.set_level(level)
|
|
|
|
else
|
|
|
|
error(string.format('Invalid log level: %q', level))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-31 07:52:14 -07:00
|
|
|
--- Gets the path of the logfile used by the LSP client.
|
2023-02-25 10:47:05 -07:00
|
|
|
---@return string path to log file
|
2019-11-13 13:55:26 -07:00
|
|
|
function lsp.get_log_path()
|
|
|
|
return log.get_filename()
|
|
|
|
end
|
|
|
|
|
2023-06-22 04:54:35 -07:00
|
|
|
---@private
|
2021-11-29 20:31:19 -07:00
|
|
|
--- Invokes a function for each LSP client attached to a buffer.
|
|
|
|
---
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param bufnr integer Buffer number
|
2021-11-29 20:31:19 -07:00
|
|
|
---@param fn function Function to run on each client attached to buffer
|
|
|
|
--- {bufnr}. The function takes the client, client ID, and
|
2023-09-14 06:23:01 -07:00
|
|
|
--- buffer number as arguments.
|
2023-07-17 09:27:16 -07:00
|
|
|
---@deprecated use lsp.get_clients({ bufnr = bufnr }) with regular loop
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 20:21:34 -07:00
|
|
|
function lsp.for_each_buffer_client(bufnr, fn)
|
2023-12-25 13:28:28 -07:00
|
|
|
vim.deprecate(
|
|
|
|
'vim.lsp.for_each_buffer_client()',
|
|
|
|
'lsp.get_clients({ bufnr = bufnr }) with regular loop',
|
|
|
|
'0.12'
|
|
|
|
)
|
2024-12-07 06:05:05 -07:00
|
|
|
bufnr = vim._resolve_bufnr(bufnr)
|
2024-03-21 08:15:20 -07:00
|
|
|
|
|
|
|
for _, client in pairs(lsp.get_clients({ bufnr = bufnr })) do
|
|
|
|
fn(client, client.id, bufnr)
|
|
|
|
end
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 20:21:34 -07:00
|
|
|
end
|
|
|
|
|
2024-10-29 02:36:02 -07:00
|
|
|
--- @deprecated
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 20:21:34 -07:00
|
|
|
--- Function to manage overriding defaults for LSP handlers.
|
2023-12-13 05:00:11 -07:00
|
|
|
---@param handler (lsp.Handler) See |lsp-handler|
|
2021-08-22 13:55:28 -07:00
|
|
|
---@param override_config (table) Table containing the keys to override behavior of the {handler}
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 20:21:34 -07:00
|
|
|
function lsp.with(handler, override_config)
|
feat(lsp)!: change handler signature
Previously, the handler signature was:
function(err, method, params, client_id, bufnr, config)
In order to better support external plugins that wish to extend the
protocol, there is other information which would be advantageous to
forward to the client, such as the original params of the request that
generated the callback.
In order to do this, we would need to break symmetry of the handlers, to
add an additional "params" as the 7th argument.
Instead, this PR changes the signature of the handlers to:
function(err, result, ctx, config)
where ctx (the context) includes params, client_id, and bufnr. This also leaves
flexibility for future use-cases.
BREAKING_CHANGE: changes the signature of the built-in client handlers, requiring
updating handler calls
2021-08-27 21:12:30 -07:00
|
|
|
return function(err, result, ctx, config)
|
|
|
|
return handler(err, result, ctx, vim.tbl_deep_extend('force', config or {}, override_config))
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 20:21:34 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-12 02:19:21 -07:00
|
|
|
--- Registry for client side commands.
|
|
|
|
--- This is an extension point for plugins to handle custom commands which are
|
|
|
|
--- not part of the core language server protocol specification.
|
|
|
|
---
|
|
|
|
--- The registry is a table where the key is a unique command name,
|
|
|
|
--- and the value is a function which is called if any LSP action
|
|
|
|
--- (code action, code lenses, ...) triggers the command.
|
|
|
|
---
|
2024-11-01 09:31:51 -07:00
|
|
|
--- If an LSP response contains a command for which no matching entry is
|
2021-03-12 02:19:21 -07:00
|
|
|
--- available in this registry, the command will be executed via the LSP server
|
|
|
|
--- using `workspace/executeCommand`.
|
|
|
|
---
|
|
|
|
--- The first argument to the function will be the `Command`:
|
|
|
|
--- Command
|
|
|
|
--- title: String
|
|
|
|
--- command: String
|
|
|
|
--- arguments?: any[]
|
|
|
|
---
|
|
|
|
--- The second argument is the `ctx` of |lsp-handler|
|
2023-12-13 05:00:11 -07:00
|
|
|
--- @type table<string,function>
|
2021-03-12 02:19:21 -07:00
|
|
|
lsp.commands = setmetatable({}, {
|
|
|
|
__newindex = function(tbl, key, value)
|
|
|
|
assert(type(key) == 'string', 'The key for commands in `vim.lsp.commands` must be a string')
|
|
|
|
assert(type(value) == 'function', 'Command added to `vim.lsp.commands` must be a function')
|
|
|
|
rawset(tbl, key, value)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2019-11-13 13:55:26 -07:00
|
|
|
return lsp
|