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

Missing imports eglot #177

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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 @@ -287,6 +287,7 @@ The rustic commands can be called with prefix `C-u` if you want to modify the pa
- `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` or `eglot`.

### Test

Expand Down
36 changes: 36 additions & 0 deletions rustic-cargo.el
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,42 @@ If running with prefix command `C-u', read whole command from minibuffer."
(concat "cargo add " (read-from-minibuffer "Crate: ")))))
(rustic-run-cargo-command command))))

(defun rustic-cargo--eglot-missing-imports ()
"Find missing imports when using eglot."
(delete-dups (seq-reduce (lambda (missing-crates clstruct)
(if (and (flymake--diag-p clstruct)
(string-match-p
(regexp-quote "unresolved import `")
(format "%s" (flymake--diag-text clstruct))))
(cons (nth 3 (split-string (flymake--diag-text clstruct) "`")) missing-crates)
missing-crates))
eglot--unreported-diagnostics '())))

(defun rustic-cargo--lsp-missing-imports ()
"Find missing imports when using lsp-mode."
(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)) '())))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can funcall the lsp function here.


(defun rustic-cargo-add-missing-imports ()
"Add missing imports to Cargo.toml.
Adds all missing imports by default.
Use with 'prefix-arg` to select imports to add."
(interactive)
(when (rustic-cargo-edit-installed-p)
(if-let ((missing-imports (if (ignore-error lsp-mode)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do something like (if (featurep 'lsp-mode) (print "foo"))

(rustic-cargo--lsp-missing-imports)
(rustic-cargo--eglot-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."))))


;;;###autoload
(defun rustic-cargo-rm (&optional arg)
"Remove crate from Cargo.toml using 'cargo rm'.
Expand Down