fix(lsp): semantic token functions allow "0" bufnr #28849

aligns with ":help dev-patterns"
This commit is contained in:
Riley Bruins 2024-05-21 09:25:54 -07:00 committed by GitHub
parent d9a2acdab3
commit a108852b00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 83 additions and 14 deletions

View File

@ -1686,9 +1686,10 @@ highlight_token({token}, {bufnr}, {client_id}, {hl_group}, {opts})
use inside |LspTokenUpdate| callbacks.
Parameters: ~
• {token} (`table`) a semantic token, found as `args.data.token` in
|LspTokenUpdate|.
• {bufnr} (`integer`) the buffer to highlight
• {token} (`table`) A semantic token, found as `args.data.token` in
|LspTokenUpdate|
• {bufnr} (`integer`) The buffer to highlight, or `0` for current
buffer
• {client_id} (`integer`) The ID of the |vim.lsp.Client|
• {hl_group} (`string`) Highlight group name
• {opts} (`table?`) Optional parameters:
@ -1709,8 +1710,8 @@ start({bufnr}, {client_id}, {opts}) *vim.lsp.semantic_tokens.start()*
<
Parameters: ~
• {bufnr} (`integer`)
• {client_id} (`integer`)
• {bufnr} (`integer`) Buffer number, or `0` for current buffer
• {client_id} (`integer`) The ID of the |vim.lsp.Client|
• {opts} (`table?`) Optional keyword arguments
• debounce (integer, default: 200): Debounce token
requests to the server by the given number in
@ -1726,8 +1727,8 @@ stop({bufnr}, {client_id}) *vim.lsp.semantic_tokens.stop()*
from the buffer.
Parameters: ~
• {bufnr} (`integer`)
• {client_id} (`integer`)
• {bufnr} (`integer`) Buffer number, or `0` for current buffer
• {client_id} (`integer`) The ID of the |vim.lsp.Client|
==============================================================================

View File

@ -570,9 +570,9 @@ local M = {}
--- client.server_capabilities.semanticTokensProvider = nil
--- ```
---
---@param bufnr integer
---@param client_id integer
---@param opts? table Optional keyword arguments
---@param bufnr (integer) Buffer number, or `0` for current buffer
---@param client_id (integer) The ID of the |vim.lsp.Client|
---@param opts? (table) Optional keyword arguments
--- - debounce (integer, default: 200): Debounce token requests
--- to the server by the given number in milliseconds
function M.start(bufnr, client_id, opts)
@ -581,6 +581,10 @@ function M.start(bufnr, client_id, opts)
client_id = { client_id, 'n', false },
})
if bufnr == 0 then
bufnr = api.nvim_get_current_buf()
end
opts = opts or {}
assert(
(not opts.debounce or type(opts.debounce) == 'number'),
@ -626,14 +630,18 @@ end
--- of `start()`, so you should only need this function to manually disengage the semantic
--- token engine without fully detaching the LSP client from the buffer.
---
---@param bufnr integer
---@param client_id integer
---@param bufnr (integer) Buffer number, or `0` for current buffer
---@param client_id (integer) The ID of the |vim.lsp.Client|
function M.stop(bufnr, client_id)
vim.validate({
bufnr = { bufnr, 'n', false },
client_id = { client_id, 'n', false },
})
if bufnr == 0 then
bufnr = api.nvim_get_current_buf()
end
local highlighter = STHighlighter.active[bufnr]
if not highlighter then
return
@ -741,12 +749,15 @@ end
--- mark will be deleted by the semantic token engine when appropriate; for
--- example, when the LSP sends updated tokens. This function is intended for
--- use inside |LspTokenUpdate| callbacks.
---@param token (table) a semantic token, found as `args.data.token` in |LspTokenUpdate|.
---@param bufnr (integer) the buffer to highlight
---@param token (table) A semantic token, found as `args.data.token` in |LspTokenUpdate|
---@param bufnr (integer) The buffer to highlight, or `0` for current buffer
---@param client_id (integer) The ID of the |vim.lsp.Client|
---@param hl_group (string) Highlight group name
---@param opts? vim.lsp.semantic_tokens.highlight_token.Opts Optional parameters:
function M.highlight_token(token, bufnr, client_id, hl_group, opts)
if bufnr == 0 then
bufnr = api.nvim_get_current_buf()
end
local highlighter = STHighlighter.active[bufnr]
if not highlighter then
return

View File

@ -273,6 +273,63 @@ describe('semantic token highlighting', function()
end
)
it('highlights start and stop when using "0" for current buffer', function()
exec_lua([[
bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_win_set_buf(0, bufnr)
client_id = vim.lsp.start({ name = 'dummy', cmd = server.cmd })
]])
insert(text)
exec_lua([[
vim.notify = function() end
vim.lsp.semantic_tokens.stop(0, client_id)
]])
screen:expect {
grid = [[
#include <iostream> |
|
int main() |
{ |
int x; |
#ifdef __cplusplus |
std::cout << x << "\n"; |
#else |
printf("%d\n", x); |
#endif |
} |
^} |
{1:~ }|*3
|
]],
}
exec_lua([[
vim.lsp.semantic_tokens.start(0, client_id)
]])
screen:expect {
grid = [[
#include <iostream> |
|
int {8:main}() |
{ |
int {7:x}; |
#ifdef {5:__cplusplus} |
{4:std}::{2:cout} << {2:x} << "\n"; |
{6:#else} |
{6: printf("%d\n", x);} |
{6:#endif} |
} |
^} |
{1:~ }|*3
|
]],
}
end)
it('buffer is re-highlighted when force refreshed', function()
exec_lua([[
bufnr = vim.api.nvim_get_current_buf()