Skip to content

Commit 4e94198

Browse files
authored
[clangd] Remove the unused AST-based code folding Implementation. (#166189)
In clangd, we use the non-ast version one.
1 parent 31c03c9 commit 4e94198

File tree

3 files changed

+0
-152
lines changed

3 files changed

+0
-152
lines changed

clang-tools-extra/clangd/SemanticSelection.cpp

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -42,72 +42,6 @@ void addIfDistinct(const Range &R, std::vector<Range> &Result) {
4242
}
4343
}
4444

45-
std::optional<FoldingRange> toFoldingRange(SourceRange SR,
46-
const SourceManager &SM) {
47-
const auto Begin = SM.getDecomposedLoc(SR.getBegin()),
48-
End = SM.getDecomposedLoc(SR.getEnd());
49-
// Do not produce folding ranges if either range ends is not within the main
50-
// file. Macros have their own FileID so this also checks if locations are not
51-
// within the macros.
52-
if ((Begin.first != SM.getMainFileID()) || (End.first != SM.getMainFileID()))
53-
return std::nullopt;
54-
FoldingRange Range;
55-
Range.startCharacter = SM.getColumnNumber(Begin.first, Begin.second) - 1;
56-
Range.startLine = SM.getLineNumber(Begin.first, Begin.second) - 1;
57-
Range.endCharacter = SM.getColumnNumber(End.first, End.second) - 1;
58-
Range.endLine = SM.getLineNumber(End.first, End.second) - 1;
59-
return Range;
60-
}
61-
62-
std::optional<FoldingRange>
63-
extractFoldingRange(const syntax::Node *Node,
64-
const syntax::TokenBufferTokenManager &TM) {
65-
if (const auto *Stmt = dyn_cast<syntax::CompoundStatement>(Node)) {
66-
const auto *LBrace = cast_or_null<syntax::Leaf>(
67-
Stmt->findChild(syntax::NodeRole::OpenParen));
68-
// FIXME(kirillbobyrev): This should find the last child. Compound
69-
// statements have only one pair of braces so this is valid but for other
70-
// node kinds it might not be correct.
71-
const auto *RBrace = cast_or_null<syntax::Leaf>(
72-
Stmt->findChild(syntax::NodeRole::CloseParen));
73-
if (!LBrace || !RBrace)
74-
return std::nullopt;
75-
// Fold the entire range within braces, including whitespace.
76-
const SourceLocation LBraceLocInfo =
77-
TM.getToken(LBrace->getTokenKey())->endLocation(),
78-
RBraceLocInfo =
79-
TM.getToken(RBrace->getTokenKey())->location();
80-
auto Range = toFoldingRange(SourceRange(LBraceLocInfo, RBraceLocInfo),
81-
TM.sourceManager());
82-
// Do not generate folding range for compound statements without any
83-
// nodes and newlines.
84-
if (Range && Range->startLine != Range->endLine)
85-
return Range;
86-
}
87-
return std::nullopt;
88-
}
89-
90-
// Traverse the tree and collect folding ranges along the way.
91-
std::vector<FoldingRange>
92-
collectFoldingRanges(const syntax::Node *Root,
93-
const syntax::TokenBufferTokenManager &TM) {
94-
std::queue<const syntax::Node *> Nodes;
95-
Nodes.push(Root);
96-
std::vector<FoldingRange> Result;
97-
while (!Nodes.empty()) {
98-
const syntax::Node *Node = Nodes.front();
99-
Nodes.pop();
100-
const auto Range = extractFoldingRange(Node, TM);
101-
if (Range)
102-
Result.push_back(*Range);
103-
if (const auto *T = dyn_cast<syntax::Tree>(Node))
104-
for (const auto *NextNode = T->getFirstChild(); NextNode;
105-
NextNode = NextNode->getNextSibling())
106-
Nodes.push(NextNode);
107-
}
108-
return Result;
109-
}
110-
11145
} // namespace
11246

11347
llvm::Expected<SelectionRange> getSemanticRanges(ParsedAST &AST, Position Pos) {
@@ -230,18 +164,6 @@ class PragmaRegionFinder {
230164
}
231165
};
232166

233-
// FIXME(kirillbobyrev): Collect comments, PP conditional regions, includes and
234-
// other code regions (e.g. public/private/protected sections of classes,
235-
// control flow statement bodies).
236-
// Related issue: https://github.com/clangd/clangd/issues/310
237-
llvm::Expected<std::vector<FoldingRange>> getFoldingRanges(ParsedAST &AST) {
238-
syntax::Arena A;
239-
syntax::TokenBufferTokenManager TM(AST.getTokens(), AST.getLangOpts(),
240-
AST.getSourceManager());
241-
const auto *SyntaxTree = syntax::buildSyntaxTree(A, TM, AST.getASTContext());
242-
return collectFoldingRanges(SyntaxTree, TM);
243-
}
244-
245167
// FIXME( usaxena95): Collect includes and other code regions (e.g.
246168
// public/private/protected sections of classes, control flow statement bodies).
247169
// Related issue: https://github.com/clangd/clangd/issues/310

clang-tools-extra/clangd/SemanticSelection.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ namespace clangd {
2626
/// If pos is not in any interesting range, return [Pos, Pos).
2727
llvm::Expected<SelectionRange> getSemanticRanges(ParsedAST &AST, Position Pos);
2828

29-
/// Returns a list of ranges whose contents might be collapsible in an editor.
30-
/// This should include large scopes, preprocessor blocks etc.
31-
llvm::Expected<std::vector<FoldingRange>> getFoldingRanges(ParsedAST &AST);
32-
3329
/// Returns a list of ranges whose contents might be collapsible in an editor.
3430
/// This version uses the pseudoparser which does not require the AST.
3531
llvm::Expected<std::vector<FoldingRange>>

clang-tools-extra/clangd/unittests/SemanticSelectionTests.cpp

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -196,76 +196,6 @@ TEST(SemanticSelection, RunViaClangdServer) {
196196
ElementsAre(SourceAnnotations.range("empty")));
197197
}
198198

199-
TEST(FoldingRanges, ASTAll) {
200-
const char *Tests[] = {
201-
R"cpp(
202-
#define FOO int foo() {\
203-
int Variable = 42; \
204-
return 0; \
205-
}
206-
207-
// Do not generate folding range for braces within macro expansion.
208-
FOO
209-
210-
// Do not generate folding range within macro arguments.
211-
#define FUNCTOR(functor) functor
212-
void func() {[[
213-
FUNCTOR([](){});
214-
]]}
215-
216-
// Do not generate folding range with a brace coming from macro.
217-
#define LBRACE {
218-
void bar() LBRACE
219-
int X = 42;
220-
}
221-
)cpp",
222-
R"cpp(
223-
void func() {[[
224-
int Variable = 100;
225-
226-
if (Variable > 5) {[[
227-
Variable += 42;
228-
]]} else if (Variable++)
229-
++Variable;
230-
else {[[
231-
Variable--;
232-
]]}
233-
234-
// Do not generate FoldingRange for empty CompoundStmts.
235-
for (;;) {}
236-
237-
// If there are newlines between {}, we should generate one.
238-
for (;;) {[[
239-
240-
]]}
241-
]]}
242-
)cpp",
243-
R"cpp(
244-
class Foo {
245-
public:
246-
Foo() {[[
247-
int X = 1;
248-
]]}
249-
250-
private:
251-
int getBar() {[[
252-
return 42;
253-
]]}
254-
255-
// Braces are located at the same line: no folding range here.
256-
void getFooBar() { }
257-
};
258-
)cpp",
259-
};
260-
for (const char *Test : Tests) {
261-
auto T = Annotations(Test);
262-
auto AST = TestTU::withCode(T.code()).build();
263-
EXPECT_THAT(gatherFoldingRanges(llvm::cantFail(getFoldingRanges(AST))),
264-
UnorderedElementsAreArray(T.ranges()))
265-
<< Test;
266-
}
267-
}
268-
269199
TEST(FoldingRanges, PseudoParserWithoutLineFoldings) {
270200
const char *Tests[] = {
271201
R"cpp(

0 commit comments

Comments
 (0)