Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single-line chain method calls with operator #2712

Closed
hanggrian opened this issue Jun 22, 2024 · 3 comments · Fixed by #2728
Closed

Single-line chain method calls with operator #2712

hanggrian opened this issue Jun 22, 2024 · 3 comments · Fixed by #2728
Milestone

Comments

@hanggrian
Copy link
Contributor

I have two strings, both are single-line chain method calls that do not violate the max lines rule.

val left = result.value.drop(1).dropLast(1).lowercase()
val right = result.value.last()

When joined together with + operator, chain method continuation rule treats them as one statement. It demanded that the first chain be wrapped, but not the rest.

Expected Behavior

When there are mathematical and comparison operators, chained calls of each operand should be inspected individually. Alternatively, force multi-lining on the rest of the operands.

val s =
    result.value.drop(1).dropLast(1).lowercase() +
        result.value.last()

// or

val s =
    result.value
        .drop(1)
        .dropLast(1)
        .lowercase() +
        result.value
                .last()

Current Behavior

val s =
    result.value
        .drop(1)
        .dropLast(1)
        .lowercase() +
        result.value.last()

Additional information

  • Current version of ktlint: 1.3.0
@paul-dingemans
Copy link
Collaborator

When there are mathematical and comparison operators, chained calls of each operand should be inspected individually. Alternatively, force multi-lining on the rest of the operands.

This is exactly how the rule works. The outmost expression (parenthesis added for clarification) is (result.value.drop(1).dropLast(1).lowercase()) + (result.value.last()).

The first part of this expression, result.value.drop(1).dropLast(1).lowercase(), contains 4 chain operators, which as documented result in writing this expression as a multiline expression. The second subexpression has less than 4 chain operators, and as of that may be written as a single line.

When the first subexpression is shortened by remove one chain operation, you will see that the example is accepted by ktlint unchanged:

val s =
    result.value.drop(1).dropLast(1) +
        result.value.last()

@hanggrian
Copy link
Contributor Author

Hi Paul, thanks for clarifying. I missed the documented configuration of 4 chain method calls. However, explicitly setting ktlint_chain_method_rule_force_multiline_when_chain_operator_count_greater_or_equal_than to unset seems to have no effect in my test. It will revert back to 4 while the document states that it should disable the setting.

Using the original code, I can only pass the check when ktlint_chain_method_rule_force_multiline_when_chain_operator_count_greater_or_equal_than is set to 5 or more.

val s =
    result.value.drop(1).dropLast(1).lowercase() +
        result.value.last()

@paul-dingemans
Copy link
Collaborator

However, explicitly setting ktlint_chain_method_rule_force_multiline_when_chain_operator_count_greater_or_equal_than to unset seems to have no effect in my test. It will revert back to 4 while the document states that it should disable the setting

That is a good find. The code indeed sets the value to 4 whenever the property is unset. It should have set the value in that case to Int.MAX_VALUE.

@paul-dingemans paul-dingemans added this to the 1.3.1 milestone Jul 2, 2024
paul-dingemans added a commit that referenced this issue Jul 2, 2024
…ne_when_chain_operator_count_greater_or_equal_than when it has

When value is set to "unset" the number of chain operators on a single line is not restricted as long as the max line length is not exceeded.

Closes #2712
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants