Skip to content

Commit 06b770c

Browse files
authored
Deprecate shorcuts for import-ordering (pinterest#1018)
* Deprecate shorcuts for import-ordering * Update CHANGELOG.md
1 parent 13aa145 commit 06b770c

File tree

6 files changed

+42
-25
lines changed

6 files changed

+42
-25
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ insert_final_newline = true
1414
indent_size = 4
1515

1616
[*.{kt,kts}]
17-
kotlin_imports_layout=ascii
17+
ij_kotlin_imports_layout=*
1818

1919
[{Makefile,*.go}]
2020
indent_style = tab

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1010
- Empty line before primary constructor is not reported and formatted-out ([#1004](https://github.com/pinterest/ktlint/issues/1004))
1111
- Fix '.editorconfig' generation for "import-ordering" rule ([#1011](https://github.com/pinterest/ktlint/issues/1004))
1212
- Fix "filename" rule will not work when '.editorconfig' file is not found ([#997](https://github.com/pinterest/ktlint/issues/1004))
13+
- EditorConfig generation for `import-ordering` ([#1011](https://github.com/pinterest/ktlint/pull/1011))
1314

1415
### Changed
1516
- Update Gradle shadow plugin to `6.1.0` version
1617
- Align with Kotlin plugin on how alias pattern is represented for imports layout rule ([#753](https://github.com/pinterest/ktlint/issues/753))
1718
- Align with Kotlin plugin on how subpackages are represented ([#753](https://github.com/pinterest/ktlint/issues/753))
19+
- Deprecated custom `kotlin_imports_layout` EditorConfig property. Please use `ij_kotlin_imports_layout` to ensure
20+
that the Kotlin IDE plugin and ktlint use same imports layout ([#753](https://github.com/pinterest/ktlint/issues/753))
21+
- Deprecated `idea` and `ascii` shortcuts as the `ij_kotlin_imports_layout` property does not support those.
22+
Please check README on how to achieve those with patterns ([#753](https://github.com/pinterest/ktlint/issues/753))
1823

1924
### Removed
2025

README.md

+5-10
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,15 @@ max_line_length=off
9090
# by the ruleset identifier.
9191
disabled_rules=no-wildcard-imports,experimental:annotation,my-custom-ruleset:my-custom-rule
9292

93-
# Defines the imports layout. There are predefined layouts like "ascii" or "idea", as well as a custom layout.
94-
# The predefined layouts are temporary and will be deprecated in the future, once Kotlin plugin supports EditorConfig property for imports layout.
95-
# The custom layout can be composed by the following symbols:
93+
# Defines the imports layout. The layout can be composed by the following symbols:
9694
# "*" - wildcard. There must be at least one entry of a single wildcard to match all other imports. Matches anything after a specified symbol/import as well.
9795
# "|" - blank line. Supports only single blank lines between imports. No blank line is allowed in the beginning or end of the layout.
9896
# "^" - alias import, e.g. "^android.*" will match all android alias imports, "^" will match all other alias imports.
9997
# import paths - these can be full paths, e.g. "java.util.List.*" as well as wildcard paths, e.g. "kotlin.**"
100-
# Examples:
101-
kotlin_imports_layout=ascii # alphabetical with capital letters before lower case letters (e.g. Z before a), no blank lines
102-
kotlin_imports_layout=idea # default IntelliJ IDEA style, same as "ascii", but with "java", "javax", "kotlin" and alias imports in the end of the imports list
103-
kotlin_imports_layout=android.**,|,^org.junit.**,kotlin.io.Closeable.*,|,**,^ # custom imports layout
104-
# Alternatively ij_kotlin_imports_layout name can be used, in order to set an imports layout for both ktlint and IDEA via a single property
105-
# Note: this is not yet implemented on IDEA side, so it only takes effect for ktlint
106-
ij_kotlin_imports_layout=*
98+
# Examples (we use ij_kotlin_imports_layout to set an imports layout for both ktlint and IDEA via a single property):
99+
ij_kotlin_imports_layout=* # alphabetical with capital letters before lower case letters (e.g. Z before a), no blank lines
100+
ij_kotlin_imports_layout=*,java.**,javax.**,kotlin.**,^ # default IntelliJ IDEA style, same as alphabetical, but with "java", "javax", "kotlin" and alias imports in the end of the imports list
101+
ij_kotlin_imports_layout=android.**,|,^org.junit.**,kotlin.io.Closeable.*,|,*,^ # custom imports layout
107102
```
108103

109104
### Overriding Editorconfig properties for specific directories

ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/ImportOrderingRule.kt

+29-12
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl
1919
import org.jetbrains.kotlin.psi.KtImportDirective
2020

2121
/**
22-
* Import ordering is configured via EditorConfig's custom property `kotlin_imports_layout`. Supported values:
23-
* * "idea" - default IntelliJ IDEA's order, see [IDEA_PATTERN]
24-
* * "ascii" - alphabetical order as recommended in Android's Kotlin style guide, see [ASCII_PATTERN]
22+
* Import ordering is configured via EditorConfig's property `ij_kotlin_imports_layout`, so the Kotlin IDE plugin also recongizes it. Supported values:
23+
* * "*,java.**,javax.**,kotlin.**,^" - default IntelliJ IDEA's order, see [IDEA_PATTERN]
24+
* * "*" - alphabetical order as recommended in Android's Kotlin style guide, see [ASCII_PATTERN]
2525
* * custom - defined by the following set of tokens. Tokens can be combined together in a group, groups/tokens must be comma separated:
2626
* * "*" - wildcard symbol, can be used as follows:
2727
* 1. Single, meaning matching any import (<all other imports> in IDEA)
@@ -33,7 +33,7 @@ import org.jetbrains.kotlin.psi.KtImportDirective
3333
* 2. Alone, meaning matching any alias import - "^" (<all other alias imports> in IDEA)
3434
* * import paths - these can be full paths, e.g. "java.util.List.*" as well as wildcard paths meaning "with subpackages", e.g. "kotlin.**"
3535
*
36-
* In case the custom property is not provided, the rule defaults to "ascii" style in case of "android" flag supplied, or to "idea" otherwise.
36+
* In case the custom property is not provided, the rule defaults to alphabetical order in case of "android" flag supplied, or to idea otherwise.
3737
*/
3838
@OptIn(FeatureInAlphaState::class)
3939
public class ImportOrderingRule :
@@ -84,14 +84,26 @@ public class ImportOrderingRule :
8484
value,
8585
"Import layout must contain at least one entry of a wildcard symbol (*)"
8686
)
87-
value == "idea" -> PropertyType.PropertyValue.valid(
88-
value,
89-
IDEA_PATTERN
90-
)
91-
value == "ascii" -> PropertyType.PropertyValue.valid(
92-
value,
93-
ASCII_PATTERN
94-
)
87+
value == "idea" -> {
88+
println(
89+
"[WARNING] `idea` is deprecated! Please use `*,java.**,javax.**,kotlin.**,^` instead" +
90+
" to ensure that the Kotlin IDE plugin recognizes the value"
91+
)
92+
PropertyType.PropertyValue.valid(
93+
value,
94+
IDEA_PATTERN
95+
)
96+
}
97+
value == "ascii" -> {
98+
println(
99+
"[WARNING] `ascii` is deprecated! Please use `*` instead" +
100+
" to ensure that the Kotlin IDE plugin recognizes the value"
101+
)
102+
PropertyType.PropertyValue.valid(
103+
value,
104+
ASCII_PATTERN
105+
)
106+
}
95107
else -> try {
96108
PropertyType.PropertyValue.valid(
97109
value,
@@ -106,6 +118,7 @@ public class ImportOrderingRule :
106118
}
107119
}
108120

121+
@Deprecated("This custom property is deprecated in favor of IDEA's default ideaImportsLayoutProperty")
109122
internal val ktlintCustomImportsLayoutProperty =
110123
UsesEditorConfigProperties.EditorConfigProperty<List<PatternEntry>>(
111124
type = PropertyType(
@@ -219,6 +232,10 @@ public class ImportOrderingRule :
219232
private fun EditorConfigProperties.resolveImportsLayout(
220233
android: Boolean
221234
): List<PatternEntry> = if (containsKey(KTLINT_CUSTOM_IMPORTS_LAYOUT_PROPERTY_NAME)) {
235+
println(
236+
"[WARNING] `kotlin_imports_layout` is deprecated! Please use `ij_kotlin_imports_layout` to ensure" +
237+
" that the Kotlin IDE plugin and ktlint use same imports layout"
238+
)
222239
getEditorConfigValue(ktlintCustomImportsLayoutProperty, android)
223240
} else {
224241
getEditorConfigValue(ideaImportsLayoutProperty, android)

ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/importordering/ImportOrderingRuleAsciiTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ class ImportOrderingRuleAsciiTest {
297297
private fun writeAsciiImportsOrderingConfig() = editorConfigTestRule
298298
.writeToEditorConfig(
299299
mapOf(
300-
ImportOrderingRule.ideaImportsLayoutProperty.type to "ascii"
300+
ImportOrderingRule.ideaImportsLayoutProperty.type to "*"
301301
)
302302
)
303303
.absolutePath

ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/importordering/ImportOrderingRuleIdeaTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ class ImportOrderingRuleIdeaTest {
353353
private fun writeIdeaImportsOrderingConfig() = editorConfigTestRule
354354
.writeToEditorConfig(
355355
mapOf(
356-
ImportOrderingRule.ideaImportsLayoutProperty.type to "idea"
356+
ImportOrderingRule.ideaImportsLayoutProperty.type to "*,java.**,javax.**,kotlin.**,^"
357357
)
358358
)
359359
.absolutePath

0 commit comments

Comments
 (0)