diff --git a/CHANGELOG.org b/CHANGELOG.org index 14cd4e45294..a4fddcd3768 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -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~ diff --git a/clients/lsp-rust.el b/clients/lsp-rust.el index b2612cc115f..1894f4969fd 100644 --- a/clients/lsp-rust.el +++ b/clients/lsp-rust.el @@ -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))) @@ -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)) @@ -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 @@ -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 diff --git a/docs/manual-language-docs/lsp-rust-analyzer-find-related-tests.gif b/docs/manual-language-docs/lsp-rust-analyzer-find-related-tests.gif new file mode 100644 index 00000000000..bdd609ac53e Binary files /dev/null and b/docs/manual-language-docs/lsp-rust-analyzer-find-related-tests.gif differ diff --git a/docs/manual-language-docs/lsp-rust-analyzer-open-cargo-toml.gif b/docs/manual-language-docs/lsp-rust-analyzer-open-cargo-toml.gif new file mode 100644 index 00000000000..dbad8395ee9 Binary files /dev/null and b/docs/manual-language-docs/lsp-rust-analyzer-open-cargo-toml.gif differ diff --git a/docs/manual-language-docs/lsp-rust.md b/docs/manual-language-docs/lsp-rust.md index f2afa6f5bb7..bf5b1a18feb 100644 --- a/docs/manual-language-docs/lsp-rust.md +++ b/docs/manual-language-docs/lsp-rust.md @@ -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 diff --git a/lsp-protocol.el b/lsp-protocol.el index 806b1fbceab..42eada19b5a 100644 --- a/lsp-protocol.el +++ b/lsp-protocol.el @@ -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)