Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Organize symbol hierarchy in LSP documentSymbol handler #4914

Merged
merged 7 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions toolchain/language_server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ cc_library(
hdrs = ["handle.h"],
deps = [
":context",
"//common:check",
"//toolchain/base:shared_value_stores",
"//toolchain/lex",
"//toolchain/lex:token_index",
"//toolchain/lex:token_kind",
"//toolchain/parse",
"//toolchain/parse:node_kind",
"//toolchain/parse:tree",
Expand Down
84 changes: 76 additions & 8 deletions toolchain/language_server/handle_document_symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "toolchain/base/shared_value_stores.h"
#include <optional>

#include "common/check.h"
#include "toolchain/language_server/handle.h"
#include "toolchain/lex/lex.h"
#include "toolchain/lex/token_index.h"
#include "toolchain/lex/token_kind.h"
#include "toolchain/parse/node_ids.h"
#include "toolchain/parse/node_kind.h"
#include "toolchain/parse/parse.h"
#include "toolchain/parse/tree_and_subtrees.h"
#include "toolchain/source/source_buffer.h"

namespace Carbon::LanguageServer {

Expand All @@ -35,6 +37,45 @@ static auto GetIdentifierName(const Parse::TreeAndSubtrees& tree_and_subtrees,
return std::nullopt;
}

// Helper class to collect all symbols during traversal of AST.
// Symbols are "open" when their signature has been declared but their body is
// still ongoing. New symbols are added to last open symbol, or top level list
// if no symbols are open.
class SymbolStore {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick overview class comment might be handy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

public:
// Adds a symbol with no children.
auto AddSymbol(clang::clangd::DocumentSymbol symbol) -> void {
if (open_symbols_.empty()) {
top_level_symbols_.push_back(std::move(symbol));
} 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_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output of HandleDocumentSymbol needs to be a std::vector since thats the llvm::json::Value compatible type, using SmallVector here would then require a copy into a regular vector so I use std::vector for the output.

llvm::SmallVector<clang::clangd::DocumentSymbol> open_symbols_;
};

auto HandleDocumentSymbol(
Context& context, const clang::clangd::DocumentSymbolParams& params,
llvm::function_ref<
Expand All @@ -49,11 +90,16 @@ auto HandleDocumentSymbol(
const auto& tree = tree_and_subtrees.tree();
const auto& tokens = tree.tokens();

std::vector<clang::clangd::DocumentSymbol> result;
SymbolStore symbols;
for (const auto& node_id : tree.postorder()) {
auto node_kind = tree.node_kind(node_id);
clang::clangd::SymbolKind symbol_kind;
switch (tree.node_kind(node_id)) {
bool is_leaf = false;
switch (node_kind) {
case Parse::NodeKind::FunctionDecl:
is_leaf = true;
symbol_kind = clang::clangd::SymbolKind::Function;
break;
case Parse::NodeKind::FunctionDefinitionStart:
symbol_kind = clang::clangd::SymbolKind::Function;
break;
Expand All @@ -64,9 +110,26 @@ auto HandleDocumentSymbol(
case Parse::NodeKind::NamedConstraintDefinitionStart:
symbol_kind = clang::clangd::SymbolKind::Interface;
break;
case Parse::NodeKind::ClassDecl:
is_leaf = true;
symbol_kind = clang::clangd::SymbolKind::Class;
break;
case Parse::NodeKind::ClassDefinitionStart:
symbol_kind = clang::clangd::SymbolKind::Class;
break;

case Parse::NodeKind::FunctionDefinition:
case Parse::NodeKind::NamedConstraintDefinition:
case Parse::NodeKind::InterfaceDefinition:
case Parse::NodeKind::ClassDefinition: {
if (symbols.HasOpenSymbol()) {
// Symbols definition has completed, pop it from stack and add to
// parent/root.
symbols.EndSymbol();
}
continue;
}

default:
continue;
}
Expand All @@ -83,10 +146,15 @@ auto HandleDocumentSymbol(
.selectionRange = {.start = pos, .end = pos},
};

result.push_back(symbol);
if (is_leaf) {
symbols.AddSymbol(std::move(symbol));
} else {
symbols.StartSymbol(std::move(symbol));
}
}
}
on_done(result);

on_done(symbols.Collect());
}

} // namespace Carbon::LanguageServer
113 changes: 113 additions & 0 deletions toolchain/language_server/testdata/document_symbol/nested.carbon
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/language_server/testdata/document_symbol/nested.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/language_server/testdata/document_symbol/nested.carbon

// --- STDIN
[[@LSP-NOTIFY:textDocument/didOpen:
"textDocument": {"uri": "file:/class.carbon", "languageId": "carbon",
"text": "class A {\n fn F();\n fn G() {}\n}\n"}
]]
[[@LSP-CALL:textDocument/documentSymbol:
"textDocument": {"uri": "file:/class.carbon"}
]]
[[@LSP-NOTIFY:exit]]

// --- AUTOUPDATE-SPLIT

// CHECK:STDOUT: Content-Length: 145{{\r}}
// CHECK:STDOUT: {{\r}}
// CHECK:STDOUT: {
// CHECK:STDOUT: "jsonrpc": "2.0",
// CHECK:STDOUT: "method": "textDocument/publishDiagnostics",
// CHECK:STDOUT: "params": {
// CHECK:STDOUT: "diagnostics": [],
// CHECK:STDOUT: "uri": "file:///class.carbon"
// CHECK:STDOUT: }
// CHECK:STDOUT: }Content-Length: 1494{{\r}}
// CHECK:STDOUT: {{\r}}
// CHECK:STDOUT: {
// CHECK:STDOUT: "id": 1,
// CHECK:STDOUT: "jsonrpc": "2.0",
// CHECK:STDOUT: "result": [
// CHECK:STDOUT: {
// CHECK:STDOUT: "children": [
// CHECK:STDOUT: {
// CHECK:STDOUT: "kind": 12,
// CHECK:STDOUT: "name": "F",
// CHECK:STDOUT: "range": {
// CHECK:STDOUT: "end": {
// CHECK:STDOUT: "character": 8,
// CHECK:STDOUT: "line": 1
// CHECK:STDOUT: },
// CHECK:STDOUT: "start": {
// CHECK:STDOUT: "character": 8,
// CHECK:STDOUT: "line": 1
// CHECK:STDOUT: }
// CHECK:STDOUT: },
// CHECK:STDOUT: "selectionRange": {
// CHECK:STDOUT: "end": {
// CHECK:STDOUT: "character": 8,
// CHECK:STDOUT: "line": 1
// CHECK:STDOUT: },
// CHECK:STDOUT: "start": {
// CHECK:STDOUT: "character": 8,
// CHECK:STDOUT: "line": 1
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT: },
// CHECK:STDOUT: {
// CHECK:STDOUT: "kind": 12,
// CHECK:STDOUT: "name": "G",
// CHECK:STDOUT: "range": {
// CHECK:STDOUT: "end": {
// CHECK:STDOUT: "character": 9,
// CHECK:STDOUT: "line": 2
// CHECK:STDOUT: },
// CHECK:STDOUT: "start": {
// CHECK:STDOUT: "character": 9,
// CHECK:STDOUT: "line": 2
// CHECK:STDOUT: }
// CHECK:STDOUT: },
// CHECK:STDOUT: "selectionRange": {
// CHECK:STDOUT: "end": {
// CHECK:STDOUT: "character": 9,
// CHECK:STDOUT: "line": 2
// CHECK:STDOUT: },
// CHECK:STDOUT: "start": {
// CHECK:STDOUT: "character": 9,
// CHECK:STDOUT: "line": 2
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT: ],
// CHECK:STDOUT: "kind": 5,
// CHECK:STDOUT: "name": "A",
// CHECK:STDOUT: "range": {
// CHECK:STDOUT: "end": {
// CHECK:STDOUT: "character": 8,
// CHECK:STDOUT: "line": 0
// CHECK:STDOUT: },
// CHECK:STDOUT: "start": {
// CHECK:STDOUT: "character": 8,
// CHECK:STDOUT: "line": 0
// CHECK:STDOUT: }
// CHECK:STDOUT: },
// CHECK:STDOUT: "selectionRange": {
// CHECK:STDOUT: "end": {
// CHECK:STDOUT: "character": 8,
// CHECK:STDOUT: "line": 0
// CHECK:STDOUT: },
// CHECK:STDOUT: "start": {
// CHECK:STDOUT: "character": 8,
// CHECK:STDOUT: "line": 0
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT: ]
// CHECK:STDOUT: }