Do not wrap operators onto a new line in Kotlin sources#936
Open
vlsi wants to merge 1 commit into
Open
Conversation
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
force-pushed
the
kotlin-operatorwrap
branch
from
July 10, 2026 22:14
efd23fc to
a4ad45b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's changed
OperatorWrapnow only reformats Java compilation units, leaving Kotlin (and other non-JavaJavaSourceFiles) 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.NLstyle the recipe moves a binary operator to the start of the next line:becomes
The trailing
+ "c"is now aunaryPluson a separate statement, so the code no longer compiles. This surfaced running aCodeCleanupcomposite over Apache JMeter's mixed Java/Kotlin sources — aletsPlot(...) + geomLine {...} + ...chain was broken the same way.The recipe's
visitBinarymoves operators purely by relocating whitespace, which is only valid under Java's newline rules. Restricting the visitor toJ.CompilationUnit(rather than everyJavaSourceFile) 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.