Skip to content

Commit b8a7455

Browse files
authored
lsp-rust: support rust-analyzer.showReference lens (#2299)
* 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.). * lsp-lens.el: fix byte-compile warnings * Restore `lsp-execute-command' as an obsolete defun Its original function, being a `cl-defgeneric' that can be overriden per language server is gone; however, PRs were provided to eliminate all uses of it in that context. To remain backwards-compatible, it will still remain as an obsolete `defun' that calls `lsp--execute-command' internally. * Fix byte-compile * Remove `lsp-execute-command' as an obsolete defun Since it wouldn't do what it did previously (being an extension point), restoring that function doesn't actually improve backwards compatibility. Reverts 957a056. * Support `lsp-execute-command' `lsp-execute-command' now works as an extension point again, but a deprecation warning is shown if it is used to handle a command. To implement this, leverage `cl-no-applicable-method': call `lsp-execute-command' first, and then, if there is no implementation, go trough the handler hash tables as usual. * `lsp-execute-command': use `make-obsolete' The proper way to mark a function as deprecated is to use `make-obsolete'; use that instead of `lsp--warn'. * lsp-rust: remove redundant `lsp-interface' comment `lsp-interface' wasn't designed to destructure lists; stating that fact before `lsp-rust--analyzer-show-references' is as such redundant, since that is normal and not noteworthy. * `lsp-lens--create-interactive-command': non-nil The COMMAND? argument of that function mustn't be nil, which is unclear from its name (?). Note that fact in its docstring. The name of that argument is still good though, because it refers to a field name in `CodeAction' and `CodeLens'. Based on a discussion with @kiennq. * Fix `byte-compile': `lsp-execute-command' Unlike previously assumed, calling `lsp-execute-command' causes byte-compile warnings, not just using it in `cl-defmethod'. Wrap the call in `with-no-warnings', since that should be the only use of that function.
1 parent 4cdd1e9 commit b8a7455

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

clients/lsp-rust.el

Lines changed: 8 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,11 @@ 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-defun lsp-rust--analyzer-show-references
639+
((&Command :title :arguments? [_uri _filepos references]))
640+
(lsp-show-xrefs (lsp--locations-to-xref-items references) nil
641+
(s-contains-p "reference" title)))
642+
638643
(lsp-register-client
639644
(make-lsp-client
640645
:new-connection (lsp-stdio-connection
@@ -648,7 +653,8 @@ them with `crate` or the crate name they refer to."
648653
:priority (if (eq lsp-rust-server 'rust-analyzer) 1 -1)
649654
:initialization-options 'lsp-rust-analyzer--make-init-options
650655
:notification-handlers (ht<-alist lsp-rust-notification-handlers)
651-
:action-handlers (ht ("rust-analyzer.runSingle" #'lsp-rust--analyzer-run-single))
656+
:action-handlers (ht ("rust-analyzer.runSingle" #'lsp-rust--analyzer-run-single)
657+
("rust-analyzer.showReferences" #'lsp-rust--analyzer-show-references))
652658
:library-folders-fn (lambda (_workspace) lsp-rust-library-directories)
653659
:after-open-fn (lambda ()
654660
(when lsp-rust-analyzer-server-display-inlay-hints

lsp-lens.el

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,14 @@ See `lsp-lens--schedule-refresh' for details."
144144
(define-key [mouse-1] (lsp-lens--create-interactive-command command))))
145145

146146
(defun lsp-lens--create-interactive-command (command?)
147-
"Create an interactive COMMAND? for the lens."
148-
(let ((server-id (->> (lsp-workspaces)
149-
(cl-first)
150-
(or lsp--cur-workspace)
151-
(lsp--workspace-client)
152-
(lsp--client-server-id))))
153-
(if (functionp (lsp:command-command command?))
154-
(lsp:command-command command?)
155-
(lambda ()
156-
(interactive)
157-
(lsp-execute-command server-id
158-
(intern (lsp:command-command command?))
159-
(lsp:command-arguments? command?))))))
147+
"Create an interactive COMMAND? for the lens.
148+
COMMAND? shall be an `&Command' (e.g. `&CodeLens' :command?) and
149+
mustn't be nil."
150+
(if (functionp (lsp:command-command command?))
151+
(lsp:command-command command?)
152+
(lambda ()
153+
(interactive)
154+
(lsp--execute-command command?))))
160155

161156
(defun lsp-lens--display (lenses)
162157
"Show LENSES."

lsp-mode.el

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,8 @@ calling `remove-overlays'.")
969969
(defvar-local lsp--virtual-buffer-point-max nil)
970970

971971
(cl-defgeneric lsp-execute-command (server command arguments)
972-
"Ask SERVER to execute COMMAND with ARGUMENTS.")
972+
"Ask SERVER to execute COMMAND with ARGUMENTS."
973+
(declare (obsolete "use `make-lsp-client' with :action-handlers instead." "7.1.0")))
973974

974975
(defun lsp-elt (sequence n)
975976
"Return Nth element of SEQUENCE or nil if N is out of range."
@@ -5208,9 +5209,18 @@ It will filter by KIND if non nil."
52085209

52095210
(lsp-defun lsp--execute-command ((action &as &Command :command :arguments?))
52105211
"Parse and execute a code ACTION represented as a Command LSP type."
5211-
(-if-let* ((action-handler (lsp--find-action-handler command)))
5212-
(funcall action-handler action)
5213-
(lsp--send-execute-command command arguments?)))
5212+
(let ((server-id (->> (lsp-workspaces)
5213+
(cl-first)
5214+
(or lsp--cur-workspace)
5215+
(lsp--workspace-client)
5216+
(lsp--client-server-id))))
5217+
(condition-case nil
5218+
(with-no-warnings
5219+
(lsp-execute-command server-id (intern command) arguments?))
5220+
(cl-no-applicable-method
5221+
(if-let ((action-handler (lsp--find-action-handler command)))
5222+
(funcall action-handler action)
5223+
(lsp--send-execute-command command arguments?))))))
52145224

52155225
(lsp-defun lsp-execute-code-action ((action &as &CodeAction :command? :edit?))
52165226
"Execute code action ACTION.
@@ -5740,7 +5750,7 @@ REFERENCES? t when METHOD returns references."
57405750
(lsp-request "workspace/executeCommand" params)))
57415751

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

0 commit comments

Comments
 (0)