Skip to content

Commit

Permalink
allow adding missing dependencies with eglot
Browse files Browse the repository at this point in the history
close #169
  • Loading branch information
brotzeit committed Aug 17, 2022
1 parent b296d30 commit 880adc6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ modify the parameters of a command.
- `rustic-cargo-add` Add crate to Cargo.toml using 'cargo add'
- `rustic-cargo-rm` Remove crate from Cargo.toml using 'cargo rm'
- `rustic-cargo-upgrade` Upgrade dependencies as specified in the local manifest file using 'cargo upgrade'
- `rustic-cargo-add-missing-imports ` Add the missing imports for the current buffer to `cargo.toml`. This function requires `lsp-mode`.
- `rustic-cargo-add-missing-dependencies` Add the missing dependencies for the current buffer to `Cargo.toml`. This function requires either lsp-mode or eglot

### Test

Expand Down
73 changes: 58 additions & 15 deletions rustic-cargo.el
Original file line number Diff line number Diff line change
Expand Up @@ -709,24 +709,67 @@ If running with prefix command `C-u', read whole command from minibuffer."
(read-from-minibuffer "Crate: ")))))
(rustic-run-cargo-command command))))

(defun rustic-cargo-add-missing-imports ()
"Add missing imports to Cargo.toml.
Adds all missing imports by default.
(defun rustic-cargo-add-missing-dependencies (&optional arg)
"Add missing dependencies to Cargo.toml.
Adds all missing crates by default with latest version.
Use with 'prefix-arg` to select imports to add."
(interactive)
(when (rustic-cargo-edit-installed-p)
(let ((missing-imports (delete-dups
(seq-reduce (lambda (missing-crates errortable)
(if (string= "E0432" (gethash "code" errortable))
(cons (nth 3 (split-string (gethash "message" errortable) "`")) missing-crates)
missing-crates))
(gethash (buffer-file-name) (lsp-diagnostics t)) '()))))
(if missing-imports
(rustic-run-cargo-command (format "cargo add %s"
(if current-prefix-arg
(completing-read "Select import to add to Cargo.toml" missing-imports)
(mapconcat 'identity missing-imports " "))))
(message "Couldn't find any imports to add. If this was a mistake, make sure your language server is running properly.")))))
(let (deps)
(setq deps
(cond ((featurep 'lsp-mode)
(rustic-cargo-add-missing-dependencies-lsp-mode))
((featurep 'eglot)
(rustic-cargo-add-missing-dependencies-eglot))
(t
nil)))
(if deps
(progn
(when (listp deps)
(setq deps (mapconcat 'identity deps " ")))
(let (d)
(if current-prefix-arg
(setq d (completing-read "Add dependencies: " deps))
(setq d deps))
(rustic-run-cargo-command (concat (rustic-cargo-bin) " add " d))))
(message "No missing crates found. Maybe check your lsp server.")))))

(defun rustic-cargo-add-missing-dependencies-lsp-mode ()
"Return missing dependencies using `lsp-diagnostics'."
(let* ((diags (gethash (buffer-file-name) (lsp-diagnostics t)))
(lookup-missing-crates
(lambda (missing-crates errortable)
(if (string= "E0432" (gethash "code" errortable))
(cons (nth 3 (split-string (gethash "message" errortable) "`"))
missing-crates)
missing-crates))))
(delete-dups (seq-reduce lookup-missing-crates
diags
'()))))

(defun rustic-cargo-add-missing-dependencies-eglot ()
"Return missing dependencies by parsing flymake diagnostics buffer."
(let* ((buf (flymake--diagnostics-buffer-name))
crates)
;; ensure flymake diagnostics buffer exists
(unless (buffer-live-p buf)
(let* ((name (flymake--diagnostics-buffer-name))
(source (current-buffer))
(target (or (get-buffer name)
(with-current-buffer (get-buffer-create name)
(flymake-diagnostics-buffer-mode)
(current-buffer)))))
(with-current-buffer target
(setq flymake--diagnostics-buffer-source source)
(revert-buffer))))
(with-current-buffer buf
(let ((errors (split-string (buffer-substring-no-properties (point-min) (point-max)) "\n")))
(dolist (s errors)
(if (string-match-p
(regexp-quote "unresolved import")
s)
(push (string-trim (car (reverse (split-string s))) "`" "`" ) crates)))))
crates))

;;;###autoload
(defun rustic-cargo-rm (&optional arg)
Expand Down

0 comments on commit 880adc6

Please sign in to comment.