Skip to content

Commit

Permalink
Add 2 rust analyzer LSP extensions (#2776)
Browse files Browse the repository at this point in the history
* Implement OpenCargoToml ra lsp extension

Takes a universal arg to open in another window similar
to lsp-clangd-find-other-file

Implement the related tests ra extension

Extract the common code to run a given runnable in
cargo-process-mode and use it in ~lsp-rust-analyzer-run~
as well as =lsp-rust-analyzer-related-tests=

* Finish the implementation of related-tests

extend the rust-analyzer protocol with relevant entries

add a review comment about the preview function

Add manual documentation and gifs for both interactive functions
  • Loading branch information
petr-tik committed May 8, 2021
1 parent d95c2f3 commit fd05f0d
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.org
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
* Add client for SystemVerilog language server (called [[https://github.com/imc-trading/svlangserver]])
* Add client for python-lsp-server, pylsp (https://github.com/python-lsp/python-lsp-server)
* Deprecate the Palantir Python language server, pyls.
* Add 2 rust-analyzer LSP extension function ~lsp-rust-analyzer-related-tests~ and
~lsp-rust-analyzer-open-cargo-toml~
** Release 7.0.1
* Introduced ~lsp-diagnostics-mode~.
* Safe renamed ~lsp-flycheck-default-level~ -> ~lsp-diagnostics-flycheck-default-level~
Expand Down
82 changes: 73 additions & 9 deletions clients/lsp-rust.el
Original file line number Diff line number Diff line change
Expand Up @@ -862,9 +862,12 @@ them with `crate` or the crate name they refer to."
(lsp-rust-analyzer--runnables))
(-lambda ((&rust-analyzer:Runnable :label)) label)))

(defun lsp-rust-analyzer-run (runnable)
"Select and run a runnable action."
(interactive (list (lsp-rust-analyzer--select-runnable)))

(defun lsp-rust-analyzer--common-runner (runnable)
"Execute a given RUNNABLE.
Extract the arguments, prepare the minor mode (cargo-process-mode if possible)
and run a compilation"
(-let* (((&rust-analyzer:Runnable :kind :label :args) runnable)
((&rust-analyzer:RunnableArgs :cargo-args :executable-args :workspace-root?) args)
(default-directory (or workspace-root? default-directory)))
Expand All @@ -874,14 +877,20 @@ them with `crate` or the crate name they refer to."
(string-join (append (list "cargo") cargo-args (when executable-args '("--")) executable-args '()) " ")
;; cargo-process-mode is nice, but try to work without it...
(if (functionp 'cargo-process-mode) 'cargo-process-mode nil)
(lambda (_) (concat "*" label "*")))
(setq lsp-rust-analyzer--last-runnable runnable))))
(lambda (_) (concat "*" label "*"))))))


(defun lsp-rust-analyzer-run (runnable)
"Select and run a RUNNABLE action."
(interactive (list (lsp-rust-analyzer--select-runnable)))
(when (lsp-rust-analyzer--common-runner runnable)
(setq lsp-rust-analyzer--last-runnable runnable)))

(defun lsp-rust-analyzer-debug (runnable)
"Select and run a runnable action."
"Select and debug a RUNNABLE action."
(interactive (list (lsp-rust-analyzer--select-runnable)))
(unless (featurep 'dap-cpptools)
(user-error "You must require `dap-cpptools'."))
(user-error "You must require `dap-cpptools'"))
(-let (((&rust-analyzer:Runnable
:args (&rust-analyzer:RunnableArgs :cargo-args :workspace-root? :executable-args)
:label) runnable))
Expand All @@ -905,9 +914,9 @@ them with `crate` or the crate name they refer to."
(funcall
(lambda (artifact-spec)
(pcase artifact-spec
(`() (user-error "No compilation artifacts or obtaining the runnable artifacts failed."))
(`() (user-error "No compilation artifacts or obtaining the runnable artifacts failed"))
(`(,spec) spec)
(_ (user-error "Multiple compilation artifacts are not supported.")))))
(_ (user-error "Multiple compilation artifacts are not supported")))))
(list :type "cppdbg"
:request "launch"
:name label
Expand All @@ -928,5 +937,60 @@ them with `crate` or the crate name they refer to."
(interactive)
(lsp-find-locations "experimental/parentModule" nil :display-action display-action))

(defun lsp-rust-analyzer-open-cargo-toml (&optional new-window)
"Open the closest Cargo.toml from the current file.
Rust-Analyzer LSP protocol documented here and added in November 2020
https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/dev/lsp-extensions.md#open-cargotoml
If NEW-WINDOW (interactively the prefix argument) is non-nil,
open in a new window."
(interactive "P")
(-if-let (workspace (lsp-find-workspace 'rust-analyzer))
(-if-let* ((response (with-lsp-workspace workspace
(lsp-send-request (lsp-make-request
"experimental/openCargoToml"
(lsp-make-rust-analyzer-open-cargo-toml-params
:text-document (lsp--text-document-identifier))))))
((&Location :uri :range) response))
(funcall (if new-window #'find-file-other-window #'find-file)
(lsp--uri-to-path uri))
(lsp--warn "Couldn't find a Cargo.toml file or your version of rust-analyzer doesn't support this extension"))
(lsp--error "OpenCargoToml is an extension available only with rust-analyzer")))


(defun lsp-rust-analyzer--related-tests ()
"Get runnable test items related to the current TextDocumentPosition.
Calls a rust-analyzer LSP extension endpoint that returns a wrapper over Runnable[]"
(lsp-send-request (lsp-make-request
"rust-analyzer/relatedTests"
(lsp--text-document-position-params))))

(defun lsp-rust-analyzer--select-related-test ()
"Call the endpoint and ask for user selection.
Cannot reuse `lsp-rust-analyzer--select-runnable' because the runnables endpoint
responds with Runnable[], while relatedTests responds with TestInfo[], which is a wrapper
over runnable. Also, this method doesn't set the `lsp-rust-analyzer--last-runnable' variable"
(-if-let* ((resp (lsp-rust-analyzer--related-tests))
(runnables (seq-map
#'lsp:rust-analyzer-related-tests-runnable
resp)))
(lsp--completing-read
"Select test: "
runnables
#'lsp:rust-analyzer-runnable-label)))

(defun lsp-rust-analyzer-related-tests (runnable)
"Execute a RUNNABLE test related to the current document position.
Rust-Analyzer LSP protocol extension
https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/dev/lsp-extensions.md#related-tests"
(interactive (list (lsp-rust-analyzer--select-related-test)))
(if runnable
(lsp-rust-analyzer--common-runner runnable)
(lsp--info "There are no tests related to the symbol at point")))


(provide 'lsp-rust)
;;; lsp-rust.el ends here
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions docs/manual-language-docs/lsp-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ Get a list of possible auto import candidates with `lsp-execute-code-action`

![](../examples/lsp-rust-analyzer-auto-import.png)

### Open Cargo.toml

`lsp-rust-analyzer-open-cargo-toml` opens the Cargo.toml closest to the current file. Calling it with a universal argument will open the Cargo.toml in another window.

Corresponds to [the rust-analyzer LSP extension](https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/dev/lsp-extensions.md#open-cargotoml)

![](./manual-language-docs/lsp-rust-analyzer-open-cargo-toml.gif)

### Find and execute tests related to current position

`lsp-rust-analyzer-related-tests` find all tests related to the current position, asks for user completion and executes the selected test in a compilation buffer.

Corresponds to [the rust-analyzer LSP extension](https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/dev/lsp-extensions.md#related-tests)

In the example below, first you see that
+ On the left, the function `check_infer` is defined, on the right another file is opened with many test functions, some of which call `check_infer`. With the cursor on `check_infer`, call `lsp-rust-analyzer-related-tests` and select `infer_pattern_match_slice` with fuzzy matching. The test is executed on the right with compilation major mode
+ Move the cursor to `fn ellipsize` and attempt to find related tests to no avail. Confirm that the function is indeed untested by using swiper and finding one place in the file, where the function is called

![](./manual-language-docs/lsp-rust-analyzer-find-related-tests.gif)

### Caveats

- Rust Analyzer does not support disabling snippets - https://github.com/rust-analyzer/rust-analyzer/issues/2518
Expand Down
3 changes: 3 additions & 0 deletions lsp-protocol.el
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,14 @@ See `-let' for a description of the destructuring mechanism."
(rust-analyzer:ExpandMacroParams (:textDocument :position) nil)
(rust-analyzer:ExpandedMacro (:name :expansion) nil)
(rust-analyzer:MatchingBraceParams (:textDocument :positions) nil)
(rust-analyzer:OpenCargoTomlParams (:textDocument) nil)
(rust-analyzer:ResovedCodeActionParams (:id :codeActionParams) nil)
(rust-analyzer:JoinLinesParams (:textDocument :ranges) nil)
(rust-analyzer:RunnablesParams (:textDocument) (:position))
(rust-analyzer:Runnable (:label :kind :args) (:location))
(rust-analyzer:RunnableArgs (:cargoArgs :executableArgs) (:workspaceRoot))
(rust-analyzer:RelatedTestsParams (:textDocument :position) nil)
(rust-analyzer:RelatedTests (:runnable) nil)
(rust-analyzer:InlayHint (:range :label :kind) nil)
(rust-analyzer:InlayHintsParams (:textDocument) nil)
(rust-analyzer:SsrParams (:query :parseOnly) nil)
Expand Down

0 comments on commit fd05f0d

Please sign in to comment.