@@ -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
11347llvm::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
0 commit comments