sbt> scalafix dependency:OrganizeImports@com.github.liancheng:organize-imports:0.6.0
A CI-friendly Scalafix semantic rule for organizing imports
Scala
190
340 commits
updated Jul 14, 2023
|
Important
| This rule is included in Scalafix as a built-in rule since Scalafix v0.11.0. Please refer to the Scalafix GitHub repository for bug reports and feature requests. |
OrganizeImports is a CI-friendly Scalafix semantic rule that helps you organize Scala import statements.
Metals, the Scala language server, also uses OrganizeImports to power its "organize imports" code action starting from version v0.9.5.

Please refer to the Scalafix documentation for how to install Scalafix and invoking it in your sbt build.
To try this rule in the sbt console without adding this rule to your sbt build:
sbt> scalafix dependency:OrganizeImports@com.github.liancheng:organize-imports:0.6.0
To include this rule in your sbt build:
ThisBuild / scalafixDependencies += "com.github.liancheng" %% "organize-imports" % "0.6.0"
You can also include this rule in your Mill build using mill-scalafix:
def scalafixIvyDeps = Agg(ivy"com.github.liancheng::organize-imports:0.6.0")
OrganizeImports allows you to specify a preset style via the preset option. To make it easier to add OrganizeImports into existing Scala projects built using the IntelliJ Scala plugin, OrganizeImports provides a preset style compatible with the default configuration of the IntelliJ Scala import optimizer. Please check the INTELLIJ_2020_3 preset style for more details.
The OrganizeImports rule respects source-formatting tools like Scalafmt. If an import statement is already organized according to the configuration, its original source level format is preserved. Therefore, in an sbt project, if you run the following command sequence:
sbt> scalafixAll
...
sbt> scalafmtAll
...
sbt> scalafixAll --check
...
Assuming that the first two commands run successfully, the last scalafixAll --check command should not fail even if some import statements are reformatted by the scalafmtAll command.
However, you should make sure that the source-formatting tools you use do not rewrite import statements in ways that conflict with OrganizeImports. For example, when using Scalafmt together with OrganizeImports, the ExpandImportSelectors, SortImports, and AsciiSortImports rewriting rules should not be used.
Available since v0.6.0.
Running the rule on source files compiled with Scala 3 is still experimental.
Known limitations:
You must use Scalafix 0.9.28 or later
The removeUnused option must be explicitly set to false - the rule currently doesn’t remove unused imports as it’s currently not supported by the compiler.
Usage of deprecated package objects may result in incorrect imports
The groupExplicitlyImportedImplicitsSeparately option has no effect
OrganizeImports {
blankLines = Auto
coalesceToWildcardImportThreshold = null
expandRelative = false
groupExplicitlyImportedImplicitsSeparately = false
groupedImports = Explode
groups = [
"*"
"re:(javax?|scala)\\."
]
importSelectorsOrder = Ascii
importsOrder = Ascii
preset = DEFAULT
removeUnused = true
}
|
Warning
|
Please do NOT use the Scalafix built-in Scalafix rewrites source files by applying patches generated by invoked rules. Each rule generates a patch based on the original text of the source files. When two patches generated by different rules conflict with each other, Scalafix is not able to reconcile the conflicts, and may produce broken code. It is very likely to happen when By default, |
blankLinesAvailable since v0.5.0-alpha.1.
Configures whether blank lines between adjacent import groups are automatically or manually inserted. This option is used together with the --- blank line markers.
Enum: Auto | Manual
A blank line is automatically inserted between adjacent import groups. All blank line markers (---) configured in the groups option are ignored.
A blank line is inserted at all the positions where blank line markers appear in the groups option.
The following two configurations are equivalent:
OrganizeImports {
blankLines = Auto
groups = [
"re:javax?\\."
"scala."
"*"
]
}
OrganizeImports {
blankLines = Manual
groups = [
"re:javax?\\."
"---"
"scala."
"---"
"*"
]
}
Auto
AutoConfiguration:
OrganizeImports {
blankLines = Auto
groups = [
"re:javax?\\."
"scala."
"*"
]
}
Before:
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import javax.annotation.Generated
import scala.concurrent.ExecutionContext
After:
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
import sun.misc.BASE64Encoder
ManualConfiguration:
OrganizeImports {
blankLines = Manual
groups = [
"re:javax?\\."
"scala."
"---"
"*"
]
}
Before:
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import javax.annotation.Generated
import scala.concurrent.ExecutionContext
After:
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
import sun.misc.BASE64Encoder
coalesceToWildcardImportThresholdWhen the number of imported names exceeds a certain threshold, coalesce them into a wildcard import. Renames and unimports are left untouched.
|
Caution
|
Having this feature in Here is an example to illustrate the risk. The following snippet compiles successfully:
The type of However, if we coalesce the grouped imports in the second import statement into a wildcard, there will be a compilation error:
This is because the type of |
Integer. Not setting it or setting it to null disables this feature.
null
Configuration:
OrganizeImports {
groupedImports = Keep
coalesceToWildcardImportThreshold = 3
}
Before:
import scala.collection.immutable.{Seq, Map, Vector, Set}
import scala.collection.immutable.{Seq, Map, Vector}
import scala.collection.immutable.{Seq, Map, Vector => Vec, Set, Stream}
import scala.collection.immutable.{Seq, Map, Vector => _, Set, Stream}
After:
import scala.collection.immutable._
import scala.collection.immutable.{Map, Seq, Vector}
import scala.collection.immutable.{Vector => Vec, _}
import scala.collection.immutable.{Vector => _, _}
expandRelativeExpand relative imports into fully-qualified one.
|
Caution
|
Expanding relative imports may introduce new unused imports. For instance, relative imports in the following snippet
are expanded into
If neither Unfortunately, these newly introduced unused imports cannot be removed by setting |
Boolean
false
Configuration:
OrganizeImports {
expandRelative = true
groups = ["re:javax?\\.", "scala.", "*"]
}
Before:
import scala.util
import util.control
import control.NonFatal
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import javax.annotation.Generated
import scala.concurrent.ExecutionContext
After:
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
import scala.util
import scala.util.control
import scala.util.control.NonFatal
import sun.misc.BASE64Encoder
groupExplicitlyImportedImplicitsSeparatelyThis option provides a workaround to a subtle and rarely seen correctness issue related to explicitly imported implicit names.
The following snippet helps illustrate the problem:
package a
import c._
import b.i
object b { implicit def i: Int = 1 }
object c { implicit def i: Int = 2 }
object Imports {
def f()(implicit i: Int) = println(1)
def main() = f()
}
The above snippet compiles successfully and outputs 1, because the explicitly imported implicit value b.i overrides c.i, which is made available via a wildcard import. However, if we reorder the two imports into:
import b.i
import c._
The Scala compiler starts complaining:
error: could not find implicit value for parameter i: Int
def main() = f()
^
This behavior could be due to a Scala compiler bug since the Scala language specification requires that explicitly imported names should have higher precedence than names made available via a wildcard.
Unfortunately, Scalafix is not able to surgically identify conflicting implicit values behind a wildcard import. In order to guarantee correctness in all cases, when the groupExplicitlyImportedImplicitsSeparately option is set to true, all explicitly imported implicit names are moved into the trailing order-preserving import group together with relative imports, if any (see the trailing order-preserving import group section for more details).
|
Caution
| In general, order-sensitive imports are fragile, and can easily be broken by either human collaborators or tools (e.g., the IntelliJ IDEA Scala import optimizer does not handle this case correctly). They should be eliminated whenever possible. This option is mostly useful when you are dealing with a large trunk of legacy codebase, and you want to minimize manual intervention and guarantee correctness in all cases. |
|
Important
|
The |
Boolean
false
This option defaults to false due to the following reasons:
Although setting it to true avoids the aforementioned correctness issue, the result is unintuitive and confusing for many users since it looks like the groups option is not respected.
E.g., why my scala.concurrent.ExecutionContext.Implicits.global import is moved to a separate group even if I have a scala. group defined in the groups option?
The concerned correctness issue is rarely seen in real life. When it really happens, it is usually a sign of bad coding style, and you may want to tweak your imports to eliminate the root cause.
Configuration:
OrganizeImports {
groups = ["scala.", "*"]
groupExplicitlyImportedImplicitsSeparately = true // not supported in Scala 3
}
Before:
import org.apache.spark.SparkContext
import org.apache.spark.RDD
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.Buffer
import scala.concurrent.ExecutionContext.Implicits.global
import scala.sys.process.stringToProcess
After:
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.Buffer
import org.apache.spark.RDD
import org.apache.spark.SparkContext
import scala.concurrent.ExecutionContext.Implicits.global
import scala.sys.process.stringToProcess
groupedImportsConfigure how to handle grouped imports.
Enum: Explode | Merge | AggressiveMerge | Keep
ExplodeExplode grouped imports into separate import statements.
MergeMerge imports sharing the same prefix into a single grouped import statement.
|
Tip
|
You may want to check the |
|
Important
|
Scala allows a name to be renamed to multiple aliases within a single source file, which makes merging import statements tricky. For example:
The above three imports can be merged into:
but not:
because Scala disallow a name (in this case, Here’s a more complicated example:
While merging these imports, we may want to "bin-pack" them to minimize the number of the result import statements:
However, in reality, renaming aliasing a name multiple times in the same source file is rarely a practical need. Therefore, |
AggressiveMergeSimilar to Merge, but merges imports more aggressively and produces more concise results, despite a relatively low risk of introducing compilation errors.
The OrganizeImports rule tries hard to guarantee correctness in all cases. This forces it to be more conservative when merging imports, and may sometimes produce suboptimal output. Here is a concrete example about correctness:
import scala.collection.immutable._
import scala.collection.mutable.Map
import scala.collection.mutable._
object Example {
val m: Map[Int, Int] = ???
}
At a first glance, it seems feasible to simply drop the second import since mutable._ already covers mutble.Map. However, similar to the example illustrated in the section about the coalesceToWildcardImportThreshold option, the type of Example.m above is mutable.Map, because the mutable Map explicitly imported in the second import takes higher precedence than the immutable Map imported via wildcard in the first import. If we merge the last two imports naively, we’ll get:
import scala.collection.immutable._
import scala.collection.mutable._
This triggers in a compilation error, because both immutable.Map and mutable.Map are now imported via wildcards with the same precedence. This makes the type of Example.m ambiguous. The correct result should be:
import scala.collection.immutable._
import scala.collection.mutable.{Map, _}
On the other hand, the case discussed above is rarely seen in practice. A more commonly seen case is something like:
import scala.collection.mutable.Map
import scala.collection.mutable._
Instead of being conservative and produce a suboptimal output like:
import scala.collection.mutable.{Map, _}
setting groupedImports to AggressiveMerge produces
import scala.collection.mutable._
KeepLeave grouped imports and imports sharing the same prefix untouched.
Explode
Despite making the import section lengthier, exploding grouped imports into separate import statements is made the default behavior because it is more friendly to version control and less likely to create annoying merge conflicts caused by trivial import changes.
ExplodeConfiguration:
OrganizeImports.groupedImports = Explode
Before:
import scala.collection.mutable.{ArrayBuffer, Buffer, StringBuilder}
After:
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.Buffer
import scala.collection.mutable.StringBuilder
MergeConfiguration:
OrganizeImports.groupedImports = Merge
Before:
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.Buffer
import scala.collection.mutable.StringBuilder
import scala.collection.immutable.Set
import scala.collection.immutable._
After:
import scala.collection.mutable.{ArrayBuffer, Buffer, StringBuilder}
import scala.collection.immutable.{Set, _}
AggressiveMergeConfiguration:
OrganizeImports.groupedImports = AggressiveMerge
Before:
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.Buffer
import scala.collection.mutable.StringBuilder
import scala.collection.immutable.Set
import scala.collection.immutable._
After:
import scala.collection.mutable.{ArrayBuffer, Buffer, StringBuilder}
import scala.collection.immutable._
groupsDefines import groups by prefix patterns. Only global imports are processed.
All the imports matching the same prefix pattern are gathered into the same group and sorted by the order defined by the importsOrder option.
|
Caution
| Comments living between imports being processed will be removed. |
|
Tip
|
|
|
Important
|
No matter how the
This special import group is necessary because the above two kinds of imports are order sensitive:
|
An ordered list of import prefix pattern strings. A prefix pattern can be one of the following:
For instance, "scala." is a plain-text pattern that matches imports referring the scala package. Please note that the trailing dot is necessary, otherwise you may have scalafix and scala imports in the same group, which is not what you want in most cases.
A regular expression pattern starts with re:. For instance, "re:javax?\\." is such a pattern that matches both the java and the javax packages. Please refer to the java.util.regex.Pattern Javadoc page for the regular expression syntax. Note that special characters like backslashes must be escaped.
The wildcard pattern, "*", defines the wildcard group, which matches all fully-qualified imports not belonging to any other groups. It can be omitted when it’s the last group. So the following two configurations are equivalent:
OrganizeImports.groups = ["re:javax?\\.", "scala.", "*"]
OrganizeImports.groups = ["re:javax?\\.", "scala."]
Available since v0.5.0-alpha.1.
A blank line marker, "---", defines a blank line between two adjacent import groups when blankLines is set to Manual. It is ignored when blankLines is Auto. Leading and trailing blank line markers are always ignored. Multiple consecutive blank line markers are treated as a single one. So the following three configurations are all equivalent:
OrganizeImports {
blankLines = Manual
groups = [
"---"
"re:javax?\\."
"---"
"scala."
"---"
"---"
"*"
"---"
]
}
OrganizeImports {
blankLines = Manual
groups = [
"re:javax?\\."
"---"
"scala."
"---"
"*"
]
}
OrganizeImports {
blankLines = Auto
groups = [
"re:javax?\\."
"scala."
"*"
]
}
[
"*"
"re:(javax?|scala)\\."
]
This aligns with the default configuration of the IntelliJ Scala plugin version 2020.3.
Configuration:
OrganizeImports.groups = ["re:javax?\\.", "scala.", "*"]
Before:
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import javax.annotation.Generated
import scala.concurrent.ExecutionContext
After:
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
import sun.misc.BASE64Encoder
Configuration:
OrganizeImports.groups = ["re:javax?\\.", "scala.", "*"]
Before:
import scala.util
import util.control
import control.NonFatal
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import javax.annotation.Generated
import scala.concurrent.ExecutionContext
After:
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
import scala.util
import sun.misc.BASE64Encoder
import util.control
import control.NonFatal
Configuration:
OrganizeImports {
groups = ["re:javax?\\.", "scala.", "*"]
groupExplicitlyImportedImplicitsSeparately = true
}
Before:
import scala.util
import util.control
import control.NonFatal
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import javax.annotation.Generated
import scala.concurrent.ExecutionContext.Implicits.global
After:
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.util
import sun.misc.BASE64Encoder
import util.control
import control.NonFatal
import scala.concurrent.ExecutionContext.Implicits.global
Defining import groups using regular expressions can be quite flexible. For instance, the scala.meta package is not part of the Scala standard library, but the default groups defined in the OrganizeImports.groups option move imports from this package into the scala. group. The following example illustrates how to move them into the wildcard group using regular expression.
Configuration:
OrganizeImports.groups = [
"re:javax?\\."
"re:scala.(?!meta\\.)"
"*"
]
Before:
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import scala.meta.Tree
import javax.annotation.Generated
import scala.concurrent.ExecutionContext
import scala.meta.Import
import scala.meta.Pkg
After:
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
import scala.meta.Import
import scala.meta.Pkg
import scala.meta.Tree
import sun.misc.BASE64Encoder
Configuration:
OrganizeImports {
blankLines = Manual
groups = [
"*"
"---"
"re:javax?\\."
"scala."
]
}
Before:
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import javax.annotation.Generated
import scala.concurrent.ExecutionContext
After:
import sun.misc.BASE64Encoder
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
importSelectorsOrderSpecifies the order of grouped import selectors within a single import expression.
Enum: Ascii | SymbolsFirst | Keep
AsciiSort import selectors by ASCII codes, equivalent to the AsciiSortImports rewriting rule in Scalafmt.
SymbolsFirstSort import selectors by the groups: symbols, lower-case, upper-case, equivalent to the SortImports rewriting rule in Scalafmt.
KeepKeep the original order.
Ascii
AsciiConfiguration:
OrganizeImports {
groupedImports = Keep
importSelectorsOrder = Ascii
}
Before:
import foo.{~>, `symbol`, bar, Random}
After:
import foo.{Random, `symbol`, bar, ~>}
SymbolsFirstConfiguration:
OrganizeImports {
groupedImports = Keep
importSelectorsOrder = SymbolsFirst
}
Before:
import foo.{Random, `symbol`, bar, ~>}
After:
import foo.{~>, `symbol`, bar, Random}
importsOrderSpecifies the order of import statements within import groups defined by the OrganizeImports.groups option.
Enum: Ascii | SymbolsFirst | Keep
AsciiSort import statements by ASCII codes. This is the default sorting order that the IntelliJ IDEA Scala import optimizer picks ("lexicographically" option).
SymbolsFirstPut wildcard imports and grouped imports with braces first, otherwise same as Ascii. This replicates IntelliJ IDEA Scala’s "scalastyle consistent" option.
KeepKeep the original order.
Ascii
AsciiConfiguration:
OrganizeImports {
groupedImports = Keep
importsOrder = Ascii
}
Before:
import scala.concurrent._
import scala.concurrent.{Future, Promise}
import scala.concurrent.ExecutionContext.Implicits._
import scala.concurrent.duration
After:
import scala.concurrent.ExecutionContext.Implicits._
import scala.concurrent._
import scala.concurrent.duration
import scala.concurrent.{Promise, Future}
SymbolsFirstConfiguration:
OrganizeImports {
groupedImports = Keep
importsOrder = SymbolsFirst
}
Before:
import scala.concurrent.ExecutionContext.Implicits._
import scala.concurrent._
import scala.concurrent.duration
import scala.concurrent.{Promise, Future}
After:
import scala.concurrent._
import scala.concurrent.{Future, Promise}
import scala.concurrent.ExecutionContext.Implicits._
import scala.concurrent.duration
presetAvailable since v0.5.0.
Specify a preset style.
Enum: DEFAULT | INTELLIJ_2020_3
DEFAULTAn opinionated style recommended for new projects. The OrganizeImports rule tries its best to ensure correctness in all cases when possible. This default style aligns with this principal. In addition, by setting groupedImports to Explode, this style is also more friendly to version control and less likely to create annoying merge conflicts caused by trivial import changes.
OrganizeImports {
blankLines = Auto
coalesceToWildcardImportThreshold = null
expandRelative = false
groupExplicitlyImportedImplicitsSeparately = false
groupedImports = Explode
groups = [
"*"
"re:(javax?|scala)\\."
]
importSelectorsOrder = Ascii
importsOrder = Ascii
preset = DEFAULT
removeUnused = true
}
INTELLIJ_2020_3A style that is compatible with the default configuration of the IntelliJ Scala 2020.3 import optimizer. It is mostly useful for adding OrganizeImports to existing projects developed using the IntelliJ Scala plugin. However, the configuration of this style may introduce subtle correctness issues (so does the default configuration of the IntelliJ Scala plugin). Please see the coalesceToWildcardImportThreshold option for more details.
OrganizeImports {
blankLines = Auto
coalesceToWildcardImportThreshold = 5
expandRelative = false
groupExplicitlyImportedImplicitsSeparately = false
groupedImports = Merge
groups = [
"*"
"re:(javax?|scala)\\."
]
importSelectorsOrder = Ascii
importsOrder = Ascii
preset = INTELLIJ_2020_3
removeUnused = true
}
DEFAULT
removeUnusedRemove unused imports.
|
Caution
|
As mentioned in a previous section, the |
|
Important
|
The |
Boolean
true
Configuration:
OrganizeImports {
groups = ["javax?\\.", "scala.", "*"]
removeUnused = true // not supported in Scala 3
}
Before:
import scala.collection.mutable.{Buffer, ArrayBuffer}
import java.time.Clock
import java.lang.{Long => JLong, Double => JDouble}
object RemoveUnused {
val buffer: ArrayBuffer[Int] = ArrayBuffer.empty[Int]
val long: JLong = JLong.parseLong("0")
}
After:
import java.lang.{Long => JLong}
import scala.collection.mutable.ArrayBuffer
object RemoveUnused {
val buffer: ArrayBuffer[Int] = ArrayBuffer.empty[Int]
val long: JLong = JLong.parseLong("0")
}
Scala
100.0%
A CI-friendly Scalafix semantic rule for organizing imports
Scala
190
340 commits
updated Jul 14, 2023
|
Important
| This rule is included in Scalafix as a built-in rule since Scalafix v0.11.0. Please refer to the Scalafix GitHub repository for bug reports and feature requests. |
OrganizeImports is a CI-friendly Scalafix semantic rule that helps you organize Scala import statements.
Metals, the Scala language server, also uses OrganizeImports to power its "organize imports" code action starting from version v0.9.5.

Please refer to the Scalafix documentation for how to install Scalafix and invoking it in your sbt build.
To try this rule in the sbt console without adding this rule to your sbt build:
sbt> scalafix dependency:OrganizeImports@com.github.liancheng:organize-imports:0.6.0
To include this rule in your sbt build:
ThisBuild / scalafixDependencies += "com.github.liancheng" %% "organize-imports" % "0.6.0"
You can also include this rule in your Mill build using mill-scalafix:
def scalafixIvyDeps = Agg(ivy"com.github.liancheng::organize-imports:0.6.0")
OrganizeImports allows you to specify a preset style via the preset option. To make it easier to add OrganizeImports into existing Scala projects built using the IntelliJ Scala plugin, OrganizeImports provides a preset style compatible with the default configuration of the IntelliJ Scala import optimizer. Please check the INTELLIJ_2020_3 preset style for more details.
The OrganizeImports rule respects source-formatting tools like Scalafmt. If an import statement is already organized according to the configuration, its original source level format is preserved. Therefore, in an sbt project, if you run the following command sequence:
sbt> scalafixAll
...
sbt> scalafmtAll
...
sbt> scalafixAll --check
...
Assuming that the first two commands run successfully, the last scalafixAll --check command should not fail even if some import statements are reformatted by the scalafmtAll command.
However, you should make sure that the source-formatting tools you use do not rewrite import statements in ways that conflict with OrganizeImports. For example, when using Scalafmt together with OrganizeImports, the ExpandImportSelectors, SortImports, and AsciiSortImports rewriting rules should not be used.
Available since v0.6.0.
Running the rule on source files compiled with Scala 3 is still experimental.
Known limitations:
You must use Scalafix 0.9.28 or later
The removeUnused option must be explicitly set to false - the rule currently doesn’t remove unused imports as it’s currently not supported by the compiler.
Usage of deprecated package objects may result in incorrect imports
The groupExplicitlyImportedImplicitsSeparately option has no effect
OrganizeImports {
blankLines = Auto
coalesceToWildcardImportThreshold = null
expandRelative = false
groupExplicitlyImportedImplicitsSeparately = false
groupedImports = Explode
groups = [
"*"
"re:(javax?|scala)\\."
]
importSelectorsOrder = Ascii
importsOrder = Ascii
preset = DEFAULT
removeUnused = true
}
|
Warning
|
Please do NOT use the Scalafix built-in Scalafix rewrites source files by applying patches generated by invoked rules. Each rule generates a patch based on the original text of the source files. When two patches generated by different rules conflict with each other, Scalafix is not able to reconcile the conflicts, and may produce broken code. It is very likely to happen when By default, |
blankLinesAvailable since v0.5.0-alpha.1.
Configures whether blank lines between adjacent import groups are automatically or manually inserted. This option is used together with the --- blank line markers.
Enum: Auto | Manual
A blank line is automatically inserted between adjacent import groups. All blank line markers (---) configured in the groups option are ignored.
A blank line is inserted at all the positions where blank line markers appear in the groups option.
The following two configurations are equivalent:
OrganizeImports {
blankLines = Auto
groups = [
"re:javax?\\."
"scala."
"*"
]
}
OrganizeImports {
blankLines = Manual
groups = [
"re:javax?\\."
"---"
"scala."
"---"
"*"
]
}
Auto
AutoConfiguration:
OrganizeImports {
blankLines = Auto
groups = [
"re:javax?\\."
"scala."
"*"
]
}
Before:
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import javax.annotation.Generated
import scala.concurrent.ExecutionContext
After:
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
import sun.misc.BASE64Encoder
ManualConfiguration:
OrganizeImports {
blankLines = Manual
groups = [
"re:javax?\\."
"scala."
"---"
"*"
]
}
Before:
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import javax.annotation.Generated
import scala.concurrent.ExecutionContext
After:
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
import sun.misc.BASE64Encoder
coalesceToWildcardImportThresholdWhen the number of imported names exceeds a certain threshold, coalesce them into a wildcard import. Renames and unimports are left untouched.
|
Caution
|
Having this feature in Here is an example to illustrate the risk. The following snippet compiles successfully:
The type of However, if we coalesce the grouped imports in the second import statement into a wildcard, there will be a compilation error:
This is because the type of |
Integer. Not setting it or setting it to null disables this feature.
null
Configuration:
OrganizeImports {
groupedImports = Keep
coalesceToWildcardImportThreshold = 3
}
Before:
import scala.collection.immutable.{Seq, Map, Vector, Set}
import scala.collection.immutable.{Seq, Map, Vector}
import scala.collection.immutable.{Seq, Map, Vector => Vec, Set, Stream}
import scala.collection.immutable.{Seq, Map, Vector => _, Set, Stream}
After:
import scala.collection.immutable._
import scala.collection.immutable.{Map, Seq, Vector}
import scala.collection.immutable.{Vector => Vec, _}
import scala.collection.immutable.{Vector => _, _}
expandRelativeExpand relative imports into fully-qualified one.
|
Caution
|
Expanding relative imports may introduce new unused imports. For instance, relative imports in the following snippet
are expanded into
If neither Unfortunately, these newly introduced unused imports cannot be removed by setting |
Boolean
false
Configuration:
OrganizeImports {
expandRelative = true
groups = ["re:javax?\\.", "scala.", "*"]
}
Before:
import scala.util
import util.control
import control.NonFatal
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import javax.annotation.Generated
import scala.concurrent.ExecutionContext
After:
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
import scala.util
import scala.util.control
import scala.util.control.NonFatal
import sun.misc.BASE64Encoder
groupExplicitlyImportedImplicitsSeparatelyThis option provides a workaround to a subtle and rarely seen correctness issue related to explicitly imported implicit names.
The following snippet helps illustrate the problem:
package a
import c._
import b.i
object b { implicit def i: Int = 1 }
object c { implicit def i: Int = 2 }
object Imports {
def f()(implicit i: Int) = println(1)
def main() = f()
}
The above snippet compiles successfully and outputs 1, because the explicitly imported implicit value b.i overrides c.i, which is made available via a wildcard import. However, if we reorder the two imports into:
import b.i
import c._
The Scala compiler starts complaining:
error: could not find implicit value for parameter i: Int
def main() = f()
^
This behavior could be due to a Scala compiler bug since the Scala language specification requires that explicitly imported names should have higher precedence than names made available via a wildcard.
Unfortunately, Scalafix is not able to surgically identify conflicting implicit values behind a wildcard import. In order to guarantee correctness in all cases, when the groupExplicitlyImportedImplicitsSeparately option is set to true, all explicitly imported implicit names are moved into the trailing order-preserving import group together with relative imports, if any (see the trailing order-preserving import group section for more details).
|
Caution
| In general, order-sensitive imports are fragile, and can easily be broken by either human collaborators or tools (e.g., the IntelliJ IDEA Scala import optimizer does not handle this case correctly). They should be eliminated whenever possible. This option is mostly useful when you are dealing with a large trunk of legacy codebase, and you want to minimize manual intervention and guarantee correctness in all cases. |
|
Important
|
The |
Boolean
false
This option defaults to false due to the following reasons:
Although setting it to true avoids the aforementioned correctness issue, the result is unintuitive and confusing for many users since it looks like the groups option is not respected.
E.g., why my scala.concurrent.ExecutionContext.Implicits.global import is moved to a separate group even if I have a scala. group defined in the groups option?
The concerned correctness issue is rarely seen in real life. When it really happens, it is usually a sign of bad coding style, and you may want to tweak your imports to eliminate the root cause.
Configuration:
OrganizeImports {
groups = ["scala.", "*"]
groupExplicitlyImportedImplicitsSeparately = true // not supported in Scala 3
}
Before:
import org.apache.spark.SparkContext
import org.apache.spark.RDD
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.Buffer
import scala.concurrent.ExecutionContext.Implicits.global
import scala.sys.process.stringToProcess
After:
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.Buffer
import org.apache.spark.RDD
import org.apache.spark.SparkContext
import scala.concurrent.ExecutionContext.Implicits.global
import scala.sys.process.stringToProcess
groupedImportsConfigure how to handle grouped imports.
Enum: Explode | Merge | AggressiveMerge | Keep
ExplodeExplode grouped imports into separate import statements.
MergeMerge imports sharing the same prefix into a single grouped import statement.
|
Tip
|
You may want to check the |
|
Important
|
Scala allows a name to be renamed to multiple aliases within a single source file, which makes merging import statements tricky. For example:
The above three imports can be merged into:
but not:
because Scala disallow a name (in this case, Here’s a more complicated example:
While merging these imports, we may want to "bin-pack" them to minimize the number of the result import statements:
However, in reality, renaming aliasing a name multiple times in the same source file is rarely a practical need. Therefore, |
AggressiveMergeSimilar to Merge, but merges imports more aggressively and produces more concise results, despite a relatively low risk of introducing compilation errors.
The OrganizeImports rule tries hard to guarantee correctness in all cases. This forces it to be more conservative when merging imports, and may sometimes produce suboptimal output. Here is a concrete example about correctness:
import scala.collection.immutable._
import scala.collection.mutable.Map
import scala.collection.mutable._
object Example {
val m: Map[Int, Int] = ???
}
At a first glance, it seems feasible to simply drop the second import since mutable._ already covers mutble.Map. However, similar to the example illustrated in the section about the coalesceToWildcardImportThreshold option, the type of Example.m above is mutable.Map, because the mutable Map explicitly imported in the second import takes higher precedence than the immutable Map imported via wildcard in the first import. If we merge the last two imports naively, we’ll get:
import scala.collection.immutable._
import scala.collection.mutable._
This triggers in a compilation error, because both immutable.Map and mutable.Map are now imported via wildcards with the same precedence. This makes the type of Example.m ambiguous. The correct result should be:
import scala.collection.immutable._
import scala.collection.mutable.{Map, _}
On the other hand, the case discussed above is rarely seen in practice. A more commonly seen case is something like:
import scala.collection.mutable.Map
import scala.collection.mutable._
Instead of being conservative and produce a suboptimal output like:
import scala.collection.mutable.{Map, _}
setting groupedImports to AggressiveMerge produces
import scala.collection.mutable._
KeepLeave grouped imports and imports sharing the same prefix untouched.
Explode
Despite making the import section lengthier, exploding grouped imports into separate import statements is made the default behavior because it is more friendly to version control and less likely to create annoying merge conflicts caused by trivial import changes.
ExplodeConfiguration:
OrganizeImports.groupedImports = Explode
Before:
import scala.collection.mutable.{ArrayBuffer, Buffer, StringBuilder}
After:
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.Buffer
import scala.collection.mutable.StringBuilder
MergeConfiguration:
OrganizeImports.groupedImports = Merge
Before:
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.Buffer
import scala.collection.mutable.StringBuilder
import scala.collection.immutable.Set
import scala.collection.immutable._
After:
import scala.collection.mutable.{ArrayBuffer, Buffer, StringBuilder}
import scala.collection.immutable.{Set, _}
AggressiveMergeConfiguration:
OrganizeImports.groupedImports = AggressiveMerge
Before:
import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.Buffer
import scala.collection.mutable.StringBuilder
import scala.collection.immutable.Set
import scala.collection.immutable._
After:
import scala.collection.mutable.{ArrayBuffer, Buffer, StringBuilder}
import scala.collection.immutable._
groupsDefines import groups by prefix patterns. Only global imports are processed.
All the imports matching the same prefix pattern are gathered into the same group and sorted by the order defined by the importsOrder option.
|
Caution
| Comments living between imports being processed will be removed. |
|
Tip
|
|
|
Important
|
No matter how the
This special import group is necessary because the above two kinds of imports are order sensitive:
|
An ordered list of import prefix pattern strings. A prefix pattern can be one of the following:
For instance, "scala." is a plain-text pattern that matches imports referring the scala package. Please note that the trailing dot is necessary, otherwise you may have scalafix and scala imports in the same group, which is not what you want in most cases.
A regular expression pattern starts with re:. For instance, "re:javax?\\." is such a pattern that matches both the java and the javax packages. Please refer to the java.util.regex.Pattern Javadoc page for the regular expression syntax. Note that special characters like backslashes must be escaped.
The wildcard pattern, "*", defines the wildcard group, which matches all fully-qualified imports not belonging to any other groups. It can be omitted when it’s the last group. So the following two configurations are equivalent:
OrganizeImports.groups = ["re:javax?\\.", "scala.", "*"]
OrganizeImports.groups = ["re:javax?\\.", "scala."]
Available since v0.5.0-alpha.1.
A blank line marker, "---", defines a blank line between two adjacent import groups when blankLines is set to Manual. It is ignored when blankLines is Auto. Leading and trailing blank line markers are always ignored. Multiple consecutive blank line markers are treated as a single one. So the following three configurations are all equivalent:
OrganizeImports {
blankLines = Manual
groups = [
"---"
"re:javax?\\."
"---"
"scala."
"---"
"---"
"*"
"---"
]
}
OrganizeImports {
blankLines = Manual
groups = [
"re:javax?\\."
"---"
"scala."
"---"
"*"
]
}
OrganizeImports {
blankLines = Auto
groups = [
"re:javax?\\."
"scala."
"*"
]
}
[
"*"
"re:(javax?|scala)\\."
]
This aligns with the default configuration of the IntelliJ Scala plugin version 2020.3.
Configuration:
OrganizeImports.groups = ["re:javax?\\.", "scala.", "*"]
Before:
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import javax.annotation.Generated
import scala.concurrent.ExecutionContext
After:
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
import sun.misc.BASE64Encoder
Configuration:
OrganizeImports.groups = ["re:javax?\\.", "scala.", "*"]
Before:
import scala.util
import util.control
import control.NonFatal
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import javax.annotation.Generated
import scala.concurrent.ExecutionContext
After:
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
import scala.util
import sun.misc.BASE64Encoder
import util.control
import control.NonFatal
Configuration:
OrganizeImports {
groups = ["re:javax?\\.", "scala.", "*"]
groupExplicitlyImportedImplicitsSeparately = true
}
Before:
import scala.util
import util.control
import control.NonFatal
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import javax.annotation.Generated
import scala.concurrent.ExecutionContext.Implicits.global
After:
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.util
import sun.misc.BASE64Encoder
import util.control
import control.NonFatal
import scala.concurrent.ExecutionContext.Implicits.global
Defining import groups using regular expressions can be quite flexible. For instance, the scala.meta package is not part of the Scala standard library, but the default groups defined in the OrganizeImports.groups option move imports from this package into the scala. group. The following example illustrates how to move them into the wildcard group using regular expression.
Configuration:
OrganizeImports.groups = [
"re:javax?\\."
"re:scala.(?!meta\\.)"
"*"
]
Before:
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import scala.meta.Tree
import javax.annotation.Generated
import scala.concurrent.ExecutionContext
import scala.meta.Import
import scala.meta.Pkg
After:
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
import scala.meta.Import
import scala.meta.Pkg
import scala.meta.Tree
import sun.misc.BASE64Encoder
Configuration:
OrganizeImports {
blankLines = Manual
groups = [
"*"
"---"
"re:javax?\\."
"scala."
]
}
Before:
import scala.collection.JavaConverters._
import java.time.Clock
import sun.misc.BASE64Encoder
import javax.annotation.Generated
import scala.concurrent.ExecutionContext
After:
import sun.misc.BASE64Encoder
import java.time.Clock
import javax.annotation.Generated
import scala.collection.JavaConverters._
import scala.concurrent.ExecutionContext
importSelectorsOrderSpecifies the order of grouped import selectors within a single import expression.
Enum: Ascii | SymbolsFirst | Keep
AsciiSort import selectors by ASCII codes, equivalent to the AsciiSortImports rewriting rule in Scalafmt.
SymbolsFirstSort import selectors by the groups: symbols, lower-case, upper-case, equivalent to the SortImports rewriting rule in Scalafmt.
KeepKeep the original order.
Ascii
AsciiConfiguration:
OrganizeImports {
groupedImports = Keep
importSelectorsOrder = Ascii
}
Before:
import foo.{~>, `symbol`, bar, Random}
After:
import foo.{Random, `symbol`, bar, ~>}
SymbolsFirstConfiguration:
OrganizeImports {
groupedImports = Keep
importSelectorsOrder = SymbolsFirst
}
Before:
import foo.{Random, `symbol`, bar, ~>}
After:
import foo.{~>, `symbol`, bar, Random}
importsOrderSpecifies the order of import statements within import groups defined by the OrganizeImports.groups option.
Enum: Ascii | SymbolsFirst | Keep
AsciiSort import statements by ASCII codes. This is the default sorting order that the IntelliJ IDEA Scala import optimizer picks ("lexicographically" option).
SymbolsFirstPut wildcard imports and grouped imports with braces first, otherwise same as Ascii. This replicates IntelliJ IDEA Scala’s "scalastyle consistent" option.
KeepKeep the original order.
Ascii
AsciiConfiguration:
OrganizeImports {
groupedImports = Keep
importsOrder = Ascii
}
Before:
import scala.concurrent._
import scala.concurrent.{Future, Promise}
import scala.concurrent.ExecutionContext.Implicits._
import scala.concurrent.duration
After:
import scala.concurrent.ExecutionContext.Implicits._
import scala.concurrent._
import scala.concurrent.duration
import scala.concurrent.{Promise, Future}
SymbolsFirstConfiguration:
OrganizeImports {
groupedImports = Keep
importsOrder = SymbolsFirst
}
Before:
import scala.concurrent.ExecutionContext.Implicits._
import scala.concurrent._
import scala.concurrent.duration
import scala.concurrent.{Promise, Future}
After:
import scala.concurrent._
import scala.concurrent.{Future, Promise}
import scala.concurrent.ExecutionContext.Implicits._
import scala.concurrent.duration
presetAvailable since v0.5.0.
Specify a preset style.
Enum: DEFAULT | INTELLIJ_2020_3
DEFAULTAn opinionated style recommended for new projects. The OrganizeImports rule tries its best to ensure correctness in all cases when possible. This default style aligns with this principal. In addition, by setting groupedImports to Explode, this style is also more friendly to version control and less likely to create annoying merge conflicts caused by trivial import changes.
OrganizeImports {
blankLines = Auto
coalesceToWildcardImportThreshold = null
expandRelative = false
groupExplicitlyImportedImplicitsSeparately = false
groupedImports = Explode
groups = [
"*"
"re:(javax?|scala)\\."
]
importSelectorsOrder = Ascii
importsOrder = Ascii
preset = DEFAULT
removeUnused = true
}
INTELLIJ_2020_3A style that is compatible with the default configuration of the IntelliJ Scala 2020.3 import optimizer. It is mostly useful for adding OrganizeImports to existing projects developed using the IntelliJ Scala plugin. However, the configuration of this style may introduce subtle correctness issues (so does the default configuration of the IntelliJ Scala plugin). Please see the coalesceToWildcardImportThreshold option for more details.
OrganizeImports {
blankLines = Auto
coalesceToWildcardImportThreshold = 5
expandRelative = false
groupExplicitlyImportedImplicitsSeparately = false
groupedImports = Merge
groups = [
"*"
"re:(javax?|scala)\\."
]
importSelectorsOrder = Ascii
importsOrder = Ascii
preset = INTELLIJ_2020_3
removeUnused = true
}
DEFAULT
removeUnusedRemove unused imports.
|
Caution
|
As mentioned in a previous section, the |
|
Important
|
The |
Boolean
true
Configuration:
OrganizeImports {
groups = ["javax?\\.", "scala.", "*"]
removeUnused = true // not supported in Scala 3
}
Before:
import scala.collection.mutable.{Buffer, ArrayBuffer}
import java.time.Clock
import java.lang.{Long => JLong, Double => JDouble}
object RemoveUnused {
val buffer: ArrayBuffer[Int] = ArrayBuffer.empty[Int]
val long: JLong = JLong.parseLong("0")
}
After:
import java.lang.{Long => JLong}
import scala.collection.mutable.ArrayBuffer
object RemoveUnused {
val buffer: ArrayBuffer[Int] = ArrayBuffer.empty[Int]
val long: JLong = JLong.parseLong("0")
}
Scala
100.0%