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

Improve wrapping of binary expressions #2479

Merged
merged 2 commits into from
Jan 4, 2024

Conversation

paul-dingemans
Copy link
Collaborator

Description

The binary-expression-wrapping rule is now smarter.

It no longer has a rule dependency on the value-argument-wrapping rule. Instead, if a binary expression is used as value argument, and it causes the max line length to be exceeded, then it wraps the value argument.

            // $MAX_LINE_LENGTH_MARKER    $EOL_CHAR
            val foobar1 = Foo(1 * 2 * 3 * 4)
            val foobar2 = Foo(bar(1 * 2 * 3))
            val foobar3 = Foo(bar("bar" + "bazzzzzzzzzzz"))
            val foobar4 = Foo(bar("bar" + "bazzzzzzzzzzzz"))

is formatted as

            // $MAX_LINE_LENGTH_MARKER    $EOL_CHAR
            val foobar1 = Foo(
                1 * 2 * 3 * 4
            )
            val foobar2 = Foo(
                bar(1 * 2 * 3)
            )
            val foobar3 = Foo(
                bar(
                    "bar" + "bazzzzzzzzzzz"
                )
            )
            val foobar4 = Foo(
                bar(
                    "bar" +
                        "bazzzzzzzzzzzz"
                )
            )

If the left hand side of the binary expression is call expression with a lambda argument, then wrapping the lambda argument has priority over wrapping the other arguments of the call expression.

            // $MAX_LINE_LENGTH_MARKER       $EOL_CHAR
            fun foo() {
                require(bar != "barrrrrrrr") { "some longgggggggggggggggggg message" }
            }

is formatted as

            // $MAX_LINE_LENGTH_MARKER       $EOL_CHAR
            fun foo() {
                require(bar != "barrrrrrrr") {
                    "some longgggggggggggggggggg message"
                }
            }

If the binary expression contains an elvis operator, and does not fit on a single line, than the expression is wrapped before the elvis operator.

            // $MAX_LINE_LENGTH_MARKER                            $EOL_CHAR
            val foo = foobar ?: throw UnsupportedOperationException("foobar")

is formatted as

            // $MAX_LINE_LENGTH_MARKER                            $EOL_CHAR
            val foo =
                foobar
                    ?: throw UnsupportedOperationException(
                        "foobar"
                    )

Closes #2460

Checklist

Before submitting the PR, please check following (checks which are not relevant may be ignored):

  • Commit message are well written. In addition to a short title, the commit message also explain why a change is made.
  • At least one commit message contains a reference Closes #<xxx> or Fixes #<xxx> (replace<xxx> with issue number)
  • Tests are added
  • KtLint format has been applied on source code itself and violations are fixed
  • PR title is short and clear (it is used as description in the release changelog)
  • PR description added (background information)

Documentation is updated. See difference between snapshot and release documentation

  • Snapshot documentation in case documentation is to be released together with a code change
  • Release documentation in case documentation is related to a released version of ktlint and has to be published as soon as the change is merged to master

…ent list

- Remove dependency from `binary-expression-wrapping` on `value-argument-wrapping` rules
- If a binary expression is used as value argument, and it causes the max line length to be exceeded, then wrap the value argument.
- If the left hand side of the binary expression is call expression with a lambda argument, then wrapping the lambda argument has priority over wrapping the other arguments of the call expression.
- If the binary expression contains an elvis operator, and does not fit on a single line, than the expression is wrapped before the elvis operator.

Closes #2460
@paul-dingemans paul-dingemans added this to the 1.1.1 milestone Jan 1, 2024
@paul-dingemans paul-dingemans merged commit bc4b180 into master Jan 4, 2024
12 checks passed
@paul-dingemans paul-dingemans deleted the 2460-argument-list-wrapping branch January 4, 2024 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rules should consider infix functions
1 participant