Skip to content

Commit 51eaeb0

Browse files
committed
Configuration: document how imports sorting works
1 parent adf25bb commit 51eaeb0

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

docs/configuration.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3610,6 +3610,16 @@ or selector, as follows:
36103610

36113611
> Since v3.0.0.
36123612
3613+
Let's define some terminology: an import statement consists of several parts:
3614+
3615+
- keyword: `import` or `export`
3616+
- one or more comma-separated _importers_
3617+
- for instance, `import foo.bar, foo.baz.{qux => quux}` contains two
3618+
importers, `foo.bar` and `foo.baz.{qux => quux}`
3619+
- each importer is split, on the final dot, into
3620+
- _reference_: `foo` and `foo.baz` in the example above
3621+
- _selectors_: `bar` and `{qux => quux}` above
3622+
36133623
#### Imports: `expand`
36143624

36153625
This parameter will attempt to create a separate line for each selector
@@ -3637,6 +3647,21 @@ import a.{
36373647
}
36383648
```
36393649

3650+
#### Imports: sorting
3651+
3652+
Sorting is applied as follows:
3653+
3654+
- if disabled, no sorting
3655+
- if enabled, it applies to import _selectors_ within one _importer_
3656+
- if [`groups`](#imports-groups) are used, sorting will also apply to importers
3657+
in the same group
3658+
- without groups, multiple import statements will **not** be sorted
3659+
- importers are sorted one dot-separated label at a time
3660+
- importers `foo.bar.baz` and `foo.bar as fbar` will compare:
3661+
- `foo` and `foo`: equal
3662+
- `bar` and `bar as fbar`: `bar` comes earlier, just like it does
3663+
with equivalent scala2 selector syntax `{bar => fbar}`
3664+
36403665
#### Imports: `sort = none`
36413666

36423667
This default option causes no sorting.
@@ -3697,8 +3722,9 @@ import foo._
36973722
> rule like `OrganizeImports`. However, on a large codebase, the overhead
36983723
> of using semantic `scalafix` rules might be substantial.
36993724
3700-
This rule will separate all import statements into groups. If sorting is
3701-
enabled (i.e., not `none`), imports will also be sorted within each group.
3725+
This rule will separate all import statements (or, to be more precise, all
3726+
[importers](#imports) from all import statements) into groups. If sorting
3727+
is enabled (i.e., not `none`), imports will also be sorted within each group.
37023728

37033729
The rule accepts the following parameters:
37043730

@@ -3715,13 +3741,15 @@ The rule accepts the following parameters:
37153741
(source, package, template etc.)
37163742

37173743
```scala mdoc:scalafmt
3744+
runner.dialect = scala3
37183745
rewrite.rules = [Imports]
37193746
rewrite.imports.sort = ascii
37203747
rewrite.imports.groups = [
37213748
["foo\\..*"],
37223749
["bar\\..*", "baz\\..*"]
37233750
]
37243751
---
3752+
import bar.bar as bbar
37253753
import bar.bar.{Random, bar, ~>, `symbol`}
37263754
import baz.Baz.{bar => xyz, _}
37273755
import qux.`qux`.{Random, bar, ~>, `symbol`}

0 commit comments

Comments
 (0)