Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.org
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,8 @@ Commands:
- `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-test-rerun-current` re-run the test at point from the
`*cargo-test*` compile buffer. (bound to `C-c C-t`)

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

Expand Down
56 changes: 49 additions & 7 deletions rustic-cargo.el
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,14 @@ 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)
map)
"Local keymap for `rustic-cargo-test-mode' buffers.")

Expand Down Expand Up @@ -237,7 +241,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
Expand All @@ -246,20 +251,47 @@ 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."
(interactive)
(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)
Expand Down Expand Up @@ -645,6 +677,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)
Expand Down Expand Up @@ -676,17 +711,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 ()
Expand Down
7 changes: 4 additions & 3 deletions rustic-compile.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/rustic-cargo-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions test/rustic-compile-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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"))
Expand All @@ -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))))))))
Expand Down