Skip to content
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

Optimize imports command doesn't resolve imports automatically #30296

Open
JFixby opened this issue Jul 31, 2017 · 7 comments
Open

Optimize imports command doesn't resolve imports automatically #30296

JFixby opened this issue Jul 31, 2017 · 7 comments
Labels
analyzer-server area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@JFixby
Copy link

JFixby commented Jul 31, 2017

Reproduction steps:

Example project: https://github.com/JFixby/DartImportsExample

class A located in the lib/folderA/class_a_file.dart
content:

class A {
  static String fieldF = "";
}

class C located in the lib/class_c_depends_on_a.dart depends on the A
content:


class C {
  C() {
       A instanceOfB = new A(); //missing import
  }
}

What was done

click Code -> Optimize imports

image

What is expected

File class_c_depends_on_a.dart gets a new line import 'package:project_a/folderA/class_a_file.dart';

What is actually happening

nothing

Setup

IDEA version: community 2017.2
Dart plugin: 172.3317.48

@bwilkerson bwilkerson added the area-intellij Tracking issues for the Dart IntelliJ plugin. label Jul 31, 2017
@devoncarew devoncarew added the type-enhancement A request for a change that isn't a bug label Jul 31, 2017
@alexander-doroshko
Copy link

@JFixby, IDE's 'Optimize imports' effectively delegates to Dart Analyser's Organize Directives action. Its purpose is to remove unused imports and sort directives. By design it doesn't add imports and doesn't fix any red code. To fix red code navigate there (e.g. press F2) and see if there are appropriate quick fixes (Alt+Enter).

@JFixby
Copy link
Author

JFixby commented Aug 15, 2017

Well, I have past experience with Eclipse IDE. Eclipse fixes red code and auto-resolves imports.
Here I have to do monkey job clicking Alt+Enter on each line of code of each broken file and that is usually a couple hundreds of files. Would be nice if IDE could do it automatically.

@JFixby
Copy link
Author

JFixby commented Aug 15, 2017

Please treat it as a feature request and re-open.

@alexander-doroshko
Copy link

alexander-doroshko commented Aug 15, 2017

@JFixby how is the action called in Eclipse that removes unused imports, sorts them and adds missing imports in one shot?

In the IntelliJ world, there has never been such. 'Optimize Imports' in IntelliJ means remove unused and sort. However, IntelliJ's Java support has 'Add unambiguous imports on the fly' feature. With it enabled, imports appear automatically as soon as it is an obvious and the only way to fix a red reference. With it disabled it is anyway rather easy to add missing import in Java because the IDE shows you a tooltip like this:
image

Ok, I reopen the issue and move it to area-analyzer. Let @bwilkerson decide. I suggest to keep Organize Directives as is, its current behavior matches IntelliJ's workflow.

We may think about easier importing, something like what Java support in IntelliJ has. But I wouldn't like to rethink IntelliJ's 'Optimize Imports' action.

I think what will really help is #25820 and #21931

@alexander-doroshko alexander-doroshko added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. and removed area-intellij Tracking issues for the Dart IntelliJ plugin. labels Aug 15, 2017
@JFixby
Copy link
Author

JFixby commented Aug 15, 2017

In Eclipse: Source > Organize imports

In case of ambiguity in shows the following window:

image

Otherwise sorts imports, adds missing and removes unused in one shot.
Can be applied to the whole workspace.

@alexander-doroshko
Copy link

alexander-doroshko commented Aug 15, 2017

Different IDEs - different workflows. In IntelliJ Optimize Imports is rarely used itself, but often in the scope of the Reformat Code action, in one shot. And neither Reformat Code nor Optimize Imports change the semantics of the written code. Features like 'Add unambiguous imports on the fly' (default = false) and 'Show import popup' (default = true) and finally Quick Fixes do this job in Java support instead.
And code completion for sure that (on second stroke) suggests unimported classes and can insert both class name and required import in one shot. This is what I miss in Dart most of all.

@JFixby
Copy link
Author

JFixby commented Aug 15, 2017

Speaking of workflows: clicking Alt+Enter all day long is not fun :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-server area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants