Skip to content

Commit 4404bdb

Browse files
Add ability to edit command before recompile and a function to rerun (#78)
* Add ability to edit command before recompile and a function to rerun See #77 This change does three things: First it adds a prefix argument to the `recompile` and `rerun` commands so the cargo command can be edited before running. Second it adds a new function `rustic-cargo-test-rerun-current`, that can be called from a `rustic-cargo-test-mode` buffer to rerun the test at point. Bound to `C-c C-t` by default. Third, it adds minibuffer history support to the cargo-run-* and cargo-test-* commands. * Add better documentation to commands
1 parent c1e1d5d commit 4404bdb

File tree

6 files changed

+102
-22
lines changed

6 files changed

+102
-22
lines changed

CHANGELOG.org

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
- Implement ~rustic-cargo-nextest-current-test~ for running test at a
88
point using nextest.
99
- Fixed an issue where workspaces were not resolved correctly over TRAMP.
10+
- Implement ~rustic-cargo-test-rerun-current~ for rerunning the
11+
current test from the compile buffer.
1012

1113
* 3.5
1214

README.md

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,40 @@ to Cargo.toml by checking new diagnostics for 'unresolved import' errors
531531
- `rustic-cargo-use-last-stored-arguments` always use stored arguments that were provided with `C-u`(instead of requiring to run rustic "rerun" commands)
532532
- `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.
533533

534+
535+
### Keybindings
536+
537+
Note that most commands support editing the exact `cargo` arguments and flags when called with the
538+
prefix `C-u`.
539+
540+
Keybinding | Command
541+
------------------------|----------------------
542+
<kdb>C-c C-p</kdb> | rustic-popup
543+
<kdb>C-c C-c C-u</kdb> | rustic-compile
544+
<kdb>C-c C-c C-i</kdb> | rustic-recompile
545+
<kdb>C-c C-c C-o</kdb> | rustic-format-buffer
546+
<kdb>C-c C-c C-,</kdb> | rustic-docstring-dwim
547+
<kdb>C-c C-c C-b</kdb> | rustic-cargo-build
548+
<kdb>C-c C-c C-k</kdb> | rustic-cargo-check
549+
<kdb>C-c C-c C-r</kdb> | rustic-cargo-run
550+
<kdb>C-c C-c C-f</kdb> | rustic-cargo-fmt
551+
<kdb>C-c C-c C-t</kdb> | rustic-cargo-test
552+
<kdb>C-c C-c C-c</kdb> | rustic-cargo-current-test
553+
<kdb>C-c C-c C-l</kdb> | rustic-cargo-clippy
554+
<kdb>C-c C-c C-n</kdb> | rustic-cargo-outdated
555+
<kdb>C-c C-c n</kdb> | rustic-cargo-new
556+
<kdb>C-c C-c i</kdb> | rustic-cargo-init
557+
<kdb>C-c C-c b</kdb> | rustic-cargo-bench
558+
<kdb>C-c C-c d</kdb> | rustic-cargo-doc
559+
<kdb>C-c C-c c</kdb> | rustic-cargo-clean
560+
<kdb>C-c C-c k</kdb> | rustic-cargo-clippy
561+
<kdb>C-c C-c f</kdb> | rustic-cargo-clippy-fix
562+
<kdb>C-c C-c a</kdb> | rustic-cargo-add
563+
<kdb>C-c C-c r</kdb> | rustic-cargo-rm
564+
<kdb>C-c C-c u</kdb> | rustic-cargo-upgrade
565+
566+
More details on each command below
567+
534568
### Edit
535569

536570
[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
551585

552586
Commands:
553587

554-
- `rustic-cargo-test` run 'cargo test', when called with `C-u` store
555-
arguments in `rustic-test-arguments`
556-
- `rustic-cargo-test-rerun` rerun 'cargo test' with arguments stored
557-
in `rustic-test-arguments`
558-
- `rustic-cargo-current-test` run test at point, whether it's a
559-
function or a module
588+
- `rustic-cargo-test` run 'cargo test', when called with `C-u` edit the command
589+
before running and store in `rustic-test-arguments`.
590+
- `rustic-cargo-test-rerun` (`g` from compile buffer) rerun 'cargo test' with arguments stored in
591+
`rustic-test-arguments`
592+
- `rustic-cargo-current-test` run test at point, whether it's a function or a module
593+
- `rustic-cargo-test-rerun-current` (`C-c C-t` or `t` from compile buffer) re-run the test at point
594+
from the `*cargo-test*` compile buffer.
560595
- `rustic-cargo-run-nextest` command for running [nextest](https://github.com/nextest-rs/nextest)
561-
- `rustic-cargo-nextest-current-test` is the nextest equivalent for
562-
`rustic-cargo-current-test`
596+
- `rustic-cargo-nextest-current-test` is the nextest equivalent for `rustic-cargo-current-test`
563597

564598
![](https://raw.githubusercontent.com/emacs-rustic/rustic/main/img/cargo_current_test.png)
565599

rustic-cargo.el

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,15 @@ Currently only working with lsp-mode."
170170
Tests that are executed by `rustic-cargo-current-test' will also be
171171
stored in this variable.")
172172

173+
(defvar rustic-test-history nil
174+
"Holds previous arguments for 'cargo test', similar to `compile-arguments`.")
175+
173176
(defvar rustic-cargo-test-mode-map
174177
(let ((map (make-sparse-keymap)))
175178
(set-keymap-parent map rustic-compilation-mode-map)
176179
(define-key map [remap recompile] 'rustic-cargo-test-rerun)
180+
(define-key map (kbd "C-c C-t") 'rustic-cargo-test-rerun-current)
181+
(define-key map (kbd "t") 'rustic-cargo-test-rerun-current)
177182
map)
178183
"Local keymap for `rustic-cargo-test-mode' buffers.")
179184

@@ -237,7 +242,8 @@ If ARG is not nil, use value as argument and store it in
237242
(list (rustic-cargo-package-argument)
238243
rustic-test-arguments
239244
rustic-cargo-build-arguments
240-
rustic-default-test-arguments)))))
245+
rustic-default-test-arguments))
246+
nil nil 'rustic-test-history)))
241247
(rustic-cargo-use-last-stored-arguments
242248
(if (> (length rustic-test-arguments) 0)
243249
rustic-test-arguments
@@ -246,20 +252,47 @@ If ARG is not nil, use value as argument and store it in
246252
rustic-default-test-arguments))))
247253

248254
;;;###autoload
249-
(defun rustic-cargo-test-rerun ()
255+
(defun rustic-cargo-test-rerun (arg)
250256
"Run 'cargo test' with `rustic-test-arguments'."
251-
(interactive)
257+
(interactive "P")
252258
(let ((default-directory (or rustic-compilation-directory default-directory)))
259+
(setq rustic-test-arguments
260+
(if arg
261+
(read-from-minibuffer "Cargo test arguments: " rustic-test-arguments nil nil 'rustic-test-history)
262+
rustic-test-arguments))
263+
(rustic-cargo-test-run rustic-test-arguments)))
264+
265+
(defun rustic-cargo-test-rerun-current (arg)
266+
"Rerun the test at point from `rustic-cargo-test-mode'."
267+
(interactive "P")
268+
(let* ((default-directory (or rustic-compilation-directory default-directory))
269+
(test (rustic-cargo--get-test-at-point))
270+
(command (if test
271+
(concat "-- --exact " test)
272+
(error "No test found at point"))))
273+
(setq rustic-test-arguments
274+
(if arg
275+
(read-from-minibuffer "Cargo test arguments: " command nil nil 'rustic-test-history)
276+
command))
253277
(rustic-cargo-test-run rustic-test-arguments)))
254278

279+
(defun rustic-cargo--get-test-at-point ()
280+
(save-excursion
281+
(beginning-of-line)
282+
(when (re-search-forward "^test \\([^ ]+\\) ..." (line-end-position) t)
283+
(buffer-substring-no-properties (match-beginning 1) (match-end 1)))))
284+
255285
;;;###autoload
256286
(defun rustic-cargo-current-test ()
257287
"Run 'cargo test' for the test near point."
258288
(interactive)
259289
(rustic-compilation-process-live)
260290
(-if-let (test-to-run (setq rustic-test-arguments
261291
(rustic-cargo--get-test-target)))
262-
(rustic-cargo-run-test test-to-run)
292+
(progn
293+
(unless (equal (car rustic-test-history) test-to-run)
294+
(push test-to-run rustic-test-history))
295+
(rustic-cargo-run-test test-to-run))
263296
(message "Could not find test at point.")))
264297

265298
(defun rustic-cargo-run-test (test)
@@ -645,6 +678,9 @@ If BIN is not nil, create a binary application, otherwise a library."
645678
(defvar rustic-run-arguments ""
646679
"Holds arguments for 'cargo run', similar to `compilation-arguments`.")
647680

681+
(defvar rustic-run-history nil
682+
"Holds previous arguments for 'cargo run', similar to `compile-history`.")
683+
648684
(defvar rustic-cargo-run-mode-map
649685
(let ((map (make-sparse-keymap)))
650686
(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
676712
(interactive "P")
677713
(rustic-cargo-run-command
678714
(cond (arg
679-
(setq rustic-run-arguments (read-from-minibuffer "Cargo run arguments: " rustic-run-arguments)))
715+
(setq rustic-run-arguments
716+
(read-from-minibuffer "Cargo run arguments: "
717+
rustic-run-arguments nil nil 'rustic-run-history)))
680718
(rustic-cargo-use-last-stored-arguments
681719
rustic-run-arguments)
682720
((rustic--get-run-arguments))
683721
(t rustic-run-arguments))))
684722

685723
;;;###autoload
686-
(defun rustic-cargo-run-rerun ()
724+
(defun rustic-cargo-run-rerun (arg)
687725
"Run 'cargo run' with `rustic-run-arguments'."
688-
(interactive)
726+
(interactive "P")
689727
(let ((default-directory (or rustic-compilation-directory default-directory)))
728+
(setq rustic-run-arguments
729+
(if arg
730+
(read-from-minibuffer "cargo run arguments: "
731+
rustic-run-arguments nil nil 'rustic-run-history)
732+
rustic-run-arguments))
690733
(rustic-cargo-run-command rustic-run-arguments)))
691734

692735
(defun rustic--get-run-arguments ()

rustic-compile.el

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,11 @@ It's a list that looks like (list command mode name-function highlight-regexp)."
565565
(setq compilation-arguments (list command nil nil nil)))
566566

567567
;;;###autoload
568-
(defun rustic-recompile ()
568+
(defun rustic-recompile (arg)
569569
"Re-compile the program using `compilation-arguments'."
570-
(interactive)
571-
(let* ((command (or (car compilation-arguments) (format "%s %s" (rustic-compile-command) rustic-cargo-build-arguments)))
570+
(interactive "P")
571+
(let* ((comp-arguments (or (car compilation-arguments) (format "%s %s" (rustic-compile-command) rustic-cargo-build-arguments)))
572+
(command (if arg (read-from-minibuffer "recompile: " comp-arguments) comp-arguments))
572573
(dir compilation-directory))
573574
(rustic-compilation-process-live)
574575
(rustic-compilation-start (split-string command)

test/rustic-cargo-test.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ fn test2() {
352352
(proc-buf (process-buffer proc)))
353353
(while (eq (process-status proc) 'run)
354354
(sit-for 0.1)))
355-
(let* ((proc (rustic-cargo-test-rerun))
355+
(let* ((proc (rustic-cargo-test-rerun nil))
356356
(proc-buf (process-buffer proc)))
357357
(while (eq (process-status proc) 'run)
358358
(sit-for 0.1))

test/rustic-compile-test.el

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
(while (eq (process-status proc) 'run)
9292
(sit-for 0.1))
9393
(should (string= compilation-directory dir))
94-
(let ((proc (rustic-recompile)))
94+
(let ((proc (rustic-recompile nil)))
9595
(while (eq (process-status proc) 'run)
9696
(sit-for 0.1)))
9797
(should (string= (car compilation-arguments) "cargo fmt"))
@@ -108,7 +108,7 @@
108108
(while (eq (process-status proc) 'run)
109109
(sit-for 0.1))
110110
(should (string= compilation-directory dir))
111-
(let ((proc (rustic-recompile)))
111+
(let ((proc (rustic-recompile nil)))
112112
(while (eq (process-status proc) 'run)
113113
(sit-for 0.1)))
114114
(should (string= (car compilation-arguments) "cargo build"))
@@ -127,7 +127,7 @@
127127
(while (eq (process-status proc) 'run)
128128
(sit-for 0.01))
129129
(should (= 0 (process-exit-status proc)))
130-
(let ((p (rustic-recompile)))
130+
(let ((p (rustic-recompile nil)))
131131
(while (eq (process-status proc) 'run)
132132
(sit-for 0.1))
133133
(should (= 0 (process-exit-status p))))))))

0 commit comments

Comments
 (0)