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

Conversation

DavidLoftus
Copy link
Contributor

This PR updates the HandleDocumentSymbol function to build a tree of symbols, rather than a flat list. Symbols found while traversing another symbols body are added as a child.

Adds a simple test showing functions within nested class.

@DavidLoftus DavidLoftus marked this pull request as ready for review February 9, 2025 23:22
@github-actions github-actions bot requested a review from dwblaikie February 9, 2025 23:23
Copy link
Contributor

@dwblaikie dwblaikie left a comment

Choose a reason for hiding this comment

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

General idea looks pretty good to me - just a few minor things.

}

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.

}

auto Collect() -> std::vector<clang::clangd::DocumentSymbol> {
// Shouldn't happen in a valid tree but may as well handle gracefully.
Copy link
Contributor

Choose a reason for hiding this comment

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

I /tend/ to be an assert-first, loosen if necessary later. In case there are broken things, the sooner we know about it the better, perhaps?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair, I've been reluctant to place too many CARBON_CHECKS within the language server though, as it often results in crash loops. But agree probably should move whatever error handling outside of the helper class so that it can perform more appropriate warnings, e.g. emitting a diagnostic.

class SymbolStore {
public:
// Adds a symbol with no children.
void AddSymbol(clang::clangd::DocumentSymbol symbol) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It doesn't look like the codebase is entirely consistent here, but it does say to always use late bound return types, and there are many examples of "-> void" in the codebase - so maybe we should go with that here?

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, initially I assumed void was the exception to that rule since clang-tidy doesn't warn about it, but later saw that auto Foo() -> void was still used.

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

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.

@dwblaikie dwblaikie enabled auto-merge February 10, 2025 23:53
auto-merge was automatically disabled February 11, 2025 21:35

Head branch was pushed to by a user without write access

@dwblaikie dwblaikie enabled auto-merge February 11, 2025 21:48
@dwblaikie dwblaikie added this pull request to the merge queue Feb 11, 2025
Merged via the queue into carbon-language:trunk with commit 0931c60 Feb 11, 2025
8 checks passed
@DavidLoftus DavidLoftus deleted the documentSymbol branch February 14, 2025 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants