From c2a211b8e3e00dfae594ccbd9a107aef85a9919c Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Sun, 22 Aug 2021 14:55:28 -0600 Subject: docs: make Lua docstrings consistent #15255 The official developer documentation in in :h dev-lua-doc specifies to use "--@" for special/magic tokens. However, this format is not consistent with EmmyLua notation (used by some Lua language servers) nor with the C version of the magic docstring tokens which use three comment characters. Further, the code base is currently split between usage of "--@", "---@", and "--- @". In an effort to remain consistent, change all Lua magic tokens to use "---@" and update the developer documentation accordingly. --- runtime/lua/vim/F.lua | 4 +- runtime/lua/vim/highlight.lua | 14 +- runtime/lua/vim/lsp.lua | 296 +++++++++++++------------- runtime/lua/vim/lsp/buf.lua | 86 ++++---- runtime/lua/vim/lsp/codelens.lua | 8 +- runtime/lua/vim/lsp/diagnostic.lua | 70 +++---- runtime/lua/vim/lsp/handlers.lua | 28 +-- runtime/lua/vim/lsp/log.lua | 10 +- runtime/lua/vim/lsp/protocol.lua | 4 +- runtime/lua/vim/lsp/rpc.lua | 132 ++++++------ runtime/lua/vim/lsp/util.lua | 314 ++++++++++++++-------------- runtime/lua/vim/shared.lua | 138 ++++++------ runtime/lua/vim/treesitter.lua | 20 +- runtime/lua/vim/treesitter/highlighter.lua | 6 +- runtime/lua/vim/treesitter/language.lua | 8 +- runtime/lua/vim/treesitter/languagetree.lua | 34 +-- runtime/lua/vim/treesitter/query.lua | 68 +++--- runtime/lua/vim/uri.lua | 24 +-- 18 files changed, 632 insertions(+), 632 deletions(-) (limited to 'runtime/lua/vim') diff --git a/runtime/lua/vim/F.lua b/runtime/lua/vim/F.lua index 7925ff6e44..09467eb8c6 100644 --- a/runtime/lua/vim/F.lua +++ b/runtime/lua/vim/F.lua @@ -2,8 +2,8 @@ local F = {} --- Returns {a} if it is not nil, otherwise returns {b}. --- ---@param a ---@param b +---@param a +---@param b function F.if_nil(a, b) if a == nil then return b end return a diff --git a/runtime/lua/vim/highlight.lua b/runtime/lua/vim/highlight.lua index 0012dce081..4cb1994419 100644 --- a/runtime/lua/vim/highlight.lua +++ b/runtime/lua/vim/highlight.lua @@ -2,7 +2,7 @@ local api = vim.api local highlight = {} ---@private +---@private function highlight.create(higroup, hi_info, default) local options = {} -- TODO: Add validation @@ -12,7 +12,7 @@ function highlight.create(higroup, hi_info, default) vim.cmd(string.format([[highlight %s %s %s]], default and "default" or "", higroup, table.concat(options, " "))) end ---@private +---@private function highlight.link(higroup, link_to, force) vim.cmd(string.format([[highlight%s link %s %s]], force and "!" or " default", higroup, link_to)) end @@ -20,11 +20,11 @@ end --- Highlight range between two positions --- ---@param bufnr number of buffer to apply highlighting to ---@param ns namespace to add highlight to ---@param higroup highlight group to use for highlighting ---@param rtype type of range (:help setreg, default charwise) ---@param inclusive boolean indicating whether the range is end-inclusive (default false) +---@param bufnr number of buffer to apply highlighting to +---@param ns namespace to add highlight to +---@param higroup highlight group to use for highlighting +---@param rtype type of range (:help setreg, default charwise) +---@param inclusive boolean indicating whether the range is end-inclusive (default false) function highlight.range(bufnr, ns, higroup, start, finish, rtype, inclusive) rtype = rtype or 'v' inclusive = inclusive or false diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua index ca8db73d43..df48c10881 100644 --- a/runtime/lua/vim/lsp.lua +++ b/runtime/lua/vim/lsp.lua @@ -55,21 +55,21 @@ lsp._request_name_to_capability = { -- TODO improve handling of scratch buffers with LSP attached. ---@private +---@private --- Concatenates and writes a list of strings to the Vim error buffer. --- ---@param {...} (List of strings) List to write to the buffer +---@param {...} (List of strings) List to write to the buffer local function err_message(...) nvim_err_writeln(table.concat(vim.tbl_flatten{...})) nvim_command("redraw") end ---@private +---@private --- Returns the buffer number for the given {bufnr}. --- ---@param bufnr (number) Buffer number to resolve. Defaults to the current +---@param bufnr (number) Buffer number to resolve. Defaults to the current ---buffer if not given. ---@returns bufnr (number) Number of requested buffer +---@returns bufnr (number) Number of requested buffer local function resolve_bufnr(bufnr) validate { bufnr = { bufnr, 'n', true } } if bufnr == nil or bufnr == 0 then @@ -78,21 +78,21 @@ local function resolve_bufnr(bufnr) return bufnr end ---@private +---@private --- Called by the client when trying to call a method that's not --- supported in any of the servers registered for the current buffer. ---@param method (string) name of the method +---@param method (string) name of the method 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) return lsp.rpc_response_error(protocol.ErrorCodes.MethodNotFound, msg) end ---@private +---@private --- Checks whether a given path is a directory. --- ---@param filename (string) path to check ---@returns true if {filename} exists and is a directory, false otherwise +---@param filename (string) path to check +---@returns true if {filename} exists and is a directory, false otherwise local function is_dir(filename) validate{filename={filename,'s'}} local stat = uv.fs_stat(filename) @@ -108,10 +108,10 @@ local valid_encodings = { } local client_index = 0 ---@private +---@private --- Returns a new, unused client id. --- ---@returns (number) client id +---@returns (number) client id local function next_client_id() client_index = client_index + 1 return client_index @@ -124,11 +124,11 @@ local uninitialized_clients = {} -- Tracks all buffers attached to a client. local all_client_active_buffers = {} ---@private +---@private --- Invokes a function for each LSP client attached to the buffer {bufnr}. --- ---@param bufnr (Number) of buffer ---@param fn (function({client}, {client_id}, {bufnr}) Function to run on +---@param bufnr (Number) of buffer +---@param fn (function({client}, {client_id}, {bufnr}) Function to run on ---each client attached to that buffer. local function for_each_buffer_client(bufnr, fn) validate { @@ -154,11 +154,11 @@ lsp.client_errors = tbl_extend("error", lsp_rpc.client_errors, vim.tbl_add_rever ON_INIT_CALLBACK_ERROR = table.maxn(lsp_rpc.client_errors) + 1; }) ---@private +---@private --- Normalizes {encoding} to valid LSP encoding names. --- ---@param encoding (string) Encoding to normalize ---@returns (string) normalized encoding name +---@param encoding (string) Encoding to normalize +---@returns (string) normalized encoding name local function validate_encoding(encoding) validate { encoding = { encoding, 's' }; @@ -167,13 +167,13 @@ local function validate_encoding(encoding) or error(string.format("Invalid offset encoding %q. Must be one of: 'utf-8', 'utf-16', 'utf-32'", encoding)) end ---@internal +---@internal --- Parses a command invocation into the command itself and its args. If there --- are no arguments, an empty table is returned as the second argument. --- ---@param input (List) ---@returns (string) the command ---@returns (list of strings) its arguments +---@param input (List) +---@returns (string) the command +---@returns (list of strings) its arguments function lsp._cmd_parts(input) vim.validate{cmd={ input, @@ -192,12 +192,12 @@ function lsp._cmd_parts(input) return cmd, cmd_args end ---@private +---@private --- Augments a validator function with support for optional (nil) values. --- ---@param fn (function(v)) The original validator function; should return a +---@param fn (function(v)) The original validator function; should return a ---bool. ---@returns (function(v)) The augmented function. Also returns true if {v} is +---@returns (function(v)) The augmented function. Also returns true if {v} is ---`nil`. local function optional_validator(fn) return function(v) @@ -205,14 +205,14 @@ local function optional_validator(fn) end end ---@private +---@private --- Validates a client configuration as given to |vim.lsp.start_client()|. --- ---@param config (table) ---@returns (table) "Cleaned" config, containing only the command, its +---@param config (table) +---@returns (table) "Cleaned" config, containing only the command, its ---arguments, and a valid encoding. --- ---@see |vim.lsp.start_client()| +---@see |vim.lsp.start_client()| local function validate_client_config(config) validate { config = { config, 't' }; @@ -253,11 +253,11 @@ local function validate_client_config(config) } end ---@private +---@private --- Returns full text of buffer {bufnr} as a string. --- ---@param bufnr (number) Buffer handle, or 0 for current. ---@returns Buffer text as string. +---@param bufnr (number) Buffer handle, or 0 for current. +---@returns Buffer text as string. local function buf_get_full_text(bufnr) local text = table.concat(nvim_buf_get_lines(bufnr, 0, -1, true), '\n') if nvim_buf_get_option(bufnr, 'eol') then @@ -266,14 +266,14 @@ local function buf_get_full_text(bufnr) return text end ---@private +---@private --- Memoizes a function. On first run, the function return value is saved and --- 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. --- ---@param fn (function) Function to run ---@returns (function) Memoized function +---@param fn (function) Function to run +---@returns (function) Memoized function local function once(fn) local value local ran = false @@ -426,11 +426,11 @@ do end ---@private +---@private --- Default handler for the 'textDocument/didOpen' LSP notification. --- ---@param bufnr (Number) Number of the buffer, or 0 for current ---@param client Client object +---@param bufnr (Number) Number of the buffer, or 0 for current +---@param client Client object local function text_document_did_open_handler(bufnr, client) changetracking.init(client, bufnr) if not client.resolved_capabilities.text_document_open_close then @@ -548,16 +548,16 @@ end --- --- The following parameters describe fields in the {config} table. --- ---@param root_dir: (string) Directory where the LSP server will base +---@param root_dir: (string) Directory where the LSP server will base --- its rootUri on initialization. --- ---@param cmd: (required, string or list treated like |jobstart()|) Base command +---@param cmd: (required, string or list treated like |jobstart()|) Base command --- that initiates the LSP client. --- ---@param cmd_cwd: (string, default=|getcwd()|) Directory to launch +---@param cmd_cwd: (string, default=|getcwd()|) Directory to launch --- the `cmd` process. Not related to `root_dir`. --- ---@param cmd_env: (table) Environment flags to pass to the LSP on +---@param cmd_env: (table) Environment flags to pass to the LSP on --- spawn. Can be specified using keys like a map or as a list with `k=v` --- pairs or both. Non-string values are coerced to string. --- Example: @@ -565,7 +565,7 @@ end --- { "PRODUCTION=true"; "TEST=123"; PORT = 8080; HOST = "0.0.0.0"; } --- --- ---@param capabilities Map overriding the default capabilities defined by +---@param capabilities Map overriding the default capabilities defined by --- |vim.lsp.protocol.make_client_capabilities()|, passed to the language --- server on initialization. Hint: use make_client_capabilities() and modify --- its result. @@ -573,41 +573,41 @@ end --- `{[vim.type_idx]=vim.types.dictionary}`, else it will be encoded as an --- array. --- ---@param handlers Map of language server method names to |lsp-handler| +---@param handlers Map of language server method names to |lsp-handler| --- ---@param settings Map with language server specific settings. These are +---@param settings Map with language server specific settings. These are --- returned to the language server if requested via `workspace/configuration`. --- Keys are case-sensitive. --- ---@param init_options Values to pass in the initialization request +---@param init_options Values to pass in the initialization request --- as `initializationOptions`. See `initialize` in the LSP spec. --- ---@param name (string, default=client-id) Name in log messages. +---@param name (string, default=client-id) Name in log messages. -- ---@param workspace_folders (table) List of workspace folders passed to the +---@param workspace_folders (table) List of workspace folders passed to the --- language server. Defaults to root_dir if not set. See `workspaceFolders` in --- the LSP spec --- ---@param get_language_id function(bufnr, filetype) -> language ID as string. +---@param get_language_id function(bufnr, filetype) -> language ID as string. --- Defaults to the filetype. --- ---@param offset_encoding (default="utf-16") One of "utf-8", "utf-16", +---@param offset_encoding (default="utf-16") One of "utf-8", "utf-16", --- or "utf-32" which is the encoding that the LSP server expects. Client does --- not verify this is correct. --- ---@param on_error Callback with parameters (code, ...), invoked +---@param on_error Callback with parameters (code, ...), invoked --- when the client operation throws an error. `code` is a number describing --- the error. Other arguments may be passed depending on the error kind. See --- |vim.lsp.client_errors| for possible errors. --- Use `vim.lsp.client_errors[code]` to get human-friendly name. --- ---@param before_init Callback with parameters (initialize_params, config) +---@param before_init Callback with parameters (initialize_params, config) --- invoked before the LSP "initialize" phase, where `params` contains the --- parameters being sent to the server and `config` is the config that was --- passed to |vim.lsp.start_client()|. You can use this to modify parameters before --- they are sent. --- ---@param on_init Callback (client, initialize_result) invoked after LSP +---@param on_init Callback (client, initialize_result) invoked after LSP --- "initialize", where `result` is a table of `capabilities` and anything else --- the server may send. For example, clangd sends --- `initialize_result.offsetEncoding` if `capabilities.offsetEncoding` was @@ -617,24 +617,24 @@ end --- `workspace/didChangeConfiguration` notification should be sent --- to the server during on_init. --- ---@param on_exit Callback (code, signal, client_id) invoked on client +---@param on_exit Callback (code, signal, client_id) invoked on client --- exit. --- - code: exit code of the process --- - signal: number describing the signal used to terminate (if any) --- - client_id: client handle --- ---@param on_attach Callback (client, bufnr) invoked when client +---@param on_attach Callback (client, bufnr) invoked when client --- attaches to a buffer. --- ---@param trace: "off" | "messages" | "verbose" | nil passed directly to the language +---@param trace: "off" | "messages" | "verbose" | nil passed directly to the language --- server in the initialize request. Invalid/empty values will default to "off" ---@param flags: A table with flags for the client. The current (experimental) flags are: +---@param flags: A table with flags for the client. The current (experimental) flags are: --- - allow_incremental_sync (bool, default true): Allow using incremental sync for buffer edits --- - debounce_text_changes (number, default nil): Debounce didChange --- notifications to the server by the given number in milliseconds. No debounce --- occurs if nil --- ---@returns Client id. |vim.lsp.get_client_by_id()| Note: client may not be +---@returns 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. function lsp.start_client(config) @@ -657,22 +657,22 @@ function lsp.start_client(config) local dispatch = {} - --@private + ---@private --- Returns the handler associated with an LSP method. --- Returns the default handler if the user hasn't set a custom one. --- - --@param method (string) LSP method name - --@returns (fn) The handler for the given method, if defined, or the default from |vim.lsp.handlers| + ---@param method (string) LSP method name + ---@returns (fn) The handler for the given method, if defined, or the default from |vim.lsp.handlers| local function resolve_handler(method) return handlers[method] or default_handlers[method] end - --@private + ---@private --- Handles a notification sent by an LSP server by invoking the --- corresponding handler. --- - --@param method (string) LSP method name - --@param params (table) The parameters for that method. + ---@param method (string) LSP method name + ---@param params (table) The parameters for that method. function dispatch.notification(method, params) local _ = log.debug() and log.debug('notification', method, params) local handler = resolve_handler(method) @@ -682,11 +682,11 @@ function lsp.start_client(config) end end - --@private + ---@private --- Handles a request from an LSP server by invoking the corresponding handler. --- - --@param method (string) LSP method name - --@param params (table) The parameters for that method + ---@param method (string) LSP method name + ---@param params (table) The parameters for that method function dispatch.server_request(method, params) local _ = log.debug() and log.debug('server_request', method, params) local handler = resolve_handler(method) @@ -698,12 +698,12 @@ function lsp.start_client(config) return nil, lsp.rpc_response_error(protocol.ErrorCodes.MethodNotFound) end - --@private + ---@private --- Invoked when the client operation throws an error. --- - --@param code (number) Error code - --@param err (...) Other arguments may be passed depending on the error kind - --@see |vim.lsp.client_errors| for possible errors. Use + ---@param code (number) Error code + ---@param err (...) Other arguments may be passed depending on the error kind + ---@see |vim.lsp.client_errors| for possible errors. Use ---`vim.lsp.client_errors[code]` to get a human-friendly name. function dispatch.on_error(code, err) local _ = log.error() and log.error(log_prefix, "on_error", { code = lsp.client_errors[code], err = err }) @@ -717,11 +717,11 @@ function lsp.start_client(config) end end - --@private + ---@private --- Invoked on client exit. --- - --@param code (number) exit code of the process - --@param signal (number) the signal used to terminate (if any) + ---@param code (number) exit code of the process + ---@param signal (number) the signal used to terminate (if any) function dispatch.on_exit(code, signal) active_clients[client_id] = nil uninitialized_clients[client_id] = nil @@ -766,7 +766,7 @@ function lsp.start_client(config) -- Store the uninitialized_clients for cleanup in case we exit before initialize finishes. uninitialized_clients[client_id] = client; - --@private + ---@private local function initialize() local valid_traces = { off = 'off'; messages = 'messages'; verbose = 'verbose'; @@ -869,23 +869,23 @@ function lsp.start_client(config) end) end - --@private + ---@private --- Sends a request to the server. --- --- This is a thin wrapper around {client.rpc.request} with some additional --- checks for capabilities and handler availability. --- - --@param method (string) LSP method name. - --@param params (table) LSP request params. - --@param handler (function, optional) Response |lsp-handler| for this method. - --@param bufnr (number) Buffer handle (0 for current). - --@returns ({status}, [request_id]): {status} is a bool indicating + ---@param method (string) LSP method name. + ---@param params (table) LSP request params. + ---@param handler (function, optional) Response |lsp-handler| for this method. + ---@param bufnr (number) Buffer handle (0 for current). + ---@returns ({status}, [request_id]): {status} is a bool indicating ---whether the request was successful. If it is `false`, then it will ---always be `false` (the client has shutdown). If it was ---successful, then it will return {request_id} as the ---second result. You can use this with `client.cancel_request(request_id)` ---to cancel the-request. - --@see |vim.lsp.buf_request()| + ---@see |vim.lsp.buf_request()| function client.request(method, params, handler, bufnr) if not handler then handler = resolve_handler(method) @@ -900,21 +900,21 @@ function lsp.start_client(config) end) end - --@private + ---@private --- Sends a request to the server and synchronously waits for the response. --- --- This is a wrapper around {client.request} --- - --@param method (string) LSP method name. - --@param params (table) LSP request params. - --@param timeout_ms (number, optional, default=1000) Maximum time in + ---@param method (string) LSP method name. + ---@param params (table) LSP request params. + ---@param timeout_ms (number, optional, default=1000) Maximum time in ---milliseconds to wait for a result. - --@param bufnr (number) Buffer handle (0 for current). - --@returns { err=err, result=result }, a dictionary, where `err` and `result` come from the |lsp-handler|. + ---@param bufnr (number) Buffer handle (0 for current). + ---@returns { err=err, result=result }, a dictionary, where `err` and `result` come from the |lsp-handler|. ---On timeout, cancel or error, returns `(nil, err)` where `err` is a ---string describing the failure reason. If the request was unsuccessful ---returns `nil`. - --@see |vim.lsp.buf_request_sync()| + ---@see |vim.lsp.buf_request_sync()| function client.request_sync(method, params, timeout_ms, bufnr) local request_result = nil local function _sync_handler(err, _, result) @@ -936,25 +936,25 @@ function lsp.start_client(config) return request_result end - --@private + ---@private --- Sends a notification to an LSP server. --- - --@param method (string) LSP method name. - --@param params (optional, table) LSP request params. - --@param bufnr (number) Buffer handle, or 0 for current. - --@returns {status} (bool) true if the notification was successful. + ---@param method (string) LSP method name. + ---@param params (optional, table) LSP request params. + ---@param bufnr (number) Buffer handle, or 0 for current. + ---@returns {status} (bool) true if the notification was successful. ---If it is false, then it will always be false ---(the client has shutdown). function client.notify(...) return rpc.notify(...) end - --@private + ---@private --- Cancels a request with a given request id. --- - --@param id (number) id of request to cancel - --@returns true if any client returns true; false otherwise - --@see |vim.lsp.client.notify()| + ---@param id (number) id of request to cancel + ---@returns true if any client returns true; false otherwise + ---@see |vim.lsp.client.notify()| function client.cancel_request(id) validate{id = {id, 'n'}} return rpc.notify("$/cancelRequest", { id = id }) @@ -963,14 +963,14 @@ function lsp.start_client(config) -- Track this so that we can escalate automatically if we've alredy tried a -- graceful shutdown local graceful_shutdown_failed = false - --@private + ---@private --- Stops a client, optionally with force. --- ---By default, it will just ask the - server to shutdown without force. If --- you request to stop a client which has previously been requested to --- shutdown, it will automatically escalate and force shutdown. --- - --@param force (bool, optional) + ---@param force (bool, optional) function client.stop(force) lsp.diagnostic.reset(client_id, all_buffer_active_clients) @@ -1000,18 +1000,18 @@ function lsp.start_client(config) end) end - --@private + ---@private --- Checks whether a client is stopped. --- - --@returns (bool) true if client is stopped or in the process of being + ---@returns (bool) true if client is stopped or in the process of being ---stopped; false otherwise function client.is_stopped() return rpc.handle:is_closing() end - --@private + ---@private --- Runs the on_attach function from the client's config if it was defined. - --@param bufnr (number) Buffer number + ---@param bufnr (number) Buffer number function client._on_attach(bufnr) text_document_did_open_handler(bufnr, client) if config.on_attach then @@ -1025,8 +1025,8 @@ function lsp.start_client(config) return client_id end ---@private ---@fn text_document_did_change_handler(_, bufnr, changedtick, firstline, lastline, new_lastline, old_byte_size, old_utf32_size, old_utf16_size) +---@private +---@fn text_document_did_change_handler(_, bufnr, changedtick, firstline, lastline, new_lastline, old_byte_size, old_utf32_size, old_utf16_size) --- Notify all attached clients that a buffer has changed. local text_document_did_change_handler do @@ -1076,8 +1076,8 @@ end --- --- Without calling this, the server won't be notified of changes to a buffer. --- ---- @param bufnr (number) Buffer handle, or 0 for current ---- @param client_id (number) Client id +---@param bufnr (number) Buffer handle, or 0 for current +---@param client_id (number) Client id function lsp.buf_attach_client(bufnr, client_id) validate { bufnr = {bufnr, 'n', true}; @@ -1158,17 +1158,17 @@ end --- Gets a client by id, or nil if the id is invalid. --- The returned client may not yet be fully initialized. -- ---@param client_id client id number +---@param client_id client id number --- ---@returns |vim.lsp.client| object, or nil +---@returns |vim.lsp.client| object, or nil function lsp.get_client_by_id(client_id) return active_clients[client_id] or uninitialized_clients[client_id] end --- Returns list of buffers attached to client_id. -- ---@param client_id client id ---@returns list of buffer ids +---@param client_id client id +---@returns list of buffer ids function lsp.get_buffers_by_client_id(client_id) local active_client_buffers = all_client_active_buffers[client_id] if active_client_buffers then @@ -1190,8 +1190,8 @@ end --- By default asks the server to shutdown, unless stop was requested --- already for this client, then force-shutdown is attempted. --- ---@param client_id client id or |vim.lsp.client| object, or list thereof ---@param force boolean (optional) shutdown forcefully +---@param client_id client id or |vim.lsp.client| object, or list thereof +---@param force boolean (optional) shutdown forcefully function lsp.stop_client(client_id, force) local ids = type(client_id) == 'table' and client_id or {client_id} for _, id in ipairs(ids) do @@ -1207,7 +1207,7 @@ end --- Gets all active clients. --- ---@returns Table of |vim.lsp.client| objects +---@returns Table of |vim.lsp.client| objects function lsp.get_active_clients() return vim.tbl_values(active_clients) end @@ -1238,13 +1238,13 @@ nvim_command("autocmd VimLeavePre * lua vim.lsp._vim_exit_handler()") --- Sends an async request for all active clients attached to the --- buffer. --- ---@param bufnr (number) Buffer handle, or 0 for current. ---@param method (string) LSP method name ---@param params (optional, table) Parameters to send to the server ---@param handler (optional, function) See |lsp-handler| +---@param bufnr (number) Buffer handle, or 0 for current. +---@param method (string) LSP method name +---@param params (optional, table) Parameters to send to the server +---@param handler (optional, function) See |lsp-handler| -- If nil, follows resolution strategy defined in |lsp-handler-configuration| -- ---@returns 2-tuple: +---@returns 2-tuple: --- - Map of client-id:request-id pairs for all successful requests. --- - Function which can be used to cancel all the requests. You could instead --- iterate all clients and call their `cancel_request()` methods. @@ -1296,14 +1296,14 @@ end ---Parameters are the same as |vim.lsp.buf_request()| but the return result and callback are ---different. --- ---@param bufnr (number) Buffer handle, or 0 for current. ---@param method (string) LSP method name ---@param params (optional, table) Parameters to send to the server ---@param callback (function) The callback to call when all requests are finished. +---@param bufnr (number) Buffer handle, or 0 for current. +---@param method (string) LSP method name +---@param params (optional, table) Parameters to send to the server +---@param callback (function) The callback to call when all requests are finished. -- Unlike `buf_request`, this will collect all the responses from each server instead of handling them. -- A map of client_id:request_result will be provided to the callback -- ---@returns (function) A function that will cancel all requests which is the same as the one returned from `buf_request`. +---@returns (function) A function that will cancel all requests which is the same as the one returned from `buf_request`. function lsp.buf_request_all(bufnr, method, params, callback) local request_results = {} local result_count = 0 @@ -1337,13 +1337,13 @@ end --- Parameters are the same as |vim.lsp.buf_request()| but the return result is --- different. Wait maximum of {timeout_ms} (default 1000) ms. --- ---@param bufnr (number) Buffer handle, or 0 for current. ---@param method (string) LSP method name ---@param params (optional, table) Parameters to send to the server ---@param timeout_ms (optional, number, default=1000) Maximum time in +---@param bufnr (number) Buffer handle, or 0 for current. +---@param method (string) LSP method name +---@param params (optional, table) Parameters to send to the server +---@param timeout_ms (optional, number, default=1000) Maximum time in --- milliseconds to wait for a result. --- ---@returns Map of client_id:request_result. On timeout, cancel or error, +---@returns Map of client_id:request_result. On timeout, cancel or error, --- returns `(nil, err)` where `err` is a string describing the failure --- reason. function lsp.buf_request_sync(bufnr, method, params, timeout_ms) @@ -1366,11 +1366,11 @@ function lsp.buf_request_sync(bufnr, method, params, timeout_ms) end --- Send a notification to a server ---@param bufnr [number] (optional): The number of the buffer ---@param method [string]: Name of the request method ---@param params [string]: Arguments to send to the server +---@param bufnr [number] (optional): The number of the buffer +---@param method [string]: Name of the request method +---@param params [string]: Arguments to send to the server --- ---@returns true if any client returns true; false otherwise +---@returns true if any client returns true; false otherwise function lsp.buf_notify(bufnr, method, params) validate { bufnr = { bufnr, 'n', true }; @@ -1385,14 +1385,14 @@ end --- Implements 'omnifunc' compatible LSP completion. --- ---@see |complete-functions| ---@see |complete-items| ---@see |CompleteDone| +---@see |complete-functions| +---@see |complete-items| +---@see |CompleteDone| --- ---@param findstart 0 or 1, decides behavior ---@param base If findstart=0, text to match against +---@param findstart 0 or 1, decides behavior +---@param base If findstart=0, text to match against --- ---@returns (number) Decided by `findstart`: +---@returns (number) Decided by `findstart`: --- - findstart=0: column where the completion starts, or -2 or -3 --- - findstart=1: list of matches (actually just calls |complete()|) function lsp.omnifunc(findstart, base) @@ -1438,8 +1438,8 @@ end ---Checks whether a client is stopped. --- ---@param client_id (Number) ---@returns true if client is stopped, false otherwise. +---@param client_id (Number) +---@returns true if client is stopped, false otherwise. function lsp.client_is_stopped(client_id) return active_clients[client_id] == nil end @@ -1447,7 +1447,7 @@ end --- Gets a map of client_id:client pairs for the given buffer, where each value --- is a |vim.lsp.client| object. --- ---@param bufnr (optional, number): Buffer handle, or 0 for current +---@param bufnr (optional, number): Buffer handle, or 0 for current function lsp.buf_get_clients(bufnr) bufnr = resolve_bufnr(bufnr) local result = {} @@ -1472,9 +1472,9 @@ lsp.log_levels = log.levels --- --- Use `lsp.log_levels` for reverse lookup. --- ---@see |vim.lsp.log_levels| +---@see |vim.lsp.log_levels| --- ---@param level [number|string] the case insensitive level name or number +---@param level [number|string] the case insensitive level name or number function lsp.set_log_level(level) if type(level) == 'string' or type(level) == 'number' then log.set_level(level) @@ -1484,7 +1484,7 @@ function lsp.set_log_level(level) end --- Gets the path of the logfile used by the LSP client. ---@returns (String) Path to logfile. +---@returns (String) Path to logfile. function lsp.get_log_path() return log.get_filename() end @@ -1495,8 +1495,8 @@ function lsp.for_each_buffer_client(bufnr, fn) end --- Function to manage overriding defaults for LSP handlers. ---@param handler (function) See |lsp-handler| ---@param override_config (table) Table containing the keys to override behavior of the {handler} +---@param handler (function) See |lsp-handler| +---@param override_config (table) Table containing the keys to override behavior of the {handler} function lsp.with(handler, override_config) return function(err, method, params, client_id, bufnr, config) return handler(err, method, params, client_id, bufnr, vim.tbl_deep_extend("force", config or {}, override_config)) diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index 29f8d6c3bc..250af0a0a7 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -5,7 +5,7 @@ local util = require 'vim.lsp.util' local M = {} ---@private +---@private --- Returns nil if {status} is false or nil, otherwise returns the rest of the --- arguments. local function ok_or_nil(status, ...) @@ -13,31 +13,31 @@ local function ok_or_nil(status, ...) return ... end ---@private +---@private --- Swallows errors. --- ---@param fn Function to run ---@param ... Function arguments ---@returns Result of `fn(...)` if there are no errors, otherwise nil. +---@param fn Function to run +---@param ... Function arguments +---@returns Result of `fn(...)` if there are no errors, otherwise nil. --- Returns nil if errors occur during {fn}, otherwise returns local function npcall(fn, ...) return ok_or_nil(pcall(fn, ...)) end ---@private +---@private --- Sends an async request to all active clients attached to the current --- buffer. --- ---@param method (string) LSP method name ---@param params (optional, table) Parameters to send to the server ---@param handler (optional, functionnil) See |lsp-handler|. Follows |lsp-handler-resolution| +---@param method (string) LSP method name +---@param params (optional, table) Parameters to send to the server +---@param handler (optional, functionnil) See |lsp-handler|. Follows |lsp-handler-resolution| -- ---@returns 2-tuple: +---@returns 2-tuple: --- - Map of client-id:request-id pairs for all successful requests. --- - Function which can be used to cancel all the requests. You could instead --- iterate all clients and call their `cancel_request()` methods. --- ---@see |vim.lsp.buf_request()| +---@see |vim.lsp.buf_request()| local function request(method, params, handler) validate { method = {method, 's'}; @@ -49,7 +49,7 @@ end --- Checks whether the language servers attached to the current buffer are --- ready. --- ---@returns `true` if server responds. +---@returns `true` if server responds. function M.server_ready() return not not vim.lsp.buf_notify(0, "window/progress", {}) end @@ -62,7 +62,7 @@ function M.hover() end --- Jumps to the declaration of the symbol under the cursor. ---@note Many servers do not implement this method. Generally, see |vim.lsp.buf.definition()| instead. +---@note Many servers do not implement this method. Generally, see |vim.lsp.buf.definition()| instead. --- function M.declaration() local params = util.make_position_params() @@ -100,22 +100,22 @@ end --- Retrieves the completion items at the current cursor position. Can only be --- called in Insert mode. --- ---@param context (context support not yet implemented) Additional information +---@param context (context support not yet implemented) Additional information --- about the context in which a completion was triggered (how it was triggered, --- and by which trigger character, if applicable) --- ---@see |vim.lsp.protocol.constants.CompletionTriggerKind| +---@see |vim.lsp.protocol.constants.CompletionTriggerKind| function M.completion(context) local params = util.make_position_params() params.context = context return request('textDocument/completion', params) end ---@private +---@private --- If there is more than one client that supports the given method, --- asks the user to select one. -- ---@returns The client that the user selected or nil +---@returns The client that the user selected or nil local function select_client(method) local clients = vim.tbl_values(vim.lsp.buf_get_clients()); clients = vim.tbl_filter(function (client) @@ -146,11 +146,11 @@ end --- Formats the current buffer. --- ---@param options (optional, table) Can be used to specify FormattingOptions. +---@param options (optional, table) Can be used to specify FormattingOptions. --- Some unspecified options will be automatically derived from the current --- Neovim options. -- ---@see https://microsoft.github.io/language-server-protocol/specification#textDocument_formatting +---@see https://microsoft.github.io/language-server-protocol/specification#textDocument_formatting function M.formatting(options) local client = select_client("textDocument/formatting") if client == nil then return end @@ -168,9 +168,9 @@ end --- vim.api.nvim_command[[autocmd BufWritePre lua vim.lsp.buf.formatting_sync()]] --- --- ---@param options Table with valid `FormattingOptions` entries ---@param timeout_ms (number) Request timeout ---@see |vim.lsp.buf.formatting_seq_sync| +---@param options Table with valid `FormattingOptions` entries +---@param timeout_ms (number) Request timeout +---@see |vim.lsp.buf.formatting_seq_sync| function M.formatting_sync(options, timeout_ms) local client = select_client("textDocument/formatting") if client == nil then return end @@ -195,9 +195,9 @@ end --- vim.api.nvim_command[[autocmd BufWritePre lua vim.lsp.buf.formatting_seq_sync()]] --- --- ---@param options (optional, table) `FormattingOptions` entries ---@param timeout_ms (optional, number) Request timeout ---@param order (optional, table) List of client names. Formatting is requested from clients +---@param options (optional, table) `FormattingOptions` entries +---@param timeout_ms (optional, number) Request timeout +---@param order (optional, table) List of client names. Formatting is requested from clients ---in the following order: first all clients that are not in the `order` list, then ---the remaining clients in the order as they occur in the `order` list. function M.formatting_seq_sync(options, timeout_ms, order) @@ -230,10 +230,10 @@ end --- Formats a given range. --- ---@param options Table with valid `FormattingOptions` entries. ---@param start_pos ({number, number}, optional) mark-indexed position. +---@param options Table with valid `FormattingOptions` entries. +---@param start_pos ({number, number}, optional) mark-indexed position. ---Defaults to the start of the last visual selection. ---@param end_pos ({number, number}, optional) mark-indexed position. +---@param end_pos ({number, number}, optional) mark-indexed position. ---Defaults to the end of the last visual selection. function M.range_formatting(options, start_pos, end_pos) local client = select_client("textDocument/rangeFormatting") @@ -246,7 +246,7 @@ end --- Renames all references to the symbol under the cursor. --- ---@param new_name (string) If not provided, the user will be prompted for a new +---@param new_name (string) If not provided, the user will be prompted for a new ---name using |input()|. function M.rename(new_name) -- TODO(ashkan) use prepareRename @@ -260,8 +260,8 @@ end --- Lists all the references to the symbol under the cursor in the quickfix window. --- ---@param context (table) Context for the request ---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references +---@param context (table) Context for the request +---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references function M.references(context) validate { context = { context, 't', true } } local params = util.make_position_params() @@ -279,7 +279,7 @@ function M.document_symbol() request('textDocument/documentSymbol', params) end ---@private +---@private local function pick_call_hierarchy_item(call_hierarchy_items) if not call_hierarchy_items then return end if #call_hierarchy_items == 1 then @@ -297,7 +297,7 @@ local function pick_call_hierarchy_item(call_hierarchy_items) return choice end ---@private +---@private local function call_hierarchy(method) local params = util.make_position_params() request('textDocument/prepareCallHierarchy', params, function(err, _, result) @@ -389,7 +389,7 @@ end --- call, the user is prompted to enter a string on the command line. An empty --- string means no filtering is done. --- ---@param query (string, optional) +---@param query (string, optional) function M.workspace_symbol(query) query = query or npcall(vfn.input, "Query: ") local params = {query = query} @@ -424,7 +424,7 @@ end --- Requests code actions from all clients and calls the handler exactly once --- with all aggregated results ---@private +---@private local function code_action_request(params) local bufnr = vim.api.nvim_get_current_buf() local method = 'textDocument/codeAction' @@ -439,9 +439,9 @@ end --- Selects a code action from the input list that is available at the current --- cursor position. --- ---@param context: (table, optional) Valid `CodeActionContext` object ---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_codeAction +--- +---@param context: (table, optional) Valid `CodeActionContext` object +---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_codeAction function M.code_action(context) validate { context = { context, 't', true } } context = context or { diagnostics = vim.lsp.diagnostic.get_line_diagnostics() } @@ -452,10 +452,10 @@ end --- Performs |vim.lsp.buf.code_action()| for a given range. --- ---@param context: (table, optional) Valid `CodeActionContext` object ---@param start_pos ({number, number}, optional) mark-indexed position. +---@param context: (table, optional) Valid `CodeActionContext` object +---@param start_pos ({number, number}, optional) mark-indexed position. ---Defaults to the start of the last visual selection. ---@param end_pos ({number, number}, optional) mark-indexed position. +---@param end_pos ({number, number}, optional) mark-indexed position. ---Defaults to the end of the last visual selection. function M.range_code_action(context, start_pos, end_pos) validate { context = { context, 't', true } } @@ -467,8 +467,8 @@ end --- Executes an LSP server command. --- ---@param command A valid `ExecuteCommandParams` object ---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand +---@param command A valid `ExecuteCommandParams` object +---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand function M.execute_command(command) validate { command = { command.command, 's' }, diff --git a/runtime/lua/vim/lsp/codelens.lua b/runtime/lua/vim/lsp/codelens.lua index fe581e42ad..ca5a44077e 100644 --- a/runtime/lua/vim/lsp/codelens.lua +++ b/runtime/lua/vim/lsp/codelens.lua @@ -22,11 +22,11 @@ local namespaces = setmetatable({}, { end; }) ---@private +---@private M.__namespaces = namespaces ---@private +---@private local function execute_lens(lens, bufnr, client_id) local line = lens.range.start.line api.nvim_buf_clear_namespace(bufnr, namespaces[client_id], line, line + 1) @@ -151,7 +151,7 @@ function M.save(lenses, bufnr, client_id) end ---@private +---@private local function resolve_lenses(lenses, bufnr, client_id, callback) lenses = lenses or {} local num_lens = vim.tbl_count(lenses) @@ -160,7 +160,7 @@ local function resolve_lenses(lenses, bufnr, client_id, callback) return end - --@private + ---@private local function countdown() num_lens = num_lens - 1 if num_lens == 0 then diff --git a/runtime/lua/vim/lsp/diagnostic.lua b/runtime/lua/vim/lsp/diagnostic.lua index a244e3d6a4..45aeb17465 100644 --- a/runtime/lua/vim/lsp/diagnostic.lua +++ b/runtime/lua/vim/lsp/diagnostic.lua @@ -8,7 +8,7 @@ local util = require('vim.lsp.util') local if_nil = vim.F.if_nil ---@class DiagnosticSeverity +---@class DiagnosticSeverity local DiagnosticSeverity = protocol.DiagnosticSeverity local to_severity = function(severity) @@ -46,14 +46,14 @@ end ---@brief lsp-diagnostic --- ---@class Diagnostic ---@field range Range ---@field message string ---@field severity DiagnosticSeverity|nil ---@field code number | string ---@field source string ---@field tags DiagnosticTag[] ---@field relatedInformation DiagnosticRelatedInformation[] +---@class Diagnostic +---@field range Range +---@field message string +---@field severity DiagnosticSeverity|nil +---@field code number | string +---@field source string +---@field tags DiagnosticTag[] +---@field relatedInformation DiagnosticRelatedInformation[] local M = {} @@ -167,12 +167,12 @@ end local _diagnostic_namespaces = _make_namespace_table("vim_lsp_diagnostics", true) local _sign_namespaces = _make_namespace_table("vim_lsp_signs", false) ---@private +---@private function M._get_diagnostic_namespace(client_id) return _diagnostic_namespaces[client_id] end ---@private +---@private function M._get_sign_namespace(client_id) return _sign_namespaces[client_id] end @@ -255,7 +255,7 @@ local _diagnostic_counts = function(diagnostics) return counts end ---@private +---@private --- Set the different diagnostic cache after `textDocument/publishDiagnostics` ---@param diagnostics Diagnostic[] ---@param bufnr number @@ -291,7 +291,7 @@ local function set_diagnostic_cache(diagnostics, bufnr, client_id) end ---@private +---@private --- Clear the cached diagnostics ---@param bufnr number ---@param client_id number @@ -512,7 +512,7 @@ local _next_diagnostic = function(position, search_forward, bufnr, opts, client_ end end ---@private +---@private --- Helper function to return a position from a diagnostic --- ---@return table {row, col} @@ -527,7 +527,7 @@ local function _diagnostic_pos(opts, diagnostic) return to_position(diagnostic.range.start, bufnr) end ---@private +---@private -- Move to the diagnostic position local function _diagnostic_move_pos(name, opts, pos) opts = opts or {} @@ -858,7 +858,7 @@ end --- Callback scheduled for after leaving insert mode --- --- Used to handle ---@private +---@private function M._execute_scheduled_display(bufnr, client_id) local args = _bufs_waiting_to_update[bufnr][client_id] if not args then @@ -924,20 +924,20 @@ end -- Diagnostic Private Highlight Utilies {{{ --- Get the severity highlight name ---@private +---@private function M._get_severity_highlight_name(severity) return virtual_text_highlight_map[severity] end --- Get floating severity highlight name ---@private +---@private function M._get_floating_severity_highlight_name(severity) return floating_highlight_map[severity] end --- This should be called to update the highlights for the LSP client. function M._define_default_signs_and_highlights() - --@private + ---@private local function define_default_sign(name, properties) if vim.tbl_isempty(vim.fn.sign_getdefined(name)) then vim.fn.sign_define(name, properties) @@ -1054,8 +1054,8 @@ function M.on_publish_diagnostics(_, _, params, client_id, _, config) end -- restores the extmarks set by M.display ---- @param last number last line that was changed --- @private +---@param last number last line that was changed +---@private local function restore_extmarks(bufnr, last) for client_id, extmarks in pairs(diagnostic_cache_extmarks[bufnr]) do local ns = M._get_diagnostic_namespace(client_id) @@ -1084,7 +1084,7 @@ local function restore_extmarks(bufnr, last) end -- caches the extmarks set by M.display --- @private +---@private local function save_extmarks(bufnr, client_id) bufnr = bufnr == 0 and api.nvim_get_current_buf() or bufnr if not diagnostic_attached_buffers[bufnr] then @@ -1101,7 +1101,7 @@ local function save_extmarks(bufnr, client_id) diagnostic_cache_extmarks[bufnr][client_id] = api.nvim_buf_get_extmarks(bufnr, ns, 0, -1, {details = true}) end ---@private +---@private --- Display diagnostics for the buffer, given a configuration. function M.display(diagnostics, bufnr, client_id, config) if diagnostic_disabled[bufnr][client_id] then @@ -1187,10 +1187,10 @@ end --- for redrawing diagnostics after making changes in diagnostics --- configuration. |lsp-handler-configuration| --- ---- @param bufnr (optional, number): Buffer handle, defaults to current ---- @param client_id (optional, number): Redraw diagnostics for the given ---- client. The default is to redraw diagnostics for all attached ---- clients. +---@param bufnr (optional, number): Buffer handle, defaults to current +---@param client_id (optional, number): Redraw diagnostics for the given +--- client. The default is to redraw diagnostics for all attached +--- clients. function M.redraw(bufnr, client_id) bufnr = get_bufnr(bufnr) if not client_id then @@ -1410,10 +1410,10 @@ function M.set_loclist(opts) end --- Disable diagnostics for the given buffer and client ---- @param bufnr (optional, number): Buffer handle, defaults to current ---- @param client_id (optional, number): Disable diagnostics for the given ---- client. The default is to disable diagnostics for all attached ---- clients. +---@param bufnr (optional, number): Buffer handle, defaults to current +---@param client_id (optional, number): Disable diagnostics for the given +--- client. The default is to disable diagnostics for all attached +--- clients. -- Note that when diagnostics are disabled for a buffer, the server will still -- send diagnostic information and the client will still process it. The -- diagnostics are simply not displayed to the user. @@ -1429,10 +1429,10 @@ function M.disable(bufnr, client_id) end --- Enable diagnostics for the given buffer and client ---- @param bufnr (optional, number): Buffer handle, defaults to current ---- @param client_id (optional, number): Enable diagnostics for the given ---- client. The default is to enable diagnostics for all attached ---- clients. +---@param bufnr (optional, number): Buffer handle, defaults to current +---@param client_id (optional, number): Enable diagnostics for the given +--- client. The default is to enable diagnostics for all attached +--- clients. function M.enable(bufnr, client_id) if not client_id then return vim.lsp.for_each_buffer_client(bufnr, function(client) diff --git a/runtime/lua/vim/lsp/handlers.lua b/runtime/lua/vim/lsp/handlers.lua index b7f1ea0ab6..befb9fcec3 100644 --- a/runtime/lua/vim/lsp/handlers.lua +++ b/runtime/lua/vim/lsp/handlers.lua @@ -9,9 +9,9 @@ local M = {} -- FIXME: DOC: Expose in vimdocs ---@private +---@private --- Writes to error buffer. ---@param ... (table of strings) Will be concatenated before being written +---@param ... (table of strings) Will be concatenated before being written local function err_message(...) vim.notify(table.concat(vim.tbl_flatten{...}), vim.log.levels.ERROR) api.nvim_command("redraw") @@ -22,7 +22,7 @@ M['workspace/executeCommand'] = function() -- Error handling is done implicitly by wrapping all handlers; see end of this file end ---@private +---@private local function progress_handler(_, _, params, client_id) local client = vim.lsp.get_client_by_id(client_id) local client_name = client and client.name or string.format("id=%d", client_id) @@ -189,7 +189,7 @@ end ---@private +---@private --- Return a function that converts LSP responses to list items and opens the list --- --- The returned function has an optional {config} parameter that accepts a table @@ -197,8 +197,8 @@ end --- --- loclist: (boolean) use the location list (default is to use the quickfix list) --- ---- @param map_result function `((resp, bufnr) -> list)` to convert the response ---- @param entity name of the resource used in a `not found` error message +---@param map_result function `((resp, bufnr) -> list)` to convert the response +---@param entity name of the resource used in a `not found` error message local function response_to_list(map_result, entity) return function(_, _, result, _, bufnr, config) if not result or vim.tbl_isempty(result) then @@ -289,11 +289,11 @@ end --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_hover M['textDocument/hover'] = M.hover ---@private +---@private --- Jumps to a location. Used as a handler for multiple LSP methods. ---@param _ (not used) ---@param method (string) LSP method name ---@param result (table) result of LSP method; a location or a list of locations. +---@param _ (not used) +---@param method (string) LSP method name +---@param result (table) result of LSP method; a location or a list of locations. ---(`textDocument/definition` can return `Location` or `Location[]` local function location_handler(_, method, result) if result == nil or vim.tbl_isempty(result) then @@ -377,13 +377,13 @@ M['textDocument/documentHighlight'] = function(_, _, result, _, bufnr, _) util.buf_highlight_references(bufnr, result) end ---@private +---@private --- --- Displays call hierarchy in the quickfix window. --- ---@param direction `"from"` for incoming calls and `"to"` for outgoing calls ---@returns `CallHierarchyIncomingCall[]` if {direction} is `"from"`, ---@returns `CallHierarchyOutgoingCall[]` if {direction} is `"to"`, +---@param direction `"from"` for incoming calls and `"to"` for outgoing calls +---@returns `CallHierarchyIncomingCall[]` if {direction} is `"from"`, +---@returns `CallHierarchyOutgoingCall[]` if {direction} is `"to"`, local make_call_hierarchy_handler = function(direction) return function(_, _, result) if not result then return end diff --git a/runtime/lua/vim/lsp/log.lua b/runtime/lua/vim/lsp/log.lua index 73fafb9715..a2d5c3a774 100644 --- a/runtime/lua/vim/lsp/log.lua +++ b/runtime/lua/vim/lsp/log.lua @@ -18,14 +18,14 @@ local log_date_format = "%FT%H:%M:%S%z" do local path_sep = vim.loop.os_uname().version:match("Windows") and "\\" or "/" - --@private + ---@private local function path_join(...) return table.concat(vim.tbl_flatten{...}, path_sep) end local logfilename = path_join(vim.fn.stdpath('cache'), 'lsp.log') --- Returns the log filename. - --@returns (string) log filename + ---@returns (string) log filename function log.get_filename() return logfilename end @@ -77,7 +77,7 @@ end vim.tbl_add_reverse_lookup(log.levels) --- Sets the current log level. ---@param level (string or number) One of `vim.lsp.log.levels` +---@param level (string or number) One of `vim.lsp.log.levels` function log.set_level(level) if type(level) == 'string' then current_log_level = assert(log.levels[level:upper()], string.format("Invalid log level: %q", level)) @@ -89,8 +89,8 @@ function log.set_level(level) end --- Checks whether the level is sufficient for logging. ---@param level number log level ---@returns (bool) true if would log, false if not +---@param level number log level +---@returns (bool) true if would log, false if not function log.should_log(level) return level >= current_log_level end diff --git a/runtime/lua/vim/lsp/protocol.lua b/runtime/lua/vim/lsp/protocol.lua index 6d02b9ba74..ded8af5773 100644 --- a/runtime/lua/vim/lsp/protocol.lua +++ b/runtime/lua/vim/lsp/protocol.lua @@ -5,14 +5,14 @@ local if_nil = vim.F.if_nil local protocol = {} --[=[ ---@private +---@private --- Useful for interfacing with: --- https://github.com/microsoft/language-server-protocol/raw/gh-pages/_specifications/specification-3-14.md function transform_schema_comments() nvim.command [[silent! '<,'>g/\/\*\*\|\*\/\|^$/d]] nvim.command [[silent! '<,'>s/^\(\s*\) \* \=\(.*\)/\1--\2/]] end ---@private +---@private function transform_schema_to_table() transform_schema_comments() nvim.command [[silent! '<,'>s/: \S\+//]] diff --git a/runtime/lua/vim/lsp/rpc.lua b/runtime/lua/vim/lsp/rpc.lua index 4c5f02af9d..eedb708118 100644 --- a/runtime/lua/vim/lsp/rpc.lua +++ b/runtime/lua/vim/lsp/rpc.lua @@ -5,11 +5,11 @@ local protocol = require('vim.lsp.protocol') local validate, schedule, schedule_wrap = vim.validate, vim.schedule, vim.schedule_wrap -- TODO replace with a better implementation. ---@private +---@private --- Encodes to JSON. --- ---@param data (table) Data to encode ---@returns (string) Encoded object +---@param data (table) Data to encode +---@returns (string) Encoded object local function json_encode(data) local status, result = pcall(vim.fn.json_encode, data) if status then @@ -18,11 +18,11 @@ local function json_encode(data) return nil, result end end ---@private +---@private --- Decodes from JSON. --- ---@param data (string) Data to decode ---@returns (table) Decoded JSON object +---@param data (string) Data to decode +---@returns (table) Decoded JSON object local function json_decode(data) local status, result = pcall(vim.fn.json_decode, data) if status then @@ -32,10 +32,10 @@ local function json_decode(data) end end ---@private +---@private --- Checks whether a given path exists and is a directory. ---@param filename (string) path to check ---@returns (bool) +---@param filename (string) path to check +---@returns (bool) local function is_dir(filename) local stat = vim.loop.fs_stat(filename) return stat and stat.type == 'directory' or false @@ -43,7 +43,7 @@ end local NIL = vim.NIL ---@private +---@private local recursive_convert_NIL recursive_convert_NIL = function(v, tbl_processed) if v == NIL then @@ -63,15 +63,15 @@ recursive_convert_NIL = function(v, tbl_processed) return v end ---@private +---@private --- Returns its argument, but converts `vim.NIL` to Lua `nil`. ---@param v (any) Argument ---@returns (any) +---@param v (any) Argument +---@returns (any) local function convert_NIL(v) return recursive_convert_NIL(v, {}) end ---@private +---@private --- Merges current process env with the given env and returns the result as --- a list of "k=v" strings. --- @@ -81,8 +81,8 @@ end --- in: { PRODUCTION="false", PATH="/usr/bin/", PORT=123, HOST="0.0.0.0", } --- out: { "PRODUCTION=false", "PATH=/usr/bin/", "PORT=123", "HOST=0.0.0.0", } --- ---@param env (table) table of environment variable assignments ---@returns (table) list of `"k=v"` strings +---@param env (table) table of environment variable assignments +---@returns (table) list of `"k=v"` strings local function env_merge(env) if env == nil then return env @@ -97,11 +97,11 @@ local function env_merge(env) return final_env end ---@private +---@private --- Embeds the given string into a table and correctly computes `Content-Length`. --- ---@param encoded_message (string) ---@returns (table) table containing encoded message and `Content-Length` attribute +---@param encoded_message (string) +---@returns (table) table containing encoded message and `Content-Length` attribute local function format_message_with_content_length(encoded_message) return table.concat { 'Content-Length: '; tostring(#encoded_message); '\r\n\r\n'; @@ -109,11 +109,11 @@ local function format_message_with_content_length(encoded_message) } end ---@private +---@private --- Parses an LSP Message's header --- ---@param header: The header to parse. ---@returns Parsed headers +---@param header: The header to parse. +---@returns Parsed headers local function parse_headers(header) if type(header) ~= 'string' then return nil @@ -141,7 +141,7 @@ end -- case insensitive pattern. local header_start_pattern = ("content"):gsub("%w", function(c) return "["..c..c:upper().."]" end) ---@private +---@private --- The actual workhorse. local function request_parser_loop() local buffer = '' -- only for header part @@ -203,8 +203,8 @@ local client_errors = vim.tbl_add_reverse_lookup { --- Constructs an error message from an LSP error object. --- ---@param err (table) The error object ---@returns (string) The formatted error message +---@param err (table) The error object +---@returns (string) The formatted error message local function format_rpc_error(err) validate { err = { err, 't' }; @@ -233,9 +233,9 @@ end --- Creates an RPC response object/table. --- ---@param code RPC error code defined in `vim.lsp.protocol.ErrorCodes` ---@param message (optional) arbitrary message to send to server ---@param data (optional) arbitrary data to send to server +---@param code RPC error code defined in `vim.lsp.protocol.ErrorCodes` +---@param message (optional) arbitrary message to send to server +---@param data (optional) arbitrary data to send to server local function rpc_response_error(code, message, data) -- TODO should this error or just pick a sane error (like InternalError)? local code_name = assert(protocol.ErrorCodes[code], 'Invalid RPC error code') @@ -250,38 +250,38 @@ end local default_dispatchers = {} ---@private +---@private --- Default dispatcher for notifications sent to an LSP server. --- ---@param method (string) The invoked LSP method ---@param params (table): Parameters for the invoked LSP method +---@param method (string) The invoked LSP method +---@param params (table): Parameters for the invoked LSP method function default_dispatchers.notification(method, params) local _ = log.debug() and log.debug('notification', method, params) end ---@private +---@private --- Default dispatcher for requests sent to an LSP server. --- ---@param method (string) The invoked LSP method ---@param params (table): Parameters for the invoked LSP method ---@returns `nil` and `vim.lsp.protocol.ErrorCodes.MethodNotFound`. +---@param method (string) The invoked LSP method +---@param params (table): Parameters for the invoked LSP method +---@returns `nil` and `vim.lsp.protocol.ErrorCodes.MethodNotFound`. function default_dispatchers.server_request(method, params) local _ = log.debug() and log.debug('server_request', method, params) return nil, rpc_response_error(protocol.ErrorCodes.MethodNotFound) end ---@private +---@private --- Default dispatcher for when a client exits. --- ---@param code (number): Exit code ---@param signal (number): Number describing the signal used to terminate (if +---@param code (number): Exit code +---@param signal (number): Number describing the signal used to terminate (if ---any) function default_dispatchers.on_exit(code, signal) local _ = log.info() and log.info("client_exit", { code = code, signal = signal }) end ---@private +---@private --- Default dispatcher for client errors. --- ---@param code (number):