[clang-tools-extra] r369238 - [clangd] Added special HighlightingKind for function parameters.

Johan Vikstrom via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 19 00:51:39 PDT 2019


Author: jvikstrom
Date: Mon Aug 19 00:51:39 2019
New Revision: 369238

URL: http://llvm.org/viewvc/llvm-project?rev=369238&view=rev
Log:
[clangd] Added special HighlightingKind for function parameters.

Summary: This means that function parameters are no longer highlighted as variable.other.cpp but instead as variable.parameter.cpp which is the more "correct" TextMate scope for them.

Reviewers: hokein, ilya-biryukov

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

Tags: #clang

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

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=369238&r1=369237&r2=369238&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original)
+++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Mon Aug 19 00:51:39 2019
@@ -11,6 +11,7 @@
 #include "Protocol.h"
 #include "SourceCode.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/Decl.h"
 #include "clang/AST/DeclCXX.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 #include <algorithm>
@@ -200,6 +201,10 @@ private:
       addToken(Loc, HighlightingKind::EnumConstant);
       return;
     }
+    if (isa<ParmVarDecl>(D)) {
+      addToken(Loc, HighlightingKind::Parameter);
+      return;
+    }
     if (isa<VarDecl>(D)) {
       addToken(Loc, HighlightingKind::Variable);
       return;
@@ -406,6 +411,8 @@ llvm::StringRef toTextMateScope(Highligh
     return "entity.name.function.method.cpp";
   case HighlightingKind::Variable:
     return "variable.other.cpp";
+  case HighlightingKind::Parameter:
+    return "variable.parameter.cpp";
   case HighlightingKind::Field:
     return "variable.other.field.cpp";
   case HighlightingKind::Class:

Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.h?rev=369238&r1=369237&r2=369238&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/SemanticHighlighting.h (original)
+++ clang-tools-extra/trunk/clangd/SemanticHighlighting.h Mon Aug 19 00:51:39 2019
@@ -25,6 +25,7 @@ namespace clangd {
 
 enum class HighlightingKind {
   Variable = 0,
+  Parameter,
   Function,
   Method,
   Field,

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=369238&r1=369237&r2=369238&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/test/semantic-highlighting.test (original)
+++ clang-tools-extra/trunk/clangd/test/semantic-highlighting.test Mon Aug 19 00:51:39 2019
@@ -8,6 +8,9 @@
 # CHECK-NEXT:            "variable.other.cpp"
 # CHECK-NEXT:          ],
 # CHECK-NEXT:          [
+# CHECK-NEXT:            "variable.parameter.cpp"
+# CHECK-NEXT:          ],
+# CHECK-NEXT:          [
 # CHECK-NEXT:            "entity.name.function.cpp"
 # CHECK-NEXT:          ],
 # CHECK-NEXT:          [
@@ -43,7 +46,7 @@
 # CHECK-NEXT:    "lines": [
 # CHECK-NEXT:      {
 # CHECK-NEXT:        "line": 0,
-# CHECK-NEXT:        "tokens": "AAAAAAADAAkAAAAEAAEAAA=="
+# CHECK-NEXT:        "tokens": "AAAAAAADAAoAAAAEAAEAAA=="
 # CHECK-NEXT:      }
 # CHECK-NEXT:    ],
 # CHECK-NEXT:    "textDocument": {
@@ -58,11 +61,11 @@
 # CHECK-NEXT:    "lines": [
 # CHECK-NEXT:      {
 # CHECK-NEXT:        "line": 0,
-# CHECK-NEXT:        "tokens": "AAAAAAADAAkAAAAEAAEAAA=="
+# CHECK-NEXT:        "tokens": "AAAAAAADAAoAAAAEAAEAAA=="
 # CHECK-NEXT:      }
 # CHECK-NEXT:      {
 # CHECK-NEXT:        "line": 1,
-# CHECK-NEXT:        "tokens": "AAAAAAADAAkAAAAEAAEAAA=="
+# CHECK-NEXT:        "tokens": "AAAAAAADAAoAAAAEAAEAAA=="
 # CHECK-NEXT:      }
 # CHECK-NEXT:    ],
 # CHECK-NEXT:    "textDocument": {
@@ -77,7 +80,7 @@
 # CHECK-NEXT:    "lines": [
 # CHECK-NEXT:      {
 # CHECK-NEXT:        "line": 1,
-# CHECK-NEXT:        "tokens": "AAAAAAADAAkAAAAEAAEAAA=="
+# CHECK-NEXT:        "tokens": "AAAAAAADAAoAAAAEAAEAAA=="
 # CHECK-NEXT:      }
 # CHECK-NEXT:   ],
 # CHECK-NEXT:    "textDocument": {

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=369238&r1=369237&r2=369238&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Mon Aug 19 00:51:39 2019
@@ -16,6 +16,10 @@
 
 namespace clang {
 namespace clangd {
+  void PrintTo(const HighlightingToken &T, ::std::ostream *OS) {
+  *OS << "(" << T.R.start.line << ", " << T.R.start.character << ") -> (" << T.R.end.line << ", " << T.R.end.character << "): " << (int)T.Kind;
+}
+
 namespace {
 
 std::vector<HighlightingToken>
@@ -32,6 +36,7 @@ makeHighlightingTokens(llvm::ArrayRef<Ra
 std::vector<HighlightingToken> getExpectedTokens(Annotations &Test) {
   static const std::map<HighlightingKind, std::string> KindToString{
       {HighlightingKind::Variable, "Variable"},
+      {HighlightingKind::Parameter, "Parameter"},
       {HighlightingKind::Function, "Function"},
       {HighlightingKind::Class, "Class"},
       {HighlightingKind::Enum, "Enum"},
@@ -104,11 +109,11 @@ TEST(SemanticHighlighting, GetsCorrectTo
       };
       struct {
       } $Variable[[S]];
-      $Primitive[[void]] $Function[[foo]]($Primitive[[int]] $Variable[[A]], $Class[[AS]] $Variable[[As]]) {
+      $Primitive[[void]] $Function[[foo]]($Primitive[[int]] $Parameter[[A]], $Class[[AS]] $Parameter[[As]]) {
         $Primitive[[auto]] $Variable[[VeryLongVariableName]] = 12312;
         $Class[[AS]]     $Variable[[AA]];
-        $Primitive[[auto]] $Variable[[L]] = $Variable[[AA]].$Field[[SomeMember]] + $Variable[[A]];
-        auto $Variable[[FN]] = [ $Variable[[AA]]]($Primitive[[int]] $Variable[[A]]) -> $Primitive[[void]] {};
+        $Primitive[[auto]] $Variable[[L]] = $Variable[[AA]].$Field[[SomeMember]] + $Parameter[[A]];
+        auto $Variable[[FN]] = [ $Variable[[AA]]]($Primitive[[int]] $Parameter[[A]]) -> $Primitive[[void]] {};
         $Variable[[FN]](12312);
       }
     )cpp",
@@ -287,10 +292,10 @@ TEST(SemanticHighlighting, GetsCorrectTo
       struct $Class[[B]] {};
       struct $Class[[A]] {
         $Class[[B]] $Field[[BB]];
-        $Class[[A]] &operator=($Class[[A]] &&$Variable[[O]]);
+        $Class[[A]] &operator=($Class[[A]] &&$Parameter[[O]]);
       };
 
-      $Class[[A]] &$Class[[A]]::operator=($Class[[A]] &&$Variable[[O]]) = default;
+      $Class[[A]] &$Class[[A]]::operator=($Class[[A]] &&$Parameter[[O]]) = default;
     )cpp",
     R"cpp(
       enum $Enum[[En]] {
@@ -301,9 +306,9 @@ TEST(SemanticHighlighting, GetsCorrectTo
         $Class[[Foo]] $Field[[Fo]];
         $Enum[[En]] $Field[[E]];
         $Primitive[[int]] $Field[[I]];
-        $Class[[Bar]] ($Class[[Foo]] $Variable[[F]],
-                $Enum[[En]] $Variable[[E]])
-        : $Field[[Fo]] ($Variable[[F]]), $Field[[E]] ($Variable[[E]]),
+        $Class[[Bar]] ($Class[[Foo]] $Parameter[[F]],
+                $Enum[[En]] $Parameter[[E]])
+        : $Field[[Fo]] ($Parameter[[F]]), $Field[[E]] ($Parameter[[E]]),
           $Field[[I]] (123) {}
       };
       class $Class[[Bar2]] : public $Class[[Bar]] {
@@ -356,8 +361,8 @@ TEST(SemanticHighlighting, GetsCorrectTo
         $Primitive[[void]] (T::*$TemplateParameter[[method]])($Primitive[[int]])>
       struct $Class[[G]] {
         $Primitive[[void]] $Method[[foo]](
-            $TemplateParameter[[T]] *$Variable[[O]]) {
-          ($Variable[[O]]->*$TemplateParameter[[method]])(10);
+            $TemplateParameter[[T]] *$Parameter[[O]]) {
+          ($Parameter[[O]]->*$TemplateParameter[[method]])(10);
         }
       };
       struct $Class[[F]] {
@@ -438,7 +443,7 @@ TEST(SemanticHighlighting, toSemanticHig
   std::vector<SemanticHighlightingInformation> ActualResults =
       toSemanticHighlightingInformation(Tokens);
   std::vector<SemanticHighlightingInformation> ExpectedResults = {
-      {3, "AAAACAAEAAAAAAAEAAMAAQ=="}, {1, "AAAAAQAEAAA="}};
+      {3, "AAAACAAEAAAAAAAEAAMAAg=="}, {1, "AAAAAQAEAAA="}};
   EXPECT_EQ(ActualResults, ExpectedResults);
 }
 




More information about the cfe-commits mailing list