Skip to content

Commit

Permalink
Implementing suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipBAdams committed Jul 29, 2018
1 parent 92286e1 commit 2c730c4
Showing 1 changed file with 40 additions and 48 deletions.
88 changes: 40 additions & 48 deletions cargo-process.el
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
;; * cargo-process-rm - Run the optional cargo command rm.
;; * cargo-process-upgrade - Run the optional cargo command upgrade.


;;
;;; Code:

Expand Down Expand Up @@ -136,7 +135,6 @@

(defvar cargo-process-favorite-crates nil)


(defface cargo-process--ok-face
'((t (:foreground "#00ff00")))
"Ok face"
Expand Down Expand Up @@ -202,10 +200,10 @@
(buffer-substring-no-properties (line-beginning-position)
(line-end-position))))

(defun cargo-process--project-root ()
(defun cargo-process--project-root (&optional extra)
"Find the root of the current Cargo project."
(let ((root (locate-dominating-file (or buffer-file-name default-directory) "Cargo.toml")))
(and root (file-truename root))))
(and root (file-truename (concat root extra)))))

(define-derived-mode cargo-process-mode compilation-mode "Cargo-Process."
"Major mode for the Cargo process buffer."
Expand Down Expand Up @@ -349,23 +347,22 @@ Without the prefix argument, return DEFAULT immediately."
(read-shell-command "Cargo command: " default)
default))

(defun cargo-process--get-dependencies (manifest)
(defun cargo-process--get-dependencies (&optional manifest)
"Extract the list of dependencies from the
MANIFEST (i.e. Cargo.toml)."
(save-excursion
(let ((buffer (find-file-noselect manifest))
(deps-list))
(set-buffer buffer)
(setq buffer-read-only t)
(widen)
(goto-char (point-min))
(search-forward "[dependencies]" nil t)
(while (re-search-forward "^[a-z]* *=" nil t)
(backward-char 3)
(setq deps-list
(cons (thing-at-point 'word t) deps-list))
(forward-char 3))
deps-list)))
(with-current-buffer (find-file-noselect (or manifest
(cargo-process--project-root "Cargo.toml")))
(save-excursion
(let ((deps-list))
(widen)
(goto-char (point-min))
(search-forward "[dependencies]" nil t)
(while (re-search-forward "^[a-zA-Z\-\_]* *=" nil t)
(beginning-of-line)
(setq deps-list
(cons (thing-at-point 'sexp t) deps-list))
(end-of-line))
deps-list))))

;;;###autoload
(defun cargo-process-bench ()
Expand Down Expand Up @@ -544,10 +541,10 @@ With the prefix argument, modify the command's invocation.
CRATES is the name of the crate to add.
Cargo: This command allows you to add a dependency to a Cargo.toml manifest file."
(interactive (list
(completing-read "Crate(s) to add: " cargo-process-favorite-crates)))
(completing-read "Crate(s) to add: " cargo-process-favorite-crates)))
(cargo-process--start "Add" (concat cargo-process--command-add
" "
crate)))
" "
crate)))

;;;###autoload
(defun cargo-process-rm (crate)
Expand All @@ -557,40 +554,35 @@ CRATE is the name of the crate to remove.
Cargo: Remove a dependency from a Cargo.toml manifest file."
(interactive
(list
(let ((deplist (cargo-process--get-dependencies
(concat
(cargo-process--project-root) "Cargo.toml"))))
(if deplist
(completing-read "Crate to remove: "
deplist nil t)
(progn
(message "No crates used in current project.")
nil)))))
(let ((deplist (cargo-process--get-dependencies)))
(when deplist
(completing-read "Crate to remove: "
deplist nil t)))))
(if crate (cargo-process--start "Remove" (concat cargo-process--command-rm
" "
crate))))
" "
crate))
(message "No crates used in current project.")))

;;;###autoload
(defun cargo-process-upgrade (&optional all crates)
"Run the Cargo update command.
With the prefix argument, modify the command's invocation.
If ALL is t then update all crates, otherwise specify CRATES.
Cargo: Upgrade dependencies as specified in the local manifest file"
(interactive)
(let ((deplist (cargo-process--get-dependencies
(concat
(cargo-process--project-root) "Cargo.toml"))))
(if deplist
(let ((all (when (or all
(y-or-n-p "Upgrade all crates? "))
"--all"))
(crates (if (not all)
(completing-read "Crate(s) to upgrade: " deplist nil 'confirm)
nil)))
(cargo-process--start "Upgrade" (concat cargo-process--command-upgrade
" "
all
crates))))
(message "No crates used in current project.")))
(let ((deplist (cargo-process--get-dependencies)))
(if deplist
(let* ((all (when (or all
(y-or-n-p "Upgrade all crates? "))
"--all"))
(crates (when (not all)
(completing-read "Crate(s) to upgrade: " deplist nil 'confirm))))
(cargo-process--start "Upgrade" (concat cargo-process--command-upgrade
" "
all
" "
crates)))
(message "No crates used in current project."))))

;;;###autoload
(defun cargo-process-repeat ()
Expand Down

0 comments on commit 2c730c4

Please sign in to comment.