Skip to content

Commit 53c7a2b

Browse files
nbfalconyyoncho
authored andcommitted
lsp-rust: support rust-analyzer.showReference lens
Add a rust-analyzer specific :action-handler for the showReference lens, which leverages `lsp-show-xrefs'. Refactor: eliminate `lsp-execute-command', because it caused the :title to be lost, which is needed to distinguish between the "references" or "implementations" variants of that lens. Defining a new `lsp-interface' to destructure "rust-analyzer.showReference" was impossible, as the former doesn't support destructuring lists (the :arguments? paramter is a special, 3-element list.).
1 parent 9a692a1 commit 53c7a2b

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

clients/lsp-rust.el

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ PARAMS progress report notification data."
295295
(lsp-workspace-status nil workspace)
296296
(lsp-workspace-status (format "%s - %s" title (or message? "")) workspace))))
297297

298-
(cl-defmethod lsp-execute-command (_server (_command (eql rls.run)) params)
298+
(lsp-defun lsp-rust--rls-run ((&Command :arguments? params))
299299
(-let* (((&rls:Cmd :env :binary :args :cwd) (lsp-seq-first params))
300300
(default-directory (or cwd (lsp-workspace-root) default-directory) ))
301301
(compile
@@ -635,6 +635,13 @@ them with `crate` or the crate name they refer to."
635635
(lsp-defun lsp-rust--analyzer-run-single ((&Command :arguments?))
636636
(lsp-rust-analyzer-run (lsp-seq-first arguments?)))
637637

638+
;; `lsp-interface' is unsuitable for ShowReference destructuring, as that code
639+
;; action yields an array of three elements, not a map.
640+
(lsp-defun lsp-rust--analyzer-show-references
641+
((&Command :title :arguments? [_uri _filepos references]))
642+
(lsp-show-xrefs (lsp--locations-to-xref-items references) nil
643+
(s-contains-p "reference" title)))
644+
638645
(lsp-register-client
639646
(make-lsp-client
640647
:new-connection (lsp-stdio-connection
@@ -648,7 +655,8 @@ them with `crate` or the crate name they refer to."
648655
:priority (if (eq lsp-rust-server 'rust-analyzer) 1 -1)
649656
:initialization-options 'lsp-rust-analyzer--make-init-options
650657
:notification-handlers (ht<-alist lsp-rust-notification-handlers)
651-
:action-handlers (ht ("rust-analyzer.runSingle" #'lsp-rust--analyzer-run-single))
658+
:action-handlers (ht ("rust-analyzer.runSingle" #'lsp-rust--analyzer-run-single)
659+
("rust-analyzer.showReferences" #'lsp-rust--analyzer-show-references))
652660
:library-folders-fn (lambda (_workspace) lsp-rust-library-directories)
653661
:after-open-fn (lambda ()
654662
(when lsp-rust-analyzer-server-display-inlay-hints

lsp-lens.el

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ See `lsp-lens--schedule-refresh' for details."
154154
(lsp:command-command command?)
155155
(lambda ()
156156
(interactive)
157-
(lsp-execute-command server-id
158-
(intern (lsp:command-command command?))
159-
(lsp:command-arguments? command?))))))
157+
(lsp--execute-command command?)))))
160158

161159
(defun lsp-lens--display (lenses)
162160
"Show LENSES."

lsp-mode.el

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -968,9 +968,6 @@ calling `remove-overlays'.")
968968

969969
(defvar-local lsp--virtual-buffer-point-max nil)
970970

971-
(cl-defgeneric lsp-execute-command (server command arguments)
972-
"Ask SERVER to execute COMMAND with ARGUMENTS.")
973-
974971
(defun lsp-elt (sequence n)
975972
"Return Nth element of SEQUENCE or nil if N is out of range."
976973
(cond
@@ -5740,7 +5737,7 @@ REFERENCES? t when METHOD returns references."
57405737
(lsp-request "workspace/executeCommand" params)))
57415738

57425739
(defun lsp--send-execute-command (command &optional args)
5743-
"Execute workspace COMMAND with ARGS showing error if command is not mapped client-side."
5740+
"Create and send a 'workspace/executeCommand' message having command COMMAND and optional ARGS."
57445741
(condition-case-unless-debug err
57455742
(lsp-workspace-command-execute command args)
57465743
(error

0 commit comments

Comments
 (0)