jvikstrom created this revision.
jvikstrom added reviewers: hokein, sammccall, ilya-biryukov.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay.
Herald added a project: clang.

Adds option to enable semantic highlighting as an experimental capability. The 
reason for this is that it is not possible to add textDocument capabilities in 
the vscode extension as it is a strongly typed argument. The experimental 
capability is a normal JS like object and we can therefore send the capability 
there. However we can't remove the textDocument capability as theia relies on 
the capability to be in textDocument. (The same reasoning is why the server 
sends the TM scopes in the experimental capability if it got the capability as 
experimental).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64922

Files:
  clang-tools-extra/clangd/ClangdLSPServer.cpp
  clang-tools-extra/clangd/Protocol.cpp
  clang-tools-extra/clangd/Protocol.h
  clang-tools-extra/clangd/test/experimental-semantic-highlighting.test
  clang-tools-extra/clangd/test/semantic-highlighting.test

Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===================================================================
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -2,7 +2,7 @@
 {"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"textDocument":{"semanticHighlightingCapabilities":{"semanticHighlighting":true}}},"trace":"off"}}
 ---
 #      CHECK:  "id": 0,
-# CHECK:      "semanticHighlighting": {
+#      CHECK:      "semanticHighlighting": {
 # CHECK-NEXT:        "scopes": [
 # CHECK-NEXT:          [
 # CHECK-NEXT:            "variable.other.cpp"
Index: clang-tools-extra/clangd/test/experimental-semantic-highlighting.test
===================================================================
--- /dev/null
+++ clang-tools-extra/clangd/test/experimental-semantic-highlighting.test
@@ -0,0 +1,41 @@
+# RUN: clangd -lit-test < %s | FileCheck -strict-whitespace %s
+{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"experimental":{"semanticHighlightingCapabilities":{"semanticHighlighting":true}}},"trace":"off"}}
+---
+#      CHECK:  "id": 0,
+#      CHECK:      "experimental": {
+# CHECK-NEXT:        "semanticHighlighting": {
+# CHECK-NEXT:          "scopes": [
+# CHECK-NEXT:            [
+# 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:            [
+# CHECK-NEXT:              "entity.name.type.enum.cpp"
+# CHECK-NEXT:            ],
+# CHECK-NEXT:            [
+# CHECK-NEXT:              "variable.other.enummember.cpp"
+# CHECK-NEXT:            ],
+# CHECK-NEXT:            [
+# CHECK-NEXT:              "entity.name.namespace.cpp"
+# CHECK-NEXT:            ],
+# CHECK-NEXT:            [
+# CHECK-NEXT:              "entity.name.type.template.cpp"
+# CHECK-NEXT:            ]
+# CHECK-NEXT:          ]
+# CHECK-NEXT:        }
+# CHECK-NEXT:      },
+---
+{"jsonrpc":"2.0","id":3,"method":"shutdown"}
+---
+{"jsonrpc":"2.0","method":"exit"}
Index: clang-tools-extra/clangd/Protocol.h
===================================================================
--- clang-tools-extra/clangd/Protocol.h
+++ clang-tools-extra/clangd/Protocol.h
@@ -416,6 +416,10 @@
   /// textDocument.semanticHighlightingCapabilities.semanticHighlighting
   bool SemanticHighlighting = false;
 
+  // Only used because there is no other way to pass the text mate scopes to the
+  // vscode extension other than as experimental server capabilities.
+  bool ExperimentalSemanticHighlighting = false;
+
   /// Supported encodings for LSP character offsets. (clangd extension).
   llvm::Optional<std::vector<OffsetEncoding>> offsetEncoding;
 
Index: clang-tools-extra/clangd/Protocol.cpp
===================================================================
--- clang-tools-extra/clangd/Protocol.cpp
+++ clang-tools-extra/clangd/Protocol.cpp
@@ -272,6 +272,12 @@
   const llvm::json::Object *O = Params.getAsObject();
   if (!O)
     return false;
+  if (auto *Experimental = O->getObject("experimental"))
+    if (auto *SemanticHighlighting =
+            Experimental->getObject("semanticHighlightingCapabilities"))
+      if (auto SemanticHighlightingSupport =
+              SemanticHighlighting->getBoolean("semanticHighlighting"))
+        R.ExperimentalSemanticHighlighting = *SemanticHighlightingSupport;
   if (auto *TextDocument = O->getObject("textDocument")) {
     if (auto *SemanticHighlighting =
             TextDocument->getObject("semanticHighlightingCapabilities")) {
Index: clang-tools-extra/clangd/ClangdLSPServer.cpp
===================================================================
--- clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -341,7 +341,8 @@
                                *NegotiatedOffsetEncoding);
 
   ClangdServerOpts.SemanticHighlighting =
-      Params.capabilities.SemanticHighlighting;
+      Params.capabilities.SemanticHighlighting |
+      Params.capabilities.ExperimentalSemanticHighlighting;
   if (Params.rootUri && *Params.rootUri)
     ClangdServerOpts.WorkspaceRoot = Params.rootUri->file();
   else if (Params.rootPath && !Params.rootPath->empty())
@@ -423,11 +424,18 @@
         }}}};
   if (NegotiatedOffsetEncoding)
     Result["offsetEncoding"] = *NegotiatedOffsetEncoding;
+  llvm::json::Object TMScopes{{"scopes", buildHighlightScopeLookupTable()}};
+  // Theia needs the text mate scopes in the normal capabilities object.
   if (Params.capabilities.SemanticHighlighting)
     Result.getObject("capabilities")
-        ->insert(
-            {"semanticHighlighting",
-             llvm::json::Object{{"scopes", buildHighlightScopeLookupTable()}}});
+        ->insert({"semanticHighlighting", (llvm::json::Object)TMScopes});
+  // The vscode language client can currently only get the text mate scopes via
+  // the experimental server capability.
+  if (Params.capabilities.ExperimentalSemanticHighlighting)
+    Result.getObject("capabilities")
+        ->insert({"experimental",
+                  llvm::json::Object{
+                      {"semanticHighlighting", (llvm::json::Object)TMScopes}}});
   Reply(std::move(Result));
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to