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

Code before:
  return {
    ['start'] = { line = min_start or 0, character = 0 },
    ['end'] = { line = max_end or 0, character = 0 },
  }
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.
---
---@async
---@param response lsp.SemanticTokens|lsp.SemanticTokensDelta
---@private
function STHighlighter:process_response(response, client, version, range)
  local state = self.client_state[client.id]
  if not state then
    return
  end
  local request_idx = range or 'FULL'

  local request_version = state.active_requests[request_idx]
    and state.active_requests[request_idx].version

  -- ignore stale responses
  if request_version and version ~= request_version then
    return
  end

  if not api.nvim_buf_is_valid(self.bufnr) then
    return
  end

  -- 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
  local tokens ---@type integer[]
  local token_edits = response.edits
  if token_edits then
    table.sort(token_edits, function(a, b)
      return a.start < b.start
    end)

    tokens = {} --- @type integer[]
    local old_tokens = assert(state.current_result.tokens)
    local idx = 1

Review comment (suggestion):
I would create a new constant variable for `'FULL'` instead of hardcoding the string literal everywhere.

Code after:
--- Finally, a redraw command is issued to force nvim to redraw the screen to
--- pick up changed highlight tokens.
---
---@async
---@param response lsp.SemanticTokens|lsp.SemanticTokensDelta
---@param client vim.lsp.Client
---@param version integer
---@param range? lsp.Range
---@private
function STHighlighter:process_response(response, client, version, range)
  local state = self.client_state[client.id]
  if not state then
    return
  end
  local request_idx = range or FULL

  local request_version = state.active_requests[request_idx]
    and state.active_requests[request_idx].version

  -- ignore stale responses
  if request_version and version ~= request_version then
    return
  end

  if not api.nvim_buf_is_valid(self.bufnr) then
    return
  end

  -- 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
  local tokens ---@type integer[]
  local token_edits = response.edits
  if token_edits then
    table.sort(token_edits, function(a, b)
      return a.start < b.start
    end)

    tokens = {} --- @type integer[]
    local old_tokens = assert(state.current_result.tokens)
    local idx = 1
    for _, token_edit in ipairs(token_edits) do
      vim.list_extend(tokens, old_tokens, idx, token_edit.start)
      if token_edit.data then
        vim.list_extend(tokens, token_edit.data)
      end
      idx = token_edit.start + token_edit.deleteCount + 1
    end
    vim.list_extend(tokens, old_tokens, idx)
  else
    tokens = response.data
  end