Skip to content

Commit

Permalink
Loosen dependency between chain-method-continuation and argument-list…
Browse files Browse the repository at this point in the history
…-wrapping

Allow the chain-method-continuation rule to be run when argument-list-wrapping is disabled or not loaded. Whenever both rules are loaded, the argument-list-wrapping takes precedence and runs before the chain-method-continuation.
  • Loading branch information
paul-dingemans committed Dec 27, 2023
1 parent c1440aa commit b3edcff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.STRING_TEMPLATE
import com.pinterest.ktlint.rule.engine.core.api.IndentConfig
import com.pinterest.ktlint.rule.engine.core.api.Rule
import com.pinterest.ktlint.rule.engine.core.api.Rule.VisitorModifier.RunAfterRule
import com.pinterest.ktlint.rule.engine.core.api.Rule.VisitorModifier.RunAfterRule.Mode.ONLY_WHEN_RUN_AFTER_RULE_IS_LOADED_AND_ENABLED
import com.pinterest.ktlint.rule.engine.core.api.Rule.VisitorModifier.RunAfterRule.Mode.REGARDLESS_WHETHER_RUN_AFTER_RULE_IS_LOADED_OR_DISABLED
import com.pinterest.ktlint.rule.engine.core.api.RuleId
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.EXPERIMENTAL
Expand Down Expand Up @@ -71,7 +71,7 @@ import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet
public class ChainMethodContinuationRule :
StandardRule(
id = "chain-method-continuation",
visitorModifiers = setOf(RunAfterRule(ARGUMENT_LIST_WRAPPING_RULE_ID, ONLY_WHEN_RUN_AFTER_RULE_IS_LOADED_AND_ENABLED)),
visitorModifiers = setOf(RunAfterRule(ARGUMENT_LIST_WRAPPING_RULE_ID, REGARDLESS_WHETHER_RUN_AFTER_RULE_IS_LOADED_OR_DISABLED)),
usesEditorConfigProperties =
setOf(
CODE_STYLE_PROPERTY,
Expand Down Expand Up @@ -249,8 +249,6 @@ public class ChainMethodContinuationRule :

private fun ASTNode.isPrecededByComment() = treeParent.children().any { it.isPartOfComment() }

private fun ASTNode.isSucceededByComment() = nextSibling()?.isPartOfComment() ?: false

private fun insertWhiteSpaceBeforeChainOperator(
chainOperator: ASTNode,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,27 @@ class ChainMethodContinuationRuleTest {

@Nested
inner class `Given a chained expression as function argument` {
@Test
fun `Issue 2452 - Given that the argument-list-wrapping rule is not loaded or not enabled`() {
val code =
"""
val foo1 = requireNotNull(bar.filter { it == 'b' }.filter { it == 'a' }
.filter { it == 'r' })
val foo2 = requireNotNull(bar.filter { it == 'b' }.filter { it == 'a' }.filter { it == 'r' })
""".trimIndent()
val formattedCode =
"""
val foo1 = requireNotNull(bar
.filter { it == 'b' }
.filter { it == 'a' }
.filter { it == 'r' })
val foo2 = requireNotNull(bar.filter { it == 'b' }.filter { it == 'a' }.filter { it == 'r' })
""".trimIndent()
chainMethodContinuationRuleAssertThat(code)
.addAdditionalRuleProvider { IndentationRule() }
.isFormattedAs(formattedCode)
}

@Test
fun `Wrapping the argument has precedence above wrapping on the chain operator`() {
val code =
Expand All @@ -566,6 +587,7 @@ class ChainMethodContinuationRuleTest {
""".trimIndent()
chainMethodContinuationRuleAssertThat(code)
.setMaxLineLength()
.addAdditionalRuleProvider { ArgumentListWrappingRule() }
.hasNoLintViolationsExceptInAdditionalRules()
.isFormattedAs(formattedCode)
}
Expand Down

0 comments on commit b3edcff

Please sign in to comment.