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

Code before:
    return
  end

  local position_params = util.make_position_params(0, client.offset_encoding)

  ---@type lsp.SelectionRangeParams
  local params = {
    textDocument = position_params.textDocument,
    positions = { position_params.position },
  }

  timeout_ms = timeout_ms or 1000
  local result, err = lsp.buf_request_sync(0, method, params, timeout_ms)
  if err then
    lsp.log.error('selectionRange request failed: ' .. err)
    return
  end
  if not result or not result[client.id] or not result[client.id].result then
    return
  end
  if result[client.id].error then
    lsp.log.error(result[client.id].error.code, result[client.id].error.code)
  end

  -- We only requested one range, thus we get the first and only reponse here.
  local response = result[client.id].result[1] ---@type lsp.SelectionRange
  local ranges = {} ---@type lsp.Range[]
  local lines = api.nvim_buf_get_lines(0, 0, -1, false)

  -- Populate the list of ranges from the given request.
  while response do
    local range = response.range
    if not is_empty(range) then
      local start_line = range.start.line
      local end_line = range['end'].line
      range.start.character = vim.str_byteindex(
        lines[start_line + 1] or '',
        client.offset_encoding,
        range.start.character,
        false
      )
      range['end'].character = vim.str_byteindex(
        lines[end_line + 1] or '',
        client.offset_encoding,
        range['end'].character,
        false
      )
      ranges[#ranges + 1] = range
    end
    response = response.parent
  end

Review comment (suggestion):
```suggestion
  local response = assert(result[client.id].result[1]) ---@type lsp.SelectionRange
```

Code after:
  end

  local position_params = util.make_position_params(0, client.offset_encoding)

  ---@type lsp.SelectionRangeParams
  local params = {
    textDocument = position_params.textDocument,
    positions = { position_params.position },
  }

  timeout_ms = timeout_ms or 1000
  local result, err = lsp.buf_request_sync(0, method, params, timeout_ms)
  if err then
    lsp.log.error('selectionRange request failed: ' .. err)
    return
  end
  if not result or not result[client.id] or not result[client.id].result then
    return
  end
  if result[client.id].error then
    lsp.log.error(result[client.id].error.code, result[client.id].error.message)
  end

  -- We only requested one range, thus we get the first and only reponse here.
  local response = assert(result[client.id].result[1]) ---@type lsp.SelectionRange
  local ranges = {} ---@type lsp.Range[]
  local lines = api.nvim_buf_get_lines(0, 0, -1, false)

  -- Populate the list of ranges from the given request.
  while response do
    local range = response.range
    if not is_empty(range) then
      local start_line = range.start.line
      local end_line = range['end'].line
      range.start.character = vim.str_byteindex(
        lines[start_line + 1] or '',
        client.offset_encoding,
        range.start.character,
        false
      )
      range['end'].character = vim.str_byteindex(
        lines[end_line + 1] or '',
        client.offset_encoding,
        range['end'].character,
        false
      )
      ranges[#ranges + 1] = range
    end
    response = response.parent
  end