Skip to content

Commit

Permalink
Merge pull request kwrooijen#132 from nemethf/find-dependency
Browse files Browse the repository at this point in the history
Add new command: cargo-find-dependency
  • Loading branch information
kwrooijen authored Mar 2, 2022
2 parents 841ea7d + bac1667 commit a500fd2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ You will now have the following key combinations at your disposal:
<kbd>C-c C-c C-S-a</kbd> | cargo-process-audit
<kbd>C-c C-c C-S-r</kbd> | cargo-process-script
<kbd>C-c C-c C-w</kbd> | cargo-process-watch
<kbd>C-c C-c C-S-f</kbd> | cargo-find-dependency

Before executing the task, Emacs will prompt you to save any modified buffers
associated with the current Cargo project. Setting `compilation-ask-about-save`
Expand Down
9 changes: 9 additions & 0 deletions cargo-process.el
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,15 @@ If FILE-NAME is not a TRAMP file, return it unmodified."
(cdr (assoc 'workspace_root metadata-json)))))
workspace-root)))

(defun cargo-process--get-metadata ()
"Return the metadata of the current package as an alist."
(when (cargo-process--project-root)
(let ((metadata-text
(shell-command-to-string
(concat (shell-quote-argument cargo-process--custom-path-to-bin)
" metadata --format-version 1"))))
(cargo-json-read-from-string metadata-text))))

(defun manifest-path-argument (name)
(let ((manifest-filename (cargo-process--tramp-file-local-name (cargo-process--project-root "Cargo.toml"))))
(when (and manifest-filename
Expand Down
28 changes: 28 additions & 0 deletions cargo.el
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
(define-key km (kbd "C-S-a") 'cargo-process-audit)
(define-key km (kbd "C-S-r") 'cargo-process-script)
(define-key km (kbd "C-w") 'cargo-process-watch)
(define-key km (kbd "C-S-f") 'cargo-find-dependency)
km)
"Keymap for Cargo mode commands after prefix.")
(fset 'cargo-minor-mode-command-map cargo-minor-mode-command-map)
Expand All @@ -100,6 +101,33 @@

(defvar cargo-minor-mode nil)

(defun cargo-find-dependency (crate &optional metadata)
"Find CRATE's Cargo.toml.
If METADATA is non-nil, use that instead of fetching it with cargo."
(interactive (let* ((metadata
(cargo-process--get-metadata))
(crates
(mapcar (lambda (pkg) (alist-get 'name pkg))
(append (alist-get 'packages metadata) nil))))
(list
(completing-read
"Dependency: " crates nil t nil nil (symbol-at-point))
metadata)))
(let ((filenames
(cl-loop for pkg in (append (alist-get 'packages metadata) nil)
when (equal crate (alist-get 'name pkg))
collect (alist-get 'manifest_path pkg)))
;; Directly `find-file' if there's just a single match.
(xref-show-xrefs-function xref-show-definitions-function))
(if filenames
(xref-show-xrefs (mapcar (lambda (filename)
(xref-make crate
(xref-make-file-location
filename 1 0)))
filenames)
nil)
(message "Can't find: %s" crate))))

;;;###autoload
(define-minor-mode cargo-minor-mode
"Cargo minor mode. Used to hold keybindings for cargo-mode.
Expand Down

0 comments on commit a500fd2

Please sign in to comment.