semantic_tokens.lua 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. local api = vim.api
  2. local bit = require('bit')
  3. local ms = require('vim.lsp.protocol').Methods
  4. local util = require('vim.lsp.util')
  5. local Range = require('vim.treesitter._range')
  6. local uv = vim.uv
  7. local Capability = require('vim.lsp._capability')
  8. --- @class (private) STTokenRange
  9. --- @field line integer line number 0-based
  10. --- @field start_col integer start column 0-based
  11. --- @field end_line integer end line number 0-based
  12. --- @field end_col integer end column 0-based
  13. --- @field type string token type as string
  14. --- @field modifiers table<string,boolean> token modifiers as a set. E.g., { static = true, readonly = true }
  15. --- @field marked boolean whether this token has had extmarks applied
  16. ---
  17. --- @class (private) STCurrentResult
  18. --- @field version? integer document version associated with this result
  19. --- @field result_id? string resultId from the server; used with delta requests
  20. --- @field highlights? STTokenRange[] cache of highlight ranges for this document version
  21. --- @field tokens? integer[] raw token array as received by the server. used for calculating delta responses
  22. --- @field namespace_cleared? boolean whether the namespace was cleared for this result yet
  23. ---
  24. --- @class (private) STActiveRequest
  25. --- @field request_id? integer the LSP request ID of the most recent request sent to the server
  26. --- @field version? integer the document version associated with the most recent request
  27. ---
  28. --- @class (private) STClientState
  29. --- @field namespace integer
  30. --- @field active_request STActiveRequest
  31. --- @field current_result STCurrentResult
  32. ---@class (private) STHighlighter : vim.lsp.Capability
  33. ---@field active table<integer, STHighlighter>
  34. ---@field bufnr integer
  35. ---@field augroup integer augroup for buffer events
  36. ---@field debounce integer milliseconds to debounce requests for new tokens
  37. ---@field timer table uv_timer for debouncing requests for new tokens
  38. ---@field client_state table<integer, STClientState>
  39. local STHighlighter = { name = 'Semantic Tokens', active = {} }
  40. STHighlighter.__index = STHighlighter
  41. setmetatable(STHighlighter, Capability)
  42. --- Do a binary search of the tokens in the half-open range [lo, hi).
  43. ---
  44. --- Return the index i in range such that tokens[j].line < line for all j < i, and
  45. --- tokens[j].line >= line for all j >= i, or return hi if no such index is found.
  46. local function lower_bound(tokens, line, lo, hi)
  47. while lo < hi do
  48. local mid = bit.rshift(lo + hi, 1) -- Equivalent to floor((lo + hi) / 2).
  49. if tokens[mid].end_line < line then
  50. lo = mid + 1
  51. else
  52. hi = mid
  53. end
  54. end
  55. return lo
  56. end
  57. --- Do a binary search of the tokens in the half-open range [lo, hi).
  58. ---
  59. --- Return the index i in range such that tokens[j].line <= line for all j < i, and
  60. --- tokens[j].line > line for all j >= i, or return hi if no such index is found.
  61. local function upper_bound(tokens, line, lo, hi)
  62. while lo < hi do
  63. local mid = bit.rshift(lo + hi, 1) -- Equivalent to floor((lo + hi) / 2).
  64. if line < tokens[mid].line then
  65. hi = mid
  66. else
  67. lo = mid + 1
  68. end
  69. end
  70. return lo
  71. end
  72. --- Extracts modifier strings from the encoded number in the token array
  73. ---
  74. ---@param x integer
  75. ---@param modifiers_table table<integer,string>
  76. ---@return table<string, boolean>
  77. local function modifiers_from_number(x, modifiers_table)
  78. local modifiers = {} ---@type table<string,boolean>
  79. local idx = 1
  80. while x > 0 do
  81. if bit.band(x, 1) == 1 then
  82. modifiers[modifiers_table[idx]] = true
  83. end
  84. x = bit.rshift(x, 1)
  85. idx = idx + 1
  86. end
  87. return modifiers
  88. end
  89. --- Converts a raw token list to a list of highlight ranges used by the on_win callback
  90. ---
  91. ---@async
  92. ---@param data integer[]
  93. ---@param bufnr integer
  94. ---@param client vim.lsp.Client
  95. ---@param request STActiveRequest
  96. ---@return STTokenRange[]
  97. local function tokens_to_ranges(data, bufnr, client, request)
  98. local legend = client.server_capabilities.semanticTokensProvider.legend
  99. local token_types = legend.tokenTypes
  100. local token_modifiers = legend.tokenModifiers
  101. local encoding = client.offset_encoding
  102. local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false)
  103. -- For all encodings, \r\n takes up two code points, and \n (or \r) takes up one.
  104. local eol_offset = vim.bo.fileformat[bufnr] == 'dos' and 2 or 1
  105. local ranges = {} ---@type STTokenRange[]
  106. local start = uv.hrtime()
  107. local ms_to_ns = 1e6
  108. local yield_interval_ns = 5 * ms_to_ns
  109. local co, is_main = coroutine.running()
  110. local line ---@type integer?
  111. local start_char = 0
  112. for i = 1, #data, 5 do
  113. -- if this function is called from the main coroutine, let it run to completion with no yield
  114. if not is_main then
  115. local elapsed_ns = uv.hrtime() - start
  116. if elapsed_ns > yield_interval_ns then
  117. vim.schedule(function()
  118. coroutine.resume(co, util.buf_versions[bufnr])
  119. end)
  120. if request.version ~= coroutine.yield() then
  121. -- request became stale since the last time the coroutine ran.
  122. -- abandon it by yielding without a way to resume
  123. coroutine.yield()
  124. end
  125. start = uv.hrtime()
  126. end
  127. end
  128. local delta_line = data[i]
  129. line = line and line + delta_line or delta_line
  130. local delta_start = data[i + 1]
  131. start_char = delta_line == 0 and start_char + delta_start or delta_start
  132. -- data[i+3] +1 because Lua tables are 1-indexed
  133. local token_type = token_types[data[i + 3] + 1]
  134. if token_type then
  135. local modifiers = modifiers_from_number(data[i + 4], token_modifiers)
  136. local end_char = start_char + data[i + 2] --- @type integer LuaLS bug
  137. local buf_line = lines[line + 1] or ''
  138. local end_line = line ---@type integer
  139. local start_col = vim.str_byteindex(buf_line, encoding, start_char, false)
  140. ---@type integer LuaLS bug, type must be marked explicitly here
  141. local new_end_char = end_char - vim.str_utfindex(buf_line, encoding) - eol_offset
  142. -- While end_char goes past the given line, extend the token range to the next line
  143. while new_end_char > 0 do
  144. end_char = new_end_char
  145. end_line = end_line + 1
  146. buf_line = lines[end_line + 1] or ''
  147. new_end_char = new_end_char - vim.str_utfindex(buf_line, encoding) - eol_offset
  148. end
  149. local end_col = vim.str_byteindex(buf_line, encoding, end_char, false)
  150. ranges[#ranges + 1] = {
  151. line = line,
  152. end_line = end_line,
  153. start_col = start_col,
  154. end_col = end_col,
  155. type = token_type,
  156. modifiers = modifiers,
  157. marked = false,
  158. }
  159. end
  160. end
  161. return ranges
  162. end
  163. --- Construct a new STHighlighter for the buffer
  164. ---
  165. ---@private
  166. ---@param bufnr integer
  167. ---@return STHighlighter
  168. function STHighlighter:new(bufnr)
  169. self = Capability.new(self, bufnr)
  170. api.nvim_buf_attach(bufnr, false, {
  171. on_lines = function(_, buf)
  172. local highlighter = STHighlighter.active[buf]
  173. if not highlighter then
  174. return true
  175. end
  176. highlighter:on_change()
  177. end,
  178. on_reload = function(_, buf)
  179. local highlighter = STHighlighter.active[buf]
  180. if highlighter then
  181. highlighter:reset()
  182. highlighter:send_request()
  183. end
  184. end,
  185. })
  186. api.nvim_create_autocmd({ 'BufWinEnter', 'InsertLeave' }, {
  187. buffer = self.bufnr,
  188. group = self.augroup,
  189. callback = function()
  190. self:send_request()
  191. end,
  192. })
  193. return self
  194. end
  195. ---@package
  196. function STHighlighter:on_attach(client_id)
  197. local state = self.client_state[client_id]
  198. if not state then
  199. state = {
  200. namespace = api.nvim_create_namespace('nvim.lsp.semantic_tokens:' .. client_id),
  201. active_request = {},
  202. current_result = {},
  203. }
  204. self.client_state[client_id] = state
  205. end
  206. end
  207. ---@package
  208. function STHighlighter:on_detach(client_id)
  209. local state = self.client_state[client_id]
  210. if state then
  211. --TODO: delete namespace if/when that becomes possible
  212. api.nvim_buf_clear_namespace(self.bufnr, state.namespace, 0, -1)
  213. self.client_state[client_id] = nil
  214. end
  215. end
  216. --- This is the entry point for getting all the tokens in a buffer.
  217. ---
  218. --- For the given clients (or all attached, if not provided), this sends a request
  219. --- to ask for semantic tokens. If the server supports delta requests, that will
  220. --- be prioritized if we have a previous requestId and token array.
  221. ---
  222. --- This function will skip servers where there is an already an active request in
  223. --- flight for the same version. If there is a stale request in flight, that is
  224. --- cancelled prior to sending a new one.
  225. ---
  226. --- Finally, if the request was successful, the requestId and document version
  227. --- are saved to facilitate document synchronization in the response.
  228. ---
  229. ---@package
  230. function STHighlighter:send_request()
  231. local version = util.buf_versions[self.bufnr]
  232. self:reset_timer()
  233. for client_id, state in pairs(self.client_state) do
  234. local client = vim.lsp.get_client_by_id(client_id)
  235. local current_result = state.current_result
  236. local active_request = state.active_request
  237. -- Only send a request for this client if the current result is out of date and
  238. -- there isn't a current a request in flight for this version
  239. if client and current_result.version ~= version and active_request.version ~= version then
  240. -- cancel stale in-flight request
  241. if active_request.request_id then
  242. client:cancel_request(active_request.request_id)
  243. active_request = {}
  244. state.active_request = active_request
  245. end
  246. local spec = client.server_capabilities.semanticTokensProvider.full
  247. local hasEditProvider = type(spec) == 'table' and spec.delta
  248. local params = { textDocument = util.make_text_document_params(self.bufnr) }
  249. local method = ms.textDocument_semanticTokens_full
  250. if hasEditProvider and current_result.result_id then
  251. method = method .. '/delta'
  252. params.previousResultId = current_result.result_id
  253. end
  254. ---@param response? lsp.SemanticTokens|lsp.SemanticTokensDelta
  255. local success, request_id = client:request(method, params, function(err, response, ctx)
  256. -- look client up again using ctx.client_id instead of using a captured
  257. -- client object
  258. local c = vim.lsp.get_client_by_id(ctx.client_id)
  259. local bufnr = assert(ctx.bufnr)
  260. local highlighter = STHighlighter.active[bufnr]
  261. if not (c and highlighter) then
  262. return
  263. end
  264. if err or not response then
  265. highlighter.client_state[c.id].active_request = {}
  266. return
  267. end
  268. coroutine.wrap(STHighlighter.process_response)(highlighter, response, c, version)
  269. end, self.bufnr)
  270. if success then
  271. active_request.request_id = request_id
  272. active_request.version = version
  273. end
  274. end
  275. end
  276. end
  277. --- This function will parse the semantic token responses and set up the cache
  278. --- (current_result). It also performs document synchronization by checking the
  279. --- version of the document associated with the resulting request_id and only
  280. --- performing work if the response is not out-of-date.
  281. ---
  282. --- Delta edits are applied if necessary, and new highlight ranges are calculated
  283. --- and stored in the buffer state.
  284. ---
  285. --- Finally, a redraw command is issued to force nvim to redraw the screen to
  286. --- pick up changed highlight tokens.
  287. ---
  288. ---@async
  289. ---@param response lsp.SemanticTokens|lsp.SemanticTokensDelta
  290. ---@private
  291. function STHighlighter:process_response(response, client, version)
  292. local state = self.client_state[client.id]
  293. if not state then
  294. return
  295. end
  296. -- ignore stale responses
  297. if state.active_request.version and version ~= state.active_request.version then
  298. return
  299. end
  300. if not api.nvim_buf_is_valid(self.bufnr) then
  301. return
  302. end
  303. -- if we have a response to a delta request, update the state of our tokens
  304. -- appropriately. if it's a full response, just use that
  305. local tokens ---@type integer[]
  306. local token_edits = response.edits
  307. if token_edits then
  308. table.sort(token_edits, function(a, b)
  309. return a.start < b.start
  310. end)
  311. tokens = {} --- @type integer[]
  312. local old_tokens = assert(state.current_result.tokens)
  313. local idx = 1
  314. for _, token_edit in ipairs(token_edits) do
  315. vim.list_extend(tokens, old_tokens, idx, token_edit.start)
  316. if token_edit.data then
  317. vim.list_extend(tokens, token_edit.data)
  318. end
  319. idx = token_edit.start + token_edit.deleteCount + 1
  320. end
  321. vim.list_extend(tokens, old_tokens, idx)
  322. else
  323. tokens = response.data
  324. end
  325. -- convert token list to highlight ranges
  326. -- this could yield and run over multiple event loop iterations
  327. local highlights = tokens_to_ranges(tokens, self.bufnr, client, state.active_request)
  328. -- reset active request
  329. state.active_request = {}
  330. -- update the state with the new results
  331. local current_result = state.current_result
  332. current_result.version = version
  333. current_result.result_id = response.resultId
  334. current_result.tokens = tokens
  335. current_result.highlights = highlights
  336. current_result.namespace_cleared = false
  337. -- redraw all windows displaying buffer (if still valid)
  338. if api.nvim_buf_is_valid(self.bufnr) then
  339. api.nvim__redraw({ buf = self.bufnr, valid = true })
  340. end
  341. end
  342. --- @param bufnr integer
  343. --- @param ns integer
  344. --- @param token STTokenRange
  345. --- @param hl_group string
  346. --- @param priority integer
  347. local function set_mark(bufnr, ns, token, hl_group, priority)
  348. vim.api.nvim_buf_set_extmark(bufnr, ns, token.line, token.start_col, {
  349. hl_group = hl_group,
  350. end_line = token.end_line,
  351. end_col = token.end_col,
  352. priority = priority,
  353. strict = false,
  354. })
  355. end
  356. --- @param lnum integer
  357. --- @param foldend integer?
  358. --- @return boolean, integer?
  359. local function check_fold(lnum, foldend)
  360. if foldend and lnum <= foldend then
  361. return true, foldend
  362. end
  363. local folded = vim.fn.foldclosed(lnum)
  364. if folded == -1 then
  365. return false, nil
  366. end
  367. return folded ~= lnum, vim.fn.foldclosedend(lnum)
  368. end
  369. --- on_win handler for the decoration provider (see |nvim_set_decoration_provider|)
  370. ---
  371. --- If there is a current result for the buffer and the version matches the
  372. --- current document version, then the tokens are valid and can be applied. As
  373. --- the buffer is drawn, this function will add extmark highlights for every
  374. --- token in the range of visible lines. Once a highlight has been added, it
  375. --- sticks around until the document changes and there's a new set of matching
  376. --- highlight tokens available.
  377. ---
  378. --- If this is the first time a buffer is being drawn with a new set of
  379. --- highlights for the current document version, the namespace is cleared to
  380. --- remove extmarks from the last version. It's done here instead of the response
  381. --- handler to avoid the "blink" that occurs due to the timing between the
  382. --- response handler and the actual redraw.
  383. ---
  384. ---@package
  385. ---@param topline integer
  386. ---@param botline integer
  387. function STHighlighter:on_win(topline, botline)
  388. for client_id, state in pairs(self.client_state) do
  389. local current_result = state.current_result
  390. if current_result.version == util.buf_versions[self.bufnr] then
  391. if not current_result.namespace_cleared then
  392. api.nvim_buf_clear_namespace(self.bufnr, state.namespace, 0, -1)
  393. current_result.namespace_cleared = true
  394. end
  395. -- We can't use ephemeral extmarks because the buffer updates are not in
  396. -- sync with the list of semantic tokens. There's a delay between the
  397. -- buffer changing and when the LSP server can respond with updated
  398. -- tokens, and we don't want to "blink" the token highlights while
  399. -- updates are in flight, and we don't want to use stale tokens because
  400. -- they likely won't line up right with the actual buffer.
  401. --
  402. -- Instead, we have to use normal extmarks that can attach to locations
  403. -- in the buffer and are persisted between redraws.
  404. --
  405. -- `strict = false` is necessary here for the 1% of cases where the
  406. -- current result doesn't actually match the buffer contents. Some
  407. -- LSP servers can respond with stale tokens on requests if they are
  408. -- still processing changes from a didChange notification.
  409. --
  410. -- LSP servers that do this _should_ follow up known stale responses
  411. -- with a refresh notification once they've finished processing the
  412. -- didChange notification, which would re-synchronize the tokens from
  413. -- our end.
  414. --
  415. -- The server I know of that does this is clangd when the preamble of
  416. -- a file changes and the token request is processed with a stale
  417. -- preamble while the new one is still being built. Once the preamble
  418. -- finishes, clangd sends a refresh request which lets the client
  419. -- re-synchronize the tokens.
  420. local function set_mark0(token, hl_group, delta)
  421. set_mark(
  422. self.bufnr,
  423. state.namespace,
  424. token,
  425. hl_group,
  426. vim.hl.priorities.semantic_tokens + delta
  427. )
  428. end
  429. local ft = vim.bo[self.bufnr].filetype
  430. local highlights = assert(current_result.highlights)
  431. local first = lower_bound(highlights, topline, 1, #highlights + 1)
  432. local last = upper_bound(highlights, botline, first, #highlights + 1) - 1
  433. --- @type boolean?, integer?
  434. local is_folded, foldend
  435. for i = first, last do
  436. local token = assert(highlights[i])
  437. is_folded, foldend = check_fold(token.line + 1, foldend)
  438. if not is_folded and not token.marked then
  439. set_mark0(token, string.format('@lsp.type.%s.%s', token.type, ft), 0)
  440. for modifier in pairs(token.modifiers) do
  441. set_mark0(token, string.format('@lsp.mod.%s.%s', modifier, ft), 1)
  442. set_mark0(token, string.format('@lsp.typemod.%s.%s.%s', token.type, modifier, ft), 2)
  443. end
  444. token.marked = true
  445. api.nvim_exec_autocmds('LspTokenUpdate', {
  446. buffer = self.bufnr,
  447. modeline = false,
  448. data = {
  449. token = token,
  450. client_id = client_id,
  451. },
  452. })
  453. end
  454. end
  455. end
  456. end
  457. end
  458. --- Reset the buffer's highlighting state and clears the extmark highlights.
  459. ---
  460. ---@package
  461. function STHighlighter:reset()
  462. for client_id, state in pairs(self.client_state) do
  463. api.nvim_buf_clear_namespace(self.bufnr, state.namespace, 0, -1)
  464. state.current_result = {}
  465. if state.active_request.request_id then
  466. local client = vim.lsp.get_client_by_id(client_id)
  467. assert(client)
  468. client:cancel_request(state.active_request.request_id)
  469. state.active_request = {}
  470. end
  471. end
  472. end
  473. --- Mark a client's results as dirty. This method will cancel any active
  474. --- requests to the server and pause new highlights from being added
  475. --- in the on_win callback. The rest of the current results are saved
  476. --- in case the server supports delta requests.
  477. ---
  478. ---@package
  479. ---@param client_id integer
  480. function STHighlighter:mark_dirty(client_id)
  481. local state = self.client_state[client_id]
  482. assert(state)
  483. -- if we clear the version from current_result, it'll cause the
  484. -- next request to be sent and will also pause new highlights
  485. -- from being added in on_win until a new result comes from
  486. -- the server
  487. if state.current_result then
  488. state.current_result.version = nil
  489. end
  490. if state.active_request.request_id then
  491. local client = vim.lsp.get_client_by_id(client_id)
  492. assert(client)
  493. client:cancel_request(state.active_request.request_id)
  494. state.active_request = {}
  495. end
  496. end
  497. ---@package
  498. function STHighlighter:on_change()
  499. self:reset_timer()
  500. if self.debounce > 0 then
  501. self.timer = vim.defer_fn(function()
  502. self:send_request()
  503. end, self.debounce)
  504. else
  505. self:send_request()
  506. end
  507. end
  508. ---@private
  509. function STHighlighter:reset_timer()
  510. local timer = self.timer
  511. if timer then
  512. self.timer = nil
  513. if not timer:is_closing() then
  514. timer:stop()
  515. timer:close()
  516. end
  517. end
  518. end
  519. local M = {}
  520. --- Start the semantic token highlighting engine for the given buffer with the
  521. --- given client. The client must already be attached to the buffer.
  522. ---
  523. --- NOTE: This is currently called automatically by |vim.lsp.buf_attach_client()|. To
  524. --- opt-out of semantic highlighting with a server that supports it, you can
  525. --- delete the semanticTokensProvider table from the {server_capabilities} of
  526. --- your client in your |LspAttach| callback or your configuration's
  527. --- `on_attach` callback:
  528. ---
  529. --- ```lua
  530. --- client.server_capabilities.semanticTokensProvider = nil
  531. --- ```
  532. ---
  533. ---@param bufnr (integer) Buffer number, or `0` for current buffer
  534. ---@param client_id (integer) The ID of the |vim.lsp.Client|
  535. ---@param opts? (table) Optional keyword arguments
  536. --- - debounce (integer, default: 200): Debounce token requests
  537. --- to the server by the given number in milliseconds
  538. function M.start(bufnr, client_id, opts)
  539. vim.validate('bufnr', bufnr, 'number')
  540. vim.validate('client_id', client_id, 'number')
  541. bufnr = vim._resolve_bufnr(bufnr)
  542. opts = opts or {}
  543. assert(
  544. (not opts.debounce or type(opts.debounce) == 'number'),
  545. 'opts.debounce must be a number with the debounce time in milliseconds'
  546. )
  547. local client = vim.lsp.get_client_by_id(client_id)
  548. if not client then
  549. vim.notify('[LSP] No client with id ' .. client_id, vim.log.levels.ERROR)
  550. return
  551. end
  552. if not vim.lsp.buf_is_attached(bufnr, client_id) then
  553. vim.notify(
  554. '[LSP] Client with id ' .. client_id .. ' not attached to buffer ' .. bufnr,
  555. vim.log.levels.WARN
  556. )
  557. return
  558. end
  559. if not vim.tbl_get(client.server_capabilities, 'semanticTokensProvider', 'full') then
  560. vim.notify('[LSP] Server does not support semantic tokens', vim.log.levels.WARN)
  561. return
  562. end
  563. local highlighter = STHighlighter.active[bufnr]
  564. if not highlighter then
  565. highlighter = STHighlighter:new(bufnr)
  566. highlighter.debounce = opts.debounce or 200
  567. else
  568. highlighter.debounce = math.max(highlighter.debounce, opts.debounce or 200)
  569. end
  570. highlighter:on_attach(client_id)
  571. highlighter:send_request()
  572. end
  573. --- Stop the semantic token highlighting engine for the given buffer with the
  574. --- given client.
  575. ---
  576. --- NOTE: This is automatically called by a |LspDetach| autocmd that is set up as part
  577. --- of `start()`, so you should only need this function to manually disengage the semantic
  578. --- token engine without fully detaching the LSP client from the buffer.
  579. ---
  580. ---@param bufnr (integer) Buffer number, or `0` for current buffer
  581. ---@param client_id (integer) The ID of the |vim.lsp.Client|
  582. function M.stop(bufnr, client_id)
  583. vim.validate('bufnr', bufnr, 'number')
  584. vim.validate('client_id', client_id, 'number')
  585. bufnr = vim._resolve_bufnr(bufnr)
  586. local highlighter = STHighlighter.active[bufnr]
  587. if not highlighter then
  588. return
  589. end
  590. highlighter:on_detach(client_id)
  591. if vim.tbl_isempty(highlighter.client_state) then
  592. highlighter:destroy()
  593. end
  594. end
  595. --- @nodoc
  596. --- @class STTokenRangeInspect : STTokenRange
  597. --- @field client_id integer
  598. --- Return the semantic token(s) at the given position.
  599. --- If called without arguments, returns the token under the cursor.
  600. ---
  601. ---@param bufnr integer|nil Buffer number (0 for current buffer, default)
  602. ---@param row integer|nil Position row (default cursor position)
  603. ---@param col integer|nil Position column (default cursor position)
  604. ---
  605. ---@return STTokenRangeInspect[]|nil (table|nil) List of tokens at position. Each token has
  606. --- the following fields:
  607. --- - line (integer) line number, 0-based
  608. --- - start_col (integer) start column, 0-based
  609. --- - end_line (integer) end line number, 0-based
  610. --- - end_col (integer) end column, 0-based
  611. --- - type (string) token type as string, e.g. "variable"
  612. --- - modifiers (table) token modifiers as a set. E.g., { static = true, readonly = true }
  613. --- - client_id (integer)
  614. function M.get_at_pos(bufnr, row, col)
  615. bufnr = vim._resolve_bufnr(bufnr)
  616. local highlighter = STHighlighter.active[bufnr]
  617. if not highlighter then
  618. return
  619. end
  620. if row == nil or col == nil then
  621. local cursor = api.nvim_win_get_cursor(0)
  622. row, col = cursor[1] - 1, cursor[2]
  623. end
  624. local position = { row, col, row, col }
  625. local tokens = {} --- @type STTokenRangeInspect[]
  626. for client_id, client in pairs(highlighter.client_state) do
  627. local highlights = client.current_result.highlights
  628. if highlights then
  629. local idx = lower_bound(highlights, row, 1, #highlights + 1)
  630. for i = idx, #highlights do
  631. local token = highlights[i]
  632. --- @cast token STTokenRangeInspect
  633. if token.line > row then
  634. break
  635. end
  636. if
  637. Range.contains({ token.line, token.start_col, token.end_line, token.end_col }, position)
  638. then
  639. token.client_id = client_id
  640. tokens[#tokens + 1] = token
  641. end
  642. end
  643. end
  644. end
  645. return tokens
  646. end
  647. --- Force a refresh of all semantic tokens
  648. ---
  649. --- Only has an effect if the buffer is currently active for semantic token
  650. --- highlighting (|vim.lsp.semantic_tokens.start()| has been called for it)
  651. ---
  652. ---@param bufnr (integer|nil) filter by buffer. All buffers if nil, current
  653. --- buffer if 0
  654. function M.force_refresh(bufnr)
  655. vim.validate('bufnr', bufnr, 'number', true)
  656. local buffers = bufnr == nil and vim.tbl_keys(STHighlighter.active)
  657. or { vim._resolve_bufnr(bufnr) }
  658. for _, buffer in ipairs(buffers) do
  659. local highlighter = STHighlighter.active[buffer]
  660. if highlighter then
  661. highlighter:reset()
  662. highlighter:send_request()
  663. end
  664. end
  665. end
  666. --- @class vim.lsp.semantic_tokens.highlight_token.Opts
  667. --- @inlinedoc
  668. ---
  669. --- Priority for the applied extmark.
  670. --- (Default: `vim.hl.priorities.semantic_tokens + 3`)
  671. --- @field priority? integer
  672. --- Highlight a semantic token.
  673. ---
  674. --- Apply an extmark with a given highlight group for a semantic token. The
  675. --- mark will be deleted by the semantic token engine when appropriate; for
  676. --- example, when the LSP sends updated tokens. This function is intended for
  677. --- use inside |LspTokenUpdate| callbacks.
  678. ---@param token (table) A semantic token, found as `args.data.token` in |LspTokenUpdate|
  679. ---@param bufnr (integer) The buffer to highlight, or `0` for current buffer
  680. ---@param client_id (integer) The ID of the |vim.lsp.Client|
  681. ---@param hl_group (string) Highlight group name
  682. ---@param opts? vim.lsp.semantic_tokens.highlight_token.Opts Optional parameters:
  683. function M.highlight_token(token, bufnr, client_id, hl_group, opts)
  684. bufnr = vim._resolve_bufnr(bufnr)
  685. local highlighter = STHighlighter.active[bufnr]
  686. if not highlighter then
  687. return
  688. end
  689. local state = highlighter.client_state[client_id]
  690. if not state then
  691. return
  692. end
  693. local priority = opts and opts.priority or vim.hl.priorities.semantic_tokens + 3
  694. set_mark(bufnr, state.namespace, token, hl_group, priority)
  695. end
  696. --- |lsp-handler| for the method `workspace/semanticTokens/refresh`
  697. ---
  698. --- Refresh requests are sent by the server to indicate a project-wide change
  699. --- that requires all tokens to be re-requested by the client. This handler will
  700. --- invalidate the current results of all buffers and automatically kick off a
  701. --- new request for buffers that are displayed in a window. For those that aren't, a
  702. --- the BufWinEnter event should take care of it next time it's displayed.
  703. function M._refresh(err, _, ctx)
  704. if err then
  705. return vim.NIL
  706. end
  707. for _, bufnr in ipairs(vim.lsp.get_buffers_by_client_id(ctx.client_id)) do
  708. local highlighter = STHighlighter.active[bufnr]
  709. if highlighter and highlighter.client_state[ctx.client_id] then
  710. highlighter:mark_dirty(ctx.client_id)
  711. if not vim.tbl_isempty(vim.fn.win_findbuf(bufnr)) then
  712. highlighter:send_request()
  713. end
  714. end
  715. end
  716. return vim.NIL
  717. end
  718. local namespace = api.nvim_create_namespace('nvim.lsp.semantic_tokens')
  719. api.nvim_set_decoration_provider(namespace, {
  720. on_win = function(_, _, bufnr, topline, botline)
  721. local highlighter = STHighlighter.active[bufnr]
  722. if highlighter then
  723. highlighter:on_win(topline, botline)
  724. end
  725. end,
  726. })
  727. --- for testing only! there is no guarantee of API stability with this!
  728. ---
  729. ---@private
  730. M.__STHighlighter = STHighlighter
  731. return M