Will this suggestion have a SourceEdit
for the IDE to insert import (or to modify show
/hide
list)?
Yes.
When implementing this, we should be sure not to suggest test classes when completing in lib directory.
To correctly implement this we must only suggest unimported types appropriate for the file being edited. More specifically...
lib/
directory, we should only suggest types in the lib/
directory or in packages in the dependencies:
section of the pubspec.yaml
.bin/
, example/
, or web/
directories, we should only suggest types in that directory or the lib/
directory or in packages in the dependencies:
section of the pubspec.yaml
.dependencies:
or dev_dependencies:
sections of the pubspec.yaml
.lib/src
(internal by convention) similar to what was done for Quick FixThe blocking issue is that currently, there is no way for the completion service to know which packages are listed in dependencies:
or dev_dependencies:
sections of the pubspec.yaml
.
Am I right in thinking that this issue covers auto-completing to methods of types I haven't imported yet?
i.e. if I type 'new Foo()..', and 'Foo' is not imported, it will still suggest completions that come from Foo?
This is something I miss from IntelliJ's Java behaviour -- it means you can write a whole line of code before going back to add the import.
Thanks!
Yes. Implementing it correctly (see above) means some additional low level functionality in analyzer in addition to the code completion mods.
This is something I'd definitely like to see added, but have not had the cycles to get to it. There's a laundry list of things to do and I don't have a good feel for where this one fits in the priority list.
Thanks. I will stay subscribed ;)
+1, this would be super handy to make the IDE a more seamless experience.
+1, it's one of must-have features i am using everyday in Go( Goimports is automatically ran on every save of file to fix imports and format code).
@davidmorgan, answering on your question in https://github.com/dart-lang/sdk/issues/25820#issuecomment-264178623, my understanding is that answer is "no". This issue is about suggesting imports for top-level declarations from not yet imported libraries. What you are asking is kind of "speculative" resolution of some identifiers, even if there is no corresponding import, pretend that there is one. In theory this could be done, but why? If there is a missing import and you get a compilation error, use the Quick Fix and then get further analysis and code completion.
@danrubel is right in https://github.com/dart-lang/sdk/issues/25820#issuecomment-252737209 that we need to suggest only identifiers from libraries that are potentially accessible in the source library. But that's not the same as resolution without imports.
@scheglov I don't use Java anywhere near as much as I used to, so I don't remember the exact details; but IntelliJ with Java definitely does something along these lines.
e.g. if I type Thr
then it suggests completions to types that are not imported that start with Thr
. If I go on to type ThrowableExtension.
, which is one of them, then it suggests completion to static methods of that class--without it being imported. If I choose one of the completions then as well as completing where I'm working on it adds the import.
This is preferable to a separate step to add the import simply because it's quicker--one quick fix instead of two.
I tried and was not able to repro the behaviour I seem to have been describing with suggesting completions on a variable of a not-imported type. Maybe it doesn't do this any more :) or maybe I was mistaken.
Thanks.
What if there are many ThrowableExtension classes in different unimported libraries - does it show the options from all of them, or just a random(?) one?
It sounds like interesting behaviour, but if it's more work, there's great value in implementing the simpler version.
Note: The API for CompletionSuggestion
would need updating to support this - it currently exposes importUri
but provides no information on where to insert it (maybe it should have an additional set of edits attached that can just be applied?)
It shows options from all of them--and of course the import that gets added when you pick one is the one corresponding to the completion you picked.
... maybe it should have an additional set of edits attached that can just be applied?
Yes, that's the plan.
This is a very big minus so far in terms of development. I feel like I'm back in the olden days of IDE's where i have to find the imports i need while writing my code. Please let us not have to scroll all the way up or try to remember the casing of a class and then _hoping_ cmd + enter / alt + enter has the import ready to you, but wait was it "SomeClass" or "Someclass".. hope you remembered otherwise back to the top of the document you go to search import 'package:so...'
I'm just thinking out loud, but here's a compromise idea: If autocompletion doesn't find a match in a library that is already imported, it displays a list of libraries that do have a match. If you select one of them, then that selected library is imported and then it goes back to the current autocomplete behavior. Seems simpler, easier to implement, and perhaps faster (don't have to look in other libraries unless there is no match in the imported libraries), but still gets the job done without having to do a separate import.
@wmleler this will be far from ideal, but it will be an interesting first step. I've found that I'm mainly dealing with my own files/methods, so, saving me the time to import it manually will be a time-saver.
Any news to this issue? Manually importing things is just a waste of time which should be fixed for a better developer experience.
Having to move up and down to add the import for a class is a very bad experience, it is waste of time and distraction from what I am doing. So I think auto import suggestion is a required feature for modern IDE.
FYI, we implemented this, you probably will get it with the next IntelliJ and Dart SDK build.
https://github.com/JetBrains/intellij-plugins/pull/637
It will show everything available to you, prioritizing libraries that are already imports (because for these you have already made your choice), and adding new imports with or without import prefix depending on whether the new import will introduce ambiguity or will be shadowed at the port of usage.
Bonus point is that because available declarations are cached on the client (IntelliJ), we have to transfer much less data, so completions are faster. I saw improvements from 53 ms
for about 1600
completions, to 5 ms
when producing 5000+
completions.
@scheglov great! will this also be supported in Android Studio?
Yes, Android Studio uses the same Dart Analysis Server, and the same Dart plugin for IntelliJ.
When specifically, I don't know, maybe @stevemessick ?
How about Visual Studio Code? How can I make autoimport work there.
@12people VS Code work is being tracked at https://github.com/Dart-Code/Dart-Code/issues/1060. I'd like to have some additional testing before it's included in a release - if you're interested in trying it out with a preview build, please drop a comment on that issue.
I just tested version 191.6183.41
. Suggestions of Future
and List
works but I don't get any suggestions for 3rd party classes, i.e. ListMultimap
from quiver as shown in the screenshot above. (yes, I added the dependency)
@passsy, try with the latest Dart SDK 2.2.1-dev.x.x release.
I can confirm it is now working with
#IU-191.6183.87
, built on March 27, 20192.2.0
(Tue Feb 26 15:04:32 2019 +0100) on "macos_x64"191.6183.88
from Apr 02, 2019Android Studio 3.4.1 seems to be pretty far behind IntelliJ IDEA 2019.1.2 with their Dart plugin version and was lacking this functionality even though it has been available for 2 months. Moving from Android Studio to IntelliJ for Flutter development!
I am having problem with auto completing the new Extension Functions from dart 2.6.0. I have added a new file called "time.dart" with some extensions, like this:
My main file just can't auto complete it. If I try to fix the error I get the following:
As soon as I move the extensions to the same file as main, everything works:
If I add the import manually, it also works:
My Dart for Intellij plugin version is 192.6817.14
@shinayser Please open a new issue https://github.com/dart-lang/sdk/issues.
Auto-import for extensions probably will not work, because auto-import works syntactically, i.e. we just know top-level names to include. But for extensions we need to match on
clauses against target expressions.
We might implement quick fix.
A core feature of an IDE tool / SDK has still yet to be implemented... or work if it is. Is there an eta when I can write at normal speeds in dart?
Which IDE / editor are you using? The functionality is working in several editors, so I'm wondering whether the problem is specific to some editors.
Can you provide us with an example of a case in which it fails to work? There are a couple of known exceptions where it doesn't yet work, but the issue was closed because it appears to be working in general and we prefer having finer-grained issues when a piece of a feature is broken.
Most helpful comment
FYI, we implemented this, you probably will get it with the next IntelliJ and Dart SDK build.
https://github.com/JetBrains/intellij-plugins/pull/637
It will show everything available to you, prioritizing libraries that are already imports (because for these you have already made your choice), and adding new imports with or without import prefix depending on whether the new import will introduce ambiguity or will be shadowed at the port of usage.
Bonus point is that because available declarations are cached on the client (IntelliJ), we have to transfer much less data, so completions are faster. I saw improvements from
53 ms
for about1600
completions, to5 ms
when producing5000+
completions.