Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Road to auto-imports #742

Open
1 of 7 tasks
alexheretic opened this issue Feb 28, 2018 · 8 comments
Open
1 of 7 tasks

Road to auto-imports #742

alexheretic opened this issue Feb 28, 2018 · 8 comments
Labels
enhancement Indicates new feature requests

Comments

@alexheretic
Copy link
Member

alexheretic commented Feb 28, 2018

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

@jeffzoch
Copy link

jeffzoch commented Mar 1, 2018

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.

@nrc
Copy link
Member

nrc commented Mar 1, 2018

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.

@alexheretic
Copy link
Member Author

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
Copy link
Member Author

Rustc unused import suggestion issue: rust-lang/rust#47888

@Xanewok Xanewok added the enhancement Indicates new feature requests label Mar 3, 2019
@abraithwaite
Copy link

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

@alexheretic
Copy link
Member Author

alexheretic commented Nov 19, 2020

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.

@nielsle
Copy link

nielsle commented Nov 19, 2020

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

@abraithwaite
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Indicates new feature requests
Projects
None yet
Development

No branches or pull requests

6 participants