Skip to content

Commit

Permalink
Change default code style to ktlint_official (#2144)
Browse files Browse the repository at this point in the history
Closes #2143
  • Loading branch information
paul-dingemans committed Jul 22, 2023
1 parent 2c163c6 commit a31bddc
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 72 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Changed

* Change default code style to `ktlint_official` ([#2143](https://github.com/pinterest/ktlint/pull/2143))
* Update dependency gradle to v8.2.1 ([#2122](https://github.com/pinterest/ktlint/pull/2122))
* Update dependency org.codehaus.janino:janino to v3.1.10 ([#2110](https://github.com/pinterest/ktlint/pull/2110))
* Update dependency com.google.jimfs:jimfs to v1.3.0 ([#2112](https://github.com/pinterest/ktlint/pull/2112))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,16 @@ public enum class CodeStyleValue {
public val CODE_STYLE_PROPERTY_TYPE: PropertyType.LowerCasingPropertyType<CodeStyleValue> =
PropertyType.LowerCasingPropertyType(
"ktlint_code_style",
"The code style ('intellij_idea', 'android_studio' or 'ktlint_official') to be applied. Currently, the " +
"value is defaulted to 'intellij_idea' when not set. However, in the future the default code style " +
"will be changed to 'ktlint_official'.",
"The code style ('ktlint_official', 'intellij_idea' or 'android_studio') to be applied. By default the 'ktlint_official' code " +
"style is used",
SafeEnumValueParser(CodeStyleValue::class.java),
CodeStyleValue.values().map { it.name }.toSet(),
)

public val CODE_STYLE_PROPERTY: EditorConfigProperty<CodeStyleValue> =
EditorConfigProperty(
type = CODE_STYLE_PROPERTY_TYPE,
/*
* Once the [CodeStyleValue.ktlint_official] is matured, it will become the default code style of ktlint. Until
* then the [CodeStyleValue.intellij_idea] is used to remain backwards compatible.
*/
defaultValue = CodeStyleValue.intellij_idea,
defaultValue = CodeStyleValue.ktlint_official,
androidStudioCodeStyleDefaultValue = CodeStyleValue.android_studio,
intellijIdeaCodeStyleDefaultValue = CodeStyleValue.intellij_idea,
ktlintOfficialCodeStyleDefaultValue = CodeStyleValue.ktlint_official,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@ class EditorConfigTest {

val actual = editorConfig[SOME_EDITOR_CONFIG_PROPERTY]

// Note that IntelliJ IDEA is the default code style for now, this will change to KTLINT_OFFICIAL in the future
assertThat(actual).isEqualTo(SOME_PROPERTY_VALUE_INTELLIJ_IDEA)
assertThat(actual).isEqualTo(SOME_PROPERTY_VALUE_KTLINT_OFFICIAL)
}

@ParameterizedTest(name = "Code style: {0}, expected result: {1}")
@CsvSource(
value = [
"android_studio, $SOME_PROPERTY_VALUE_ANDROID_STUDIO",
"intellij_idea, $SOME_PROPERTY_VALUE_INTELLIJ_IDEA",
"ktlint_official, $SOME_PROPERTY_VALUE_OFFICIAL",
"ktlint_official, $SOME_PROPERTY_VALUE_KTLINT_OFFICIAL",
],
)
fun `Given an EditorConfig with a defined code style and add a property with default value then the default value can be retrieved for the default code style`(
Expand Down Expand Up @@ -119,8 +118,8 @@ class EditorConfigTest {
val editorConfig =
EditorConfig()
.addPropertiesWithDefaultValueIfMissing(
SOME_EDITOR_CONFIG_PROPERTY.copy(name = propertyName1, intellijIdeaCodeStyleDefaultValue = propertyValue1),
SOME_EDITOR_CONFIG_PROPERTY.copy(name = propertyName2, intellijIdeaCodeStyleDefaultValue = propertyValue2),
SOME_EDITOR_CONFIG_PROPERTY.copy(name = propertyName1, ktlintOfficialCodeStyleDefaultValue = propertyValue1),
SOME_EDITOR_CONFIG_PROPERTY.copy(name = propertyName2, ktlintOfficialCodeStyleDefaultValue = propertyValue2),
)

val actual = editorConfig.map { property -> property.name.uppercase() to property.sourceValue.uppercase() }
Expand Down Expand Up @@ -198,7 +197,7 @@ class EditorConfigTest {
val editorConfigPropertyWithPropertyMapper =
SOME_EDITOR_CONFIG_PROPERTY
.copy(
propertyMapper = { _, _ -> SOME_PROPERTY_VALUE_OFFICIAL },
propertyMapper = { _, _ -> SOME_PROPERTY_VALUE_KTLINT_OFFICIAL },
defaultValue = SOME_PROPERTY_VALUE,
androidStudioCodeStyleDefaultValue = null,
intellijIdeaCodeStyleDefaultValue = null,
Expand All @@ -208,7 +207,7 @@ class EditorConfigTest {

val actual = editorConfig[editorConfigPropertyWithPropertyMapper]

assertThat(actual).isEqualTo(SOME_PROPERTY_VALUE_OFFICIAL)
assertThat(actual).isEqualTo(SOME_PROPERTY_VALUE_KTLINT_OFFICIAL)
}

@Test
Expand All @@ -220,7 +219,7 @@ class EditorConfigTest {

val actual = editorConfig[SOME_EDITOR_CONFIG_PROPERTY]

assertThat(actual).isEqualTo(SOME_PROPERTY_VALUE_INTELLIJ_IDEA)
assertThat(actual).isEqualTo(SOME_PROPERTY_VALUE_KTLINT_OFFICIAL)
}

@Test
Expand Down Expand Up @@ -324,7 +323,7 @@ class EditorConfigTest {
const val SOME_PROPERTY_VALUE_ANDROID_STUDIO = "some-property-value-android"
const val SOME_PROPERTY_VALUE_DEFAULT = "some-property-value-default"
const val SOME_PROPERTY_VALUE_INTELLIJ_IDEA = "some-property-value-intellij-idea"
const val SOME_PROPERTY_VALUE_OFFICIAL = "some-property-value-official"
const val SOME_PROPERTY_VALUE_KTLINT_OFFICIAL = "some-property-value-ktlint-official"
val SOME_EDITOR_CONFIG_PROPERTY =
EditorConfigProperty(
name = SOME_PROPERTY_NAME,
Expand All @@ -337,7 +336,7 @@ class EditorConfigTest {
),
defaultValue = SOME_PROPERTY_VALUE_DEFAULT,
androidStudioCodeStyleDefaultValue = SOME_PROPERTY_VALUE_ANDROID_STUDIO,
ktlintOfficialCodeStyleDefaultValue = SOME_PROPERTY_VALUE_OFFICIAL,
ktlintOfficialCodeStyleDefaultValue = SOME_PROPERTY_VALUE_KTLINT_OFFICIAL,
intellijIdeaCodeStyleDefaultValue = SOME_PROPERTY_VALUE_INTELLIJ_IDEA,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ internal class EditorConfigLoaderTest {
"ij_formatter_tags_enabled = false",
"ij_formatter_off_tag = @formatter:off",
"ij_formatter_on_tag = @formatter:on",
"ktlint_code_style = intellij_idea",
"ktlint_code_style = ktlint_official",
"ktlint_experimental = disabled",
)
}
Expand Down Expand Up @@ -306,7 +306,7 @@ internal class EditorConfigLoaderTest {
"ij_formatter_tags_enabled = false",
"ij_formatter_off_tag = @formatter:off",
"ij_formatter_on_tag = @formatter:on",
"ktlint_code_style = intellij_idea",
"ktlint_code_style = ktlint_official",
"ktlint_experimental = disabled",
)
}
Expand Down Expand Up @@ -357,7 +357,7 @@ internal class EditorConfigLoaderTest {
"ij_formatter_tags_enabled = false",
"ij_formatter_off_tag = @formatter:off",
"ij_formatter_on_tag = @formatter:on",
"ktlint_code_style = intellij_idea",
"ktlint_code_style = ktlint_official",
"ktlint_experimental = disabled",
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public class ClassSignatureRule :
require(node.elementType == CLASS)

val wrapPrimaryConstructorParameters =
node.hasMinimumNumberOfParameters() ||
node.hasTooManyParameters() ||
node.containsMultilineParameter() ||
(codeStyle == ktlint_official && node.containsAnnotatedParameter()) ||
(isMaxLineLengthSet() && classSignatureExcludingSuperTypesExceedsMaxLineLength(node, emit, autoCorrect)) ||
Expand Down Expand Up @@ -648,7 +648,7 @@ public class ClassSignatureRule :

private fun List<ASTNode>.joinTextToString(): String = collectLeavesRecursively().joinToString(separator = "") { it.text }

private fun ASTNode.hasMinimumNumberOfParameters(): Boolean = countParameters() >= classSignatureWrappingMinimumParameters
private fun ASTNode.hasTooManyParameters(): Boolean = countParameters() >= classSignatureWrappingMinimumParameters

private fun ASTNode.countParameters() =
getPrimaryConstructorParameterListOrNull()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.editorconfig.CODE_STYLE_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.CodeStyleValue
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.CodeStyleValue.ktlint_official
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
import com.pinterest.ktlint.test.LintViolation
Expand Down Expand Up @@ -806,12 +807,14 @@ class AnnotationRuleTest {
@Nested
inner class `Given a class with a primary constructor` {
@Test
fun `Issue 628 - Given an annotation followed by other modifier before the primary constructor (non ktlint_official code style)`() {
fun `Issue 628 - Given a non-ktlint_official code style and an annotation followed by other modifier before the primary constructor (non ktlint_official code style)`() {
val code =
"""
class Foo @Inject internal constructor()
""".trimIndent()
annotationRuleAssertThat(code).hasNoLintViolations()
annotationRuleAssertThat(code)
.withEditorConfigOverride(CODE_STYLE_PROPERTY to CodeStyleValue.intellij_idea)
.hasNoLintViolations()
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ClassSignatureRuleTest {
""".trimIndent()
classSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.hasNoLintViolations()
}

Expand Down Expand Up @@ -81,6 +82,7 @@ class ClassSignatureRuleTest {
classSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.addAdditionalRuleProvider { FunctionStartOfBodySpacingRule() }
.withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.hasLintViolation(2, 34, "Expected a single space before class body")
.isFormattedAs(formattedCode)
}
Expand Down Expand Up @@ -357,6 +359,7 @@ class ClassSignatureRuleTest {
""".trimIndent()
classSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.hasLintViolations(
LintViolation(3, 5, "No whitespace expected between opening parenthesis and first parameter name"),
LintViolation(4, 5, "Single whitespace expected before parameter"),
Expand Down Expand Up @@ -485,6 +488,7 @@ class ClassSignatureRuleTest {
""".trimIndent()
classSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.hasNoLintViolations()
}

Expand Down Expand Up @@ -515,6 +519,7 @@ class ClassSignatureRuleTest {
""".trimIndent()
classSignatureWrappingRuleAssertThat(code)
.addAdditionalRuleProvider { NullableTypeSpacingRule() }
.withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.hasNoLintViolationsExceptInAdditionalRules()
.isFormattedAs(formattedCode)
}
Expand Down Expand Up @@ -583,7 +588,8 @@ class ClassSignatureRuleTest {
{ SpacingAroundParensRule() },
{ SpacingAroundCommaRule() },
{ SpacingAroundColonRule() },
).hasLintViolations(
).withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.hasLintViolations(
LintViolation(3, 14, "No whitespace expected between opening parenthesis and first parameter name"),
LintViolation(7, 21, "Single whitespace expected before parameter"),
LintViolation(8, 26, "No whitespace expected between last parameter and closing parenthesis"),
Expand Down Expand Up @@ -625,7 +631,8 @@ class ClassSignatureRuleTest {
{ SpacingAroundColonRule() },
{ SpacingAroundCommaRule() },
{ SpacingAroundOperatorsRule() },
).hasLintViolations(
).withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.hasLintViolations(
LintViolation(2, 19, "Single whitespace expected before parameter"),
LintViolation(4, 13, "Expected single space before the super type"),
).isFormattedAs(formattedCode)
Expand Down Expand Up @@ -725,6 +732,7 @@ class ClassSignatureRuleTest {
""".trimIndent()
classSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.hasNoLintViolations()
}

Expand All @@ -743,6 +751,7 @@ class ClassSignatureRuleTest {
""".trimIndent()
classSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.hasLintViolation(2, 37, "Super type should start on a newline")
.isFormattedAs(formattedCode)
}
Expand Down Expand Up @@ -1324,6 +1333,7 @@ class ClassSignatureRuleTest {
""".trimIndent()
classSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.hasLintViolations(
LintViolation(3, 5, "No whitespace expected between opening parenthesis and first parameter name"),
LintViolation(4, 5, "Single whitespace expected before parameter"),
Expand Down Expand Up @@ -1355,6 +1365,7 @@ class ClassSignatureRuleTest {
""".trimIndent()
classSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.hasLintViolations(
LintViolation(2, 37, "Super type should start on a newline"),
LintViolation(2, 49, "Super type should start on a newline"),
Expand Down Expand Up @@ -1383,6 +1394,7 @@ class ClassSignatureRuleTest {
""".trimIndent()
classSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.hasLintViolations(
LintViolation(2, 37, "Super type should start on a newline"),
LintViolation(2, 43, "Super type call must be first super type"),
Expand Down Expand Up @@ -1412,6 +1424,7 @@ class ClassSignatureRuleTest {
""".trimIndent()
classSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.hasLintViolations(
LintViolation(2, 37, "Super type should start on a newline"),
LintViolation(2, 43, "Super type should start on a newline"),
Expand Down Expand Up @@ -1440,6 +1453,7 @@ class ClassSignatureRuleTest {
""".trimIndent()
classSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.hasLintViolation(2, 23, "Super type should start on a newline")
.isFormattedAs(formattedCode)
}
Expand Down Expand Up @@ -1470,6 +1484,7 @@ class ClassSignatureRuleTest {
classSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.addAdditionalRuleProvider { IndentationRule() }
.withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset")
.hasLintViolation(2, 37, "Super type should start on a newline")
.isFormattedAs(formattedCode)
}
Expand All @@ -1494,9 +1509,9 @@ class ClassSignatureRuleTest {
b: Any,
c: Any
) : Bar(
a,
c
) {
a,
c
) {
// body
}
""".trimIndent()
Expand Down
Loading

0 comments on commit a31bddc

Please sign in to comment.