diff --git a/documentation/snapshot/docs/rules/standard.md b/documentation/snapshot/docs/rules/standard.md index 1fb8eca029..33879cd566 100644 --- a/documentation/snapshot/docs/rules/standard.md +++ b/documentation/snapshot/docs/rules/standard.md @@ -103,6 +103,10 @@ Wraps binary expression at the operator reference whenever the binary expression } ``` +| Configuration setting | ktlint_official | intellij_idea | android_studio | +|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| +| `max_line_length`
Maximum length of a (regular) line. This property is ignored in case the `max-line-length` rule is disabled, or when using Ktlint via a third party integration that does not provide this rule. | 140 | `off` | `100` | + Rule id: `standard:binary-expression-wrapping` Suppress or disable rule (1) @@ -306,6 +310,7 @@ The `.` in `java.class` is ignored when wrapping on chain operators. | Configuration setting | ktlint_official | intellij_idea | android_studio | |:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| | `ktlint_chain_method_rule_force_multiline_when_chain_operator_count_greater_or_equal_than`
Force wrapping of chained methods in case an expression contains at least the specified number of chain operators. If a chained method contains nested expressions, the chain operators of the inner expression are not taken into account. Use value `unset` (default) to disable this setting. | 4 | 4 | 4 | +| `max_line_length`
Maximum length of a (regular) line. This property is ignored in case the `max-line-length` rule is disabled, or when using Ktlint via a third party integration that does not provide this rule. | 140 | `off` | `100` | Rule id: `standard:chain-method-continuation` @@ -559,6 +564,7 @@ The other code styles allow an infinite amount of parameters on the same line (a | Configuration setting | ktlint_official | intellij_idea | android_studio | |:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| | `ktlint_class_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than`
Force wrapping of the parameters of the class signature in case it contains at least the specified number of parameters, even in case the entire class signature would fit on a single line. Use value `unset` to disable this setting. | 1 | `unset` | `unset` | +| `max_line_length`
Maximum length of a (regular) line. This property is ignored in case the `max-line-length` rule is disabled, or when using Ktlint via a third party integration that does not provide this rule. | 140 | `off` | `100` | Rule id: `standard:class-signature` @@ -808,6 +814,10 @@ If the function literal contains multiple parameters and at least one parameter } ``` +| Configuration setting | ktlint_official | intellij_idea | android_studio | +|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| +| `max_line_length`
Maximum length of a (regular) line. This property is ignored in case the `max-line-length` rule is disabled, or when using Ktlint via a third party integration that does not provide this rule. | 140 | `off` | `100` | + Rule id: `standard:function-literal` Suppress or disable rule (1) @@ -978,6 +988,10 @@ Rewrites the function signature to a single line when possible (e.g. when not ex .uppercase() ``` +| Configuration setting | ktlint_official | intellij_idea | android_studio | +|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| +| `max_line_length`
Maximum length of a (regular) line. This property is ignored in case the `max-line-length` rule is disabled, or when using Ktlint via a third party integration that does not provide this rule. | 140 | `off` | `100` | + Rule id: `standard:function-signature` Suppress or disable rule (1) @@ -1854,7 +1868,9 @@ Rule id: `standard:ktlint-suppression` ## Max line length -Ensures that lines do not exceed the maximum length of a line. This rule does not apply in a number of situations. The `.editorconfig` property `ktlint_ignore_back_ticked_identifier` can be set to ignore identifiers which are enclosed in backticks, which for example is very useful when you want to allow longer names for unit tests. +Ensures that lines do not exceed the maximum length of a line as specified in `.editorconfig` property `max_line_length`. + +This rule does not apply in a number of situations. The `.editorconfig` property `ktlint_ignore_back_ticked_identifier` can be set to ignore identifiers which are enclosed in backticks, which for example is very useful when you want to allow longer names for unit tests. === "[:material-heart:](#) Ktlint" @@ -1889,10 +1905,10 @@ Ensures that lines do not exceed the maximum length of a line. This rule does no ``` -| Configuration setting | ktlint_official | intellij_idea | android_studio | -|:--------------------------------------------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| -| `ktlint_ignore_back_ticked_identifier`
Defines whether the backticked identifier (``) should be ignored. | `false` | `false` | `false` | -| `max_line_length`
Maximum length of a (regular) line. | 140 | `off` | `100` | +| Configuration setting | ktlint_official | intellij_idea | android_studio | +|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| +| `ktlint_ignore_back_ticked_identifier`
Defines whether the backticked identifier (``) should be ignored. | `false` | `false` | `false` | +| `max_line_length`
Maximum length of a (regular) line. This property is ignored in case the `max-line-length` rule is disabled, or when using Ktlint via a third party integration that does not provide this rule. | 140 | `off` | `100` | Rule id: `standard:max-line-length` @@ -2968,6 +2984,10 @@ Consistent spacing around the function return type. String = "some-result" ``` +| Configuration setting | ktlint_official | intellij_idea | android_studio | +|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| +| `max_line_length`
Maximum length of a (regular) line. This property is ignored in case the `max-line-length` rule is disabled, or when using Ktlint via a third party integration that does not provide this rule. | 140 | `off` | `100` | + Rule id: `standard:function-return-type-spacing` Suppress or disable rule (1) @@ -3346,6 +3366,10 @@ Consistent spacing inside the parameter list. ) = "some-result" ``` +| Configuration setting | ktlint_official | intellij_idea | android_studio | +|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| +| `max_line_length`
Maximum length of a (regular) line. This property is ignored in case the `max-line-length` rule is disabled, or when using Ktlint via a third party integration that does not provide this rule. | 140 | `off` | `100` | + Rule id: `standard:parameter-list-spacing` Suppress or disable rule (1) @@ -4124,9 +4148,10 @@ All arguments should be on the same line, or every argument should be on a separ ) ``` -| Configuration setting | ktlint_official | intellij_idea | android_studio | -|:----------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| -| `ktlint_argument_list_wrapping_ignore_when_parameter_count_greater_or_equal_than` | `unset` | 8 | 8 | +| Configuration setting | ktlint_official | intellij_idea | android_studio | +|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| +| `ktlint_argument_list_wrapping_ignore_when_parameter_count_greater_or_equal_than` | `unset` | 8 | 8 | +| `max_line_length`
Maximum length of a (regular) line. This property is ignored in case the `max-line-length` rule is disabled, or when using Ktlint via a third party integration that does not provide this rule. | 140 | `off` | `100` | Rule id: `standard:argument-list-wrapping` @@ -4334,6 +4359,10 @@ Wraps the content receiver list to a separate line regardless of maximum line le fun fooBar() ``` +| Configuration setting | ktlint_official | intellij_idea | android_studio | +|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| +| `max_line_length`
Maximum length of a (regular) line. This property is ignored in case the `max-line-length` rule is disabled, or when using Ktlint via a third party integration that does not provide this rule. | 140 | `off` | `100` | + Rule id: `standard:context-receiver-wrapping` Suppress or disable rule (1) @@ -4562,6 +4591,10 @@ When class/function signature doesn't fit on a single line, each parameter must ) ``` +| Configuration setting | ktlint_official | intellij_idea | android_studio | +|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| +| `max_line_length`
Maximum length of a (regular) line. This property is ignored in case the `max-line-length` rule is disabled, or when using Ktlint via a third party integration that does not provide this rule. | 140 | `off` | `100` | + Rule id: `standard:parameter-list-wrapping` Suppress or disable rule (1) @@ -4628,6 +4661,10 @@ When a function or class parameter doesn't fit on a single line, wrap the type o ) ``` +| Configuration setting | ktlint_official | intellij_idea | android_studio | +|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| +| `max_line_length`
Maximum length of a (regular) line. This property is ignored in case the `max-line-length` rule is disabled, or when using Ktlint via a third party integration that does not provide this rule. | 140 | `off` | `100` | + Rule id: `standard:parameter-wrapping` Suppress or disable rule (1) @@ -4666,6 +4703,10 @@ When a property doesn't fit on a single line, wrap the type or value to a separa val aVariableWithALooooooooooooongName: String ``` +| Configuration setting | ktlint_official | intellij_idea | android_studio | +|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| +| `max_line_length`
Maximum length of a (regular) line. This property is ignored in case the `max-line-length` rule is disabled, or when using Ktlint via a third party integration that does not provide this rule. | 140 | `off` | `100` | + Rule id: `standard:property-wrapping` Suppress or disable rule (1) @@ -4764,6 +4805,10 @@ Inserts missing newlines (for example between parentheses of a multi-line functi c) ``` +| Configuration setting | ktlint_official | intellij_idea | android_studio | +|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:---------------:|:-------------:|:--------------:| +| `max_line_length`
Maximum length of a (regular) line. This property is ignored in case the `max-line-length` rule is disabled, or when using Ktlint via a third party integration that does not provide this rule. | 140 | `off` | `100` | + Rule id: `standard:wrapping` Suppress or disable rule (1) diff --git a/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/internal/RuleExecutionContext.kt b/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/internal/RuleExecutionContext.kt index 0a04ea5a0d..e4204671e8 100644 --- a/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/internal/RuleExecutionContext.kt +++ b/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/internal/RuleExecutionContext.kt @@ -9,9 +9,12 @@ import com.pinterest.ktlint.rule.engine.api.KtLintRuleException import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision import com.pinterest.ktlint.rule.engine.core.api.Rule import com.pinterest.ktlint.rule.engine.core.api.RuleAutocorrectApproveHandler +import com.pinterest.ktlint.rule.engine.core.api.RuleId import com.pinterest.ktlint.rule.engine.core.api.RuleProvider import com.pinterest.ktlint.rule.engine.core.api.editorconfig.CODE_STYLE_PROPERTY import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfig +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.RuleExecution +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.createRuleExecutionEditorConfigProperty import com.pinterest.ktlint.rule.engine.internal.rulefilter.InternalRuleProvidersFilter import com.pinterest.ktlint.rule.engine.internal.rulefilter.RuleExecutionRuleFilter import com.pinterest.ktlint.rule.engine.internal.rulefilter.RunAfterRuleFilter @@ -46,12 +49,28 @@ internal class RuleExecutionContext private constructor( try { rule.startTraversalOfAST() rule.beforeFirstNode( - // The rule get access to an EditConfig which is filtered by the properties which are actually registered as being used by + // The rule gets access to an EditConfig which is filtered by the properties which are actually registered as being used by // the rule. In this way it can be forced that the rule actually registers the properties that it uses and the field becomes // reliable to be used by for example the ".editorconfig" file generator. - // Note that also the CODE_STYLE_PROPERTY is provided because that property is needed to determine the default value of an - // EditorConfigProperty that is not explicitly defined. - editorConfig.filterBy(rule.usesEditorConfigProperties.plus(CODE_STYLE_PROPERTY)), + editorConfig.filterBy( + rule.usesEditorConfigProperties + // Provide the CODE_STYLE_PROPERTY as this property is needed to determine the default value of an + // EditorConfigProperty that is not explicitly defined. + .plus(CODE_STYLE_PROPERTY) + // Provide the rule execution property for the "standard:max-line-length" property based on whether a rule provider + // for this rule exists. This property is required to determine whether the property `max_line_length` needs to be + // taken into account. + .plus( + RuleId("standard:max-line-length") + .createRuleExecutionEditorConfigProperty( + if (ruleProviders.any { it.ruleId.value == "standard:max-line-length" }) { + RuleExecution.enabled + } else { + RuleExecution.disabled + }, + ), + ), + ), ) this.executeRuleOnNodeRecursively(rootNode, rule, autocorrectHandler, emitAndApprove) rule.afterLastNode() diff --git a/ktlint-ruleset-standard/api/ktlint-ruleset-standard.api b/ktlint-ruleset-standard/api/ktlint-ruleset-standard.api index 1a13908a2b..d23b5980fe 100644 --- a/ktlint-ruleset-standard/api/ktlint-ruleset-standard.api +++ b/ktlint-ruleset-standard/api/ktlint-ruleset-standard.api @@ -418,6 +418,7 @@ public final class com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRule public final class com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRuleKt { public static final fun getMAX_LINE_LENGTH_RULE_ID ()Lcom/pinterest/ktlint/rule/engine/core/api/RuleId; + public static final fun maxLineLength (Lcom/pinterest/ktlint/rule/engine/core/api/editorconfig/EditorConfig;)I } public final class com/pinterest/ktlint/ruleset/standard/rules/MixedConditionOperatorsRule : com/pinterest/ktlint/ruleset/standard/StandardRule, com/pinterest/ktlint/rule/engine/core/api/Rule$Experimental { diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ArgumentListWrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ArgumentListWrappingRule.kt index ad47b8c1a0..fa4d840e6f 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ArgumentListWrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ArgumentListWrappingRule.kt @@ -92,7 +92,7 @@ public class ArgumentListWrappingRule : indentStyle = editorConfig[INDENT_STYLE_PROPERTY], tabWidth = editorConfig[INDENT_SIZE_PROPERTY], ) - maxLineLength = editorConfig[MAX_LINE_LENGTH_PROPERTY] + maxLineLength = editorConfig.maxLineLength() ignoreWhenParameterCountGreaterOrEqualThanProperty = editorConfig[IGNORE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY] } diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BinaryExpressionWrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BinaryExpressionWrappingRule.kt index 0f9895b9c1..7d77c4004b 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BinaryExpressionWrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BinaryExpressionWrappingRule.kt @@ -70,7 +70,7 @@ public class BinaryExpressionWrappingRule : indentStyle = editorConfig[INDENT_STYLE_PROPERTY], tabWidth = editorConfig[INDENT_SIZE_PROPERTY], ) - maxLineLength = editorConfig[MAX_LINE_LENGTH_PROPERTY] + maxLineLength = editorConfig.maxLineLength() } override fun beforeVisitChildNodes( diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRule.kt index 9824bdcf34..299ce796b9 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRule.kt @@ -99,7 +99,7 @@ public class ChainMethodContinuationRule : if (indentConfig.disabled) { stopTraversalOfAST() } - maxLineLength = editorConfig[MAX_LINE_LENGTH_PROPERTY] + maxLineLength = editorConfig.maxLineLength() forceMultilineWhenChainOperatorCountGreaterOrEqualThanProperty = editorConfig[FORCE_MULTILINE_WHEN_CHAIN_OPERATOR_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY] } diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRule.kt index 52828390cd..1635d5c1b9 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRule.kt @@ -100,7 +100,7 @@ public class ClassSignatureRule : indentStyle = editorConfig[INDENT_STYLE_PROPERTY], tabWidth = editorConfig[INDENT_SIZE_PROPERTY], ) - maxLineLength = editorConfig[MAX_LINE_LENGTH_PROPERTY] + maxLineLength = editorConfig.maxLineLength() } override fun beforeVisitChildNodes( @@ -467,7 +467,7 @@ public class ClassSignatureRule : emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> AutocorrectDecision, wrappedPrimaryConstructor: Boolean, ): Int { - var whiteSpaceCorrection = 0 + val whiteSpaceCorrection = 0 val superTypes = node.superTypes() ?: return 0 diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ContextReceiverWrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ContextReceiverWrappingRule.kt index 75d81c1425..1b14e576dd 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ContextReceiverWrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ContextReceiverWrappingRule.kt @@ -58,7 +58,7 @@ public class ContextReceiverWrappingRule : indentStyle = editorConfig[INDENT_STYLE_PROPERTY], tabWidth = editorConfig[INDENT_SIZE_PROPERTY], ) - maxLineLength = editorConfig[MAX_LINE_LENGTH_PROPERTY] + maxLineLength = editorConfig.maxLineLength() } override fun beforeVisitChildNodes( diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionLiteralRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionLiteralRule.kt index 165da8546d..25314e50c1 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionLiteralRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionLiteralRule.kt @@ -96,7 +96,7 @@ public class FunctionLiteralRule : override fun beforeFirstNode(editorConfig: EditorConfig) { codeStyle = editorConfig[CODE_STYLE_PROPERTY] - maxLineLength = editorConfig[MAX_LINE_LENGTH_PROPERTY] + maxLineLength = editorConfig.maxLineLength() indentConfig = IndentConfig( indentStyle = editorConfig[INDENT_STYLE_PROPERTY], diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionReturnTypeSpacingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionReturnTypeSpacingRule.kt index 9ce9a83313..961865ccdb 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionReturnTypeSpacingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionReturnTypeSpacingRule.kt @@ -29,7 +29,7 @@ public class FunctionReturnTypeSpacingRule : private var maxLineLength = MAX_LINE_LENGTH_PROPERTY.defaultValue override fun beforeFirstNode(editorConfig: EditorConfig) { - maxLineLength = editorConfig[MAX_LINE_LENGTH_PROPERTY] + maxLineLength = editorConfig.maxLineLength() } override fun beforeVisitChildNodes( diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRule.kt index be2158d987..a86c87f0fc 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRule.kt @@ -104,7 +104,7 @@ public class FunctionSignatureRule : indentStyle = editorConfig[INDENT_STYLE_PROPERTY], tabWidth = editorConfig[INDENT_SIZE_PROPERTY], ) - maxLineLength = editorConfig[MAX_LINE_LENGTH_PROPERTY] + maxLineLength = editorConfig.maxLineLength() } override fun beforeVisitChildNodes( diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRule.kt index c3c3ad5595..87b2e8cebf 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRule.kt @@ -12,6 +12,9 @@ import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfig import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfigProperty import com.pinterest.ktlint.rule.engine.core.api.editorconfig.MAX_LINE_LENGTH_PROPERTY import com.pinterest.ktlint.rule.engine.core.api.editorconfig.MAX_LINE_LENGTH_PROPERTY_OFF +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.RULE_EXECUTION_PROPERTY_TYPE +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.RuleExecution +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.ktLintRuleExecutionPropertyName import com.pinterest.ktlint.rule.engine.core.api.isPartOf import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithNewline @@ -62,7 +65,7 @@ public class MaxLineLengthRule : override fun beforeFirstNode(editorConfig: EditorConfig) { ignoreBackTickedIdentifier = editorConfig[IGNORE_BACKTICKED_IDENTIFIER_PROPERTY] - maxLineLength = editorConfig[MAX_LINE_LENGTH_PROPERTY] + maxLineLength = editorConfig.maxLineLength() if (maxLineLength == MAX_LINE_LENGTH_PROPERTY_OFF) { stopTraversalOfAST() } @@ -146,4 +149,26 @@ public class MaxLineLengthRule : } } +/** + * Gets the max_line_length property if the `max-line-length` rule is enabled. Otherwise, returns [Int.MAX_VALUE]. + * + * Normally, rules should not have direct dependencies on other rules. This rule is an exception to that. In case the `max-line-length` + * property in the `.editorconfig` is set, or inferred via a default value based on the `ktlint_code_style`, but the `max-line-length` rule + * is disabled, then those other rules might start wrapping lines. Conceptually, the `max-line-length` rule determines whether ktlint should + * or should not use the `max_line_length` property. + */ +public fun EditorConfig.maxLineLength(): Int = + if (maxLineLengthRuleEnabled()) { + this[MAX_LINE_LENGTH_PROPERTY] + } else { + Int.MAX_VALUE + } + +private fun EditorConfig.maxLineLengthRuleEnabled(): Boolean = + RuleExecution.enabled == + getEditorConfigValueOrNull( + RULE_EXECUTION_PROPERTY_TYPE, + MAX_LINE_LENGTH_RULE_ID.ktLintRuleExecutionPropertyName(), + ) + public val MAX_LINE_LENGTH_RULE_ID: RuleId = MaxLineLengthRule().ruleId diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRule.kt index 2d631c4a4a..97f3f6b0b2 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRule.kt @@ -47,7 +47,7 @@ public class ParameterListSpacingRule : private var maxLineLength = MAX_LINE_LENGTH_PROPERTY.defaultValue override fun beforeFirstNode(editorConfig: EditorConfig) { - maxLineLength = editorConfig[MAX_LINE_LENGTH_PROPERTY] + maxLineLength = editorConfig.maxLineLength() } override fun beforeVisitChildNodes( diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListWrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListWrappingRule.kt index 00cb9efd1c..551d0c76c1 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListWrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListWrappingRule.kt @@ -60,7 +60,7 @@ public class ParameterListWrappingRule : override fun beforeFirstNode(editorConfig: EditorConfig) { codeStyle = editorConfig[CODE_STYLE_PROPERTY] - maxLineLength = editorConfig[MAX_LINE_LENGTH_PROPERTY] + maxLineLength = editorConfig.maxLineLength() indentConfig = IndentConfig( indentStyle = editorConfig[INDENT_STYLE_PROPERTY], diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterWrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterWrappingRule.kt index 2975241c27..d3559ce462 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterWrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterWrappingRule.kt @@ -65,7 +65,7 @@ public class ParameterWrappingRule : indentStyle = editorConfig[INDENT_STYLE_PROPERTY], tabWidth = editorConfig[INDENT_SIZE_PROPERTY], ) - maxLineLength = editorConfig[MAX_LINE_LENGTH_PROPERTY] + maxLineLength = editorConfig.maxLineLength() } override fun beforeVisitChildNodes( diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyWrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyWrappingRule.kt index b31d69d359..b99d76101f 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyWrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyWrappingRule.kt @@ -63,7 +63,7 @@ public class PropertyWrappingRule : indentStyle = editorConfig[INDENT_STYLE_PROPERTY], tabWidth = editorConfig[INDENT_SIZE_PROPERTY], ) - maxLineLength = editorConfig[MAX_LINE_LENGTH_PROPERTY] + maxLineLength = editorConfig.maxLineLength() } override fun beforeVisitChildNodes( diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt index 3d4e5e0fb3..c4551d4b35 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRule.kt @@ -126,7 +126,7 @@ public class WrappingRule : indentStyle = editorConfig[INDENT_STYLE_PROPERTY], tabWidth = editorConfig[INDENT_SIZE_PROPERTY], ) - maxLineLength = editorConfig[MAX_LINE_LENGTH_PROPERTY] + maxLineLength = editorConfig.maxLineLength() } override fun beforeVisitChildNodes( diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ArgumentListWrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ArgumentListWrappingRuleTest.kt index 1177ab1f02..468dce9d33 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ArgumentListWrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ArgumentListWrappingRuleTest.kt @@ -20,6 +20,7 @@ import com.pinterest.ktlint.ruleset.standard.rules.ClassSignatureRule.Companion. class ArgumentListWrappingRuleTest { private val argumentListWrappingRuleAssertThat = assertThatRuleBuilder { ArgumentListWrappingRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } .addRequiredRuleProviderDependenciesFrom(StandardRuleSetProvider()) .assertThat() @@ -311,7 +312,7 @@ class ArgumentListWrappingRuleTest { argumentListWrappingRuleAssertThat(code) .setMaxLineLength() .withEditorConfigOverride(IGNORE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to 8) - .hasNoLintViolations() + .hasNoLintViolationsExceptInAdditionalRules() } @Test @@ -942,8 +943,10 @@ class ArgumentListWrappingRuleTest { .addAdditionalRuleProvider { ClassSignatureRule() } .addRequiredRuleProviderDependenciesFrom(StandardRuleSetProvider()) .withEditorConfigOverride(CLASS_SIGNATURE_FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to 4) - .hasLintViolationForAdditionalRule(5, 37, "Super type should start on a newline") - .hasLintViolations( + .hasLintViolationsForAdditionalRule( + LintViolation(5, 37, "Super type should start on a newline"), + LintViolation(5, 56, "Exceeded max line length (55)", canBeAutoCorrected = false), + ).hasLintViolations( LintViolation(5, 44, "Argument should be on a separate line (unless all arguments can fit a single line)"), LintViolation(5, 47, "Argument should be on a separate line (unless all arguments can fit a single line)"), LintViolation(5, 88, "Missing newline before \")\""), @@ -977,6 +980,7 @@ class ArgumentListWrappingRuleTest { .hasLintViolationsForAdditionalRule( LintViolation(3, 12, "Newline expected after '{'"), LintViolation(3, 12, "Missing newline after \"{\""), + LintViolation(3, 45, "Exceeded max line length (44)", canBeAutoCorrected = false), LintViolation(3, 50, "Newline expected before '}'"), // Lint violation below only occurs during linting. Resolving violations above, prevents the next violation from occurring LintViolation(3, 59, "Line is exceeding max line length. Break line after 'returns' in binary expression"), diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BinaryExpressionWrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BinaryExpressionWrappingRuleTest.kt index b86f656af4..e7615127d7 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BinaryExpressionWrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/BinaryExpressionWrappingRuleTest.kt @@ -3,13 +3,16 @@ package com.pinterest.ktlint.ruleset.standard.rules import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider import com.pinterest.ktlint.test.KtLintAssertThat.Companion.EOL_CHAR import com.pinterest.ktlint.test.KtLintAssertThat.Companion.MAX_LINE_LENGTH_MARKER -import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule +import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRuleBuilder import com.pinterest.ktlint.test.LintViolation import com.pinterest.ktlint.test.MULTILINE_STRING_QUOTE import org.junit.jupiter.api.Test class BinaryExpressionWrappingRuleTest { - private val binaryExpressionWrappingRuleAssertThat = assertThatRule { BinaryExpressionWrappingRule() } + private val binaryExpressionWrappingRuleAssertThat = + assertThatRuleBuilder { BinaryExpressionWrappingRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } + .assertThat() @Test fun `Given a property with a binary expression on same line as equals, and it exceeds the max line length then wrap before the expression`() { @@ -192,7 +195,7 @@ class BinaryExpressionWrappingRuleTest { """.trimIndent() binaryExpressionWrappingRuleAssertThat(code) .setMaxLineLength() - .hasNoLintViolations() + .hasNoLintViolationsExceptInAdditionalRules() } @Test @@ -293,16 +296,20 @@ class BinaryExpressionWrappingRuleTest { ).hasLintViolationsForAdditionalRules( LintViolation(2, 19, "Argument should be on a separate line (unless all arguments can fit a single line)"), LintViolation(2, 32, "Missing newline before \")\""), + LintViolation(2, 32, "Exceeded max line length (31)", canBeAutoCorrected = false), LintViolation(3, 19, "Argument should be on a separate line (unless all arguments can fit a single line)"), LintViolation(3, 23, "Argument should be on a separate line (unless all arguments can fit a single line)"), LintViolation(3, 32, "Missing newline before \")\""), + LintViolation(3, 32, "Exceeded max line length (31)", canBeAutoCorrected = false), LintViolation(3, 33, "Missing newline before \")\""), LintViolation(4, 19, "Argument should be on a separate line (unless all arguments can fit a single line)"), LintViolation(4, 23, "Argument should be on a separate line (unless all arguments can fit a single line)"), + LintViolation(4, 32, "Exceeded max line length (31)", canBeAutoCorrected = false), LintViolation(4, 46, "Missing newline before \")\""), LintViolation(4, 47, "Missing newline before \")\""), LintViolation(5, 19, "Argument should be on a separate line (unless all arguments can fit a single line)"), LintViolation(5, 23, "Argument should be on a separate line (unless all arguments can fit a single line)"), + LintViolation(5, 32, "Exceeded max line length (31)", canBeAutoCorrected = false), LintViolation(5, 47, "Missing newline before \")\""), LintViolation(5, 48, "Missing newline before \")\""), ).isFormattedAs(formattedCode) @@ -433,6 +440,7 @@ class BinaryExpressionWrappingRuleTest { .hasLintViolationsForAdditionalRule( LintViolation(3, 12, "Missing newline after \"{\""), LintViolation(3, 21, "Argument should be on a separate line (unless all arguments can fit a single line)"), + LintViolation(3, 45, "Exceeded max line length (44)", canBeAutoCorrected = false), LintViolation(3, 48, "Missing newline before \")\""), ).hasLintViolations( LintViolation(3, 12, "Newline expected after '{'"), @@ -455,7 +463,7 @@ class BinaryExpressionWrappingRuleTest { """.trimIndent() binaryExpressionWrappingRuleAssertThat(code) .setMaxLineLength() - .hasNoLintViolations() + .hasNoLintViolationsExceptInAdditionalRules() } @Test @@ -489,6 +497,6 @@ class BinaryExpressionWrappingRuleTest { """.trimIndent() binaryExpressionWrappingRuleAssertThat(code) .setMaxLineLength() - .hasNoLintViolations() + .hasNoLintViolationsExceptInAdditionalRules() } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRuleTest.kt index 9907a90320..fb074ea293 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRuleTest.kt @@ -16,6 +16,7 @@ import org.junit.jupiter.params.provider.ValueSource class ChainMethodContinuationRuleTest { private val chainMethodContinuationRuleAssertThat = assertThatRuleBuilder { ChainMethodContinuationRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } .addRequiredRuleProviderDependenciesFrom(StandardRuleSetProvider()) .assertThat() @@ -613,6 +614,7 @@ class ChainMethodContinuationRuleTest { .setMaxLineLength() .addAdditionalRuleProvider { ArgumentListWrappingRule() } .addAdditionalRuleProvider { FunctionLiteralRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } .hasLintViolations( // Lint violation below will not be triggered during format as argument-list-wrapping rule prevents this error from occurring LintViolation(2, 30, "Expected newline before '.'"), @@ -650,6 +652,7 @@ class ChainMethodContinuationRuleTest { .addAdditionalRuleProvider { FunctionLiteralRule() } .addAdditionalRuleProvider { MultilineExpressionWrappingRule() } .addAdditionalRuleProvider { IndentationRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } .isFormattedAs(formattedCode) .hasLintViolations( // Lint violation below will not be triggered during format as argument-list-wrapping rule prevents this error from occurring diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRuleTest.kt index e1c422ce2e..a0de8fdce4 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRuleTest.kt @@ -25,6 +25,7 @@ import org.junit.jupiter.params.provider.ValueSource class ClassSignatureRuleTest { private val classSignatureWrappingRuleAssertThat = assertThatRuleBuilder { ClassSignatureRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } .addRequiredRuleProviderDependenciesFrom(StandardRuleSetProvider()) .assertThat() @@ -56,7 +57,7 @@ class ClassSignatureRuleTest { """.trimIndent() classSignatureWrappingRuleAssertThat(code) .setMaxLineLength() - .hasNoLintViolations() + .hasNoLintViolationsExceptInAdditionalRules() } @Test @@ -733,7 +734,7 @@ class ClassSignatureRuleTest { classSignatureWrappingRuleAssertThat(code) .setMaxLineLength() .withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset") - .hasNoLintViolations() + .hasNoLintViolationsExceptInAdditionalRules() } @Test @@ -1828,7 +1829,7 @@ class ClassSignatureRuleTest { ) """.trimIndent() classSignatureWrappingRuleAssertThat(code) - .withEditorConfigOverride(CODE_STYLE_PROPERTY to CodeStyleValue.intellij_idea) + .withEditorConfigOverride(CODE_STYLE_PROPERTY to intellij_idea) // Set max_line_length as other the class signature would not be rewritten to single line .withEditorConfigOverride(MAX_LINE_LENGTH_PROPERTY to 999) .hasNoLintViolations() diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ContextReceiverWrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ContextReceiverWrappingRuleTest.kt index f6c78482b1..779168608b 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ContextReceiverWrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ContextReceiverWrappingRuleTest.kt @@ -1,14 +1,16 @@ package com.pinterest.ktlint.ruleset.standard.rules -import com.pinterest.ktlint.test.KtLintAssertThat import com.pinterest.ktlint.test.KtLintAssertThat.Companion.EOL_CHAR import com.pinterest.ktlint.test.KtLintAssertThat.Companion.MAX_LINE_LENGTH_MARKER +import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRuleBuilder import com.pinterest.ktlint.test.LintViolation import org.junit.jupiter.api.Test class ContextReceiverWrappingRuleTest { private val contextReceiverWrappingRuleAssertThat = - KtLintAssertThat.assertThatRule { ContextReceiverWrappingRule() } + assertThatRuleBuilder { ContextReceiverWrappingRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } + .assertThat() @Test fun `Given a function without modifiers and with a context receiver on the same line as the fun keyword then wrap the fun keyword to a newline`() { diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionLiteralRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionLiteralRuleTest.kt index 58c3ff8435..0ebefd4ac6 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionLiteralRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionLiteralRuleTest.kt @@ -1,14 +1,21 @@ package com.pinterest.ktlint.ruleset.standard.rules +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.RuleExecution.disabled +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.RuleExecution.enabled +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.createRuleExecutionEditorConfigProperty import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider import com.pinterest.ktlint.test.KtLintAssertThat.Companion.EOL_CHAR import com.pinterest.ktlint.test.KtLintAssertThat.Companion.MAX_LINE_LENGTH_MARKER -import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule +import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRuleBuilder import com.pinterest.ktlint.test.LintViolation +import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test class FunctionLiteralRuleTest { - private val functionLiteralRuleAssertThat = assertThatRule { FunctionLiteralRule() } + private val functionLiteralRuleAssertThat = + assertThatRuleBuilder { FunctionLiteralRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } + .assertThat() @Test fun `Given a single line lambda without parameters`() { @@ -99,7 +106,7 @@ class FunctionLiteralRuleTest { """.trimIndent() functionLiteralRuleAssertThat(code) .setMaxLineLength() - .hasNoLintViolations() + .hasNoLintViolationsExceptInAdditionalRules() } @Test @@ -142,9 +149,9 @@ class FunctionLiteralRuleTest { functionLiteralRuleAssertThat(code).hasNoLintViolations() } - @Test - fun `Given a lambda with multiple parameters but not fitting on the first line`() { - val code = + @Nested + inner class `Given a lambda with multiple parameters but not fitting on the first line` { + private val code = """ // $MAX_LINE_LENGTH_MARKER $EOL_CHAR val foobar = @@ -152,24 +159,37 @@ class FunctionLiteralRuleTest { foo + bar } """.trimIndent() - val formattedCode = - """ - // $MAX_LINE_LENGTH_MARKER $EOL_CHAR - val foobar = - { - fooooo: Foo, - bar: Bar - -> - foo + bar - } - """.trimIndent() - functionLiteralRuleAssertThat(code) - .setMaxLineLength() - .hasLintViolations( - LintViolation(3, 7, "Newline expected before parameter"), - LintViolation(3, 20, "Newline expected before parameter"), - LintViolation(3, 29, "Newline expected before arrow"), - ).isFormattedAs(formattedCode) + + @Test + fun `Given that max-line-length rule is enabled`() { + val formattedCode = + """ + // $MAX_LINE_LENGTH_MARKER $EOL_CHAR + val foobar = + { + fooooo: Foo, + bar: Bar + -> + foo + bar + } + """.trimIndent() + functionLiteralRuleAssertThat(code) + .setMaxLineLength() + .withEditorConfigOverride(MAX_LINE_LENGTH_RULE_ID.createRuleExecutionEditorConfigProperty() to enabled) + .hasLintViolations( + LintViolation(3, 7, "Newline expected before parameter"), + LintViolation(3, 20, "Newline expected before parameter"), + LintViolation(3, 29, "Newline expected before arrow"), + ).isFormattedAs(formattedCode) + } + + @Test + fun `Given that max-line-length rule is disabled`() { + functionLiteralRuleAssertThat(code) + .setMaxLineLength() + .withEditorConfigOverride(MAX_LINE_LENGTH_RULE_ID.createRuleExecutionEditorConfigProperty() to disabled) + .hasNoLintViolations() + } } @Test @@ -359,7 +379,6 @@ class FunctionLiteralRuleTest { """.trimIndent() functionLiteralRuleAssertThat(code) .setMaxLineLength() - .addAdditionalRuleProvider { MaxLineLengthRule() } .hasLintViolationsForAdditionalRule( LintViolation(3, 30, "Exceeded max line length (29)", false), LintViolation(5, 30, "Exceeded max line length (29)", false), diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionReturnTypeSpacingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionReturnTypeSpacingRuleTest.kt index da13469929..c1e5826991 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionReturnTypeSpacingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionReturnTypeSpacingRuleTest.kt @@ -2,11 +2,14 @@ package com.pinterest.ktlint.ruleset.standard.rules import com.pinterest.ktlint.test.KtLintAssertThat.Companion.EOL_CHAR import com.pinterest.ktlint.test.KtLintAssertThat.Companion.MAX_LINE_LENGTH_MARKER -import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule +import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRuleBuilder import org.junit.jupiter.api.Test class FunctionReturnTypeSpacingRuleTest { - private val functionReturnTypeSpacingRuleAssertThat = assertThatRule { FunctionReturnTypeSpacingRule() } + private val functionReturnTypeSpacingRuleAssertThat = + assertThatRuleBuilder { FunctionReturnTypeSpacingRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } + .assertThat() @Test fun `Given a function signature without whitespace between the closing parenthesis and the colon of the return type then do not reformat`() { diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRuleTest.kt index 0fbf6717e5..b8833626c9 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionSignatureRuleTest.kt @@ -26,6 +26,7 @@ import org.junit.jupiter.params.provider.EnumSource class FunctionSignatureRuleTest { private val functionSignatureWrappingRuleAssertThat = assertThatRuleBuilder { FunctionSignatureRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } .addRequiredRuleProviderDependenciesFrom(StandardRuleSetProvider()) .assertThat() @@ -224,9 +225,18 @@ class FunctionSignatureRuleTest { .setMaxLineLength() .addAdditionalRuleProvider { ValueParameterCommentRule() } .hasLintViolationsForAdditionalRule( + LintViolation(2, 49, "Exceeded max line length (48)", canBeAutoCorrected = false), + LintViolation(3, 49, "Exceeded max line length (48)", canBeAutoCorrected = false), + LintViolation(4, 49, "Exceeded max line length (48)", canBeAutoCorrected = false), LintViolation(5, 18, "A comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above.", false), + LintViolation(5, 49, "Exceeded max line length (48)", canBeAutoCorrected = false), LintViolation(6, 19, "A comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above.", false), + LintViolation(6, 49, "Exceeded max line length (48)", canBeAutoCorrected = false), LintViolation(7, 23, "A comment inside or on same line after a 'value_parameter' is not allowed. It may be placed on a separate line above.", false), + LintViolation(7, 49, "Exceeded max line length (48)", canBeAutoCorrected = false), + LintViolation(8, 49, "Exceeded max line length (48)", canBeAutoCorrected = false), + LintViolation(9, 49, "Exceeded max line length (48)", canBeAutoCorrected = false), + LintViolation(10, 49, "Exceeded max line length (48)", canBeAutoCorrected = false), ).hasNoLintViolationsExceptInAdditionalRules() } @@ -1031,7 +1041,7 @@ class FunctionSignatureRuleTest { .setMaxLineLength() .withEditorConfigOverride(FUNCTION_BODY_EXPRESSION_WRAPPING_PROPERTY to bodyExpressionWrapping) .addAdditionalRuleProvider { IndentationRule() } - .hasNoLintViolations() + .hasNoLintViolationsExceptInAdditionalRules() } @Nested @@ -1142,7 +1152,7 @@ class FunctionSignatureRuleTest { functionSignatureWrappingRuleAssertThat(code) .setMaxLineLength() .withEditorConfigOverride(FORCE_MULTILINE_WHEN_PARAMETER_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset") - .hasNoLintViolations() + .hasNoLintViolationsExceptInAdditionalRules() } @Nested @@ -1157,7 +1167,7 @@ class FunctionSignatureRuleTest { """.trimIndent() functionSignatureWrappingRuleAssertThat(code) .setMaxLineLength() - .hasNoLintViolations() + .hasNoLintViolationsExceptInAdditionalRules() } @Test @@ -1170,7 +1180,7 @@ class FunctionSignatureRuleTest { """.trimIndent() functionSignatureWrappingRuleAssertThat(code) .setMaxLineLength() - .hasNoLintViolations() + .hasNoLintViolationsExceptInAdditionalRules() } @Test @@ -1184,7 +1194,7 @@ class FunctionSignatureRuleTest { """.trimIndent() functionSignatureWrappingRuleAssertThat(code) .setMaxLineLength() - .hasNoLintViolations() + .hasNoLintViolationsExceptInAdditionalRules() } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRuleTest.kt index 240139ffe6..07abbb57f9 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRuleTest.kt @@ -5058,6 +5058,7 @@ internal class IndentationRuleTest { """.trimIndent() indentationRuleAssertThat(code) .addAdditionalRuleProvider { ParameterWrappingRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } .withEditorConfigOverride(CODE_STYLE_PROPERTY to ktlint_official) .setMaxLineLength() .isFormattedAs(formattedCode) diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRuleTest.kt index 5c0b9c0bef..54836d7f61 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRuleTest.kt @@ -1,12 +1,21 @@ 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.EditorConfig import com.pinterest.ktlint.rule.engine.core.api.editorconfig.MAX_LINE_LENGTH_PROPERTY +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.RuleExecution.disabled +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.RuleExecution.enabled +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.createRuleExecutionEditorConfigProperty +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.ec4j.toPropertyWithValue +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.ktLintRuleExecutionPropertyName import com.pinterest.ktlint.ruleset.standard.rules.MaxLineLengthRule.Companion.IGNORE_BACKTICKED_IDENTIFIER_PROPERTY import com.pinterest.ktlint.test.KtLintAssertThat.Companion.EOL_CHAR import com.pinterest.ktlint.test.KtLintAssertThat.Companion.MAX_LINE_LENGTH_MARKER import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule import com.pinterest.ktlint.test.LintViolation import com.pinterest.ktlint.test.MULTILINE_STRING_QUOTE +import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test @@ -226,4 +235,51 @@ class MaxLineLengthRuleTest { .setMaxLineLength() .hasNoLintViolations() } + + @Test + fun `Given max_line_length property is set and max-line-length rule is enabled then return the max_line_length`() { + val editorConfig = + EditorConfig( + mapOf( + MAX_LINE_LENGTH to MAX_LINE_LENGTH_PROPERTY.toPropertyWithValue(SOME_MAX_LINE_LENGTH.toString()), + KTLINT_RULE_EXECUTION_PROPERTY_NAME to MAX_LINE_LENGTH_RULE_EXECUTION_PROPERTY.toPropertyWithValue(enabled.name), + ), + ) + + assertThat(editorConfig.maxLineLength()).isEqualTo(SOME_MAX_LINE_LENGTH) + } + + @Test + fun `Given max_line_length property is set and max-line-length rule is disabled then return Int MAX_VALUE`() { + val editorConfig = + EditorConfig( + mapOf( + MAX_LINE_LENGTH to MAX_LINE_LENGTH_PROPERTY.toPropertyWithValue(SOME_MAX_LINE_LENGTH.toString()), + KTLINT_RULE_EXECUTION_PROPERTY_NAME to MAX_LINE_LENGTH_RULE_EXECUTION_PROPERTY.toPropertyWithValue(disabled.name), + ), + ) + + assertThat(editorConfig.maxLineLength()).isEqualTo(Int.MAX_VALUE) + } + + @Test + fun `Given intellij_idea code style, and max_line_length property equals 'unset' and max-line-length rule is enabled then return Int MAX_VALUE`() { + val editorConfig = + EditorConfig( + mapOf( + MAX_LINE_LENGTH to MAX_LINE_LENGTH_PROPERTY.toPropertyWithValue("unset"), + CODE_STYLE_PROPERTY.name to CODE_STYLE_PROPERTY.toPropertyWithValue(CodeStyleValue.intellij_idea.name), + KTLINT_RULE_EXECUTION_PROPERTY_NAME to MAX_LINE_LENGTH_RULE_EXECUTION_PROPERTY.toPropertyWithValue(enabled.name), + ), + ) + + assertThat(editorConfig.maxLineLength()).isEqualTo(Int.MAX_VALUE) + } + + private companion object { + const val MAX_LINE_LENGTH = "max_line_length" + const val SOME_MAX_LINE_LENGTH = 123 + val KTLINT_RULE_EXECUTION_PROPERTY_NAME = MAX_LINE_LENGTH_RULE_ID.ktLintRuleExecutionPropertyName() + val MAX_LINE_LENGTH_RULE_EXECUTION_PROPERTY = MAX_LINE_LENGTH_RULE_ID.createRuleExecutionEditorConfigProperty() + } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRuleTest.kt index c78ad85c5d..214d8b3127 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListSpacingRuleTest.kt @@ -2,12 +2,15 @@ package com.pinterest.ktlint.ruleset.standard.rules import com.pinterest.ktlint.test.KtLintAssertThat.Companion.EOL_CHAR import com.pinterest.ktlint.test.KtLintAssertThat.Companion.MAX_LINE_LENGTH_MARKER -import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule +import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRuleBuilder import com.pinterest.ktlint.test.LintViolation import org.junit.jupiter.api.Test class ParameterListSpacingRuleTest { - private val parameterListSpacingRuleAssertThat = assertThatRule { ParameterListSpacingRule() } + private val parameterListSpacingRuleAssertThat = + assertThatRuleBuilder { ParameterListSpacingRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } + .assertThat() @Test fun `Given a function signature which does not contain redundant spaces then do no reformat`() { diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListWrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListWrappingRuleTest.kt index 6ceda332b2..b332d67962 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListWrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterListWrappingRuleTest.kt @@ -15,6 +15,7 @@ import org.junit.jupiter.params.provider.EnumSource class ParameterListWrappingRuleTest { private val parameterListWrappingRuleAssertThat = assertThatRuleBuilder { ParameterListWrappingRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } // Keep formatted code readable .addAdditionalRuleProvider { IndentationRule() } .assertThat() @@ -77,7 +78,7 @@ class ParameterListWrappingRuleTest { """.trimIndent() parameterListWrappingRuleAssertThat(code) .setMaxLineLength() - .hasNoLintViolations() + .hasNoLintViolationsExceptInAdditionalRules() } @Test diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterWrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterWrappingRuleTest.kt index 4518554d8f..b675dff488 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterWrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ParameterWrappingRuleTest.kt @@ -1,13 +1,16 @@ package com.pinterest.ktlint.ruleset.standard.rules -import com.pinterest.ktlint.test.KtLintAssertThat import com.pinterest.ktlint.test.KtLintAssertThat.Companion.EOL_CHAR import com.pinterest.ktlint.test.KtLintAssertThat.Companion.MAX_LINE_LENGTH_MARKER +import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRuleBuilder import com.pinterest.ktlint.test.LintViolation import org.junit.jupiter.api.Test internal class ParameterWrappingRuleTest { - private val parameterWrappingRuleAssertThat = KtLintAssertThat.assertThatRule { ParameterWrappingRule() } + private val parameterWrappingRuleAssertThat = + assertThatRuleBuilder { ParameterWrappingRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } + .assertThat() @Test fun `Given that the variable name and the following colon do not fit on the same line as val or var keyword then wrap after the colon`() { diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyWrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyWrappingRuleTest.kt index 52535a0d16..0287dfd8e3 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyWrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/PropertyWrappingRuleTest.kt @@ -1,13 +1,16 @@ package com.pinterest.ktlint.ruleset.standard.rules -import com.pinterest.ktlint.test.KtLintAssertThat import com.pinterest.ktlint.test.KtLintAssertThat.Companion.EOL_CHAR import com.pinterest.ktlint.test.KtLintAssertThat.Companion.MAX_LINE_LENGTH_MARKER +import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRuleBuilder import com.pinterest.ktlint.test.LintViolation import org.junit.jupiter.api.Test internal class PropertyWrappingRuleTest { - private val propertyWrappingRuleAssertThat = KtLintAssertThat.assertThatRule { PropertyWrappingRule() } + private val propertyWrappingRuleAssertThat = + assertThatRuleBuilder { PropertyWrappingRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } + .assertThat() @Test fun `Given that the variable name and the following colon do not fit on the same line as val or var keyword then wrap after the colon`() { diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRuleTest.kt index 06f834a346..8cf1999c5c 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/WrappingRuleTest.kt @@ -3,7 +3,7 @@ package com.pinterest.ktlint.ruleset.standard.rules import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPERTY import com.pinterest.ktlint.test.KtLintAssertThat.Companion.EOL_CHAR import com.pinterest.ktlint.test.KtLintAssertThat.Companion.MAX_LINE_LENGTH_MARKER -import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule +import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRuleBuilder import com.pinterest.ktlint.test.LintViolation import com.pinterest.ktlint.test.MULTILINE_STRING_QUOTE import com.pinterest.ktlint.test.TAB @@ -13,7 +13,10 @@ import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test internal class WrappingRuleTest { - private val wrappingRuleAssertThat = assertThatRule { WrappingRule() } + private val wrappingRuleAssertThat = + assertThatRuleBuilder { WrappingRule() } + .addAdditionalRuleProvider { MaxLineLengthRule() } + .assertThat() @Test fun `Given a multiline string containing a string-template as parameter value but then wrap the value to a start and end on separate lines`() {