[PATCH] D130011: Use pseudoparser-based folding ranges in ClangdServer.

Utkarsh Saxena via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 18 07:30:31 PDT 2022


usaxena95 created this revision.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
usaxena95 requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D130011

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/ClangdServer.h


Index: clang-tools-extra/clangd/ClangdServer.h
===================================================================
--- clang-tools-extra/clangd/ClangdServer.h
+++ clang-tools-extra/clangd/ClangdServer.h
@@ -281,7 +281,9 @@
                        Callback<std::vector<DocumentSymbol>> CB);
 
   /// Retrieve ranges that can be used to fold code within the specified file.
-  void foldingRanges(StringRef File, Callback<std::vector<FoldingRange>> CB);
+  /// Uses the pseudo parser to compute the ranges when `UseAST` is false.
+  void foldingRanges(StringRef File, bool UseAST,
+                     Callback<std::vector<FoldingRange>> CB);
 
   /// Retrieve implementations for virtual method.
   void findImplementations(PathRef File, Position Pos,
Index: clang-tools-extra/clangd/ClangdServer.cpp
===================================================================
--- clang-tools-extra/clangd/ClangdServer.cpp
+++ 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<std::vector<FoldingRange>> CB) {
-  auto Action =
-      [CB = std::move(CB)](llvm::Expected<InputsAndAST> 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<InputsAndAST> 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<LSPError>(
+          "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,
Index: clang-tools-extra/clangd/ClangdLSPServer.cpp
===================================================================
--- clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -939,7 +939,8 @@
 void ClangdLSPServer::onFoldingRange(
     const FoldingRangeParams &Params,
     Callback<std::vector<FoldingRange>> Reply) {
-  Server->foldingRanges(Params.textDocument.uri.file(), std::move(Reply));
+  Server->foldingRanges(Params.textDocument.uri.file(), /*UseAST=*/true,
+                        std::move(Reply));
 }
 
 static llvm::Optional<Command> asCommand(const CodeAction &Action) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130011.445492.patch
Type: text/x-patch
Size: 3057 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220718/18f7d1fc/attachment.bin>


More information about the cfe-commits mailing list