Skip to content

Commit

Permalink
Merge pull request #1011 from romtsn/fix-import-ordering-editorconfig
Browse files Browse the repository at this point in the history
Fix import-ordering editorconifg generation
  • Loading branch information
Tapchicoma authored Dec 16, 2020
2 parents 7983445 + 93a4366 commit 2538730
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,32 @@ public interface UsesEditorConfigProperties {
?: if (isAndroidCodeStyle) property.defaultAndroidValue else property.defaultValue
}

/**
* Write the string representation of [EditorConfigProperty]
*/
public fun <T> EditorConfigProperties.writeEditorConfigProperty(
property: EditorConfigProperty<T>,
isAndroidCodeStyle: Boolean
): String {
return property.propertyWriter(getEditorConfigValue(property, isAndroidCodeStyle))
}

/**
* Supported `.editorconfig` property.
*
* @param type type of property. Could be one of default ones (see [PropertyType.STANDARD_TYPES]) or custom one.
* @param defaultValue default value for property if it does not exist in loaded properties.
* @param defaultAndroidValue default value for android codestyle. You should set different value only when it
* differs from [defaultValue].
* @param propertyWriter custom function that represents [T] as String. Defaults to the standard `toString()` call.
* You should override the default implementation in case you need a different behavior than the standard `toString()`
* (e.g. for collections joinToString() is more applicable).
*/
public data class EditorConfigProperty<T>(
public val type: PropertyType<T>,
public val defaultValue: T,
public val defaultAndroidValue: T = defaultValue
public val defaultAndroidValue: T = defaultValue,
public val propertyWriter: (T) -> String = { it.toString() }
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ internal class EditorConfigGenerator(
rule.editorConfigProperties.forEach { prop ->
if (debug) println("Setting '${prop.type.name}' property value")
acc[prop.type.name] = with(rule) {
editorConfig.getEditorConfigValue(
editorConfig.writeEditorConfigProperty(
prop,
isAndroidCodeStyle
).toString()
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,9 @@ public class ImportOrderingRule :
private val editorConfigPropertyParser: (String, String?) -> PropertyType.PropertyValue<List<PatternEntry>> =
{ _, value ->
when {
value == null -> PropertyType.PropertyValue.invalid(
value.isNullOrBlank() -> PropertyType.PropertyValue.invalid(
value,
"Null is not supported for import layout"
)
value.isBlank() -> PropertyType.PropertyValue.valid(
value,
emptyList()
"Import layout must contain at least one entry of a wildcard symbol (*)"
)
value == "idea" -> PropertyType.PropertyValue.valid(
value,
Expand Down Expand Up @@ -118,7 +114,8 @@ public class ImportOrderingRule :
editorConfigPropertyParser
),
defaultValue = IDEA_PATTERN,
defaultAndroidValue = ASCII_PATTERN
defaultAndroidValue = ASCII_PATTERN,
propertyWriter = { it.joinToString(separator = ",") }
)

internal val ideaImportsLayoutProperty =
Expand All @@ -129,7 +126,8 @@ public class ImportOrderingRule :
editorConfigPropertyParser
),
defaultValue = IDEA_PATTERN,
defaultAndroidValue = ASCII_PATTERN
defaultAndroidValue = ASCII_PATTERN,
propertyWriter = { it.joinToString(separator = ",") }
)
}

Expand Down Expand Up @@ -220,13 +218,10 @@ public class ImportOrderingRule :

private fun EditorConfigProperties.resolveImportsLayout(
android: Boolean
): List<PatternEntry> = when {
containsKey(KTLINT_CUSTOM_IMPORTS_LAYOUT_PROPERTY_NAME) ->
getValue(KTLINT_CUSTOM_IMPORTS_LAYOUT_PROPERTY_NAME).getValueAs()
containsKey(IDEA_IMPORTS_LAYOUT_PROPERTY_NAME) ->
getValue(IDEA_IMPORTS_LAYOUT_PROPERTY_NAME).getValueAs()
else ->
if (android) ideaImportsLayoutProperty.defaultAndroidValue else ideaImportsLayoutProperty.defaultValue
): List<PatternEntry> = if (containsKey(KTLINT_CUSTOM_IMPORTS_LAYOUT_PROPERTY_NAME)) {
getEditorConfigValue(ktlintCustomImportsLayoutProperty, android)
} else {
getEditorConfigValue(ideaImportsLayoutProperty, android)
}

private fun importsAreEqual(actual: List<ASTNode>, expected: List<ASTNode>): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ internal class PatternEntry(
return entry.packageName.count { it == '.' } < packageName.count { it == '.' }
}

override fun toString(): String = packageName
override fun toString(): String = when (this) {
ALL_OTHER_IMPORTS_ENTRY -> WILDCARD_CHAR
ALL_OTHER_ALIAS_IMPORTS_ENTRY -> ALIAS_CHAR
else -> "$packageName.$WILDCARD_CHAR" + if (withSubpackages) WILDCARD_CHAR else ""
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand All @@ -67,6 +71,6 @@ internal class PatternEntry(
companion object {
val BLANK_LINE_ENTRY = PatternEntry(BLANK_LINE_CHAR, withSubpackages = true, hasAlias = false)
val ALL_OTHER_IMPORTS_ENTRY = PatternEntry(WILDCARD_CHAR, withSubpackages = true, hasAlias = false)
val ALL_OTHER_ALIAS_IMPORTS_ENTRY = PatternEntry((ALIAS_CHAR + WILDCARD_CHAR), withSubpackages = true, hasAlias = true)
val ALL_OTHER_ALIAS_IMPORTS_ENTRY = PatternEntry(ALIAS_CHAR, withSubpackages = true, hasAlias = true)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.pinterest.ktlint.ruleset.standard.importordering

import com.pinterest.ktlint.core.api.EditorConfigProperties
import com.pinterest.ktlint.core.api.FeatureInAlphaState
import com.pinterest.ktlint.ruleset.standard.ImportOrderingRule
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

@OptIn(FeatureInAlphaState::class)
class ImportOrderingEditorconfigTest {

@Test
fun `import ordering gets written correctly to editorconfig`() {
val properties: EditorConfigProperties = emptyMap()
val rule = ImportOrderingRule()
with(rule) {
val raw = properties.writeEditorConfigProperty(ImportOrderingRule.ideaImportsLayoutProperty, false)
assertThat(raw).isEqualTo("*,java.**,javax.**,kotlin.**,^")
}
}
}

0 comments on commit 2538730

Please sign in to comment.