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

Code before:

        local method = 'textDocument/semanticTokens/full'

        if hasRangeProvider then
          method = 'textDocument/semanticTokens/range'
          if range then
            params.range = range
          else
            -- If no range is provided, send requests for all visible ranges
            -- This should be made better/removed once we can record capability  for textDocument/semanticTokens/range
            -- only
            local visible_range = self:get_visible_range()
            self:send_request(visible_range)
            return
          end
        elseif hasEditProvider then
          if current_result.result_id then
            method = 'textDocument/semanticTokens/full/delta'
            params.previousResultId = current_result.result_id
          end
        elseif not hasFullOnlyProvider then
          -- No suitable provider, skip this client
          return
        end

        ---@cast method vim.lsp.protocol.Method.ClientToServer.Request
        ---@param response? lsp.SemanticTokens|lsp.SemanticTokensDelta
        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)
          local bufnr = assert(ctx.bufnr)
          local highlighter = STHighlighter.active[bufnr]
          if not (c and highlighter) then
            return
          end

          if err or not response then
            highlighter.client_state[c.id].active_requests[range or 'FULL'] = {}
            return
          end

          coroutine.wrap(STHighlighter.process_response)(highlighter, response, c, version)
        end, self.bufnr)

        if success then
          active_requests[range or 'FULL'] = { request_id = request_id, version = version }
        end
      end
    end
  end

Review comment (suggestion):
Nit: To avoid the cast here I think you can type `method` in its declaration.

Code after:
          end
        elseif not client:supports_method('textDocument/semanticTokens/full', self.bufnr) then
          -- No suitable provider, skip this client
          return
        end

        ---@param response? lsp.SemanticTokens|lsp.SemanticTokensDelta
        local success, request_id = client:request(method, params, function(err, response, ctx)
          local bufnr = assert(ctx.bufnr)
          local highlighter = STHighlighter.active[bufnr]
          if not highlighter then
            return
          end

          if err or not response then
            highlighter.client_state[client.id].active_requests[range or FULL] = {}
            return
          end

          coroutine.wrap(STHighlighter.process_response)(highlighter, response, client, version)
        end, self.bufnr)

        if success then
          active_requests[range or FULL] = { request_id = request_id, version = version }
        end
      end
    end
  end
end

--- Gets a range that encompasses all visible lines across all windows
--- @private
--- @return lsp.Range
function STHighlighter:get_visible_range()
  local wins = vim.fn.win_findbuf(self.bufnr)
  local min_start, max_end = nil, nil

  for _, win in ipairs(wins) do
    local wininfo = vim.fn.getwininfo(win)[1]
    if wininfo then
      local start_line = wininfo.topline - 1
      local end_line = wininfo.botline
      if not min_start or start_line < min_start then
        min_start = start_line
      end
      if not max_end or end_line > max_end then
        max_end = end_line
      end
    end
  end