]> git.eshelyaron.com Git - emacs.git/commitdiff
Handle lsp 3.15's ispreferred code action property
authorDamien Merenne <damien@bloom-life.com>
Wed, 28 Oct 2020 20:40:32 +0000 (21:40 +0100)
committerJoão Távora <joaotavora@gmail.com>
Sun, 1 Nov 2020 18:28:05 +0000 (18:28 +0000)
* eglot.el (eglot--lsp-interface-alist): Add :isPreferred to CodeAction.
(eglot-client-capabilities):  Announce :isPreferredSupport.
(eglot-code-actions): Consider preferred CodeAction item.

Co-authored-by: João Távora <joaotavora@gmail.com>
GitHub-reference: close https://github.com/joaotavora/eglot/issues/558

lisp/progmodes/eglot.el

index 60bc56e8aa6cbc0d90bfd21971e9aed4c7042fd2..bd3bb5337ab0d16202c8492e2f5508ff4e5442c9 100644 (file)
@@ -244,7 +244,7 @@ let the buffer grow forever."
 (eval-and-compile
   (defvar eglot--lsp-interface-alist
     `(
-      (CodeAction (:title) (:kind :diagnostics :edit :command))
+      (CodeAction (:title) (:kind :diagnostics :edit :command :isPreferred))
       (ConfigurationItem () (:scopeUri :section))
       (Command ((:title . string) (:command . string)) (:arguments))
       (CompletionItem (:label)
@@ -576,7 +576,8 @@ treated as in `eglot-dbind'."
                                      ["quickfix"
                                       "refactor" "refactor.extract"
                                       "refactor.inline" "refactor.rewrite"
-                                      "source" "source.organizeImports"])))
+                                      "source" "source.organizeImports"]))
+                                  :isPreferredSupport t)
              :formatting         `(:dynamicRegistration :json-false)
              :rangeFormatting    `(:dynamicRegistration :json-false)
              :rename             `(:dynamicRegistration :json-false)
@@ -2516,12 +2517,19 @@ code actions at point"
                         (cons title all))
                       actions)
               (eglot--error "No code actions here")))
+         (preferred-action (cl-find-if
+                             (jsonrpc-lambda (&key isPreferred &allow-other-keys)
+                               isPreferred)
+                             actions))
          (menu `("Eglot code actions:" ("dummy" ,@menu-items)))
          (action (if (listp last-nonmenu-event)
                      (x-popup-menu last-nonmenu-event menu)
                    (cdr (assoc (completing-read "[eglot] Pick an action: "
                                                 menu-items nil t
-                                                nil nil (car menu-items))
+                                                nil nil (or (plist-get
+                                                             preferred-action
+                                                             :title)
+                                                            (car menu-items)))
                                menu-items)))))
     (eglot--dcase action
       (((Command) command arguments)