[clang-tools-extra] r366047 - [clangd] Added highlighting for members and methods.

Johan Vikstrom via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 15 01:12:22 PDT 2019


Author: jvikstrom
Date: Mon Jul 15 01:12:21 2019
New Revision: 366047

URL: http://llvm.org/viewvc/llvm-project?rev=366047&view=rev
Log:
[clangd] Added highlighting for members and methods.

Summary: Added highlighting for members and methods.

Reviewers: hokein, sammccall, ilya-biryukov

Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D64617

Modified:
    clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
    clang-tools-extra/trunk/clangd/SemanticHighlighting.h
    clang-tools-extra/trunk/clangd/test/semantic-highlighting.test
    clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp

Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=366047&r1=366046&r2=366047&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original)
+++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Mon Jul 15 01:12:21 2019
@@ -40,6 +40,16 @@ public:
     return true;
   }
 
+  bool VisitMemberExpr(MemberExpr *ME) {
+    const auto *MD = ME->getMemberDecl();
+    if (isa<CXXDestructorDecl>(MD))
+      // When calling the destructor manually like: AAA::~A(); The ~ is a
+      // MemberExpr. Other methods should still be highlighted though.
+      return true;
+    addToken(ME->getMemberLoc(), MD);
+    return true;
+  }
+
   bool VisitNamedDecl(NamedDecl *ND) {
     // UsingDirectiveDecl's namespaces do not show up anywhere else in the
     // Visit/Traverse mehods. But they should also be highlighted as a
@@ -115,6 +125,14 @@ private:
       addToken(Loc, HighlightingKind::Class);
       return;
     }
+    if (isa<CXXMethodDecl>(D)) {
+      addToken(Loc, HighlightingKind::Method);
+      return;
+    }
+    if (isa<FieldDecl>(D)) {
+      addToken(Loc, HighlightingKind::Field);
+      return;
+    }
     if (isa<EnumDecl>(D)) {
       addToken(Loc, HighlightingKind::Enum);
       return;
@@ -247,8 +265,12 @@ llvm::StringRef toTextMateScope(Highligh
   switch (Kind) {
   case HighlightingKind::Function:
     return "entity.name.function.cpp";
+  case HighlightingKind::Method:
+    return "entity.name.function.method.cpp";
   case HighlightingKind::Variable:
-    return "variable.cpp";
+    return "variable.other.cpp";
+  case HighlightingKind::Field:
+    return "variable.other.field.cpp";
   case HighlightingKind::Class:
     return "entity.name.type.class.cpp";
   case HighlightingKind::Enum:

Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.h?rev=366047&r1=366046&r2=366047&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/SemanticHighlighting.h (original)
+++ clang-tools-extra/trunk/clangd/SemanticHighlighting.h Mon Jul 15 01:12:21 2019
@@ -26,6 +26,8 @@ namespace clangd {
 enum class HighlightingKind {
   Variable = 0,
   Function,
+  Method,
+  Field,
   Class,
   Enum,
   EnumConstant,

Modified: clang-tools-extra/trunk/clangd/test/semantic-highlighting.test
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/test/semantic-highlighting.test?rev=366047&r1=366046&r2=366047&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/test/semantic-highlighting.test (original)
+++ clang-tools-extra/trunk/clangd/test/semantic-highlighting.test Mon Jul 15 01:12:21 2019
@@ -5,12 +5,18 @@
 # CHECK:      "semanticHighlighting": {
 # CHECK-NEXT:        "scopes": [
 # CHECK-NEXT:          [
-# CHECK-NEXT:            "variable.cpp"
+# CHECK-NEXT:            "variable.other.cpp"
 # CHECK-NEXT:          ],
 # CHECK-NEXT:          [
 # CHECK-NEXT:            "entity.name.function.cpp"
 # CHECK-NEXT:          ],
 # CHECK-NEXT:          [
+# CHECK-NEXT:            "entity.name.function.method.cpp"
+# CHECK-NEXT:          ],
+# CHECK-NEXT:          [
+# CHECK-NEXT:            "variable.other.field.cpp"
+# CHECK-NEXT:          ],
+# CHECK-NEXT:          [
 # CHECK-NEXT:            "entity.name.type.class.cpp"
 # CHECK-NEXT:          ],
 # CHECK-NEXT:          [

Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=366047&r1=366046&r2=366047&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Mon Jul 15 01:12:21 2019
@@ -38,7 +38,9 @@ void checkHighlightings(llvm::StringRef
       {HighlightingKind::Class, "Class"},
       {HighlightingKind::Enum, "Enum"},
       {HighlightingKind::Namespace, "Namespace"},
-      {HighlightingKind::EnumConstant, "EnumConstant"}};
+      {HighlightingKind::EnumConstant, "EnumConstant"},
+      {HighlightingKind::Field, "Field"},
+      {HighlightingKind::Method, "Method"}};
   std::vector<HighlightingToken> ExpectedTokens;
   for (const auto &KindString : KindToString) {
     std::vector<HighlightingToken> Toks = makeHighlightingTokens(
@@ -54,14 +56,14 @@ TEST(SemanticHighlighting, GetsCorrectTo
   const char *TestCases[] = {
     R"cpp(
       struct $Class[[AS]] {
-        double SomeMember;
+        double $Field[[SomeMember]];
       };
       struct {
       } $Variable[[S]];
       void $Function[[foo]](int $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
         auto $Variable[[VeryLongVariableName]] = 12312;
         $Class[[AS]]     $Variable[[AA]];
-        auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
+        auto $Variable[[L]] = $Variable[[AA]].$Field[[SomeMember]] + $Variable[[A]];
         auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
         $Variable[[FN]](12312);
       }
@@ -73,19 +75,19 @@ TEST(SemanticHighlighting, GetsCorrectTo
         auto $Variable[[Bou]] = $Function[[Gah]];
       }
       struct $Class[[A]] {
-        void $Function[[abc]]();
+        void $Method[[abc]]();
       };
     )cpp",
     R"cpp(
       namespace $Namespace[[abc]] {
         template<typename T>
         struct $Class[[A]] {
-          T t;
+          T $Field[[t]];
         };
       }
       template<typename T>
       struct $Class[[C]] : $Namespace[[abc]]::A<T> {
-        typename T::A* D;
+        typename T::A* $Field[[D]];
       };
       $Namespace[[abc]]::$Class[[A]]<int> $Variable[[AA]];
       typedef $Namespace[[abc]]::$Class[[A]]<int> AAA;
@@ -93,7 +95,7 @@ TEST(SemanticHighlighting, GetsCorrectTo
         $Class[[B]]();
         ~$Class[[B]]();
         void operator<<($Class[[B]]);
-        $Class[[AAA]] AA;
+        $Class[[AAA]] $Field[[AA]];
       };
       $Class[[B]]::$Class[[B]]() {}
       $Class[[B]]::~$Class[[B]]() {}
@@ -112,8 +114,8 @@ TEST(SemanticHighlighting, GetsCorrectTo
         $EnumConstant[[Hi]],
       };
       struct $Class[[A]] {
-        $Enum[[E]] EEE;
-        $Enum[[EE]] EEEE;
+        $Enum[[E]] $Field[[EEE]];
+        $Enum[[EE]] $Field[[EEEE]];
       };
       int $Variable[[I]] = $EnumConstant[[Hi]];
       $Enum[[E]] $Variable[[L]] = $Enum[[E]]::$EnumConstant[[B]];
@@ -140,6 +142,30 @@ TEST(SemanticHighlighting, GetsCorrectTo
             $Namespace[[vwz]]::$Class[[A]]::$Enum[[B]]::$EnumConstant[[Hi]];
       ::$Namespace[[vwz]]::$Class[[A]] $Variable[[B]];
       ::$Namespace[[abc]]::$Namespace[[bcd]]::$Class[[A]] $Variable[[BB]];
+    )cpp",
+    R"cpp(
+      struct $Class[[D]] {
+        double $Field[[C]];
+      };
+      struct $Class[[A]] {
+        double $Field[[B]];
+        $Class[[D]] $Field[[E]];
+        static double $Variable[[S]];
+        void $Method[[foo]]() {
+          $Field[[B]] = 123;
+          this->$Field[[B]] = 156;
+          this->$Method[[foo]]();
+          $Method[[foo]]();
+          $Variable[[S]] = 90.1;
+        }
+      };
+      void $Function[[foo]]() {
+        $Class[[A]] $Variable[[AA]];
+        $Variable[[AA]].$Field[[B]] += 2;
+        $Variable[[AA]].$Method[[foo]]();
+        $Variable[[AA]].$Field[[E]].$Field[[C]];
+        $Class[[A]]::$Variable[[S]] = 90;
+      }
     )cpp"};
   for (const auto &TestCase : TestCases) {
     checkHighlightings(TestCase);




More information about the cfe-commits mailing list