diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp --- a/clang-tools-extra/clangd/ClangdLSPServer.cpp +++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -939,7 +939,8 @@ void ClangdLSPServer::onFoldingRange( const FoldingRangeParams &Params, Callback> Reply) { - Server->foldingRanges(Params.textDocument.uri.file(), std::move(Reply)); + Server->foldingRanges(Params.textDocument.uri.file(), /*UseAST=*/true, + std::move(Reply)); } static llvm::Optional asCommand(const CodeAction &Action) { diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h --- a/clang-tools-extra/clangd/ClangdServer.h +++ b/clang-tools-extra/clangd/ClangdServer.h @@ -281,7 +281,9 @@ Callback> CB); /// Retrieve ranges that can be used to fold code within the specified file. - void foldingRanges(StringRef File, Callback> CB); + /// Uses the pseudo parser to compute the ranges when `UseAST` is false. + void foldingRanges(StringRef File, bool UseAST, + Callback> CB); /// Retrieve implementations for virtual method. void findImplementations(PathRef File, Position Pos, diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -832,16 +832,28 @@ Transient); } -void ClangdServer::foldingRanges(llvm::StringRef File, +void ClangdServer::foldingRanges(llvm::StringRef File, bool UseAST, Callback> CB) { - auto Action = - [CB = std::move(CB)](llvm::Expected InpAST) mutable { - if (!InpAST) - return CB(InpAST.takeError()); - CB(clangd::getFoldingRanges(InpAST->AST)); - }; - WorkScheduler->runWithAST("FoldingRanges", File, std::move(Action), - Transient); + if (UseAST) { + auto Action = + [CB = std::move(CB)](llvm::Expected InpAST) mutable { + if (!InpAST) + return CB(InpAST.takeError()); + CB(clangd::getFoldingRanges(InpAST->AST)); + }; + WorkScheduler->runWithAST("FoldingRanges", File, std::move(Action), + Transient); + } else { + auto Code = getDraft(File); + if (!Code) + return CB(llvm::make_error( + "trying to compute folding ranges for non-added document", + ErrorCode::InvalidParams)); + auto Action = [CB = std::move(CB), Code = std::move(*Code)]() mutable { + CB(clangd::getFoldingRanges(Code)); + }; + WorkScheduler->runQuick("FoldingRangesWithoutAST", File, std::move(Action)); + } } void ClangdServer::findType(llvm::StringRef File, Position Pos,