-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clangd] Implement fold range for #pragma region #168177
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
Open
hokein
wants to merge
1
commit into
llvm:main
Choose a base branch
from
hokein:fold-region
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+90
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,9 +11,13 @@ | |
| #include "Protocol.h" | ||
| #include "Selection.h" | ||
| #include "SourceCode.h" | ||
| #include "support/Bracket.h" | ||
| #include "support/DirectiveTree.h" | ||
| #include "support/Token.h" | ||
| #include "clang/AST/DeclBase.h" | ||
| #include "clang/Basic/SourceLocation.h" | ||
| #include "clang/Basic/SourceManager.h" | ||
| #include "clang/Basic/TokenKinds.h" | ||
| #include "clang/Tooling/Syntax/BuildTree.h" | ||
| #include "clang/Tooling/Syntax/Nodes.h" | ||
| #include "clang/Tooling/Syntax/TokenBufferTokenManager.h" | ||
|
|
@@ -22,9 +26,6 @@ | |
| #include "llvm/ADT/StringRef.h" | ||
| #include "llvm/Support/Casting.h" | ||
| #include "llvm/Support/Error.h" | ||
| #include "support/Bracket.h" | ||
| #include "support/DirectiveTree.h" | ||
| #include "support/Token.h" | ||
| #include <optional> | ||
| #include <queue> | ||
| #include <vector> | ||
|
|
@@ -163,6 +164,66 @@ llvm::Expected<SelectionRange> getSemanticRanges(ParsedAST &AST, Position Pos) { | |
| return std::move(Head); | ||
| } | ||
|
|
||
| class PragmaRegionFinder { | ||
| // Record the token range of a region: | ||
| // | ||
| // #pragma region [[name | ||
| // ... | ||
| // ]]#pragma region | ||
| std::vector<Token::Range> &Ranges; | ||
| const TokenStream &Code; | ||
| // Stack of starting token (the name of the region) indices for nested #pragma | ||
| // region. | ||
| std::vector<unsigned> Stack; | ||
|
|
||
| public: | ||
| PragmaRegionFinder(std::vector<Token::Range> &Ranges, const TokenStream &Code) | ||
| : Ranges(Ranges), Code(Code) {} | ||
|
|
||
| void walk(const DirectiveTree &T) { | ||
| for (const auto &C : T.Chunks) | ||
| std::visit(*this, C); | ||
| } | ||
|
|
||
| void operator()(const DirectiveTree::Code &C) {} | ||
|
|
||
| void operator()(const DirectiveTree::Directive &D) { | ||
| // Get the tokens that make up this directive. | ||
| auto Tokens = Code.tokens(D.Tokens); | ||
| if (Tokens.empty()) | ||
| return; | ||
| const Token &HashToken = Tokens.front(); | ||
| assert(HashToken.Kind == tok::hash); | ||
| const Token &Pragma = HashToken.nextNC(); | ||
| if (Pragma.text() != "pragma") | ||
| return; | ||
| const Token &Value = Pragma.nextNC(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there can be a comment between |
||
|
|
||
| // Handle "#pragma region name" | ||
| if (Value.text() == "region") { | ||
| // Record the name token. | ||
| if (&Value < Tokens.end()) | ||
| Stack.push_back((&Value + 1)->OriginalIndex); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... there could also be one between |
||
| return; | ||
| } | ||
|
|
||
| // Handle "#pragma endregion" | ||
| if (Value.text() == "endregion") { | ||
| if (Stack.empty()) | ||
| return; // unmatched end region; ignore. | ||
|
|
||
| unsigned StartIdx = Stack.back(); | ||
| Stack.pop_back(); | ||
| Ranges.push_back(Token::Range{StartIdx, HashToken.OriginalIndex}); | ||
| } | ||
| } | ||
|
|
||
| void operator()(const DirectiveTree::Conditional &C) { | ||
| for (const auto &[_, SubTree] : C.Branches) | ||
| walk(SubTree); | ||
| } | ||
| }; | ||
|
|
||
| // FIXME(kirillbobyrev): Collect comments, PP conditional regions, includes and | ||
| // other code regions (e.g. public/private/protected sections of classes, | ||
| // control flow statement bodies). | ||
|
|
@@ -286,6 +347,17 @@ getFoldingRanges(const std::string &Code, bool LineFoldingOnly) { | |
| } | ||
| AddFoldingRange(Start, End, FoldingRange::COMMENT_KIND); | ||
| } | ||
|
|
||
| // #pragma region | ||
| std::vector<Token::Range> Ranges; | ||
| PragmaRegionFinder(Ranges, OrigStream).walk(DirectiveStructure); | ||
| auto Ts = OrigStream.tokens(); | ||
| for (const auto &R : Ranges) { | ||
| auto End = StartPosition(Ts[R.End]); | ||
| if (LineFoldingOnly) | ||
| End.line--; | ||
| AddFoldingRange(EndPosition(Ts[R.Begin]), End, FoldingRange::REGION_KIND); | ||
| } | ||
| return Result; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
endregion?