rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

Command/Code Action to add all missing imports

Open OmarTawfik opened this issue 3 years ago • 5 comments

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!

OmarTawfik avatar Jul 14 '22 01:07 OmarTawfik

We don't have a way of prompting the user when there are ambiguous imports, but apart from that I think we should have this. (We could even try to be a bit smarter in ambiguous situations by looking at the types, but that's a separate topic.)

flodiebold avatar Jul 14 '22 09:07 flodiebold

Thanks for accepting the suggestion!

We don't have a way of prompting the user when there are ambiguous imports

This is what I mean by prompting the user when there are ambiguities. It is already implemented in RA:

image

OmarTawfik avatar Jul 19 '22 22:07 OmarTawfik

Couldn't we use ShowMessageRequest to prompt user in case of ambiguous imports?

osiewicz avatar Aug 22 '24 14:08 osiewicz

Couldn't we use ShowMessageRequest to prompt user in case of ambiguous imports?

@osiewicz If I can suggest, showMessageRequest is not document-specific, and lacks filtering, search, and other capabilities that are offered by the VS Code dialogs that Rust Analyzer already uses for imports (example).

OmarTawfik avatar Aug 23 '24 13:08 OmarTawfik

Yeah, that's fair, though VSC is not the only consumer of RA (albeit it's definitely the biggest one). I'd love to have add all missing imports in Zed, hence my suggested solution is not specific to VSC.

osiewicz avatar Sep 06 '24 15:09 osiewicz

@osiewicz Good point. I looked more into it, and it looks like feature can actually be supported in an idiomatic way similar to how other languages already do it, but there is a tiny issue in Rust Analyzer that prevent this from happening: It needs to set the correct kind property on generated code actions. It currently just sets all of them to quickfix, even for missing imports, which is incorrect. It might consider switching to using the new scoped CodeActionKind kinds and use a scoped string source.addMissingImports .

This allows users to set key bindings to add all missing imports in one step:

{
    "key": "ctrl+shift+i",
    "command": "editor.action.sourceAction",
    "args": {
        "kind": "source.addMissingImports",
        "apply": "first" // or "never" or "ifSingle"
    }
}

Or even do it on every file save:

"editor.codeActionsOnSave": [
    "source.addMissingImports"
]

More information in this release note: https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_46.md#add-all-missing-imports-source-action

And checking Zed docs, it seems the same method is also supported via code_actions_on_format and codeActionOnSave settings: https://zed.dev/docs/configuring-zed#code-actions-on-format

OmarTawfik avatar Sep 16 '24 01:09 OmarTawfik

I would love to have this as well! Is there any existing solution for this?

Tuditi avatar Mar 07 '25 09:03 Tuditi