rust-lang / rust-analyzer

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

Configuration to change where `use` statements are inserted when auto-importing #16921

Open jvcmarcenes opened 6 months ago

jvcmarcenes commented 6 months ago

I would like for the auto-import code action to insert the use statement inside the function where it was invoked, and if there is no function, to put it at the module level.

I prefer having my imports inside functions when possible because it makes it easier to move the function, and to see what this specific function depends on. It's common for me to be in situations where a single function makes plenty of imports that other parts of the same module won't reference, cluttering the import section at the top of the module.

I'd be willing to implement this myself, but I don't know how the development process works here and sadly I don't have the time to look into it. However, if someone points me towards some direction I might be able to pick it up.

Veykril commented 6 months ago

The relevant code is here https://github.com/rust-lang/rust-analyzer/blob/c68316ac78628b84105ed1c95e2c8f3d7ca78db2/crates/ide-db/src/imports/insert_use.rs#L100