Skip to content

Commit

Permalink
Finish the implementation of related-tests
Browse files Browse the repository at this point in the history
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 7, 2021
1 parent 87fe99f commit f2ac5e4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 29 deletions.
54 changes: 25 additions & 29 deletions clients/lsp-rust.el
Original file line number Diff line number Diff line change
Expand Up @@ -881,9 +881,9 @@ and run a compilation"


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

(defun lsp-rust-analyzer-debug (runnable)
Expand Down Expand Up @@ -940,29 +940,23 @@ and run a compilation"
(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
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."
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* ((params (lsp-make-rust-analyzer-syntax-tree-params
:text-document (lsp--text-document-identifier)))
;; REVIEW is there a convention how to define and call rust-analyzer
;; lsp extension methods?
(cargo-toml-location (with-lsp-workspace workspace
(lsp-send-request (lsp-make-request
"experimental/openCargoToml"
params))))
;; REVIEW is there a more idiomatic to retrieve the :uri key
;; or default to nil?
(cargo-toml-uri (gethash "uri" cargo-toml-location)))
(-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 cargo-toml-uri))
;; REVIEW make it an error and/or add more actionable/debuggable info?
(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--warn "Not running rust-analyzer")))
(lsp--error "OpenCargoToml is an extension available only with rust-analyzer")))


(defun lsp-rust-analyzer--related-tests ()
Expand All @@ -978,22 +972,24 @@ Calls a rust-analyzer LSP extension endpoint that returns a wrapper over Runnabl
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"
(lsp--completing-read
"Select test: "
;; since the endpoint returns a list of hash tables that
;; stores &rust-analyzer:Runnable as a value under key
;; "runnable"
;; we need to unpack them first
(mapcar (lambda (runnable) (gethash "runnable" runnable)) (lsp-rust-analyzer--related-tests))
(-lambda ((&rust-analyzer:Runnable :label)) label)))
(-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)))
(lsp-rust-analyzer--common-runner runnable))
(if runnable
(lsp-rust-analyzer--common-runner runnable)
(lsp--info "There are no tests related to the symbol at point")))


(provide 'lsp-rust)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
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 f2ac5e4

Please sign in to comment.