New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto import #2180
Comments
Why not add missing imports as an auto completion option, and then auto-import when the completion option is chosen, like in Intellij-Rust and most other editors? |
That would require 100% correct resolve to not be annoying. We'll get there one day. |
Is there any infrastructure in RA related to reexports? ( I'm going through the flow suggested and currently at this step
I have a |
No, there’s nothing for reexports yet. It might be ok to ignore them at first: adding them later shouldn’t be harder. A simple rule might be to just check if an item is visible in an ancestor path with the same visibility. |
2716: Allow assists with multiple selectable actions r=SomeoneToIgnore a=SomeoneToIgnore This PR prepares an infra for #2180 task by adding a possibility to specify multiple actions in one assist as multiple edit parameters to the `applySourceChange` command. When this is done, the command opens a selection dialog, allowing the user to pick the edit to be applied. I have no working example to test in this PR, but here's a demo of an auto import feature (a separate PR coming later for that one) using this functionality: ![out](https://user-images.githubusercontent.com/2690773/71633614-f8ea4d80-2c1d-11ea-9b15-0e13611a7aa4.gif) The PR is not that massive as it may seem: all the assist files' changes are very generic and similar. Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
I've considered options, here are a few observations:
Additionally, I need a few fields and methods from
The issue here is to understand how to fit it into sort that
The only alternative I can imagine currently is to pack the functionality needed into some kind of a struct/closure/trait impl in This way we do not need to change that many imports (we only need AssistCtx and its source_analyzer method to be public and this is also avoidable, I think), have less test issues (we still have to mock that struct/closure/trait impl, but no import craziness at least), but need to construct and pass that object into
Here's a branch with an alternative way: https://github.com/rust-analyzer/rust-analyzer/compare/master...SomeoneToIgnore:auto-import?expand=1 Which way do you like more? Maybe there's more opportunities to make it proper? |
I think its important to tackle "implement new feature" and "refactor to the good design" separately. Specifically, first we should add new stuff in maybe questionable ways (as long as those are relatively isolated), and then we should refactor. So, I'd go with the simplest possible way, which is probably just making a bunch of stuff public (and maybe even moving the assist registry itself to |
Well, somehow I did not feel that approach worked well for me in #2716 hence asking upfront :) Ok, I'll go with the current changes in my branch then, with a new trait in ra_assist and its implementation passed from ra_ide into assists method as a new parameter. |
#2716 failed the "relatively isolated" rule, as the initial formed touched every assist and broke the public-ish LSP API. But yeah, it's also true that I, as a maintainer, sometimes try too hard to push the upfront cleanup :( |
2716: Allow assists with multiple selectable actions r=SomeoneToIgnore a=SomeoneToIgnore This PR prepares an infra for rust-lang/rust-analyzer#2180 task by adding a possibility to specify multiple actions in one assist as multiple edit parameters to the `applySourceChange` command. When this is done, the command opens a selection dialog, allowing the user to pick the edit to be applied. I have no working example to test in this PR, but here's a demo of an auto import feature (a separate PR coming later for that one) using this functionality: ![out](https://user-images.githubusercontent.com/2690773/71633614-f8ea4d80-2c1d-11ea-9b15-0e13611a7aa4.gif) The PR is not that massive as it may seem: all the assist files' changes are very generic and similar. Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
Auto import here doesn't include importing automatically with text autocompletion or e.g. shortcut to auto import all missing imports in the file, right? |
Auto import here is meant as the explicit assist to import an unresolved item via user action, what you refer to sounds more like the auto import completion #1062 |
I think we've reached the state where we can have a useful auto-import functionality!
UI
We don't have 100% reliable resolve yet, so we can't implement auto-import as a fix for "unresovled reference" diagnostic. Instead, we should implement it as an assist, available when the cursor is on an identifier, which doesn't resolve to anything
General Flow
The assist should work like this:
ast::NameRef
classify_name
should work here)hir
type, we should construct a canonical path to this item from it's crate root (crate::foo::bar::Thing
)crate::
with the correct crate name for the start.Details
One interesting thing here is that we use text-based index for finding auto-import candidates. I think this is essential to make this feature scale to gigantic code-bases. At the moment, this won't work perfectly because symbol index misses items declared by macros, but this should be fixed by improving the index. Note that symbol index implementation already has a mode for precise, non-fuzzy queries (though I don't think that it is actually used anywhere, so it might have bit-rotten). Note that symbol_index is strictly and IDE part, not a compiler part. For this reason, the code for auto-import should live in the
ra_ide_api
crate. That is, for the time being, this should be the only assist that lives outside ofra_assist
crate.classify_name / classify_name_ref I think could be used for switching between semantic hir and semantic-less AST. I am not 100% sure here, maybe it's better to write some custom code.
construction of a canonical path for an item is a well-isolated task that can be done separately. It's even useful indepndently: for example. currently "run test" functionality uses only a function name, which might run more than one test. Ideally, we should use full path and
--exact
flag.The code for inserting an import path into a file already exists. I am not sure if it has the right interface to be extracted, some refactoring might be required to make it really work for this use-case.
The text was updated successfully, but these errors were encountered: