aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/lua')
-rw-r--r--runtime/lua/vim/fs.lua4
-rw-r--r--runtime/lua/vim/iter.lua5
-rw-r--r--runtime/lua/vim/lsp.lua72
-rw-r--r--runtime/lua/vim/lsp/_inlay_hint.lua162
-rw-r--r--runtime/lua/vim/lsp/buf.lua33
-rw-r--r--runtime/lua/vim/lsp/codelens.lua24
-rw-r--r--runtime/lua/vim/lsp/handlers.lua3
-rw-r--r--runtime/lua/vim/lsp/types.lua2
-rw-r--r--runtime/lua/vim/treesitter/highlighter.lua4
9 files changed, 192 insertions, 117 deletions
diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua
index c89147dbd2..cdb314fa3d 100644
--- a/runtime/lua/vim/fs.lua
+++ b/runtime/lua/vim/fs.lua
@@ -348,9 +348,7 @@ function M.normalize(path, opts)
path = path:gsub('%$([%w_]+)', vim.uv.os_getenv)
end
- path = path:gsub('\\', '/'):gsub('/+', '/')
-
- return path:sub(-1) == '/' and path:sub(1, -2) or path
+ return (path:gsub('\\', '/'):gsub('/+', '/'):gsub('(.)/$', '%1'))
end
return M
diff --git a/runtime/lua/vim/iter.lua b/runtime/lua/vim/iter.lua
index 204d22b9be..9c7bd13164 100644
--- a/runtime/lua/vim/iter.lua
+++ b/runtime/lua/vim/iter.lua
@@ -1,7 +1,8 @@
---@defgroup lua-iter
---
---- The \*vim.iter\* module provides a generic "iterator" interface over tables
---- and iterator functions.
+--- @brief The \*vim.iter\* module provides a generic interface for working with
+--- iterables: tables, lists, iterator functions, pair()/ipair()-like iterators,
+--- and \`vim.iter()\` objects.
---
--- \*vim.iter()\* wraps its table or function argument into an \*Iter\* object
--- with methods (such as |Iter:filter()| and |Iter:map()|) that transform the
diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua
index 9118e7e2e1..25e69a8006 100644
--- a/runtime/lua/vim/lsp.lua
+++ b/runtime/lua/vim/lsp.lua
@@ -17,7 +17,6 @@ local if_nil = vim.F.if_nil
local lsp = {
protocol = protocol,
- _inlay_hint = require('vim.lsp._inlay_hint'),
handlers = default_handlers,
@@ -1491,7 +1490,7 @@ function lsp.start_client(config)
---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_all()|
function client.request(method, params, handler, bufnr)
if not handler then
handler = assert(
@@ -1655,6 +1654,46 @@ function lsp.start_client(config)
end
---@private
+ --- Execute a lsp command, either via client command function (if available)
+ --- or via workspace/executeCommand (if supported by the server)
+ ---
+ ---@param command lsp.Command
+ ---@param context? {bufnr: integer}
+ ---@param handler? lsp-handler only called if a server command
+ function client._exec_cmd(command, context, handler)
+ context = vim.deepcopy(context or {})
+ context.bufnr = context.bufnr or api.nvim_get_current_buf()
+ context.client_id = client.id
+ local cmdname = command.command
+ local fn = client.commands[cmdname] or lsp.commands[cmdname]
+ if fn then
+ fn(command, context)
+ return
+ end
+
+ local command_provider = client.server_capabilities.executeCommandProvider
+ local commands = type(command_provider) == 'table' and command_provider.commands or {}
+ if not vim.list_contains(commands, cmdname) then
+ vim.notify_once(
+ string.format(
+ 'Language server `%s` does not support command `%s`. This command may require a client extension.',
+ client.name,
+ cmdname
+ ),
+ vim.log.levels.WARN
+ )
+ return
+ end
+ -- Not using command directly to exclude extra properties,
+ -- see https://github.com/python-lsp/python-lsp-server/issues/146
+ local params = {
+ command = command.command,
+ arguments = command.arguments,
+ }
+ client.request('workspace/executeCommand', params, handler, context.bufnr)
+ end
+
+ ---@private
--- Runs the on_attach function from the client's config if it was defined.
---@param bufnr integer Buffer number
function client._on_attach(bufnr)
@@ -2119,22 +2158,19 @@ function lsp.buf_request(bufnr, method, params, handler)
return client_request_ids, _cancel_all_requests
end
----Sends an async request for all active clients attached to the buffer.
----Executes the callback on the combined result.
----Parameters are the same as |vim.lsp.buf_request()| but the return result and callback are
----different.
+--- Sends an async request for all active clients attached to the buffer and executes the `handler`
+--- callback with the combined result.
---
---@param bufnr (integer) Buffer handle, or 0 for current.
---@param method (string) LSP method name
---@param params (table|nil) Parameters to send to the server
----@param callback fun(request_results: table<integer, {error: lsp.ResponseError, result: any}>) (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.
----
----@return fun() cancel A function that will cancel all requests
-function lsp.buf_request_all(bufnr, method, params, callback)
- local request_results = {}
+---@param handler fun(results: table<integer, {error: lsp.ResponseError, result: any}>) (function)
+--- Handler called after all requests are completed. Server results are passed as
+--- a `client_id:result` map.
+---
+---@return fun() cancel Function that cancels all requests.
+function lsp.buf_request_all(bufnr, method, params, handler)
+ local results = {}
local result_count = 0
local expected_result_count = 0
@@ -2147,12 +2183,12 @@ function lsp.buf_request_all(bufnr, method, params, callback)
end)
local function _sync_handler(err, result, ctx)
- request_results[ctx.client_id] = { error = err, result = result }
+ results[ctx.client_id] = { error = err, result = result }
result_count = result_count + 1
set_expected_result_count()
if result_count >= expected_result_count then
- callback(request_results)
+ handler(results)
end
end
@@ -2164,8 +2200,8 @@ end
--- Sends a request to all server and waits for the response of all of them.
---
--- Calls |vim.lsp.buf_request_all()| but blocks Nvim while awaiting the result.
---- Parameters are the same as |vim.lsp.buf_request()| but the return result is
---- different. Wait maximum of {timeout_ms} (default 1000) ms.
+--- Parameters are the same as |vim.lsp.buf_request_all()| but the result is
+--- different. Waits a maximum of {timeout_ms} (default 1000) ms.
---
---@param bufnr (integer) Buffer handle, or 0 for current.
---@param method (string) LSP method name
diff --git a/runtime/lua/vim/lsp/_inlay_hint.lua b/runtime/lua/vim/lsp/_inlay_hint.lua
index aa6ec9aca8..e7cc8ba7ae 100644
--- a/runtime/lua/vim/lsp/_inlay_hint.lua
+++ b/runtime/lua/vim/lsp/_inlay_hint.lua
@@ -6,22 +6,30 @@ local M = {}
---@class lsp._inlay_hint.bufstate
---@field version integer
---@field client_hint table<integer, table<integer, lsp.InlayHint[]>> client_id -> (lnum -> hints)
+---@field enabled boolean Whether inlay hints are enabled for the buffer
+---@field timer uv.uv_timer_t? Debounce timer associated with the buffer
---@type table<integer, lsp._inlay_hint.bufstate>
-local hint_cache_by_buf = setmetatable({}, {
- __index = function(t, b)
- local key = b > 0 and b or api.nvim_get_current_buf()
- return rawget(t, key)
- end,
-})
+local bufstates = {}
local namespace = api.nvim_create_namespace('vim_lsp_inlayhint')
+local augroup = api.nvim_create_augroup('vim_lsp_inlayhint', {})
-M.__explicit_buffers = {}
+--- Reset the request debounce timer of a buffer
+---@private
+local function reset_timer(reset_bufnr)
+ local timer = bufstates[reset_bufnr].timer
+ if timer then
+ bufstates[reset_bufnr].timer = nil
+ if not timer:is_closing() then
+ timer:stop()
+ timer:close()
+ end
+ end
+end
--- |lsp-handler| for the method `textDocument/inlayHint`
--- Store hints for a specific buffer and client
---- Resolves unresolved hints
---@private
function M.on_inlayhint(err, result, ctx, _)
if err then
@@ -36,23 +44,10 @@ function M.on_inlayhint(err, result, ctx, _)
if not result then
return
end
- local bufstate = hint_cache_by_buf[bufnr]
- if not bufstate then
- bufstate = {
- client_hint = vim.defaulttable(),
- version = ctx.version,
- }
- hint_cache_by_buf[bufnr] = bufstate
- api.nvim_buf_attach(bufnr, false, {
- on_detach = function(_, b)
- api.nvim_buf_clear_namespace(b, namespace, 0, -1)
- hint_cache_by_buf[b] = nil
- end,
- on_reload = function(_, b)
- api.nvim_buf_clear_namespace(b, namespace, 0, -1)
- hint_cache_by_buf[b] = nil
- end,
- })
+ local bufstate = bufstates[bufnr]
+ if not (bufstate.client_hint and bufstate.version) then
+ bufstate.client_hint = vim.defaulttable()
+ bufstate.version = ctx.version
end
local hints_by_client = bufstate.client_hint
local client = vim.lsp.get_client_by_id(client_id)
@@ -95,28 +90,24 @@ end
---@private
local function resolve_bufnr(bufnr)
- return bufnr == 0 and api.nvim_get_current_buf() or bufnr
+ return bufnr > 0 and bufnr or api.nvim_get_current_buf()
end
--- Refresh inlay hints for a buffer
---
---- It is recommended to trigger this using an autocmd or via keymap.
---@param opts (nil|table) Optional arguments
--- - bufnr (integer, default: 0): Buffer whose hints to refresh
--- - only_visible (boolean, default: false): Whether to only refresh hints for the visible regions of the buffer
---
---- Example:
---- <pre>vim
---- autocmd BufEnter,InsertLeave,BufWritePost <buffer> lua vim.lsp._inlay_hint.refresh()
---- </pre>
----
---@private
function M.refresh(opts)
opts = opts or {}
- local bufnr = opts.bufnr or 0
+ local bufnr = resolve_bufnr(opts.bufnr or 0)
+ local bufstate = bufstates[bufnr]
+ if not (bufstate and bufstate.enabled) then
+ return
+ end
local only_visible = opts.only_visible or false
- bufnr = resolve_bufnr(bufnr)
- M.__explicit_buffers[bufnr] = true
local buffer_windows = {}
for _, winid in ipairs(api.nvim_list_wins()) do
if api.nvim_win_get_buf(winid) == bufnr then
@@ -148,30 +139,97 @@ function M.refresh(opts)
end
--- Clear inlay hints
----
----@param client_id integer|nil filter by client_id. All clients if nil
----@param bufnr integer|nil filter by buffer. All buffers if nil
+---@param bufnr (integer) Buffer handle, or 0 for current
---@private
-function M.clear(client_id, bufnr)
- local buffers = bufnr and { resolve_bufnr(bufnr) } or vim.tbl_keys(hint_cache_by_buf)
- for _, iter_bufnr in ipairs(buffers) do
- M.__explicit_buffers[iter_bufnr] = false
- local bufstate = hint_cache_by_buf[iter_bufnr]
- local client_lens = (bufstate or {}).client_hint or {}
- local client_ids = client_id and { client_id } or vim.tbl_keys(client_lens)
- for _, iter_client_id in ipairs(client_ids) do
- if bufstate then
- bufstate.client_hint[iter_client_id] = {}
- end
+local function clear(bufnr)
+ bufnr = resolve_bufnr(bufnr)
+ reset_timer(bufnr)
+ local bufstate = bufstates[bufnr]
+ local client_lens = (bufstate or {}).client_hint or {}
+ local client_ids = vim.tbl_keys(client_lens)
+ for _, iter_client_id in ipairs(client_ids) do
+ if bufstate then
+ bufstate.client_hint[iter_client_id] = {}
end
- api.nvim_buf_clear_namespace(iter_bufnr, namespace, 0, -1)
end
- vim.cmd('redraw!')
+ api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1)
+ api.nvim__buf_redraw_range(bufnr, 0, -1)
+end
+
+---@private
+local function make_request(request_bufnr)
+ reset_timer(request_bufnr)
+ M.refresh({ bufnr = request_bufnr })
+end
+
+--- Enable inlay hints for a buffer
+---@param bufnr (integer) Buffer handle, or 0 for current
+---@private
+function M.enable(bufnr)
+ bufnr = resolve_bufnr(bufnr)
+ local bufstate = bufstates[bufnr]
+ if not (bufstate and bufstate.enabled) then
+ bufstates[bufnr] = { enabled = true, timer = nil }
+ M.refresh({ bufnr = bufnr })
+ api.nvim_buf_attach(bufnr, true, {
+ on_lines = function(_, cb_bufnr)
+ if not bufstates[cb_bufnr].enabled then
+ return true
+ end
+ reset_timer(cb_bufnr)
+ bufstates[cb_bufnr].timer = vim.defer_fn(function()
+ make_request(cb_bufnr)
+ end, 200)
+ end,
+ on_reload = function(_, cb_bufnr)
+ clear(cb_bufnr)
+ bufstates[cb_bufnr] = nil
+ M.refresh({ bufnr = cb_bufnr })
+ end,
+ on_detach = function(_, cb_bufnr)
+ clear(cb_bufnr)
+ bufstates[cb_bufnr] = nil
+ end,
+ })
+ api.nvim_create_autocmd('LspDetach', {
+ buffer = bufnr,
+ callback = function(opts)
+ clear(opts.buf)
+ end,
+ once = true,
+ group = augroup,
+ })
+ end
+end
+
+--- Disable inlay hints for a buffer
+---@param bufnr (integer) Buffer handle, or 0 for current
+---@private
+function M.disable(bufnr)
+ bufnr = resolve_bufnr(bufnr)
+ if bufstates[bufnr] and bufstates[bufnr].enabled then
+ clear(bufnr)
+ bufstates[bufnr].enabled = nil
+ bufstates[bufnr].timer = nil
+ end
+end
+
+--- Toggle inlay hints for a buffer
+---@param bufnr (integer) Buffer handle, or 0 for current
+---@private
+function M.toggle(bufnr)
+ bufnr = resolve_bufnr(bufnr)
+ local bufstate = bufstates[bufnr]
+ if bufstate and bufstate.enabled then
+ M.disable(bufnr)
+ else
+ M.enable(bufnr)
+ end
end
api.nvim_set_decoration_provider(namespace, {
on_win = function(_, _, bufnr, topline, botline)
- local bufstate = hint_cache_by_buf[bufnr]
+ local bufstate = bufstates[bufnr]
if not bufstate then
return
end
diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua
index e0034cf86e..45056cf272 100644
--- a/runtime/lua/vim/lsp/buf.lua
+++ b/runtime/lua/vim/lsp/buf.lua
@@ -646,21 +646,7 @@ local function on_code_action_results(results, ctx, options)
end
if action.command then
local command = type(action.command) == 'table' and action.command or action
- local fn = client.commands[command.command] or vim.lsp.commands[command.command]
- if fn then
- local enriched_ctx = vim.deepcopy(ctx)
- enriched_ctx.client_id = client.id
- fn(command, enriched_ctx)
- else
- -- Not using command directly to exclude extra properties,
- -- see https://github.com/python-lsp/python-lsp-server/issues/146
- local params = {
- command = command.command,
- arguments = command.arguments,
- workDoneToken = command.workDoneToken,
- }
- client.request('workspace/executeCommand', params, nil, ctx.bufnr)
- end
+ client._exec_cmd(command, ctx)
end
end
@@ -697,7 +683,7 @@ local function on_code_action_results(results, ctx, options)
return
end
apply_action(resolved_action, client)
- end)
+ end, ctx.bufnr)
else
apply_action(action, client)
end
@@ -810,4 +796,19 @@ function M.execute_command(command_params)
request('workspace/executeCommand', command_params)
end
+--- Enable/disable/toggle inlay hints for a buffer
+---@param bufnr (integer) Buffer handle, or 0 for current
+---@param enable (boolean|nil) true/false to enable/disable, nil to toggle
+function M.inlay_hint(bufnr, enable)
+ vim.validate({ enable = { enable, { 'boolean', 'nil' } }, bufnr = { bufnr, 'number' } })
+ local inlay_hint = require('vim.lsp._inlay_hint')
+ if enable then
+ inlay_hint.enable(bufnr)
+ elseif enable == false then
+ inlay_hint.disable(bufnr)
+ else
+ inlay_hint.toggle(bufnr)
+ end
+end
+
return M
diff --git a/runtime/lua/vim/lsp/codelens.lua b/runtime/lua/vim/lsp/codelens.lua
index e26bdcc6d4..5acfe90d5e 100644
--- a/runtime/lua/vim/lsp/codelens.lua
+++ b/runtime/lua/vim/lsp/codelens.lua
@@ -33,30 +33,12 @@ local function execute_lens(lens, bufnr, client_id)
local client = vim.lsp.get_client_by_id(client_id)
assert(client, 'Client is required to execute lens, client_id=' .. client_id)
- local command = lens.command
- local fn = client.commands[command.command] or vim.lsp.commands[command.command]
- if fn then
- fn(command, { bufnr = bufnr, client_id = client_id })
- return
- end
- -- Need to use the client that returned the lens → must not use buf_request
- local command_provider = client.server_capabilities.executeCommandProvider
- local commands = type(command_provider) == 'table' and command_provider.commands or {}
- if not vim.list_contains(commands, command.command) then
- vim.notify(
- string.format(
- 'Language server does not support command `%s`. This command may require a client extension.',
- command.command
- ),
- vim.log.levels.WARN
- )
- return
- end
- client.request('workspace/executeCommand', command, function(...)
+
+ client._exec_cmd(lens.command, { bufnr = bufnr }, function(...)
local result = vim.lsp.handlers['workspace/executeCommand'](...)
M.refresh()
return result
- end, bufnr)
+ end)
end
--- Return all lenses for the given buffer
diff --git a/runtime/lua/vim/lsp/handlers.lua b/runtime/lua/vim/lsp/handlers.lua
index 44a9a58aca..284e3ef2d0 100644
--- a/runtime/lua/vim/lsp/handlers.lua
+++ b/runtime/lua/vim/lsp/handlers.lua
@@ -619,9 +619,6 @@ end
---@see https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_inlayHint_refresh
M['workspace/inlayHint/refresh'] = function(err, _, ctx)
local inlay_hint = require('vim.lsp._inlay_hint')
- if not inlay_hint.__explicit_buffers[ctx.bufnr] then
- return vim.NIL
- end
if err then
return vim.NIL
end
diff --git a/runtime/lua/vim/lsp/types.lua b/runtime/lua/vim/lsp/types.lua
index 108aeeb922..cdfbcfb11b 100644
--- a/runtime/lua/vim/lsp/types.lua
+++ b/runtime/lua/vim/lsp/types.lua
@@ -1,6 +1,6 @@
---@meta
----@alias lsp-handler fun(err: lsp.ResponseError|nil, result: any, context: lsp.HandlerContext, config: table|nil)
+---@alias lsp-handler fun(err: lsp.ResponseError|nil, result: any, context: lsp.HandlerContext, config: table|nil): any?
---@class lsp.HandlerContext
---@field method string
diff --git a/runtime/lua/vim/treesitter/highlighter.lua b/runtime/lua/vim/treesitter/highlighter.lua
index 4bb764c5c6..d4db6bc404 100644
--- a/runtime/lua/vim/treesitter/highlighter.lua
+++ b/runtime/lua/vim/treesitter/highlighter.lua
@@ -260,12 +260,14 @@ local function on_line_impl(self, buf, line, is_spell_nav)
local spell_pri_offset = capture_name == 'nospell' and 1 or 0
if hl and end_row >= line and (not is_spell_nav or spell ~= nil) then
+ local priority = (tonumber(metadata.priority) or vim.highlight.priorities.treesitter)
+ + spell_pri_offset
api.nvim_buf_set_extmark(buf, ns, start_row, start_col, {
end_line = end_row,
end_col = end_col,
hl_group = hl,
ephemeral = true,
- priority = (tonumber(metadata.priority) or 100) + spell_pri_offset, -- Low but leaves room below
+ priority = priority,
conceal = metadata.conceal,
spell = spell,
})