Skip to content

Commit

Permalink
Merge pull request #44 from emacs-rustic/pkg-name
Browse files Browse the repository at this point in the history
Ability to auto populate correct package flag based on project
  • Loading branch information
psibi authored Sep 12, 2024
2 parents d765680 + d61dfb4 commit b6a75c7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.org
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
* v3.5
* Unreleased

- Implement ~rustic-cargo-populate-package-name~ customization
option. This is handy when you are working on multiple
projects. Refer the variable docs and README for more details.

* 3.5

- Revamp testing in CI: Use only cask.
- Replace Makefile with justfile.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ Customization:
- `rustic-cargo-auto-add-missing-dependencies` automatically add missing dependencies
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.

### Edit

Expand Down
39 changes: 37 additions & 2 deletions rustic-cargo.el
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,40 @@
:type 'string
:group 'rustic-cargo)

(defcustom rustic-cargo-populate-package-name nil
"Populate package name automatically when used with universal argument."
:type 'boolean
:group 'rustic-cargo)

(defvar rustic--package-names (make-hash-table :test #'equal))

(defun rustic-cargo-cached-package-name ()
(let ((package-name (gethash default-directory rustic--package-names)))
(if package-name
package-name
(progn
(let ((pkg-name (rustic-cargo-package-name)))
(setf (gethash default-directory rustic--package-names) pkg-name))
(gethash default-directory rustic--package-names)))))

(defun rustic-cargo-package-argument ()
(if rustic-cargo-populate-package-name
(let ((package-name (rustic-cargo-cached-package-name)))
(when package-name
(format "--package %s" package-name)))))

(defun rustic-cargo-package-name ()
(let ((buffer (get-buffer "*cargo-manifest*")))
(if buffer
(kill-buffer buffer)))
(let* ((buffer (get-buffer-create "*cargo-manifest*"))
(exit-code (call-process (rustic-cargo-bin) nil buffer nil "read-manifest")))
(if (eq exit-code 0)
(with-current-buffer buffer
(let ((json-parsed-data (json-read-from-string (buffer-string))))
(cdr (assoc 'name json-parsed-data))))
nil)))

(defun rustic-cargo-bin ()
(if (file-remote-p (or (buffer-file-name) ""))
rustic-cargo-bin-remote
Expand Down Expand Up @@ -186,7 +220,8 @@ If ARG is not nil, use value as argument and store it in
(setq rustic-test-arguments
(read-from-minibuffer "Cargo test arguments: "
(rustic--populate-minibuffer
(list rustic-test-arguments
(list (rustic-cargo-package-argument)
rustic-test-arguments
rustic-cargo-build-arguments
rustic-default-test-arguments)))))
(rustic-cargo-test-run rustic-test-arguments))
Expand Down Expand Up @@ -685,7 +720,7 @@ in your project like `pwd'"
(interactive "P")
(when arg
(setq rustic-cargo-build-arguments
(read-string "Cargo build arguments: " (rustic--populate-minibuffer (list rustic-cargo-build-arguments)))))
(read-string "Cargo build arguments: " (rustic--populate-minibuffer (list (rustic-cargo-package-argument) rustic-cargo-build-arguments)))))
(rustic-run-cargo-command `(,(rustic-cargo-bin)
,rustic-cargo-build-exec-command
,@(split-string rustic-cargo-build-arguments))
Expand Down
1 change: 1 addition & 0 deletions rustic-clippy.el
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ When calling this function from `rustic-popup-mode', always use the value of
(read-from-minibuffer "Cargo clippy arguments: "
(rustic--populate-minibuffer
(list
(rustic-cargo-package-argument)
rustic-clippy-arguments
rustic-cargo-build-arguments
rustic-default-clippy-arguments
Expand Down

0 comments on commit b6a75c7

Please sign in to comment.