Skip to content

suggest identifiers that are not yet imported #25820

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

Closed
danrubel opened this issue Feb 19, 2016 · 32 comments
Closed

suggest identifiers that are not yet imported #25820

danrubel opened this issue Feb 19, 2016 · 32 comments
Assignees
Labels
devexp-completion Issues with the analysis server's code completion feature legacy-area-analyzer Use area-devexp instead. P2 A bug or feature request we're likely to work on

Comments

@danrubel
Copy link

No description provided.

@danrubel danrubel added Priority-Medium legacy-area-analyzer Use area-devexp instead. devexp-completion Issues with the analysis server's code completion feature labels Feb 19, 2016
@alexander-doroshko
Copy link

Will this suggestion have a SourceEdit for the IDE to insert import (or to modify show/hide list)?

@danrubel
Copy link
Author

Yes.

@kevmoo kevmoo added P2 A bug or feature request we're likely to work on and removed Priority-Medium labels Mar 1, 2016
@danrubel
Copy link
Author

When implementing this, we should be sure not to suggest test classes when completing in lib directory.

@danrubel
Copy link
Author

danrubel commented Oct 10, 2016

To correctly implement this we must only suggest unimported types appropriate for the file being edited. More specifically...

  • when editing a file in the lib/ directory, we should only suggest types in the lib/ directory or in packages in the dependencies: section of the pubspec.yaml.
  • when editing a file in the 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.
  • when editing other files, we can suggest types anywhere in the package or in either the dependencies: or dev_dependencies: sections of the pubspec.yaml.
  • when suggesting types from other packages, do not suggest types only accessible from lib/src (internal by convention) similar to what was done for Quick Fix

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

@davidmorgan
Copy link
Contributor

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!

@danrubel
Copy link
Author

danrubel commented Dec 1, 2016

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.

@davidmorgan
Copy link
Contributor

Thanks. I will stay subscribed ;)

@manuelF
Copy link

manuelF commented Dec 2, 2016

+1, this would be super handy to make the IDE a more seamless experience.

@hooluupog
Copy link

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

@scheglov
Copy link
Contributor

scheglov commented Apr 1, 2018

@davidmorgan, answering on your question in #25820 (comment), 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 #25820 (comment) 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.

@davidmorgan
Copy link
Contributor

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

@DanTup
Copy link
Collaborator

DanTup commented Apr 4, 2018

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

@davidmorgan
Copy link
Contributor

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.

@bwilkerson
Copy link
Member

... maybe it should have an additional set of edits attached that can just be applied?

Yes, that's the plan.

@patrykpoborca
Copy link

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

@wmleler
Copy link

wmleler commented Aug 9, 2018

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.

@sergiandreplace
Copy link

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

@GreenTigerEye
Copy link

Any news to this issue? Manually importing things is just a waste of time which should be fixed for a better developer experience.

@kandroid29
Copy link

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.

@scheglov
Copy link
Contributor

scheglov commented Feb 26, 2019

FYI, we implemented this, you probably will get it with the next IntelliJ and Dart SDK build.
JetBrains/intellij-plugins#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.

image

@kandroid29
Copy link

@scheglov great! will this also be supported in Android Studio?

@scheglov
Copy link
Contributor

Yes, Android Studio uses the same Dart Analysis Server, and the same Dart plugin for IntelliJ.
When specifically, I don't know, maybe @stevemessick ?

@12people
Copy link

How about Visual Studio Code? How can I make autoimport work there.

@DanTup
Copy link
Collaborator

DanTup commented Mar 25, 2019

@12people VS Code work is being tracked at Dart-Code/Dart-Code#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.

@passsy
Copy link

passsy commented Mar 25, 2019

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)

Screen-Shot-2019-03-25-10-51-17 06

@alexander-doroshko
Copy link

@passsy, try with the latest Dart SDK 2.2.1-dev.x.x release.

@passsy
Copy link

passsy commented Apr 3, 2019

I can confirm it is now working with

  • IntelliJ IDEA 2019.1 (Ultimate Edition) Build #IU-191.6183.87, built on March 27, 2019
  • Dart VM version: 2.2.0 (Tue Feb 26 15:04:32 2019 +0100) on "macos_x64"
  • Dart plugin version 191.6183.88 from Apr 02, 2019

@vrendina
Copy link

Android 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!

@shinayser
Copy link

shinayser commented Oct 14, 2019

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:

image

My main file just can't auto complete it. If I try to fix the error I get the following:

image

As soon as I move the extensions to the same file as main, everything works:

image

If I add the import manually, it also works:

image

My Dart for Intellij plugin version is 192.6817.14

@scheglov
Copy link
Contributor

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

@cyberpwnn
Copy link

cyberpwnn commented Nov 19, 2020

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?

@bwilkerson
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
devexp-completion Issues with the analysis server's code completion feature legacy-area-analyzer Use area-devexp instead. P2 A bug or feature request we're likely to work on
Projects
None yet
Development

No branches or pull requests