diff --git a/CHANGELOG.org b/CHANGELOG.org index 8a55347..95b0bb8 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -7,6 +7,8 @@ - Implement ~rustic-cargo-nextest-current-test~ for running test at a point using nextest. - Fixed an issue where workspaces were not resolved correctly over TRAMP. +- Implement ~rustic-cargo-test-rerun-current~ for rerunning the + current test from the compile buffer. * 3.5 diff --git a/README.md b/README.md index 194c478..0c94d19 100644 --- a/README.md +++ b/README.md @@ -531,6 +531,40 @@ to Cargo.toml by checking new diagnostics for 'unresolved import' errors - `rustic-cargo-use-last-stored-arguments` always use stored arguments that were provided with `C-u`(instead of requiring to run rustic "rerun" commands) - `rustic-cargo-populate-package-name` for auto populating the correct package name when used with universal argument. This comes in handy when you are working with multiple projects. Not enabled by default, but recommened to enable it. + +### Keybindings + +Note that most commands support editing the exact `cargo` arguments and flags when called with the +prefix `C-u`. + + Keybinding | Command +------------------------|---------------------- + C-c C-p | rustic-popup + C-c C-c C-u | rustic-compile + C-c C-c C-i | rustic-recompile + C-c C-c C-o | rustic-format-buffer + C-c C-c C-, | rustic-docstring-dwim + C-c C-c C-b | rustic-cargo-build + C-c C-c C-k | rustic-cargo-check + C-c C-c C-r | rustic-cargo-run + C-c C-c C-f | rustic-cargo-fmt + C-c C-c C-t | rustic-cargo-test + C-c C-c C-c | rustic-cargo-current-test + C-c C-c C-l | rustic-cargo-clippy + C-c C-c C-n | rustic-cargo-outdated + C-c C-c n | rustic-cargo-new + C-c C-c i | rustic-cargo-init + C-c C-c b | rustic-cargo-bench + C-c C-c d | rustic-cargo-doc + C-c C-c c | rustic-cargo-clean + C-c C-c k | rustic-cargo-clippy + C-c C-c f | rustic-cargo-clippy-fix + C-c C-c a | rustic-cargo-add + C-c C-c r | rustic-cargo-rm + C-c C-c u | rustic-cargo-upgrade + + More details on each command below + ### Edit [cargo-edit](https://github.com/killercup/cargo-edit) provides commands to edit @@ -551,15 +585,15 @@ If you want to disable warnings when running cargo-test commands, you can set Commands: -- `rustic-cargo-test` run 'cargo test', when called with `C-u` store - arguments in `rustic-test-arguments` -- `rustic-cargo-test-rerun` rerun 'cargo test' with arguments stored - in `rustic-test-arguments` -- `rustic-cargo-current-test` run test at point, whether it's a - function or a module +- `rustic-cargo-test` run 'cargo test', when called with `C-u` edit the command + before running and store in `rustic-test-arguments`. +- `rustic-cargo-test-rerun` (`g` from compile buffer) rerun 'cargo test' with arguments stored in + `rustic-test-arguments` +- `rustic-cargo-current-test` run test at point, whether it's a function or a module +- `rustic-cargo-test-rerun-current` (`C-c C-t` or `t` from compile buffer) re-run the test at point + from the `*cargo-test*` compile buffer. - `rustic-cargo-run-nextest` command for running [nextest](https://github.com/nextest-rs/nextest) -- `rustic-cargo-nextest-current-test` is the nextest equivalent for - `rustic-cargo-current-test` +- `rustic-cargo-nextest-current-test` is the nextest equivalent for `rustic-cargo-current-test` ![](https://raw.githubusercontent.com/emacs-rustic/rustic/main/img/cargo_current_test.png) diff --git a/rustic-cargo.el b/rustic-cargo.el index 2cf7927..85bc838 100644 --- a/rustic-cargo.el +++ b/rustic-cargo.el @@ -170,10 +170,15 @@ Currently only working with lsp-mode." Tests that are executed by `rustic-cargo-current-test' will also be stored in this variable.") +(defvar rustic-test-history nil + "Holds previous arguments for 'cargo test', similar to `compile-arguments`.") + (defvar rustic-cargo-test-mode-map (let ((map (make-sparse-keymap))) (set-keymap-parent map rustic-compilation-mode-map) (define-key map [remap recompile] 'rustic-cargo-test-rerun) + (define-key map (kbd "C-c C-t") 'rustic-cargo-test-rerun-current) + (define-key map (kbd "t") 'rustic-cargo-test-rerun-current) map) "Local keymap for `rustic-cargo-test-mode' buffers.") @@ -237,7 +242,8 @@ If ARG is not nil, use value as argument and store it in (list (rustic-cargo-package-argument) rustic-test-arguments rustic-cargo-build-arguments - rustic-default-test-arguments))))) + rustic-default-test-arguments)) + nil nil 'rustic-test-history))) (rustic-cargo-use-last-stored-arguments (if (> (length rustic-test-arguments) 0) rustic-test-arguments @@ -246,12 +252,36 @@ If ARG is not nil, use value as argument and store it in rustic-default-test-arguments)))) ;;;###autoload -(defun rustic-cargo-test-rerun () +(defun rustic-cargo-test-rerun (arg) "Run 'cargo test' with `rustic-test-arguments'." - (interactive) + (interactive "P") (let ((default-directory (or rustic-compilation-directory default-directory))) + (setq rustic-test-arguments + (if arg + (read-from-minibuffer "Cargo test arguments: " rustic-test-arguments nil nil 'rustic-test-history) + rustic-test-arguments)) + (rustic-cargo-test-run rustic-test-arguments))) + +(defun rustic-cargo-test-rerun-current (arg) + "Rerun the test at point from `rustic-cargo-test-mode'." + (interactive "P") + (let* ((default-directory (or rustic-compilation-directory default-directory)) + (test (rustic-cargo--get-test-at-point)) + (command (if test + (concat "-- --exact " test) + (error "No test found at point")))) + (setq rustic-test-arguments + (if arg + (read-from-minibuffer "Cargo test arguments: " command nil nil 'rustic-test-history) + command)) (rustic-cargo-test-run rustic-test-arguments))) +(defun rustic-cargo--get-test-at-point () + (save-excursion + (beginning-of-line) + (when (re-search-forward "^test \\([^ ]+\\) ..." (line-end-position) t) + (buffer-substring-no-properties (match-beginning 1) (match-end 1))))) + ;;;###autoload (defun rustic-cargo-current-test () "Run 'cargo test' for the test near point." @@ -259,7 +289,10 @@ If ARG is not nil, use value as argument and store it in (rustic-compilation-process-live) (-if-let (test-to-run (setq rustic-test-arguments (rustic-cargo--get-test-target))) - (rustic-cargo-run-test test-to-run) + (progn + (unless (equal (car rustic-test-history) test-to-run) + (push test-to-run rustic-test-history)) + (rustic-cargo-run-test test-to-run)) (message "Could not find test at point."))) (defun rustic-cargo-run-test (test) @@ -645,6 +678,9 @@ If BIN is not nil, create a binary application, otherwise a library." (defvar rustic-run-arguments "" "Holds arguments for 'cargo run', similar to `compilation-arguments`.") +(defvar rustic-run-history nil + "Holds previous arguments for 'cargo run', similar to `compile-history`.") + (defvar rustic-cargo-run-mode-map (let ((map (make-sparse-keymap))) (define-key map [remap recompile] 'rustic-cargo-run-rerun) @@ -676,17 +712,24 @@ When calling this function from `rustic-popup-mode', always use the value of (interactive "P") (rustic-cargo-run-command (cond (arg - (setq rustic-run-arguments (read-from-minibuffer "Cargo run arguments: " rustic-run-arguments))) + (setq rustic-run-arguments + (read-from-minibuffer "Cargo run arguments: " + rustic-run-arguments nil nil 'rustic-run-history))) (rustic-cargo-use-last-stored-arguments rustic-run-arguments) ((rustic--get-run-arguments)) (t rustic-run-arguments)))) ;;;###autoload -(defun rustic-cargo-run-rerun () +(defun rustic-cargo-run-rerun (arg) "Run 'cargo run' with `rustic-run-arguments'." - (interactive) + (interactive "P") (let ((default-directory (or rustic-compilation-directory default-directory))) + (setq rustic-run-arguments + (if arg + (read-from-minibuffer "cargo run arguments: " + rustic-run-arguments nil nil 'rustic-run-history) + rustic-run-arguments)) (rustic-cargo-run-command rustic-run-arguments))) (defun rustic--get-run-arguments () diff --git a/rustic-compile.el b/rustic-compile.el index ee19cbb..76d30d3 100644 --- a/rustic-compile.el +++ b/rustic-compile.el @@ -565,10 +565,11 @@ It's a list that looks like (list command mode name-function highlight-regexp)." (setq compilation-arguments (list command nil nil nil))) ;;;###autoload -(defun rustic-recompile () +(defun rustic-recompile (arg) "Re-compile the program using `compilation-arguments'." - (interactive) - (let* ((command (or (car compilation-arguments) (format "%s %s" (rustic-compile-command) rustic-cargo-build-arguments))) + (interactive "P") + (let* ((comp-arguments (or (car compilation-arguments) (format "%s %s" (rustic-compile-command) rustic-cargo-build-arguments))) + (command (if arg (read-from-minibuffer "recompile: " comp-arguments) comp-arguments)) (dir compilation-directory)) (rustic-compilation-process-live) (rustic-compilation-start (split-string command) diff --git a/test/rustic-cargo-test.el b/test/rustic-cargo-test.el index 18158ae..a026511 100644 --- a/test/rustic-cargo-test.el +++ b/test/rustic-cargo-test.el @@ -352,7 +352,7 @@ fn test2() { (proc-buf (process-buffer proc))) (while (eq (process-status proc) 'run) (sit-for 0.1))) - (let* ((proc (rustic-cargo-test-rerun)) + (let* ((proc (rustic-cargo-test-rerun nil)) (proc-buf (process-buffer proc))) (while (eq (process-status proc) 'run) (sit-for 0.1)) diff --git a/test/rustic-compile-test.el b/test/rustic-compile-test.el index 4a5c656..1ba9cbb 100644 --- a/test/rustic-compile-test.el +++ b/test/rustic-compile-test.el @@ -91,7 +91,7 @@ (while (eq (process-status proc) 'run) (sit-for 0.1)) (should (string= compilation-directory dir)) - (let ((proc (rustic-recompile))) + (let ((proc (rustic-recompile nil))) (while (eq (process-status proc) 'run) (sit-for 0.1))) (should (string= (car compilation-arguments) "cargo fmt")) @@ -108,7 +108,7 @@ (while (eq (process-status proc) 'run) (sit-for 0.1)) (should (string= compilation-directory dir)) - (let ((proc (rustic-recompile))) + (let ((proc (rustic-recompile nil))) (while (eq (process-status proc) 'run) (sit-for 0.1))) (should (string= (car compilation-arguments) "cargo build")) @@ -127,7 +127,7 @@ (while (eq (process-status proc) 'run) (sit-for 0.01)) (should (= 0 (process-exit-status proc))) - (let ((p (rustic-recompile))) + (let ((p (rustic-recompile nil))) (while (eq (process-status proc) 'run) (sit-for 0.1)) (should (= 0 (process-exit-status p))))))))