Repository: carbon-language/carbon-lang
File: toolchain/language_server/handle_document_symbol.cpp (C++)

Code before:
                          Lex::TokenIndex end) -> clang::clangd::Range {
  auto start_line = tokens.GetLine(start);
  auto start_col = tokens.GetColumnNumber(start);
  auto [end_line, end_col] = tokens.GetEndLoc(end);

  return clang::clangd::Range{
      .start = {.line = start_line.index, .character = start_col - 1},
      .end = {.line = end_line.index, .character = end_col - 1},
  };
}

// Finds a spanning range for provided definition / declaration ast node.
// In the case of a definition start, will include the body aswell.
static auto GetSymbolRange(const Parse::TreeAndSubtrees& tree_and_subtrees,
                           const Parse::NodeId& ast_node)
    -> clang::clangd::Range {
  const auto& tokens = tree_and_subtrees.tree().tokens();

  // Left most node will always be first node in postorder traversal.
  auto start_node = *tree_and_subtrees.postorder(ast_node).begin();

  auto start_token = tree_and_subtrees.tree().node_token(start_node);
  auto end_token = tree_and_subtrees.tree().node_token(ast_node);
  if (tokens.GetKind(end_token).is_opening_symbol()) {
    // DefinitionStart nodes use a opening token, so find its closing token to
    // span entire class/function body.
    return GetTokenRange(tokens, start_token,
                         tokens.GetMatchedClosingToken(end_token));
  } else {
    return GetTokenRange(tokens, start_token, end_token);
  }
}

auto HandleDocumentSymbol(
    Context& context, const clang::clangd::DocumentSymbolParams& params,
    llvm::function_ref<
        void(llvm::Expected<std::vector<clang::clangd::DocumentSymbol>>)>
        on_done) -> void {
  auto* file = context.LookupFile(params.textDocument.uri.file());
  if (!file) {
    return;
  }

  const auto& tree_and_subtrees = file->tree_and_subtrees();
  const auto& tree = tree_and_subtrees.tree();
  const auto& tokens = tree.tokens();

  std::vector<clang::clangd::DocumentSymbol> result;
  for (const auto& node_id : tree.postorder()) {
    clang::clangd::SymbolKind symbol_kind;
    switch (tree.node_kind(node_id)) {

Review comment (suggestion):
```suggestion
    // DefinitionStart nodes use an opening token, so find its closing token to
    // span the entire class/function body.
```

Code after:
    -> clang::clangd::Range {
  auto start_line = tokens.GetLine(start);
  auto start_col = tokens.GetColumnNumber(start);
  auto [end_line, end_col] = tokens.GetEndLoc(end);

  return clang::clangd::Range{
      .start = {.line = start_line.index, .character = start_col - 1},
      .end = {.line = end_line.index, .character = end_col - 1},
  };
}

// Finds a spanning range for the provided definition / declaration ast node.
// In the case of a definition start, will include the body as well.
static auto GetSymbolRange(const Parse::TreeAndSubtrees& tree_and_subtrees,
                           const Parse::NodeId& ast_node)
    -> clang::clangd::Range {
  const auto& tokens = tree_and_subtrees.tree().tokens();

  // The left-most node will always be the first node in postorder traversal.
  auto start_node = *tree_and_subtrees.postorder(ast_node).begin();

  auto start_token = tree_and_subtrees.tree().node_token(start_node);
  auto end_token = tree_and_subtrees.tree().node_token(ast_node);
  if (tokens.GetKind(end_token).is_opening_symbol()) {
    // DefinitionStart nodes use an opening token, so find its closing token to
    // span the entire class/function body.
    return GetTokenRange(tokens, start_token,
                         tokens.GetMatchedClosingToken(end_token));
  } else {
    return GetTokenRange(tokens, start_token, end_token);
  }
}

auto HandleDocumentSymbol(
    Context& context, const clang::clangd::DocumentSymbolParams& params,
    llvm::function_ref<
        void(llvm::Expected<std::vector<clang::clangd::DocumentSymbol>>)>
        on_done) -> void {
  auto* file = context.LookupFile(params.textDocument.uri.file());
  if (!file) {
    return;
  }

  const auto& tree_and_subtrees = file->tree_and_subtrees();
  const auto& tree = tree_and_subtrees.tree();
  const auto& tokens = tree.tokens();

  std::vector<clang::clangd::DocumentSymbol> result;
  for (const auto& node_id : tree.postorder()) {
    clang::clangd::SymbolKind symbol_kind;
    switch (tree.node_kind(node_id)) {