Organize import should remove nested imports
Organize imports on the following
produces
The nested import in foo should be removed.
Attached is a simple Eclipse project that can be used to reproduce the issue
package foo
class Foo {
def foo() {
import ctes.Ctes._
A + B
}
}
produces
package foo
import ctes.Ctes.A
import ctes.Ctes.B
class Foo {
def foo() {
import ctes.Ctes._ // <-- this should be removed
A + B
}
}
The nested import in foo should be removed.
Attached is a simple Eclipse project that can be used to reproduce the issue
Leave a comment
file:dnj5caTLyr4BamacwqjQYw
self-contained eclipse project to reproduce the issue
self-contained eclipse project to reproduce the issue
on 2012-07-25 19:18 *
By Mirko Stocker
Eclipse version changed from Indigo to All
Milestone set to Enhancements
on 2012-07-25 19:30 *
By Mirco Dotta
Mirko, is this a tough one to fix? I'm asking because I find the current behavior annoying.
By the way, I have now a different opinion than when I filed the ticket on the ideal behavior.
Organize import on the following:
Should produce
And, as a bonus (which could be a separate ticket), it would be nice to have a checkbox to let the user decide if the organize import action should move imports to the top, i.e.
WDYT?
By the way, I have now a different opinion than when I filed the ticket on the ideal behavior.
Organize import on the following:
package foo
class Foo {
def foo() {
import ctes.Ctes._
A + B
}
}
Should produce
package foo
class Foo {
def foo() {
import ctes.Ctes.{A, B}
A + B
}
}
And, as a bonus (which could be a separate ticket), it would be nice to have a checkbox to let the user decide if the organize import action should move imports to the top, i.e.
package foo
import ctes.Ctes.{A, B}
class Foo {
def foo() {
A + B
}
}
WDYT?
I definitely want to implement this, but it has a lower priority than bug reports for me that's why I set the milestone to Enhancements :-) So, the user should be able to choose if he wants to "pull up imports to the top" or "preserve local imports" (do you have a better word for "local"?). And then the ususal options still apply, like sorting, omitting the scala prefix, etc. Ok?
on 2012-07-26 12:27 *
By Mirco Dotta
Yep, that's it. And I really like the name "local import" (that's really what it is)
One more thing to consider is what should happen in the following:
I'm not sure I like it or not that all imports are pulled to the top :)
One more thing to consider is what should happen in the following:
import collection.mutable.ListBuffer
class Foo {
val d = new ListBuffer[Any]
}
import collection.mutable.HashMap
class Bar {
val d = new HashMap[Any,Any]
}
I'm not sure I like it or not that all imports are pulled to the top :)
on 2012-07-26 13:55 *
By Mirko Stocker
> One more thing to consider is what should happen in the following:
> ...
Hm, I'd say that with "pull up imports to the top" they are all moved to the top, and with "preserve local imports" they just stay where they are. Maybe the local import isn't the best name in that case..
> ...
Hm, I'd say that with "pull up imports to the top" they are all moved to the top, and with "preserve local imports" they just stay where they are. Maybe the local import isn't the best name in that case..
on 2012-07-26 14:00 *
By Mirco Dotta
Makes sense to me.
Ticket assignment reverted due to inactivity.
on 2015-03-13 22:15 *
By Simon Schäfer
Eclipse version changed from All to Luna - Eclipse 4.4
Version changed from 2.1.0-nightly-29 to 4.0.0
Milestone changed from Enhancements to -none-
on 2016-01-18 08:54 *
By Anonymous
Description changed from Organize imports on the fol... to Organize imports on the fol...
currently (4.4.0 - nightly)
is not changed by Organize Imports
but following:
is turned to
notice that besides adding of imports below 'package' definition, import inside method is not sorted.
package acme.foo
class Foo {
def foo() = {
import acme.ctes.Ctes._
A + B
}
}
is not changed by Organize Imports
but following:
package acme.foo
class Foo {
def foo() = {
import acme.ctes.Ctes.{B, A}
A + B
}
}
is turned to
package acme.foo
import acme.ctes.Ctes.A
import acme.ctes.Ctes.B
class Foo {
def foo() = {
import acme.ctes.Ctes.{B, A}
A + B
}
}
notice that besides adding of imports below 'package' definition, import inside method is not sorted.
on 2016-01-18 10:14 *
By Simon Schäfer
I would argue, that for nested imports there should be an option that control whether nested imports are removed and added to the top or if they stay the way they are.
on 2016-01-18 10:21 *
By wpopielarski769697
I thought that in this ticket we could just sort {B, A} and not to promote them to overall imports so after organize imports we should get
and generally sorted inner imports, so if input is:
then output:
package acme.foo
class Foo {
def foo() = {
import acme.ctes.Ctes.{A, B}
A + B
}
}
and generally sorted inner imports, so if input is:
package acme.foo
class Foo {
def foo() = {
import acme.ctes.Ctes.C
import acme.ctes.Ctes.{B, A}
A + B + C
}
}
then output:
package acme.foo
class Foo {
def foo() = {
import acme.ctes.Ctes.{A, B}
import acme.ctes.Ctes.C
A + B + C
}
}
Fixes Organize Imports in non-package blocks
Organize Imports is not able to rearrange imports in blocks of
codes which are not top level package one. So it is not rearrange
following imports in snippet below:
```
package acme.foo
class Foo {
def foo() = {
import acme.ctes.Ctes.C
import acme.ctes.Ctes.{ B, A }
C + B + A
}
}
```
with this fix it should turn to:
```
package acme.foo
class Foo {
def foo() = {
import acme.ctes.Ctes.{ A, B }
import acme.ctes.Ctes.C
C + B + A
}
}
```
Fixes #1001078
Branch: master
Commit: scala-ide:c170e908d5
Organize Imports is not able to rearrange imports in blocks of
codes which are not top level package one. So it is not rearrange
following imports in snippet below:
```
package acme.foo
class Foo {
def foo() = {
import acme.ctes.Ctes.C
import acme.ctes.Ctes.{ B, A }
C + B + A
}
}
```
with this fix it should turn to:
```
package acme.foo
class Foo {
def foo() = {
import acme.ctes.Ctes.{ A, B }
import acme.ctes.Ctes.C
C + B + A
}
}
```
Fixes #1001078
Branch: master
Commit: scala-ide:c170e908d5