Skip to content

Do not wrap operators onto a new line in Kotlin sources#936

Open
vlsi wants to merge 1 commit into
openrewrite:mainfrom
vlsi:kotlin-operatorwrap
Open

Do not wrap operators onto a new line in Kotlin sources#936
vlsi wants to merge 1 commit into
openrewrite:mainfrom
vlsi:kotlin-operatorwrap

Conversation

@vlsi

@vlsi vlsi commented Jul 10, 2026

Copy link
Copy Markdown

What's changed

OperatorWrap now only reformats Java compilation units, leaving Kotlin (and other non-Java JavaSourceFiles) untouched. Adds a Kotlin test.

Why

In Kotlin a newline terminates an expression, so a leading binary operator is parsed as a unary operator on a new statement. With the default WrapOption.NL style the recipe moves a binary operator to the start of the next line:

val s = "aaa" + "b" +
    "c"

becomes

val s = "aaa" + "b"
    + "c"

The trailing + "c" is now a unaryPlus on a separate statement, so the code no longer compiles. This surfaced running a CodeCleanup composite over Apache JMeter's mixed Java/Kotlin sources — a letsPlot(...) + geomLine {...} + ... chain was broken the same way.

The recipe's visitBinary moves operators purely by relocating whitespace, which is only valid under Java's newline rules. Restricting the visitor to J.CompilationUnit (rather than every JavaSourceFile) keeps the Java behaviour and stops it corrupting Kotlin.

How to verify

./gradlew test --tests "*.OperatorWrapKotlinTest" --tests "*.OperatorWrapTest" — the new Kotlin test passes and the existing Java suite (19 tests) is unchanged.

In Kotlin a newline terminates an expression, so a leading binary operator is
parsed as a unary operator on a new statement. With the default `WrapOption.NL`
style, `OperatorWrap` moved a binary operator to the start of the next line,
turning

    val s = "aaa" + "b" +
        "c"

into the uncompilable

    val s = "aaa" + "b"
        + "c"

(the leading `+` becomes `unaryPlus`). This surfaced running a `CodeCleanup`
composite over Apache JMeter's mixed Java/Kotlin sources.

Restrict the visitor to `J.CompilationUnit` so it only reformats Java and leaves
Kotlin (and other non-Java `JavaSourceFile`s) untouched.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@vlsi
vlsi force-pushed the kotlin-operatorwrap branch from efd23fc to a4ad45b Compare July 10, 2026 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant