rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.27k stars 1.61k forks source link

Enable auto-import assist for prelude and glob imported items #13796

Open Azorlogh opened 1 year ago

Azorlogh commented 1 year ago

Use case

The anyhow crate provides the following definition:

pub type Result<T, E = Error> = core::result::Result<T, E>;

If you create a new file, and use the type Result<()> somewhere, there is no way to automatically add use anyhow::Result;, because Result is already a type that exists.

Proposed solution:

If the item under the cursor has been imported from prelude or a glob import, allow auto-importing an alternative item with the same name.

~~Provide a new assist for changing the import of the item under the cursor. If the item is defined in the current module, no assist is shown. If the item is already imported from a use, allow selecting an alternative import which replaces the old one. If the item is imported by default, like std's Result, allow selecting an import to hide it.~~

jhgg commented 1 year ago

I think that this makes sense for things that are in prelude - but I don't think this is preferable in the situation where you want to change an import.

Veykril commented 1 year ago

I agree, for prelude items this would make sense (and maybe glob imported ones). No need for a new assist though, we can just improve the already existing import assist.

Azorlogh commented 1 year ago

Yes to be fair I think I've only ever needed it for prelude and globs, so I would be perfectly fine with just those. I'll rename the issue! :+1: