Skip to content

Commit b6a75c7

Browse files
authored
Merge pull request #44 from emacs-rustic/pkg-name
Ability to auto populate correct package flag based on project
2 parents d765680 + d61dfb4 commit b6a75c7

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

CHANGELOG.org

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
* v3.5
1+
* Unreleased
2+
3+
- Implement ~rustic-cargo-populate-package-name~ customization
4+
option. This is handy when you are working on multiple
5+
projects. Refer the variable docs and README for more details.
6+
7+
* 3.5
28

39
- Revamp testing in CI: Use only cask.
410
- Replace Makefile with justfile.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ Customization:
529529
- `rustic-cargo-auto-add-missing-dependencies` automatically add missing dependencies
530530
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)
532+
- `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.
532533

533534
### Edit
534535

rustic-cargo.el

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,40 @@
4949
:type 'string
5050
:group 'rustic-cargo)
5151

52+
(defcustom rustic-cargo-populate-package-name nil
53+
"Populate package name automatically when used with universal argument."
54+
:type 'boolean
55+
:group 'rustic-cargo)
56+
57+
(defvar rustic--package-names (make-hash-table :test #'equal))
58+
59+
(defun rustic-cargo-cached-package-name ()
60+
(let ((package-name (gethash default-directory rustic--package-names)))
61+
(if package-name
62+
package-name
63+
(progn
64+
(let ((pkg-name (rustic-cargo-package-name)))
65+
(setf (gethash default-directory rustic--package-names) pkg-name))
66+
(gethash default-directory rustic--package-names)))))
67+
68+
(defun rustic-cargo-package-argument ()
69+
(if rustic-cargo-populate-package-name
70+
(let ((package-name (rustic-cargo-cached-package-name)))
71+
(when package-name
72+
(format "--package %s" package-name)))))
73+
74+
(defun rustic-cargo-package-name ()
75+
(let ((buffer (get-buffer "*cargo-manifest*")))
76+
(if buffer
77+
(kill-buffer buffer)))
78+
(let* ((buffer (get-buffer-create "*cargo-manifest*"))
79+
(exit-code (call-process (rustic-cargo-bin) nil buffer nil "read-manifest")))
80+
(if (eq exit-code 0)
81+
(with-current-buffer buffer
82+
(let ((json-parsed-data (json-read-from-string (buffer-string))))
83+
(cdr (assoc 'name json-parsed-data))))
84+
nil)))
85+
5286
(defun rustic-cargo-bin ()
5387
(if (file-remote-p (or (buffer-file-name) ""))
5488
rustic-cargo-bin-remote
@@ -186,7 +220,8 @@ If ARG is not nil, use value as argument and store it in
186220
(setq rustic-test-arguments
187221
(read-from-minibuffer "Cargo test arguments: "
188222
(rustic--populate-minibuffer
189-
(list rustic-test-arguments
223+
(list (rustic-cargo-package-argument)
224+
rustic-test-arguments
190225
rustic-cargo-build-arguments
191226
rustic-default-test-arguments)))))
192227
(rustic-cargo-test-run rustic-test-arguments))
@@ -685,7 +720,7 @@ in your project like `pwd'"
685720
(interactive "P")
686721
(when arg
687722
(setq rustic-cargo-build-arguments
688-
(read-string "Cargo build arguments: " (rustic--populate-minibuffer (list rustic-cargo-build-arguments)))))
723+
(read-string "Cargo build arguments: " (rustic--populate-minibuffer (list (rustic-cargo-package-argument) rustic-cargo-build-arguments)))))
689724
(rustic-run-cargo-command `(,(rustic-cargo-bin)
690725
,rustic-cargo-build-exec-command
691726
,@(split-string rustic-cargo-build-arguments))

rustic-clippy.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ When calling this function from `rustic-popup-mode', always use the value of
8484
(read-from-minibuffer "Cargo clippy arguments: "
8585
(rustic--populate-minibuffer
8686
(list
87+
(rustic-cargo-package-argument)
8788
rustic-clippy-arguments
8889
rustic-cargo-build-arguments
8990
rustic-default-clippy-arguments

0 commit comments

Comments
 (0)