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

Code before:

  // Starts a symbol potentially with children.
  void StartSymbol(clang::clangd::DocumentSymbol symbol) {
    open_symbols_.push_back(std::move(symbol));
  }

  auto HasOpenSymbol() const -> bool { return !open_symbols_.empty(); }

  // Completes a symbol, appending to parent list.
  void EndSymbol() {
    CARBON_CHECK(HasOpenSymbol());
    AddSymbol(open_symbols_.pop_back_val());
  }

  auto Collect() -> std::vector<clang::clangd::DocumentSymbol> {
    // Shouldn't happen in a valid tree but may as well handle gracefully.
    while (!open_symbols_.empty()) {
      EndSymbol();
    }

    return std::move(top_level_symbols_);
  }

 private:
  std::vector<clang::clangd::DocumentSymbol> top_level_symbols_;
  llvm::SmallVector<clang::clangd::DocumentSymbol> open_symbols_;
};

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();

  SymbolStore symbols;
  for (const auto& node_id : tree.postorder()) {
    auto node_kind = tree.node_kind(node_id);
    clang::clangd::SymbolKind symbol_kind;
    bool is_leaf = false;
    switch (node_kind) {
      case Parse::NodeKind::FunctionDecl:
        is_leaf = true;
        symbol_kind = clang::clangd::SymbolKind::Function;

Review comment (suggestion):
Should this be a SmallVector too? Slightly curious to see a std::vector next to a SmallVector (would usually expect one or the other to be used consistently - is there some difference you're relying on/this patch benefits from in some way?)

Code after:
    } else {
      open_symbols_.back().children.push_back(symbol);
    }
  }

  // Starts a symbol potentially with children.
  auto StartSymbol(clang::clangd::DocumentSymbol symbol) -> void {
    open_symbols_.push_back(std::move(symbol));
  }

  auto HasOpenSymbol() const -> bool { return !open_symbols_.empty(); }

  // Completes a symbol, appending to parent list.
  auto EndSymbol() -> void {
    CARBON_CHECK(HasOpenSymbol());
    AddSymbol(open_symbols_.pop_back_val());
  }

  // Returns final top level symbols.
  auto Collect() -> std::vector<clang::clangd::DocumentSymbol> {
    CARBON_CHECK(!HasOpenSymbol());
    return std::move(top_level_symbols_);
  }

 private:
  std::vector<clang::clangd::DocumentSymbol> top_level_symbols_;
  llvm::SmallVector<clang::clangd::DocumentSymbol> open_symbols_;
};

// Constructs a Range from a closed interval of tokens [start, end].
static auto GetTokenRange(const Lex::TokenizedBuffer& tokens,
                          Lex::TokenIndex start, 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 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.