rls icon indicating copy to clipboard operation
rls copied to clipboard

Road to auto-imports

Open alexheretic opened this issue 7 years ago • 8 comments

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:

  • [ ] Remove unused import (per unused import) upstream issue
  • [x] Add import - cannot find declared type
  • [ ] Add import - failed to resolve type or module upstream issue
  • [ ] Remove all unused imports in file on each unused diagnostics (available when more than 1 exists in the file)
  • [ ] Add all missing imports in file (where no choice of import exists)
  • [ ] "Order imports" Rustfmt only the imports with reorder_imported, reorder_imported_names (available when this would change something)
  • [ ] "Organise imports": Remove all unused + "Order imports"

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.

alexheretic avatar Feb 28 '18 19:02 alexheretic

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.

jeffzoch avatar Mar 01 '18 19:03 jeffzoch

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 [pub] use (where actually pub can be more complicated, but whatever). Alternatively it would be pretty easy to add functionality to rustfmt to only format imports and skip other items.

nrc avatar Mar 01 '18 19:03 nrc

Just wanted to say I think this would be incredibly useful. Coming from large projects in Java

Exactly in Java you just don't even need to touch the imports really because of auto-imports. I too got used to this.

We definitely want the guts of this stuff to be in rustc

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.

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.

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).

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 [pub] use (where actually pub can be more complicated, but whatever). Alternatively it would be pretty easy to add functionality to rustfmt to only format imports and skip other items.

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.

alexheretic avatar Mar 02 '18 00:03 alexheretic

Rustc unused import suggestion issue: https://github.com/rust-lang/rust/issues/47888

alexheretic avatar Mar 03 '18 16:03 alexheretic

Is this issue alive? Coming from Golang and would <3 this feature back. :-)

abraithwaite avatar Nov 19 '20 02:11 abraithwaite

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.

alexheretic avatar Nov 19 '20 04:11 alexheretic

auto-imports allready work in rust-analyzer in many cases (Move cursor to unimported class, press ctrl-. and select import)

nielsle avatar Nov 19 '20 06:11 nielsle

Cool! I wasn't aware of rust-analyzer. I reached here via other open github tickets on rust vim. I'll check it out!

abraithwaite avatar Nov 19 '20 14:11 abraithwaite