Skip to content

Commit

Permalink
Align with kotlin plugin on how subpackages are represented (#998)
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn authored Dec 12, 2020
1 parent e33adfc commit cd6fb0b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ disabled_rules=no-wildcard-imports,experimental:annotation,my-custom-ruleset:my-
# "*" - 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.
# "|" - blank line. Supports only single blank lines between imports. No blank line is allowed in the beginning or end of the layout.
# "^" - alias import, e.g. "^android.*" will match all android alias imports, "^" will match all other alias imports.
# import paths - these can be full paths, e.g. "java.util.List" as well as wildcard paths, e.g. "kotlin.*"
# import paths - these can be full paths, e.g. "java.util.List.*" as well as wildcard paths, e.g. "kotlin.**"
# Examples:
kotlin_imports_layout=ascii # alphabetical with capital letters before lower case letters (e.g. Z before a), no blank lines
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
kotlin_imports_layout=android.*,|,^org.junit.*,kotlin.io.Closeable,|,*,^ # custom imports layout
kotlin_imports_layout=android.**,|,^org.junit.**,kotlin.io.Closeable.*,|,**,^ # custom imports layout
# 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
# Note: this is not yet implemented on IDEA side, so it only takes effect for ktlint
ij_kotlin_imports_layout=*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import org.jetbrains.kotlin.psi.KtImportDirective
* * custom - defined by the following set of tokens. Tokens can be combined together in a group, groups/tokens must be comma separated:
* * "*" - wildcard symbol, can be used as follows:
* 1. Single, meaning matching any import (<all other imports> in IDEA)
* 2. After an import path, e.g. "java.*" or "kotlin.io.*"
* 2. After an import path, e.g. "java.*" or "kotlin.io.Closeable.*"
* 3. Doubled after an import path, e.g. "java.**" or "kotlin.io.**", meaning "with subpackages"
* * "|" - blank line symbol. Only supported single blank lines between imports. Multiple blank lines will be ignored. Blank lines are not allowed outside of import list.
* * "^" - alias symbol, can be used as follows:
* 1. In front of an import path, meaning matching all alias imports from this path, e.g. "^android.*"
* 2. Alone, meaning matching any alias import - "^" (<all other alias imports> in IDEA)
* * import paths - these can be full paths, e.g. "java.util.List" as well as wildcard paths, e.g. "kotlin.*"
* * import paths - these can be full paths, e.g. "java.util.List.*" as well as wildcard paths meaning "with subpackages", e.g. "kotlin.**"
*
* In case the custom property is not provided, the rule defaults to "ascii" style in case of "android" flag supplied, or to "idea" otherwise.
*/
Expand Down Expand Up @@ -64,7 +65,7 @@ public class ImportOrderingRule :
*
* https://github.com/JetBrains/kotlin/blob/ffdab473e28d0d872136b910eb2e0f4beea2e19c/idea/formatter/src/org/jetbrains/kotlin/idea/core/formatter/KotlinCodeStyleSettings.java#L87-L91
*/
private val IDEA_PATTERN = parseImportsLayout("*,java.*,javax.*,kotlin.*,^")
private val IDEA_PATTERN = parseImportsLayout("*,java.**,javax.**,kotlin.**,^")

private const val IDEA_ERROR_MESSAGE = "Imports must be ordered in lexicographic order without any empty lines in-between " +
"with \"java\", \"javax\", \"kotlin\" and aliases in the end"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ internal const val WILDCARD_CHAR = "*"
internal const val ALIAS_CHAR = "^"

/**
* Adopted from https://github.com/JetBrains/intellij-community/blob/70fd799e94246f2c0fe924763ed892765c0dff9a/java/java-impl/src/com/intellij/psi/codeStyle/JavaPackageEntryTableAccessor.java#L25
* Adapted from https://github.com/JetBrains/intellij-kotlin/blob/73b5a484198f02518c9ece2fb453d27cead680fb/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinPackageEntryTableAccessor.kt#L27-L43
*/
internal fun parseImportsLayout(importsLayout: String): List<PatternEntry> {
val importsList = importsLayout.split(",").onEach { it.trim() }
Expand All @@ -29,7 +29,8 @@ internal fun parseImportsLayout(importsLayout: String): List<PatternEntry> {
import = import.substring(1).trim()
hasAlias = true
}
if (import.endsWith(WILDCARD_CHAR)) {
if (import.endsWith(WILDCARD_CHAR + WILDCARD_CHAR)) { // java.**
import = import.substringBeforeLast(WILDCARD_CHAR)
withSubpackages = true
}
return@map if (import == WILDCARD_CHAR) { // *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ class ImportLayoutParserTest {

@Test(expected = IllegalArgumentException::class)
fun `pattern without single wildcard is not allowed`() {
parseImportsLayout("java.util.List")
parseImportsLayout("java.util.List.*")
}

@Test
fun `parses correctly`() {
val expected = listOf(
PatternEntry("android", withSubpackages = true, hasAlias = false),
PatternEntry("android.*", withSubpackages = true, hasAlias = false),
PatternEntry.BLANK_LINE_ENTRY,
PatternEntry("org.junit", withSubpackages = true, hasAlias = false),
PatternEntry("org.junit.*", withSubpackages = true, hasAlias = false),
PatternEntry.BLANK_LINE_ENTRY,
PatternEntry("android", withSubpackages = true, hasAlias = true),
PatternEntry("android.*", withSubpackages = true, hasAlias = true),
PatternEntry.ALL_OTHER_IMPORTS_ENTRY,
PatternEntry("kotlin.io.Closeable.*", withSubpackages = false, hasAlias = false),
PatternEntry.ALL_OTHER_ALIAS_IMPORTS_ENTRY
)
val actual = parseImportsLayout("android.*,|,org.junit.*,|,^android.*,*,^")
val actual = parseImportsLayout("android.**,|,org.junit.**,|,^android.**,*,kotlin.io.Closeable.*,^")

assertThat(actual).isEqualTo(expected)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class ImportOrderingRuleCustomTest {
""".trimIndent()

val testFile = writeCustomImportsOrderingConfig(
"android.*,|,org.junit.*,|,net.*,|,org.*,|,java.*,|,com.*,|,javax.*,|,*"
"android.**,|,org.junit.**,|,net.**,|,org.**,|,java.**,|,com.**,|,javax.**,|,*"
)

assertThat(
Expand Down Expand Up @@ -195,7 +195,7 @@ class ImportOrderingRuleCustomTest {
""".trimIndent()

val testFile = writeCustomImportsOrderingConfig(
"android.*,|,org.junit.*,|,net.*,|,org.*,|,java.*,|,com.*,|,javax.*,|,*"
"android.**,|,org.junit.**,|,net.**,|,org.**,|,java.**,|,com.**,|,javax.**,|,*"
)

assertThat(
Expand All @@ -220,7 +220,7 @@ class ImportOrderingRuleCustomTest {
""".trimIndent()

val testFile = writeCustomImportsOrderingConfig(
"java.*,|,|,|,kotlin.*,*"
"java.**,|,|,|,kotlin.**,*"
)

assertThat(
Expand Down Expand Up @@ -257,7 +257,7 @@ class ImportOrderingRuleCustomTest {
""".trimIndent()

val testFile = writeCustomImportsOrderingConfig(
"java.*,|,|,|,kotlin.*,*"
"java.**,|,|,|,kotlin.**,*"
)

assertThat(
Expand All @@ -282,7 +282,7 @@ class ImportOrderingRuleCustomTest {
""".trimIndent()

val testFile = writeCustomImportsOrderingConfig(
"^kotlin.*,^android.*,android.*,|,*,^"
"^kotlin.**,^android.**,android.**,|,*,^"
)

assertThat(
Expand Down Expand Up @@ -316,7 +316,7 @@ class ImportOrderingRuleCustomTest {
""".trimIndent()

val testFile = writeCustomImportsOrderingConfig(
"^kotlin.*,^android.*,android.*,|,^,*"
"^kotlin.**,^android.**,android.**,|,^,*"
)

assertThat(
Expand All @@ -340,7 +340,7 @@ class ImportOrderingRuleCustomTest {
""".trimIndent()

val testFile = writeCustomImportsOrderingConfig(
"kotlin.io.Closeable,kotlin.*,*"
"kotlin.io.Closeable.*,kotlin.**,*"
)

assertThat(
Expand Down Expand Up @@ -374,7 +374,7 @@ class ImportOrderingRuleCustomTest {
""".trimIndent()

val testFile = writeCustomImportsOrderingConfig(
"kotlin.io.Closeable,kotlin.*,*"
"kotlin.io.Closeable.*,kotlin.**,*"
)

assertThat(
Expand Down

0 comments on commit cd6fb0b

Please sign in to comment.