[PATCH] D158964: [clangd] support folding multi-line `#include`

Vincent Hong via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 27 23:23:31 PDT 2023


v1nh1shungry created this revision.
v1nh1shungry added a reviewer: sammccall.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
v1nh1shungry 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/D158964

Files:
  clang-tools-extra/clangd/SemanticSelection.cpp
  clang-tools-extra/clangd/unittests/SemanticSelectionTests.cpp


Index: clang-tools-extra/clangd/unittests/SemanticSelectionTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/SemanticSelectionTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticSelectionTests.cpp
@@ -453,6 +453,31 @@
         << Test;
   }
 }
+
+TEST(FoldingRanges, Includes) {
+  const char *Test = R"cpp(
+    [[#include <cmath>
+    #include <cstdio>
+    #include <iostream>]]
+
+    #include <vector>
+
+    [[#include "external/Logger.h"
+    #include "external/Vector.h"
+    #include "project/DataStructures/Trie.h"]]
+
+    [[#include "project/File.h"
+    #include <map>
+    #include <set>]]
+
+    #include "math.h"
+  )cpp";
+  auto T = Annotations{Test};
+  EXPECT_THAT(gatherFoldingRanges(
+                  llvm::cantFail(getFoldingRanges(T.code().str(), false))),
+              UnorderedElementsAreArray(T.ranges()))
+      << Test;
+}
 } // namespace
 } // namespace clangd
 } // namespace clang
Index: clang-tools-extra/clangd/SemanticSelection.cpp
===================================================================
--- clang-tools-extra/clangd/SemanticSelection.cpp
+++ clang-tools-extra/clangd/SemanticSelection.cpp
@@ -175,7 +175,32 @@
   return collectFoldingRanges(SyntaxTree, TM);
 }
 
-// FIXME( usaxena95): Collect PP conditional regions, includes and other code
+namespace {
+struct IncludeCollector {
+  explicit IncludeCollector(const pseudo::DirectiveTree &T) { walk(T); }
+
+  void walk(const pseudo::DirectiveTree &T) {
+    for (const auto &C : T.Chunks)
+      std::visit(*this, C);
+  }
+
+  void operator()(const pseudo::DirectiveTree::Code &) {}
+
+  void operator()(const pseudo::DirectiveTree::Directive &D) {
+    if (D.Kind == tok::pp_include)
+      Results.push_back(D);
+  }
+
+  void operator()(const pseudo::DirectiveTree::Conditional &C) {
+    if (C.Taken)
+      walk(C.Branches[*C.Taken].second);
+  }
+
+  std::vector<pseudo::DirectiveTree::Directive> Results;
+};
+} // namespace
+
+// FIXME( usaxena95): Collect PP conditional regions 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
@@ -267,6 +292,20 @@
     }
     AddFoldingRange(Start, End, FoldingRange::COMMENT_KIND);
   }
+  // Multi-line `#include`
+  auto Includes = IncludeCollector{DirectiveStructure}.Results;
+  for (auto It = Includes.begin(); It != Includes.end();) {
+    Position Start = StartPosition(OrigStream.tokens(It->Tokens).front());
+    Position End = EndPosition(OrigStream.tokens(It->Tokens).back());
+    It++;
+    while (It != Includes.end() &&
+           StartPosition(OrigStream.tokens(It->Tokens).front()).line ==
+               End.line + 1) {
+      End = EndPosition(OrigStream.tokens(It->Tokens).back());
+      It++;
+    }
+    AddFoldingRange(Start, End, FoldingRange::IMPORT_KIND);
+  }
   return Result;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158964.553844.patch
Type: text/x-patch
Size: 2964 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230828/adbe8dbb/attachment.bin>


More information about the cfe-commits mailing list