Skip to content
Closed
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
6 changes: 1 addition & 5 deletions CHANGELOG.org
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
option. This is handy when you are working on multiple
projects. Refer the variable docs and README for more details.
- Replace Cask with Eask for CI testing.
- 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.
- Provde integration with [[https://nexte.st/][cargo nextest]]

* 3.5

Expand Down
50 changes: 8 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -531,40 +531,6 @@ 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
------------------------|----------------------
<kbd>C-c C-p</kbd> | rustic-popup
<kbd>C-c C-c C-u</kbd> | rustic-compile
<kbd>C-c C-c C-i</kbd> | rustic-recompile
<kbd>C-c C-c C-o</kbd> | rustic-format-buffer
<kbd>C-c C-c C-,</kbd> | rustic-docstring-dwim
<kbd>C-c C-c C-b</kbd> | rustic-cargo-build
<kbd>C-c C-c C-k</kbd> | rustic-cargo-check
<kbd>C-c C-c C-r</kbd> | rustic-cargo-run
<kbd>C-c C-c C-f</kbd> | rustic-cargo-fmt
<kbd>C-c C-c C-t</kbd> | rustic-cargo-test
<kbd>C-c C-c C-c</kbd> | rustic-cargo-current-test
<kbd>C-c C-c C-l</kbd> | rustic-cargo-clippy
<kbd>C-c C-c C-n</kbd> | rustic-cargo-outdated
<kbd>C-c C-c n</kbd> | rustic-cargo-new
<kbd>C-c C-c i</kbd> | rustic-cargo-init
<kbd>C-c C-c b</kbd> | rustic-cargo-bench
<kbd>C-c C-c d</kbd> | rustic-cargo-doc
<kbd>C-c C-c c</kbd> | rustic-cargo-clean
<kbd>C-c C-c k</kbd> | rustic-cargo-clippy
<kbd>C-c C-c f</kbd> | rustic-cargo-clippy-fix
<kbd>C-c C-c a</kbd> | rustic-cargo-add
<kbd>C-c C-c r</kbd> | rustic-cargo-rm
<kbd>C-c C-c u</kbd> | rustic-cargo-upgrade

More details on each command below

### Edit

[cargo-edit](https://github.com/killercup/cargo-edit) provides commands to edit
Expand All @@ -585,15 +551,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` 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-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-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
63 changes: 19 additions & 44 deletions rustic-cargo.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@
:type 'string
:group 'rustic-cargo)

(defcustom rustic-cargo-nextest-exec-command "nextest run"
"Execute command to run `nextest'."
(defcustom rustic-cargo-test-runner 'cargo
"Test runner to use for running tests. By default uses cargo."
:type '(choice (const cargo)
(const nextest))
:group 'rustic-cargo)

(defcustom rustic-cargo-nextest-exec-command (list "nextest" "run")
"Execute command to run nextest."
:type 'string
:group 'rustic-cargo)

Expand Down Expand Up @@ -170,15 +176,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)
(define-key map (kbd "t") 'rustic-cargo-test-rerun-current)
map)
"Local keymap for `rustic-cargo-test-mode' buffers.")

Expand All @@ -188,37 +193,18 @@ stored in this variable.")
(when rustic-cargo-test-disable-warnings
(setq-local rustic-compile-rustflags (concat rustic-compile-rustflags " -Awarnings"))))

(defun rustic-cargo-run-nextest (&optional arg)
"Command for running nextest.

If ARG is not nil, get input from minibuffer."
(interactive "P")
(let* ((nextest (if arg
(read-from-minibuffer "nextest command: " rustic-cargo-nextest-exec-command)
rustic-cargo-nextest-exec-command))
(c (-flatten (list (rustic-cargo-bin) (split-string nextest))))
(buf rustic-test-buffer-name)
(proc rustic-test-process-name)
(mode 'rustic-cargo-test-mode))
(rustic-compilation c (list :buffer buf :process proc :mode mode))))

(defun rustic-cargo-nextest-current-test ()
"Run `cargo nextest run' for the test near point."
(interactive)
(rustic-compilation-process-live)
(-if-let (test-to-run (setq rustic-test-arguments
(rustic-cargo--get-test-target)))
(let ((rustic-cargo-nextest-exec-command
(format "%s %s" rustic-cargo-nextest-exec-command test-to-run)))
(rustic-cargo-run-nextest))
(message "Could not find test at point.")))
(defun rustic--cargo-test-runner ()
"Return the test runner command. IS-FILTER indicates if only specific test are filtered."
(cond ((eq rustic-cargo-test-runner 'cargo) rustic-cargo-test-exec-command)
((eq rustic-cargo-test-runner 'nextest) rustic-cargo-nextest-exec-command)
(t (user-error "Invalid configured value for rustic-cargo-test-runner variable"))))

;;;###autoload
(defun rustic-cargo-test-run (&optional test-args)
"Start compilation process for `cargo test' with optional TEST-ARGS."
(interactive)
(rustic-compilation-process-live)
(let* ((command (list (rustic-cargo-bin) rustic-cargo-test-exec-command))
(let* ((command (flatten-list (list (rustic-cargo-bin) (rustic--cargo-test-runner))))
(c (append command (split-string (if test-args test-args ""))))
(buf rustic-test-buffer-name)
(proc rustic-test-process-name)
Expand All @@ -242,8 +228,7 @@ 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))
nil nil 'rustic-test-history)))
rustic-default-test-arguments)))))
(rustic-cargo-use-last-stored-arguments
(if (> (length rustic-test-arguments) 0)
rustic-test-arguments
Expand Down Expand Up @@ -289,15 +274,12 @@ 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)))
(progn
(unless (equal (car rustic-test-history) test-to-run)
(push test-to-run rustic-test-history))
(rustic-cargo-run-test test-to-run))
(rustic-cargo-run-test test-to-run)
(message "Could not find test at point.")))

(defun rustic-cargo-run-test (test)
"Run TEST which can be a single test or mod name."
(let* ((c (list (rustic-cargo-bin) rustic-cargo-test-exec-command test))
(let* ((c (flatten-list (list (rustic-cargo-bin) (rustic--cargo-test-runner) test)))
(buf rustic-test-buffer-name)
(proc rustic-test-process-name)
(mode 'rustic-cargo-test-mode))
Expand Down Expand Up @@ -712,9 +694,7 @@ 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 nil nil 'rustic-run-history)))
(setq rustic-run-arguments (read-from-minibuffer "Cargo run arguments: " rustic-run-arguments)))
(rustic-cargo-use-last-stored-arguments
rustic-run-arguments)
((rustic--get-run-arguments))
Expand All @@ -725,11 +705,6 @@ When calling this function from `rustic-popup-mode', always use the value of
"Run `cargo run' with `rustic-run-arguments'."
(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: 3 additions & 4 deletions rustic-compile.el
Original file line number Diff line number Diff line change
Expand Up @@ -565,11 +565,10 @@ 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 (arg)
(defun rustic-recompile ()
"Re-compile the program using `compilation-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))
(interactive)
(let* ((command (or (car compilation-arguments) (format "%s %s" (rustic-compile-command) rustic-cargo-build-arguments)))
(dir compilation-directory))
(rustic-compilation-process-live)
(rustic-compilation-start (split-string command)
Expand Down
25 changes: 11 additions & 14 deletions rustic.el
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,17 @@ as for that macro."
;; this variable is buffer local so we can use the cached value
(if rustic--buffer-workspace
rustic--buffer-workspace
;; Resolve the bin path while still buffer local (in cases like
;; remote via TRAMP)
(let ((cargo-bin (rustic-cargo-bin)))
(rustic--with-temp-process-buffer
(let ((ret (process-file cargo-bin nil (list (current-buffer) nil) nil "locate-project" "--workspace")))
(cond ((and (/= ret 0) nodefault)
(error "`cargo locate-project' returned %s status: %s" ret (buffer-string)))
((and (/= ret 0) (not nodefault))
(setq rustic--buffer-workspace default-directory))
(t
(goto-char 0)
(let* ((output (json-read))
(dir (file-name-directory (cdr (assoc-string "root" output)))))
(setq rustic--buffer-workspace dir)))))))))
(rustic--with-temp-process-buffer
(let ((ret (process-file (rustic-cargo-bin) nil (list (current-buffer) nil) nil "locate-project" "--workspace")))
(cond ((and (/= ret 0) nodefault)
(error "`cargo locate-project' returned %s status: %s" ret (buffer-string)))
((and (/= ret 0) (not nodefault))
(setq rustic--buffer-workspace default-directory))
(t
(goto-char 0)
(let* ((output (json-read))
(dir (file-name-directory (cdr (assoc-string "root" output)))))
(setq rustic--buffer-workspace dir))))))))

(defun rustic-buffer-crate (&optional nodefault)
"Return the crate for the current buffer.
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 nil))
(let* ((proc (rustic-cargo-test-rerun))
(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 nil)))
(let ((proc (rustic-recompile)))
(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 nil)))
(let ((proc (rustic-recompile)))
(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 nil)))
(let ((p (rustic-recompile)))
(while (eq (process-status proc) 'run)
(sit-for 0.1))
(should (= 0 (process-exit-status p))))))))
Expand Down
Loading