AddImport removes grouping of imports
Whenever I choose a quick-fix to add an import, it removes all blank lines in my import statements. This is extremely annoying. It needs to decide where to add the import, but not touch any of my grouping of imports. Example:
After choosing a quick fix to add the missing type, I get:
import scala.collection.mutable
import java.io.File
class Foo {
def main(args: Array[String]): Unit = {
val x = new ArrayBuffer // quick fix here
}
}
After choosing a quick fix to add the missing type, I get:
import scala.collection.mutable
import java.io.File
import scala.collection.mutable.ArrayBuffer
class Foo {
def main(args: Array[String]): Unit = {
val x = new ArrayBuffer
}
}
Leave a comment
on 2015-10-15 16:51 *
By Simon Schäfer
Version changed from 4.1.0 to 4.2.0
Status changed from New to Accepted
Assigned to set to Simon Schäfer
Do not depend on tree transformations for the add import refactoring
This is a similar change to the rename refactoring. The overall idea is
to not depend on any tree transformations when an import is added to a
file. Theoretically, doing any transformations on the tree is the
optimal solution but only if the tree is a concrete syntax tree. The
scalac trees however are abstract syntax trees and therefore don't do
what we want. For the add import refactoring, they remove all the
formatting of the imports, especially removed new lines are a concern.
This commit therefore replaces the tree transformations by generating
textual code changes. This way, the refactoring is only doing a minimal
change, which in our case is to add one or multiple imports. A side
effect is that some functionality is lost but I consider that as a good
change. Formerly, the add import refactoring also did some minor
cleanups to the regions around imports. It added newlines, which means
that an import that got added to a code snippet like the following
class A {
val f = new File
}
did result in this:
import java.io.File
class A {
val f = new File
}
Whereas after the refactorings of this commit the extra newline is no
longer printed:
import java.io.File
class A {
val f = new File
}
This may not look better but it makes the implementation easier because
we don't have to check if a newline already exists. Also, conceptually
it is the job of the organize imports refactoring to take care of such a
cleanup. The add import refactoring should only do a minimal change and
that is exactly what it is doing right now.
Fix #1002514
Branch: master
Commit: scala-ide:1996012fc3
This is a similar change to the rename refactoring. The overall idea is
to not depend on any tree transformations when an import is added to a
file. Theoretically, doing any transformations on the tree is the
optimal solution but only if the tree is a concrete syntax tree. The
scalac trees however are abstract syntax trees and therefore don't do
what we want. For the add import refactoring, they remove all the
formatting of the imports, especially removed new lines are a concern.
This commit therefore replaces the tree transformations by generating
textual code changes. This way, the refactoring is only doing a minimal
change, which in our case is to add one or multiple imports. A side
effect is that some functionality is lost but I consider that as a good
change. Formerly, the add import refactoring also did some minor
cleanups to the regions around imports. It added newlines, which means
that an import that got added to a code snippet like the following
class A {
val f = new File
}
did result in this:
import java.io.File
class A {
val f = new File
}
Whereas after the refactorings of this commit the extra newline is no
longer printed:
import java.io.File
class A {
val f = new File
}
This may not look better but it makes the implementation easier because
we don't have to check if a newline already exists. Also, conceptually
it is the job of the organize imports refactoring to take care of such a
cleanup. The add import refactoring should only do a minimal change and
that is exactly what it is doing right now.
Fix #1002514
Branch: master
Commit: scala-ide:1996012fc3