2022-11-23 09:06:36 -07:00
|
|
|
local api = vim.api
|
2023-04-30 22:15:32 -07:00
|
|
|
local bit = require('bit')
|
2022-11-23 09:06:36 -07:00
|
|
|
local handlers = require('vim.lsp.handlers')
|
2023-08-03 04:03:48 -07:00
|
|
|
local ms = require('vim.lsp.protocol').Methods
|
2022-11-23 09:06:36 -07:00
|
|
|
local util = require('vim.lsp.util')
|
2023-06-03 03:06:00 -07:00
|
|
|
local uv = vim.uv
|
2022-11-23 09:06:36 -07:00
|
|
|
|
|
|
|
--- @class STTokenRange
|
2023-03-06 23:17:52 -07:00
|
|
|
--- @field line integer line number 0-based
|
|
|
|
--- @field start_col integer start column 0-based
|
|
|
|
--- @field end_col integer end column 0-based
|
2022-11-23 09:06:36 -07:00
|
|
|
--- @field type string token type as string
|
2023-12-13 05:00:11 -07:00
|
|
|
--- @field modifiers table<string,boolean> token modifiers as a set. E.g., { static = true, readonly = true }
|
2023-03-06 11:03:13 -07:00
|
|
|
--- @field marked boolean whether this token has had extmarks applied
|
2022-11-23 09:06:36 -07:00
|
|
|
---
|
|
|
|
--- @class STCurrentResult
|
2023-08-09 02:06:13 -07:00
|
|
|
--- @field version? integer document version associated with this result
|
|
|
|
--- @field result_id? string resultId from the server; used with delta requests
|
|
|
|
--- @field highlights? STTokenRange[] cache of highlight ranges for this document version
|
|
|
|
--- @field tokens? integer[] raw token array as received by the server. used for calculating delta responses
|
|
|
|
--- @field namespace_cleared? boolean whether the namespace was cleared for this result yet
|
2022-11-23 09:06:36 -07:00
|
|
|
---
|
|
|
|
--- @class STActiveRequest
|
2023-12-13 05:00:11 -07:00
|
|
|
--- @field request_id? integer the LSP request ID of the most recent request sent to the server
|
|
|
|
--- @field version? integer the document version associated with the most recent request
|
2022-11-23 09:06:36 -07:00
|
|
|
---
|
|
|
|
--- @class STClientState
|
2023-03-06 23:17:52 -07:00
|
|
|
--- @field namespace integer
|
2022-11-23 09:06:36 -07:00
|
|
|
--- @field active_request STActiveRequest
|
|
|
|
--- @field current_result STCurrentResult
|
|
|
|
|
|
|
|
---@class STHighlighter
|
2023-03-06 23:17:52 -07:00
|
|
|
---@field active table<integer, STHighlighter>
|
|
|
|
---@field bufnr integer
|
|
|
|
---@field augroup integer augroup for buffer events
|
|
|
|
---@field debounce integer milliseconds to debounce requests for new tokens
|
2022-11-23 09:06:36 -07:00
|
|
|
---@field timer table uv_timer for debouncing requests for new tokens
|
2023-03-06 23:17:52 -07:00
|
|
|
---@field client_state table<integer, STClientState>
|
2022-11-23 09:06:36 -07:00
|
|
|
local STHighlighter = { active = {} }
|
|
|
|
|
2023-03-06 11:03:13 -07:00
|
|
|
--- Do a binary search of the tokens in the half-open range [lo, hi).
|
|
|
|
---
|
|
|
|
--- Return the index i in range such that tokens[j].line < line for all j < i, and
|
|
|
|
--- tokens[j].line >= line for all j >= i, or return hi if no such index is found.
|
|
|
|
local function lower_bound(tokens, line, lo, hi)
|
2022-11-23 09:06:36 -07:00
|
|
|
while lo < hi do
|
2023-03-10 06:10:38 -07:00
|
|
|
local mid = bit.rshift(lo + hi, 1) -- Equivalent to floor((lo + hi) / 2).
|
2022-11-23 09:06:36 -07:00
|
|
|
if tokens[mid].line < line then
|
|
|
|
lo = mid + 1
|
|
|
|
else
|
|
|
|
hi = mid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return lo
|
|
|
|
end
|
|
|
|
|
2023-03-06 11:03:13 -07:00
|
|
|
--- Do a binary search of the tokens in the half-open range [lo, hi).
|
|
|
|
---
|
|
|
|
--- Return the index i in range such that tokens[j].line <= line for all j < i, and
|
|
|
|
--- tokens[j].line > line for all j >= i, or return hi if no such index is found.
|
|
|
|
local function upper_bound(tokens, line, lo, hi)
|
|
|
|
while lo < hi do
|
2023-03-10 06:10:38 -07:00
|
|
|
local mid = bit.rshift(lo + hi, 1) -- Equivalent to floor((lo + hi) / 2).
|
2023-03-06 11:03:13 -07:00
|
|
|
if line < tokens[mid].line then
|
|
|
|
hi = mid
|
|
|
|
else
|
|
|
|
lo = mid + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return lo
|
|
|
|
end
|
|
|
|
|
2022-11-23 09:06:36 -07:00
|
|
|
--- Extracts modifier strings from the encoded number in the token array
|
|
|
|
---
|
2023-12-13 05:00:11 -07:00
|
|
|
---@param x integer
|
|
|
|
---@param modifiers_table table<integer,string>
|
2023-03-06 11:03:13 -07:00
|
|
|
---@return table<string, boolean>
|
2022-11-23 09:06:36 -07:00
|
|
|
local function modifiers_from_number(x, modifiers_table)
|
2023-12-13 05:00:11 -07:00
|
|
|
local modifiers = {} ---@type table<string,boolean>
|
2022-12-12 10:42:37 -07:00
|
|
|
local idx = 1
|
|
|
|
while x > 0 do
|
2023-02-23 05:35:46 -07:00
|
|
|
if bit.band(x, 1) == 1 then
|
2023-03-06 11:03:13 -07:00
|
|
|
modifiers[modifiers_table[idx]] = true
|
2022-11-23 09:06:36 -07:00
|
|
|
end
|
2023-02-23 05:35:46 -07:00
|
|
|
x = bit.rshift(x, 1)
|
2022-12-12 10:42:37 -07:00
|
|
|
idx = idx + 1
|
2022-11-23 09:06:36 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
return modifiers
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Converts a raw token list to a list of highlight ranges used by the on_win callback
|
|
|
|
---
|
2023-12-13 05:00:11 -07:00
|
|
|
---@param data integer[]
|
|
|
|
---@param bufnr integer
|
|
|
|
---@param client lsp.Client
|
|
|
|
---@param request STActiveRequest
|
2022-11-23 09:06:36 -07:00
|
|
|
---@return STTokenRange[]
|
2023-04-30 22:15:32 -07:00
|
|
|
local function tokens_to_ranges(data, bufnr, client, request)
|
2022-11-23 09:06:36 -07:00
|
|
|
local legend = client.server_capabilities.semanticTokensProvider.legend
|
|
|
|
local token_types = legend.tokenTypes
|
|
|
|
local token_modifiers = legend.tokenModifiers
|
2023-05-04 22:41:36 -07:00
|
|
|
local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
2023-12-13 05:00:11 -07:00
|
|
|
local ranges = {} ---@type STTokenRange[]
|
2022-11-23 09:06:36 -07:00
|
|
|
|
2023-04-30 22:15:32 -07:00
|
|
|
local start = uv.hrtime()
|
|
|
|
local ms_to_ns = 1000 * 1000
|
|
|
|
local yield_interval_ns = 5 * ms_to_ns
|
|
|
|
local co, is_main = coroutine.running()
|
|
|
|
|
2023-12-13 05:00:11 -07:00
|
|
|
local line ---@type integer?
|
2022-11-23 09:06:36 -07:00
|
|
|
local start_char = 0
|
|
|
|
for i = 1, #data, 5 do
|
2023-04-30 22:15:32 -07:00
|
|
|
-- if this function is called from the main coroutine, let it run to completion with no yield
|
|
|
|
if not is_main then
|
|
|
|
local elapsed_ns = uv.hrtime() - start
|
|
|
|
|
|
|
|
if elapsed_ns > yield_interval_ns then
|
|
|
|
vim.schedule(function()
|
|
|
|
coroutine.resume(co, util.buf_versions[bufnr])
|
|
|
|
end)
|
|
|
|
if request.version ~= coroutine.yield() then
|
|
|
|
-- request became stale since the last time the coroutine ran.
|
|
|
|
-- abandon it by yielding without a way to resume
|
|
|
|
coroutine.yield()
|
|
|
|
end
|
|
|
|
|
|
|
|
start = uv.hrtime()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-23 09:06:36 -07:00
|
|
|
local delta_line = data[i]
|
|
|
|
line = line and line + delta_line or delta_line
|
|
|
|
local delta_start = data[i + 1]
|
|
|
|
start_char = delta_line == 0 and start_char + delta_start or delta_start
|
|
|
|
|
|
|
|
-- data[i+3] +1 because Lua tables are 1-indexed
|
|
|
|
local token_type = token_types[data[i + 3] + 1]
|
|
|
|
local modifiers = modifiers_from_number(data[i + 4], token_modifiers)
|
|
|
|
|
2023-05-04 22:41:36 -07:00
|
|
|
local function _get_byte_pos(col)
|
|
|
|
if col > 0 then
|
|
|
|
local buf_line = lines[line + 1] or ''
|
|
|
|
local ok, result
|
|
|
|
ok, result = pcall(util._str_byteindex_enc, buf_line, col, client.offset_encoding)
|
|
|
|
if ok then
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
return math.min(#buf_line, col)
|
|
|
|
end
|
|
|
|
return col
|
2022-11-23 09:06:36 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local start_col = _get_byte_pos(start_char)
|
|
|
|
local end_col = _get_byte_pos(start_char + data[i + 2])
|
|
|
|
|
|
|
|
if token_type then
|
|
|
|
ranges[#ranges + 1] = {
|
|
|
|
line = line,
|
|
|
|
start_col = start_col,
|
|
|
|
end_col = end_col,
|
|
|
|
type = token_type,
|
|
|
|
modifiers = modifiers,
|
2023-03-06 11:03:13 -07:00
|
|
|
marked = false,
|
2022-11-23 09:06:36 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return ranges
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Construct a new STHighlighter for the buffer
|
|
|
|
---
|
|
|
|
---@private
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param bufnr integer
|
2023-12-13 05:00:11 -07:00
|
|
|
---@return STHighlighter
|
2022-11-23 09:06:36 -07:00
|
|
|
function STHighlighter.new(bufnr)
|
|
|
|
local self = setmetatable({}, { __index = STHighlighter })
|
|
|
|
|
|
|
|
self.bufnr = bufnr
|
|
|
|
self.augroup = api.nvim_create_augroup('vim_lsp_semantic_tokens:' .. bufnr, { clear = true })
|
|
|
|
self.client_state = {}
|
|
|
|
|
|
|
|
STHighlighter.active[bufnr] = self
|
|
|
|
|
|
|
|
api.nvim_buf_attach(bufnr, false, {
|
|
|
|
on_lines = function(_, buf)
|
|
|
|
local highlighter = STHighlighter.active[buf]
|
|
|
|
if not highlighter then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
highlighter:on_change()
|
|
|
|
end,
|
|
|
|
on_reload = function(_, buf)
|
|
|
|
local highlighter = STHighlighter.active[buf]
|
|
|
|
if highlighter then
|
|
|
|
highlighter:reset()
|
|
|
|
highlighter:send_request()
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
on_detach = function(_, buf)
|
|
|
|
local highlighter = STHighlighter.active[buf]
|
|
|
|
if highlighter then
|
|
|
|
highlighter:destroy()
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
api.nvim_create_autocmd({ 'BufWinEnter', 'InsertLeave' }, {
|
|
|
|
buffer = self.bufnr,
|
|
|
|
group = self.augroup,
|
|
|
|
callback = function()
|
|
|
|
self:send_request()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
api.nvim_create_autocmd('LspDetach', {
|
|
|
|
buffer = self.bufnr,
|
|
|
|
group = self.augroup,
|
|
|
|
callback = function(args)
|
|
|
|
self:detach(args.data.client_id)
|
|
|
|
if vim.tbl_isempty(self.client_state) then
|
|
|
|
self:destroy()
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2023-12-13 05:00:11 -07:00
|
|
|
---@package
|
2022-11-23 09:06:36 -07:00
|
|
|
function STHighlighter:destroy()
|
|
|
|
for client_id, _ in pairs(self.client_state) do
|
|
|
|
self:detach(client_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
api.nvim_del_augroup_by_id(self.augroup)
|
|
|
|
STHighlighter.active[self.bufnr] = nil
|
|
|
|
end
|
|
|
|
|
2023-12-13 05:00:11 -07:00
|
|
|
---@package
|
2022-11-23 09:06:36 -07:00
|
|
|
function STHighlighter:attach(client_id)
|
|
|
|
local state = self.client_state[client_id]
|
|
|
|
if not state then
|
|
|
|
state = {
|
|
|
|
namespace = api.nvim_create_namespace('vim_lsp_semantic_tokens:' .. client_id),
|
|
|
|
active_request = {},
|
|
|
|
current_result = {},
|
|
|
|
}
|
|
|
|
self.client_state[client_id] = state
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-13 05:00:11 -07:00
|
|
|
---@package
|
2022-11-23 09:06:36 -07:00
|
|
|
function STHighlighter:detach(client_id)
|
|
|
|
local state = self.client_state[client_id]
|
|
|
|
if state then
|
|
|
|
--TODO: delete namespace if/when that becomes possible
|
|
|
|
api.nvim_buf_clear_namespace(self.bufnr, state.namespace, 0, -1)
|
|
|
|
self.client_state[client_id] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- This is the entry point for getting all the tokens in a buffer.
|
|
|
|
---
|
|
|
|
--- For the given clients (or all attached, if not provided), this sends a request
|
|
|
|
--- to ask for semantic tokens. If the server supports delta requests, that will
|
|
|
|
--- be prioritized if we have a previous requestId and token array.
|
|
|
|
---
|
|
|
|
--- This function will skip servers where there is an already an active request in
|
|
|
|
--- flight for the same version. If there is a stale request in flight, that is
|
|
|
|
--- cancelled prior to sending a new one.
|
|
|
|
---
|
|
|
|
--- Finally, if the request was successful, the requestId and document version
|
|
|
|
--- are saved to facilitate document synchronization in the response.
|
|
|
|
---
|
2023-12-13 05:00:11 -07:00
|
|
|
---@package
|
2022-11-23 09:06:36 -07:00
|
|
|
function STHighlighter:send_request()
|
|
|
|
local version = util.buf_versions[self.bufnr]
|
|
|
|
|
|
|
|
self:reset_timer()
|
|
|
|
|
|
|
|
for client_id, state in pairs(self.client_state) do
|
|
|
|
local client = vim.lsp.get_client_by_id(client_id)
|
|
|
|
|
|
|
|
local current_result = state.current_result
|
|
|
|
local active_request = state.active_request
|
|
|
|
|
|
|
|
-- Only send a request for this client if the current result is out of date and
|
|
|
|
-- there isn't a current a request in flight for this version
|
|
|
|
if client and current_result.version ~= version and active_request.version ~= version then
|
|
|
|
-- cancel stale in-flight request
|
|
|
|
if active_request.request_id then
|
|
|
|
client.cancel_request(active_request.request_id)
|
|
|
|
active_request = {}
|
|
|
|
state.active_request = active_request
|
|
|
|
end
|
|
|
|
|
|
|
|
local spec = client.server_capabilities.semanticTokensProvider.full
|
|
|
|
local hasEditProvider = type(spec) == 'table' and spec.delta
|
|
|
|
|
|
|
|
local params = { textDocument = util.make_text_document_params(self.bufnr) }
|
2023-08-03 04:03:48 -07:00
|
|
|
local method = ms.textDocument_semanticTokens_full
|
2022-11-23 09:06:36 -07:00
|
|
|
|
|
|
|
if hasEditProvider and current_result.result_id then
|
|
|
|
method = method .. '/delta'
|
|
|
|
params.previousResultId = current_result.result_id
|
|
|
|
end
|
|
|
|
local success, request_id = client.request(method, params, function(err, response, ctx)
|
|
|
|
-- look client up again using ctx.client_id instead of using a captured
|
|
|
|
-- client object
|
|
|
|
local c = vim.lsp.get_client_by_id(ctx.client_id)
|
2023-12-13 05:00:11 -07:00
|
|
|
local bufnr = assert(ctx.bufnr)
|
|
|
|
local highlighter = STHighlighter.active[bufnr]
|
2022-11-23 09:06:36 -07:00
|
|
|
if not err and c and highlighter then
|
2023-04-30 22:15:32 -07:00
|
|
|
coroutine.wrap(STHighlighter.process_response)(highlighter, response, c, version)
|
2022-11-23 09:06:36 -07:00
|
|
|
end
|
|
|
|
end, self.bufnr)
|
|
|
|
|
|
|
|
if success then
|
|
|
|
active_request.request_id = request_id
|
|
|
|
active_request.version = version
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- This function will parse the semantic token responses and set up the cache
|
|
|
|
--- (current_result). It also performs document synchronization by checking the
|
|
|
|
--- version of the document associated with the resulting request_id and only
|
|
|
|
--- performing work if the response is not out-of-date.
|
|
|
|
---
|
|
|
|
--- Delta edits are applied if necessary, and new highlight ranges are calculated
|
|
|
|
--- and stored in the buffer state.
|
|
|
|
---
|
|
|
|
--- Finally, a redraw command is issued to force nvim to redraw the screen to
|
|
|
|
--- pick up changed highlight tokens.
|
|
|
|
---
|
2023-12-13 05:00:11 -07:00
|
|
|
---@param response lsp.SemanticTokens|lsp.SemanticTokensDelta
|
2022-11-23 09:06:36 -07:00
|
|
|
---@private
|
|
|
|
function STHighlighter:process_response(response, client, version)
|
|
|
|
local state = self.client_state[client.id]
|
|
|
|
if not state then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- ignore stale responses
|
|
|
|
if state.active_request.version and version ~= state.active_request.version then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-12-10 04:16:33 -07:00
|
|
|
-- skip nil responses
|
|
|
|
if response == nil then
|
2023-04-30 22:15:32 -07:00
|
|
|
state.active_request = {}
|
2022-12-10 04:16:33 -07:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-11-23 09:06:36 -07:00
|
|
|
-- if we have a response to a delta request, update the state of our tokens
|
|
|
|
-- appropriately. if it's a full response, just use that
|
2023-12-13 05:00:11 -07:00
|
|
|
local tokens ---@type integer[]
|
2022-11-23 09:06:36 -07:00
|
|
|
local token_edits = response.edits
|
|
|
|
if token_edits then
|
|
|
|
table.sort(token_edits, function(a, b)
|
|
|
|
return a.start < b.start
|
|
|
|
end)
|
|
|
|
|
2023-12-13 05:00:11 -07:00
|
|
|
tokens = {} --- @type integer[]
|
|
|
|
local old_tokens = assert(state.current_result.tokens)
|
2022-12-09 03:54:09 -07:00
|
|
|
local idx = 1
|
2022-11-23 09:06:36 -07:00
|
|
|
for _, token_edit in ipairs(token_edits) do
|
2022-12-09 03:54:09 -07:00
|
|
|
vim.list_extend(tokens, old_tokens, idx, token_edit.start)
|
2022-12-19 01:24:27 -07:00
|
|
|
if token_edit.data then
|
|
|
|
vim.list_extend(tokens, token_edit.data)
|
|
|
|
end
|
2022-12-09 03:54:09 -07:00
|
|
|
idx = token_edit.start + token_edit.deleteCount + 1
|
2022-11-23 09:06:36 -07:00
|
|
|
end
|
2022-12-09 03:54:09 -07:00
|
|
|
vim.list_extend(tokens, old_tokens, idx)
|
2022-11-23 09:06:36 -07:00
|
|
|
else
|
|
|
|
tokens = response.data
|
|
|
|
end
|
|
|
|
|
2023-04-30 22:15:32 -07:00
|
|
|
-- convert token list to highlight ranges
|
|
|
|
-- this could yield and run over multiple event loop iterations
|
|
|
|
local highlights = tokens_to_ranges(tokens, self.bufnr, client, state.active_request)
|
|
|
|
|
|
|
|
-- reset active request
|
|
|
|
state.active_request = {}
|
|
|
|
|
|
|
|
-- update the state with the new results
|
2022-11-23 09:06:36 -07:00
|
|
|
local current_result = state.current_result
|
|
|
|
current_result.version = version
|
|
|
|
current_result.result_id = response.resultId
|
|
|
|
current_result.tokens = tokens
|
2023-04-30 22:15:32 -07:00
|
|
|
current_result.highlights = highlights
|
2022-11-23 09:06:36 -07:00
|
|
|
current_result.namespace_cleared = false
|
|
|
|
|
2023-02-28 04:38:33 -07:00
|
|
|
-- redraw all windows displaying buffer
|
|
|
|
api.nvim__buf_redraw_range(self.bufnr, 0, -1)
|
2022-11-23 09:06:36 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
--- on_win handler for the decoration provider (see |nvim_set_decoration_provider|)
|
|
|
|
---
|
|
|
|
--- If there is a current result for the buffer and the version matches the
|
|
|
|
--- current document version, then the tokens are valid and can be applied. As
|
|
|
|
--- the buffer is drawn, this function will add extmark highlights for every
|
|
|
|
--- token in the range of visible lines. Once a highlight has been added, it
|
|
|
|
--- sticks around until the document changes and there's a new set of matching
|
|
|
|
--- highlight tokens available.
|
|
|
|
---
|
|
|
|
--- If this is the first time a buffer is being drawn with a new set of
|
|
|
|
--- highlights for the current document version, the namespace is cleared to
|
|
|
|
--- remove extmarks from the last version. It's done here instead of the response
|
|
|
|
--- handler to avoid the "blink" that occurs due to the timing between the
|
|
|
|
--- response handler and the actual redraw.
|
|
|
|
---
|
2023-12-13 05:00:11 -07:00
|
|
|
---@package
|
|
|
|
---@param topline integer
|
|
|
|
---@param botline integer
|
2022-11-23 09:06:36 -07:00
|
|
|
function STHighlighter:on_win(topline, botline)
|
2023-03-06 11:03:13 -07:00
|
|
|
for client_id, state in pairs(self.client_state) do
|
2022-11-23 09:06:36 -07:00
|
|
|
local current_result = state.current_result
|
|
|
|
if current_result.version and current_result.version == util.buf_versions[self.bufnr] then
|
|
|
|
if not current_result.namespace_cleared then
|
|
|
|
api.nvim_buf_clear_namespace(self.bufnr, state.namespace, 0, -1)
|
|
|
|
current_result.namespace_cleared = true
|
|
|
|
end
|
|
|
|
|
|
|
|
-- We can't use ephemeral extmarks because the buffer updates are not in
|
|
|
|
-- sync with the list of semantic tokens. There's a delay between the
|
|
|
|
-- buffer changing and when the LSP server can respond with updated
|
|
|
|
-- tokens, and we don't want to "blink" the token highlights while
|
|
|
|
-- updates are in flight, and we don't want to use stale tokens because
|
|
|
|
-- they likely won't line up right with the actual buffer.
|
|
|
|
--
|
|
|
|
-- Instead, we have to use normal extmarks that can attach to locations
|
|
|
|
-- in the buffer and are persisted between redraws.
|
2023-03-06 11:03:13 -07:00
|
|
|
--
|
|
|
|
-- `strict = false` is necessary here for the 1% of cases where the
|
|
|
|
-- current result doesn't actually match the buffer contents. Some
|
|
|
|
-- LSP servers can respond with stale tokens on requests if they are
|
|
|
|
-- still processing changes from a didChange notification.
|
|
|
|
--
|
|
|
|
-- LSP servers that do this _should_ follow up known stale responses
|
|
|
|
-- with a refresh notification once they've finished processing the
|
|
|
|
-- didChange notification, which would re-synchronize the tokens from
|
|
|
|
-- our end.
|
|
|
|
--
|
|
|
|
-- The server I know of that does this is clangd when the preamble of
|
|
|
|
-- a file changes and the token request is processed with a stale
|
|
|
|
-- preamble while the new one is still being built. Once the preamble
|
|
|
|
-- finishes, clangd sends a refresh request which lets the client
|
|
|
|
-- re-synchronize the tokens.
|
|
|
|
|
|
|
|
local set_mark = function(token, hl_group, delta)
|
|
|
|
vim.api.nvim_buf_set_extmark(self.bufnr, state.namespace, token.line, token.start_col, {
|
|
|
|
hl_group = hl_group,
|
|
|
|
end_col = token.end_col,
|
|
|
|
priority = vim.highlight.priorities.semantic_tokens + delta,
|
|
|
|
strict = false,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
local ft = vim.bo[self.bufnr].filetype
|
2023-12-13 05:00:11 -07:00
|
|
|
local highlights = assert(current_result.highlights)
|
2023-03-06 11:03:13 -07:00
|
|
|
local first = lower_bound(highlights, topline, 1, #highlights + 1)
|
|
|
|
local last = upper_bound(highlights, botline, first, #highlights + 1) - 1
|
2022-11-23 09:06:36 -07:00
|
|
|
|
2023-03-06 11:03:13 -07:00
|
|
|
for i = first, last do
|
2022-11-23 09:06:36 -07:00
|
|
|
local token = highlights[i]
|
2023-03-06 11:03:13 -07:00
|
|
|
if not token.marked then
|
|
|
|
set_mark(token, string.format('@lsp.type.%s.%s', token.type, ft), 0)
|
|
|
|
for modifier, _ in pairs(token.modifiers) do
|
|
|
|
set_mark(token, string.format('@lsp.mod.%s.%s', modifier, ft), 1)
|
|
|
|
set_mark(token, string.format('@lsp.typemod.%s.%s.%s', token.type, modifier, ft), 2)
|
2022-12-12 12:43:14 -07:00
|
|
|
end
|
2023-03-06 11:03:13 -07:00
|
|
|
token.marked = true
|
|
|
|
|
|
|
|
api.nvim_exec_autocmds('LspTokenUpdate', {
|
2023-03-07 09:35:12 -07:00
|
|
|
buffer = self.bufnr,
|
2023-03-06 11:03:13 -07:00
|
|
|
modeline = false,
|
|
|
|
data = {
|
|
|
|
token = token,
|
|
|
|
client_id = client_id,
|
|
|
|
},
|
|
|
|
})
|
2022-11-23 09:06:36 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Reset the buffer's highlighting state and clears the extmark highlights.
|
|
|
|
---
|
2023-12-13 05:00:11 -07:00
|
|
|
---@package
|
2022-11-23 09:06:36 -07:00
|
|
|
function STHighlighter:reset()
|
|
|
|
for client_id, state in pairs(self.client_state) do
|
|
|
|
api.nvim_buf_clear_namespace(self.bufnr, state.namespace, 0, -1)
|
|
|
|
state.current_result = {}
|
|
|
|
if state.active_request.request_id then
|
|
|
|
local client = vim.lsp.get_client_by_id(client_id)
|
|
|
|
assert(client)
|
|
|
|
client.cancel_request(state.active_request.request_id)
|
|
|
|
state.active_request = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Mark a client's results as dirty. This method will cancel any active
|
|
|
|
--- requests to the server and pause new highlights from being added
|
|
|
|
--- in the on_win callback. The rest of the current results are saved
|
|
|
|
--- in case the server supports delta requests.
|
|
|
|
---
|
2023-12-13 05:00:11 -07:00
|
|
|
---@package
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param client_id integer
|
2022-11-23 09:06:36 -07:00
|
|
|
function STHighlighter:mark_dirty(client_id)
|
|
|
|
local state = self.client_state[client_id]
|
|
|
|
assert(state)
|
|
|
|
|
|
|
|
-- if we clear the version from current_result, it'll cause the
|
|
|
|
-- next request to be sent and will also pause new highlights
|
|
|
|
-- from being added in on_win until a new result comes from
|
|
|
|
-- the server
|
|
|
|
if state.current_result then
|
|
|
|
state.current_result.version = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if state.active_request.request_id then
|
|
|
|
local client = vim.lsp.get_client_by_id(client_id)
|
|
|
|
assert(client)
|
|
|
|
client.cancel_request(state.active_request.request_id)
|
|
|
|
state.active_request = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-13 05:00:11 -07:00
|
|
|
---@package
|
2022-11-23 09:06:36 -07:00
|
|
|
function STHighlighter:on_change()
|
|
|
|
self:reset_timer()
|
|
|
|
if self.debounce > 0 then
|
|
|
|
self.timer = vim.defer_fn(function()
|
|
|
|
self:send_request()
|
|
|
|
end, self.debounce)
|
|
|
|
else
|
|
|
|
self:send_request()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@private
|
|
|
|
function STHighlighter:reset_timer()
|
|
|
|
local timer = self.timer
|
|
|
|
if timer then
|
|
|
|
self.timer = nil
|
|
|
|
if not timer:is_closing() then
|
|
|
|
timer:stop()
|
|
|
|
timer:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
--- Start the semantic token highlighting engine for the given buffer with the
|
|
|
|
--- given client. The client must already be attached to the buffer.
|
|
|
|
---
|
|
|
|
--- NOTE: This is currently called automatically by |vim.lsp.buf_attach_client()|. To
|
|
|
|
--- opt-out of semantic highlighting with a server that supports it, you can
|
|
|
|
--- delete the semanticTokensProvider table from the {server_capabilities} of
|
|
|
|
--- your client in your |LspAttach| callback or your configuration's
|
2022-12-12 12:43:14 -07:00
|
|
|
--- `on_attach` callback:
|
2023-09-14 06:23:01 -07:00
|
|
|
---
|
|
|
|
--- ```lua
|
|
|
|
--- client.server_capabilities.semanticTokensProvider = nil
|
|
|
|
--- ```
|
2022-11-23 09:06:36 -07:00
|
|
|
---
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param bufnr integer
|
|
|
|
---@param client_id integer
|
2022-11-23 09:06:36 -07:00
|
|
|
---@param opts (nil|table) Optional keyword arguments
|
2023-03-06 23:17:52 -07:00
|
|
|
--- - debounce (integer, default: 200): Debounce token requests
|
2022-11-23 09:06:36 -07:00
|
|
|
--- to the server by the given number in milliseconds
|
|
|
|
function M.start(bufnr, client_id, opts)
|
|
|
|
vim.validate({
|
|
|
|
bufnr = { bufnr, 'n', false },
|
|
|
|
client_id = { client_id, 'n', false },
|
|
|
|
})
|
|
|
|
|
|
|
|
opts = opts or {}
|
|
|
|
assert(
|
|
|
|
(not opts.debounce or type(opts.debounce) == 'number'),
|
|
|
|
'opts.debounce must be a number with the debounce time in milliseconds'
|
|
|
|
)
|
|
|
|
|
|
|
|
local client = vim.lsp.get_client_by_id(client_id)
|
|
|
|
if not client then
|
|
|
|
vim.notify('[LSP] No client with id ' .. client_id, vim.log.levels.ERROR)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if not vim.lsp.buf_is_attached(bufnr, client_id) then
|
|
|
|
vim.notify(
|
|
|
|
'[LSP] Client with id ' .. client_id .. ' not attached to buffer ' .. bufnr,
|
|
|
|
vim.log.levels.WARN
|
|
|
|
)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if not vim.tbl_get(client.server_capabilities, 'semanticTokensProvider', 'full') then
|
|
|
|
vim.notify('[LSP] Server does not support semantic tokens', vim.log.levels.WARN)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local highlighter = STHighlighter.active[bufnr]
|
|
|
|
|
|
|
|
if not highlighter then
|
|
|
|
highlighter = STHighlighter.new(bufnr)
|
|
|
|
highlighter.debounce = opts.debounce or 200
|
|
|
|
else
|
|
|
|
highlighter.debounce = math.max(highlighter.debounce, opts.debounce or 200)
|
|
|
|
end
|
|
|
|
|
|
|
|
highlighter:attach(client_id)
|
|
|
|
highlighter:send_request()
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Stop the semantic token highlighting engine for the given buffer with the
|
|
|
|
--- given client.
|
|
|
|
---
|
|
|
|
--- NOTE: This is automatically called by a |LspDetach| autocmd that is set up as part
|
|
|
|
--- 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.
|
|
|
|
---
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param bufnr integer
|
|
|
|
---@param client_id integer
|
2022-11-23 09:06:36 -07:00
|
|
|
function M.stop(bufnr, client_id)
|
|
|
|
vim.validate({
|
|
|
|
bufnr = { bufnr, 'n', false },
|
|
|
|
client_id = { client_id, 'n', false },
|
|
|
|
})
|
|
|
|
|
|
|
|
local highlighter = STHighlighter.active[bufnr]
|
|
|
|
if not highlighter then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
highlighter:detach(client_id)
|
|
|
|
|
|
|
|
if vim.tbl_isempty(highlighter.client_state) then
|
|
|
|
highlighter:destroy()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-13 05:00:11 -07:00
|
|
|
--- @class STTokenRangeInspect : STTokenRange
|
|
|
|
--- @field client_id integer
|
|
|
|
|
2022-12-10 05:17:07 -07:00
|
|
|
--- Return the semantic token(s) at the given position.
|
2022-12-14 02:46:54 -07:00
|
|
|
--- If called without arguments, returns the token under the cursor.
|
2022-12-10 05:17:07 -07:00
|
|
|
---
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param bufnr integer|nil Buffer number (0 for current buffer, default)
|
|
|
|
---@param row integer|nil Position row (default cursor position)
|
|
|
|
---@param col integer|nil Position column (default cursor position)
|
2022-12-10 05:17:07 -07:00
|
|
|
---
|
2023-12-13 05:00:11 -07:00
|
|
|
---@return STTokenRangeInspect[]|nil (table|nil) List of tokens at position. Each token has
|
2023-03-06 11:03:13 -07:00
|
|
|
--- the following fields:
|
2023-03-06 23:17:52 -07:00
|
|
|
--- - line (integer) line number, 0-based
|
|
|
|
--- - start_col (integer) start column, 0-based
|
|
|
|
--- - end_col (integer) end column, 0-based
|
2023-03-06 11:03:13 -07:00
|
|
|
--- - type (string) token type as string, e.g. "variable"
|
|
|
|
--- - modifiers (table) token modifiers as a set. E.g., { static = true, readonly = true }
|
2023-12-13 05:00:11 -07:00
|
|
|
--- - client_id (integer)
|
2022-12-10 05:17:07 -07:00
|
|
|
function M.get_at_pos(bufnr, row, col)
|
|
|
|
if bufnr == nil or bufnr == 0 then
|
|
|
|
bufnr = api.nvim_get_current_buf()
|
|
|
|
end
|
|
|
|
|
|
|
|
local highlighter = STHighlighter.active[bufnr]
|
|
|
|
if not highlighter then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if row == nil or col == nil then
|
|
|
|
local cursor = api.nvim_win_get_cursor(0)
|
|
|
|
row, col = cursor[1] - 1, cursor[2]
|
|
|
|
end
|
|
|
|
|
2023-12-13 05:00:11 -07:00
|
|
|
local tokens = {} --- @type STTokenRangeInspect[]
|
2022-12-14 02:46:54 -07:00
|
|
|
for client_id, client in pairs(highlighter.client_state) do
|
2022-12-10 05:17:07 -07:00
|
|
|
local highlights = client.current_result.highlights
|
|
|
|
if highlights then
|
2023-03-06 11:03:13 -07:00
|
|
|
local idx = lower_bound(highlights, row, 1, #highlights + 1)
|
2022-12-10 05:17:07 -07:00
|
|
|
for i = idx, #highlights do
|
|
|
|
local token = highlights[i]
|
2023-12-13 05:00:11 -07:00
|
|
|
--- @cast token STTokenRangeInspect
|
2022-12-10 05:17:07 -07:00
|
|
|
|
|
|
|
if token.line > row then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
if token.start_col <= col and token.end_col > col then
|
2022-12-14 02:46:54 -07:00
|
|
|
token.client_id = client_id
|
2022-12-10 05:17:07 -07:00
|
|
|
tokens[#tokens + 1] = token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return tokens
|
|
|
|
end
|
|
|
|
|
2022-11-23 09:06:36 -07:00
|
|
|
--- Force a refresh of all semantic tokens
|
|
|
|
---
|
|
|
|
--- Only has an effect if the buffer is currently active for semantic token
|
|
|
|
--- highlighting (|vim.lsp.semantic_tokens.start()| has been called for it)
|
|
|
|
---
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param bufnr (integer|nil) filter by buffer. All buffers if nil, current
|
2023-03-06 11:03:13 -07:00
|
|
|
--- buffer if 0
|
2022-11-23 09:06:36 -07:00
|
|
|
function M.force_refresh(bufnr)
|
|
|
|
vim.validate({
|
|
|
|
bufnr = { bufnr, 'n', true },
|
|
|
|
})
|
|
|
|
|
2023-03-06 11:03:13 -07:00
|
|
|
local buffers = bufnr == nil and vim.tbl_keys(STHighlighter.active)
|
|
|
|
or bufnr == 0 and { api.nvim_get_current_buf() }
|
|
|
|
or { bufnr }
|
|
|
|
|
|
|
|
for _, buffer in ipairs(buffers) do
|
|
|
|
local highlighter = STHighlighter.active[buffer]
|
|
|
|
if highlighter then
|
|
|
|
highlighter:reset()
|
|
|
|
highlighter:send_request()
|
|
|
|
end
|
2022-11-23 09:06:36 -07:00
|
|
|
end
|
2023-03-06 11:03:13 -07:00
|
|
|
end
|
2022-11-23 09:06:36 -07:00
|
|
|
|
2023-03-06 11:03:13 -07:00
|
|
|
--- Highlight a semantic token.
|
|
|
|
---
|
|
|
|
--- Apply an extmark with a given highlight group for a semantic token. The
|
|
|
|
--- 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.
|
2023-08-09 02:06:13 -07:00
|
|
|
---@param token (table) a semantic token, found as `args.data.token` in |LspTokenUpdate|.
|
2023-03-06 23:17:52 -07:00
|
|
|
---@param bufnr (integer) the buffer to highlight
|
|
|
|
---@param client_id (integer) The ID of the |vim.lsp.client|
|
2023-03-06 11:03:13 -07:00
|
|
|
---@param hl_group (string) Highlight group name
|
|
|
|
---@param opts (table|nil) Optional parameters.
|
2023-03-06 23:17:52 -07:00
|
|
|
--- - priority: (integer|nil) Priority for the applied extmark. Defaults
|
2023-03-06 11:03:13 -07:00
|
|
|
--- to `vim.highlight.priorities.semantic_tokens + 3`
|
|
|
|
function M.highlight_token(token, bufnr, client_id, hl_group, opts)
|
2022-11-23 09:06:36 -07:00
|
|
|
local highlighter = STHighlighter.active[bufnr]
|
|
|
|
if not highlighter then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-03-06 11:03:13 -07:00
|
|
|
local state = highlighter.client_state[client_id]
|
|
|
|
if not state then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
opts = opts or {}
|
|
|
|
local priority = opts.priority or vim.highlight.priorities.semantic_tokens + 3
|
|
|
|
|
|
|
|
vim.api.nvim_buf_set_extmark(bufnr, state.namespace, token.line, token.start_col, {
|
|
|
|
hl_group = hl_group,
|
|
|
|
end_col = token.end_col,
|
|
|
|
priority = priority,
|
|
|
|
strict = false,
|
|
|
|
})
|
2022-11-23 09:06:36 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
--- |lsp-handler| for the method `workspace/semanticTokens/refresh`
|
|
|
|
---
|
|
|
|
--- Refresh requests are sent by the server to indicate a project-wide change
|
|
|
|
--- that requires all tokens to be re-requested by the client. This handler will
|
|
|
|
--- invalidate the current results of all buffers and automatically kick off a
|
|
|
|
--- new request for buffers that are displayed in a window. For those that aren't, a
|
|
|
|
--- the BufWinEnter event should take care of it next time it's displayed.
|
|
|
|
---
|
|
|
|
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#semanticTokens_refreshRequest
|
2023-08-03 04:03:48 -07:00
|
|
|
handlers[ms.workspace_semanticTokens_refresh] = function(err, _, ctx)
|
2022-11-23 09:06:36 -07:00
|
|
|
if err then
|
|
|
|
return vim.NIL
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, bufnr in ipairs(vim.lsp.get_buffers_by_client_id(ctx.client_id)) do
|
|
|
|
local highlighter = STHighlighter.active[bufnr]
|
|
|
|
if highlighter and highlighter.client_state[ctx.client_id] then
|
|
|
|
highlighter:mark_dirty(ctx.client_id)
|
|
|
|
|
|
|
|
if not vim.tbl_isempty(vim.fn.win_findbuf(bufnr)) then
|
|
|
|
highlighter:send_request()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return vim.NIL
|
|
|
|
end
|
|
|
|
|
|
|
|
local namespace = api.nvim_create_namespace('vim_lsp_semantic_tokens')
|
|
|
|
api.nvim_set_decoration_provider(namespace, {
|
|
|
|
on_win = function(_, _, bufnr, topline, botline)
|
|
|
|
local highlighter = STHighlighter.active[bufnr]
|
|
|
|
if highlighter then
|
|
|
|
highlighter:on_win(topline, botline)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
--- for testing only! there is no guarantee of API stability with this!
|
|
|
|
---
|
|
|
|
---@private
|
|
|
|
M.__STHighlighter = STHighlighter
|
|
|
|
|
|
|
|
return M
|