Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new command: cargo-find-dependency #132

Merged
merged 1 commit into from
Mar 2, 2022
Merged
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
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