[PATCH] D90452: [clangd] Respect codeAction.isPreferredSupport in client capabilities

Kadir Cetinkaya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 30 03:44:13 PDT 2020


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

Fixes https://github.com/clangd/clangd/issues/573.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D90452

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


Index: clang-tools-extra/clangd/Protocol.h
===================================================================
--- clang-tools-extra/clangd/Protocol.h
+++ clang-tools-extra/clangd/Protocol.h
@@ -445,6 +445,10 @@
   /// textDocument.codeAction.codeActionLiteralSupport.
   bool CodeActionStructure = false;
 
+  /// Client supports isPreferred property on CodeActions.
+  /// textDocument.codeAction.isPreferredSupport.
+  bool CodeActionIsPreferred = false;
+
   /// Client advertises support for the semanticTokens feature.
   /// We support the textDocument/semanticTokens request in any case.
   /// textDocument.semanticTokens
Index: clang-tools-extra/clangd/Protocol.cpp
===================================================================
--- clang-tools-extra/clangd/Protocol.cpp
+++ clang-tools-extra/clangd/Protocol.cpp
@@ -346,6 +346,8 @@
     if (auto *CodeAction = TextDocument->getObject("codeAction")) {
       if (CodeAction->getObject("codeActionLiteralSupport"))
         R.CodeActionStructure = true;
+      if (auto IsPreferred = CodeAction->getBoolean("isPreferredSupport"))
+        R.CodeActionIsPreferred = *IsPreferred;
     }
     if (auto *DocumentSymbol = TextDocument->getObject("documentSymbol")) {
       if (auto HierarchicalSupport =
Index: clang-tools-extra/clangd/ClangdLSPServer.h
===================================================================
--- clang-tools-extra/clangd/ClangdLSPServer.h
+++ clang-tools-extra/clangd/ClangdLSPServer.h
@@ -263,6 +263,9 @@
   CompletionItemKindBitset SupportedCompletionItemKinds;
   /// Whether the client supports CodeAction response objects.
   bool SupportsCodeAction = false;
+  /// Whether the client supports isPreferred property on CodeAction response
+  /// objects.
+  bool SupportsCodeActionIsPreferred = false;
   /// From capabilities of textDocument/documentSymbol.
   bool SupportsHierarchicalDocumentSymbol = false;
   /// Whether the client supports showing file status.
Index: clang-tools-extra/clangd/ClangdLSPServer.cpp
===================================================================
--- clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -532,6 +532,7 @@
   if (Params.capabilities.CompletionItemKinds)
     SupportedCompletionItemKinds |= *Params.capabilities.CompletionItemKinds;
   SupportsCodeAction = Params.capabilities.CodeActionStructure;
+  SupportsCodeActionIsPreferred = Params.capabilities.CodeActionIsPreferred;
   SupportsHierarchicalDocumentSymbol =
       Params.capabilities.HierarchicalDocumentSymbol;
   SupportFileStatus = Params.initializationOptions.FileStatus;
@@ -1029,15 +1030,17 @@
 
         // If there's exactly one quick-fix, call it "preferred".
         // We never consider refactorings etc as preferred.
-        CodeAction *OnlyFix = nullptr;
-        for (auto &Action : Actions) {
-          if (Action.kind && *Action.kind == CodeAction::QUICKFIX_KIND) {
-            if (OnlyFix) {
-              OnlyFix->isPreferred = false;
-              break;
+        if (SupportsCodeActionIsPreferred) {
+          CodeAction *OnlyFix = nullptr;
+          for (auto &Action : Actions) {
+            if (Action.kind && *Action.kind == CodeAction::QUICKFIX_KIND) {
+              if (OnlyFix) {
+                OnlyFix->isPreferred = false;
+                break;
+              }
+              Action.isPreferred = true;
+              OnlyFix = &Action;
             }
-            Action.isPreferred = true;
-            OnlyFix = &Action;
           }
         }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D90452.301851.patch
Type: text/x-patch
Size: 3547 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201030/2c964284/attachment.bin>


More information about the cfe-commits mailing list