-
Notifications
You must be signed in to change notification settings - Fork 256
Road to auto-imports #742
Comments
Just wanted to say I think this would be incredibly useful. Coming from large projects in Java, managing imports was a pain and with rust esp. I find myself having to look up docs for functions i'm aware of solely because I dont remember the package name or I only imported std::collections::hash_map instead of std::collections::hash_map::Entry; or something similar. Auto importing and removal of old ones would go a long way in more rapid prototyping as well as maintainability. |
We definitely want the guts of this stuff to be in rustc - importing depends on name resolution and that is really complex :-( "Remove unused import" should be really easy, just needs to add a suggestion to the warning in rustc. The others I think are a bit tricky. In particular the problem I've been wrestling with here is that if we want things to happen without there being an error (or not a strict code action thing, such as 'add all missing imports'). Then there is not an obvious way to fit it into the LSP/VSCode model. For "order imports" I think range formatting should work pretty well for this case, you just need to determine the ranges to format. That is pretty easy in the common case - find all lines that are not in comments and which start with |
Exactly in Java you just don't even need to touch the imports really because of auto-imports. I too got used to this.
Ok agreed, so we first need to get rustc to properly suggest to add and remove imports. Since it already suggests add in 1 case that should be doable. I'll raise / look for some issues in rustc.
My thinking was simply have Rls provide the "Orginise imports in this file" functionality like a CodeAction/Lens, a bit like we currently provide "rustfmt this file". Then its up to the client to be configured to call it automatically in some cases. That should fit in the LSP model ok. (For example currently you can configure atom to format after saving / closing the file).
That sounds promising, which approach would you favour? I suppose a dedicated format-imports command could handle spread-out imports better, and would avoid having to calculate the correct range to format in Rls. |
Rustc unused import suggestion issue: rust-lang/rust#47888 |
Is this issue alive? Coming from Golang and would <3 this feature back. :-) |
In general rust-analyzer is the future and rls the past. So probably worth pursuing auto-imports in ra. After almost 3 years there seems to have been little progress upstream implementing proper warnings+suggestions. That pretty much blocks this functionality, or requires workarounds, in downstream rls & rust-analyzer. |
auto-imports allready work in rust-analyzer in many cases (Move cursor to unimported class, press ctrl-. and select import) |
Cool! I wasn't aware of rust-analyzer. I reached here via other open github tickets on rust vim. I'll check it out! |
I'd particularly love to have automatic import functionality in Rls clients. We need to provide clients a few things to do that
CodeActions/CodeLens:
reorder_imported
,reorder_imported_names
(available when this would change something)With these it would be possible to enable automatic import functionality in clients where they run "Organise imports" + Add imports actions on change/open current file whenever the project is without compile error.
Implementation ideas
I think rustc should provide basic add & remove import suggestions. It currently provides 1/2 add import suggestions. Or should we implement the missing functionality here?
With the above Rls could fairly easily provide the do-all-in-file functionality.
Ordering import without ordering the rest of the code requires the range format functionality that currently doesn't work very well. With that it should be possible.
The text was updated successfully, but these errors were encountered: