Repository: neovim/neovim
File: runtime/lua/vim/lsp/semantic_tokens.lua (Lua)

Code before:
---
--- 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.
---
---@package
---@param range? lsp.Range
function STHighlighter:send_request(range)
  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)
    if client then
      local current_result = state.current_result
      local active_requests = state.active_requests

      -- Capability checks
      local hasRangeProvider =
        vim.tbl_get(client.server_capabilities, 'semanticTokensProvider', 'range')
      local hasEditProvider =
        vim.tbl_get(client.server_capabilities, 'semanticTokensProvider', 'full', 'delta')
      local hasFullOnlyProvider = vim.tbl_get(
        client.server_capabilities,
        'semanticTokensProvider',
        'full'
      ) == true or vim.tbl_get(
        client.server_capabilities,
        'semanticTokensProvider',
        'full',
        'delta'
      ) == false

      local full_request_version = active_requests['FULL'] and active_requests['FULL'].version

      local new_version = current_result.version ~= version and full_request_version ~= version

      if (new_version or range) and not (range and not hasRangeProvider) then
        -- Cancel stale in-flight request
        if new_version then
          self:cancel_all_requests(client)
        end


Review comment (suggestion):
Since these capabilities support dynamic registration, we should use `client:supports_method` (which takes into account support added after the initialize request).

Code after:
    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.
---
---@package
---@param range? lsp.Range
function STHighlighter:send_request(range)
  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)
    if client then
      local current_result = state.current_result
      local active_requests = state.active_requests

      local full_request_version = active_requests[FULL] and active_requests[FULL].version

      local new_version = current_result.version ~= version and full_request_version ~= version

      if new_version or range then
        -- Cancel stale in-flight request
        if new_version then
          self:cancel_all_requests(client)
        end

        local params = { textDocument = util.make_text_document_params(self.bufnr) }

        ---@type vim.lsp.protocol.Method.ClientToServer.Request
        local method = 'textDocument/semanticTokens/full'

        if client:supports_method('textDocument/semanticTokens/range', self.bufnr) then
          method = 'textDocument/semanticTokens/range'
          if range then
            params.range = range
          else
            -- If no range is provided, send requests for all visible ranges