Skip to content

Commit 5b91d05

Browse files
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.
1 parent c1e1d5d commit 5b91d05

File tree

6 files changed

+61
-14
lines changed

6 files changed

+61
-14
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,8 @@ Commands:
560560
- `rustic-cargo-run-nextest` command for running [nextest](https://github.com/nextest-rs/nextest)
561561
- `rustic-cargo-nextest-current-test` is the nextest equivalent for
562562
`rustic-cargo-current-test`
563+
- `rustic-cargo-test-rerun-current` re-run the test at point from the
564+
`*cargo-test*` compile buffer. (bound to `C-c C-t`)
563565

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

rustic-cargo.el

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,14 @@ 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)
177181
map)
178182
"Local keymap for `rustic-cargo-test-mode' buffers.")
179183

@@ -237,7 +241,8 @@ If ARG is not nil, use value as argument and store it in
237241
(list (rustic-cargo-package-argument)
238242
rustic-test-arguments
239243
rustic-cargo-build-arguments
240-
rustic-default-test-arguments)))))
244+
rustic-default-test-arguments))
245+
nil nil 'rustic-test-history)))
241246
(rustic-cargo-use-last-stored-arguments
242247
(if (> (length rustic-test-arguments) 0)
243248
rustic-test-arguments
@@ -246,20 +251,47 @@ If ARG is not nil, use value as argument and store it in
246251
rustic-default-test-arguments))))
247252

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

278+
(defun rustic-cargo--get-test-at-point ()
279+
(save-excursion
280+
(beginning-of-line)
281+
(when (re-search-forward "^test \\([^ ]+\\) ..." (line-end-position) t)
282+
(buffer-substring-no-properties (match-beginning 1) (match-end 1)))))
283+
255284
;;;###autoload
256285
(defun rustic-cargo-current-test ()
257286
"Run 'cargo test' for the test near point."
258287
(interactive)
259288
(rustic-compilation-process-live)
260289
(-if-let (test-to-run (setq rustic-test-arguments
261290
(rustic-cargo--get-test-target)))
262-
(rustic-cargo-run-test test-to-run)
291+
(progn
292+
(unless (equal (car rustic-test-history) test-to-run)
293+
(push test-to-run rustic-test-history))
294+
(rustic-cargo-run-test test-to-run))
263295
(message "Could not find test at point.")))
264296

265297
(defun rustic-cargo-run-test (test)
@@ -645,6 +677,9 @@ If BIN is not nil, create a binary application, otherwise a library."
645677
(defvar rustic-run-arguments ""
646678
"Holds arguments for 'cargo run', similar to `compilation-arguments`.")
647679

680+
(defvar rustic-run-history nil
681+
"Holds previous arguments for 'cargo run', similar to `compile-history`.")
682+
648683
(defvar rustic-cargo-run-mode-map
649684
(let ((map (make-sparse-keymap)))
650685
(define-key map [remap recompile] 'rustic-cargo-run-rerun)
@@ -676,17 +711,24 @@ When calling this function from `rustic-popup-mode', always use the value of
676711
(interactive "P")
677712
(rustic-cargo-run-command
678713
(cond (arg
679-
(setq rustic-run-arguments (read-from-minibuffer "Cargo run arguments: " rustic-run-arguments)))
714+
(setq rustic-run-arguments
715+
(read-from-minibuffer "Cargo run arguments: "
716+
rustic-run-arguments nil nil 'rustic-run-history)))
680717
(rustic-cargo-use-last-stored-arguments
681718
rustic-run-arguments)
682719
((rustic--get-run-arguments))
683720
(t rustic-run-arguments))))
684721

685722
;;;###autoload
686-
(defun rustic-cargo-run-rerun ()
723+
(defun rustic-cargo-run-rerun (arg)
687724
"Run 'cargo run' with `rustic-run-arguments'."
688-
(interactive)
725+
(interactive "P")
689726
(let ((default-directory (or rustic-compilation-directory default-directory)))
727+
(setq rustic-run-arguments
728+
(if arg
729+
(read-from-minibuffer "cargo run arguments: "
730+
rustic-run-arguments nil nil 'rustic-run-history)
731+
rustic-run-arguments))
690732
(rustic-cargo-run-command rustic-run-arguments)))
691733

692734
(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)