Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 42 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------------|----------------------
<kdb>C-c C-p</kdb> | rustic-popup
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these supposed to be kbd?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes... 🤦‍♂️

<kdb>C-c C-c C-u</kdb> | rustic-compile
<kdb>C-c C-c C-i</kdb> | rustic-recompile
<kdb>C-c C-c C-o</kdb> | rustic-format-buffer
<kdb>C-c C-c C-,</kdb> | rustic-docstring-dwim
<kdb>C-c C-c C-b</kdb> | rustic-cargo-build
<kdb>C-c C-c C-k</kdb> | rustic-cargo-check
<kdb>C-c C-c C-r</kdb> | rustic-cargo-run
<kdb>C-c C-c C-f</kdb> | rustic-cargo-fmt
<kdb>C-c C-c C-t</kdb> | rustic-cargo-test
<kdb>C-c C-c C-c</kdb> | rustic-cargo-current-test
<kdb>C-c C-c C-l</kdb> | rustic-cargo-clippy
<kdb>C-c C-c C-n</kdb> | rustic-cargo-outdated
<kdb>C-c C-c n</kdb> | rustic-cargo-new
<kdb>C-c C-c i</kdb> | rustic-cargo-init
<kdb>C-c C-c b</kdb> | rustic-cargo-bench
<kdb>C-c C-c d</kdb> | rustic-cargo-doc
<kdb>C-c C-c c</kdb> | rustic-cargo-clean
<kdb>C-c C-c k</kdb> | rustic-cargo-clippy
<kdb>C-c C-c f</kdb> | rustic-cargo-clippy-fix
<kdb>C-c C-c a</kdb> | rustic-cargo-add
<kdb>C-c C-c r</kdb> | rustic-cargo-rm
<kdb>C-c C-c u</kdb> | rustic-cargo-upgrade

More details on each command below

### Edit

[cargo-edit](https://github.com/killercup/cargo-edit) provides commands to edit
Expand All @@ -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)

Expand Down
57 changes: 50 additions & 7 deletions rustic-cargo.el
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down Expand Up @@ -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
Expand All @@ -246,20 +252,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 +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)
Expand Down Expand Up @@ -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 ()
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