rust-lang / rust-lang/rust-analyzer

Command/Code Action to add all missing imports

Open
#12,761 8 comments 31 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-assists C-feature
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

The current VS Code extension already offers code actions to add missing imports, but users have to iterate over all missing ones manually to add them. This effort is compounded when a large mod/function is copied/pasted into a different file, without the imports it uses. Users would manually have to:

  1. Find the line with the diagnostic/error about the missing import.
  2. Trigger code actions list.
  3. Execute the Add Foo code action.
  4. Possibly choose between multiple possible import sources.
  5. Save the file.
  6. Wait for updated diagnostics from cargo check.
  7. Look for the next error.
  8. Rinse and repeat for each one.

My suggestion is to offer a new command (and possibly a code action) titled Add all missing imports. Since the functionality is already there, I suspect it is just a matter of:

  1. Iterate on all current "missing imports" diagnostics, and for each:
  2. If import already added, skip it.
  3. Add the missing import, and record it.
  4. If there are multiple possible import sources, let the user choose one.

This way, the user workflow becomes:

  1. Execute the Add all missing imports command.
  2. If all missing imports have a single source, we are done!
  3. If not, the choose dialogs can be shown in order, to select the ambiguous ones.

I suspect this is not perfect, and there can be corner cases, like importing two different symbols with the same name, but different paths. But for the amount of time this will save, I think most users would be ecstatic to quickly review the added imports afterwards to fix any issues. At least I would be :)

Feature already exists in other languages like TypeScript: [Release Notes] [Source Code]

image

Related to and possibly complements #5131 for improving the overall UX when working with large file imports.
Thanks for the amazing work on the extension!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the existing VS Code code actions for missing imports and how their diagnostics are supplied by cargo check. Review the linked TypeScript import-all implementation for comparison, then define the command's behavior for single-source and ambiguous imports. Done means one command processes all current missing-import diagnostics, skips imports already added, and prompts for ambiguous choices.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, vscode
Domain
developer-experience, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.