diff --git a/clang-tools-extra/clangd/SemanticSelection.cpp b/clang-tools-extra/clangd/SemanticSelection.cpp --- a/clang-tools-extra/clangd/SemanticSelection.cpp +++ b/clang-tools-extra/clangd/SemanticSelection.cpp @@ -13,6 +13,8 @@ #include "SourceCode.h" #include "clang/AST/DeclBase.h" #include "clang/Basic/SourceLocation.h" +#include "clang/Tooling/Syntax/BuildTree.h" +#include "clang/Tooling/Syntax/Tree.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/Support/Error.h" @@ -28,19 +30,6 @@ } } -// Recursively collects FoldingRange from a symbol and its children. -void collectFoldingRanges(DocumentSymbol Symbol, - std::vector &Result) { - FoldingRange Range; - Range.startLine = Symbol.range.start.line; - Range.startCharacter = Symbol.range.start.character; - Range.endLine = Symbol.range.end.line; - Range.endCharacter = Symbol.range.end.character; - Result.push_back(Range); - for (const auto &Child : Symbol.children) - collectFoldingRanges(Child, Result); -} - } // namespace llvm::Expected getSemanticRanges(ParsedAST &AST, Position Pos) { @@ -100,19 +89,12 @@ // FIXME(kirillbobyrev): Collect comments, PP conditional regions, includes and // other code regions (e.g. public/private/protected sections of classes, // control flow statement bodies). -// Related issue: -// https://github.com/clangd/clangd/issues/310 +// Related issue: https://github.com/clangd/clangd/issues/310 llvm::Expected> getFoldingRanges(ParsedAST &AST) { - // FIXME(kirillbobyrev): getDocumentSymbols() is conveniently available but - // limited (e.g. doesn't yield blocks inside functions and provides ranges for - // nodes themselves instead of their contents which is less useful). Replace - // this with a more general RecursiveASTVisitor implementation instead. - auto DocumentSymbols = getDocumentSymbols(AST); - if (!DocumentSymbols) - return DocumentSymbols.takeError(); + syntax::Arena A(AST.getSourceManager(), AST.getLangOpts(), AST.getTokens()); + auto *SyntaxTree = + syntax::buildSyntaxTree(A, *AST.getASTContext().getTranslationUnitDecl()); std::vector Result; - for (const auto &Symbol : *DocumentSymbols) - collectFoldingRanges(Symbol, Result); return Result; }