From c92823769992d43b244e8f59dfff23cf727f30e1 Mon Sep 17 00:00:00 2001 From: Natalia Ukhorskaya Date: Wed, 2 Mar 2016 10:03:11 +0300 Subject: [PATCH] J2K: drop line breaks in some polyadic expressions --- Changelog.md | 3 +- .../kotlin/j2k/ExpressionConverter.kt | 7 +-- .../jetbrains/kotlin/j2k/ast/Expressions.kt | 10 ++++ .../binaryExpression/orWithNewLine.java | 46 +++++++++++++++++++ .../binaryExpression/orWithNewLine.kt | 25 ++++++++++ ...otlinConverterForWebDemoTestGenerated.java | 6 +++ ...otlinConverterSingleFileTestGenerated.java | 6 +++ 7 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 j2k/testData/fileOrElement/binaryExpression/orWithNewLine.java create mode 100644 j2k/testData/fileOrElement/binaryExpression/orWithNewLine.kt diff --git a/Changelog.md b/Changelog.md index 6c82d3c4c64..1b33a4f4c57 100644 --- a/Changelog.md +++ b/Changelog.md @@ -10,4 +10,5 @@ ### Tools. J2K - Protected members used outside of inheritors are converted as public - Support conversion for annotation constructor calls -- Place comments from the middle of the call to the end \ No newline at end of file +- Place comments from the middle of the call to the end +- Drop line breaks between operator arguments (except '+', "-", "&&" and "||") \ No newline at end of file diff --git a/j2k/src/org/jetbrains/kotlin/j2k/ExpressionConverter.kt b/j2k/src/org/jetbrains/kotlin/j2k/ExpressionConverter.kt index 10464ac6360..1b88ca79eea 100644 --- a/j2k/src/org/jetbrains/kotlin/j2k/ExpressionConverter.kt +++ b/j2k/src/org/jetbrains/kotlin/j2k/ExpressionConverter.kt @@ -614,13 +614,14 @@ class DefaultExpressionConverter : JavaElementVisitor(), ExpressionConverter { } override fun visitPolyadicExpression(expression: PsiPolyadicExpression) { - val commentsAndSpacesInheritance = CommentsAndSpacesInheritance.LINE_BREAKS val args = expression.operands.map { - codeConverter.convertExpression(it, expression.type).assignPrototype(it, commentsAndSpacesInheritance) + codeConverter.convertExpression(it, expression.type).assignPrototype(it, CommentsAndSpacesInheritance.LINE_BREAKS) } val operators = expression.operands.mapNotNull { expression.getTokenBeforeOperand(it)?.let { - Operator(it.tokenType).assignPrototype(it, commentsAndSpacesInheritance) + val operator = Operator(it.tokenType) + val commentsAndSpacesInheritance = if (operator.acceptLineBreakBefore()) CommentsAndSpacesInheritance.LINE_BREAKS else CommentsAndSpacesInheritance.NO_SPACES + operator.assignPrototype(it, commentsAndSpacesInheritance) } } diff --git a/j2k/src/org/jetbrains/kotlin/j2k/ast/Expressions.kt b/j2k/src/org/jetbrains/kotlin/j2k/ast/Expressions.kt index 654745a2a5b..eba58d2c442 100644 --- a/j2k/src/org/jetbrains/kotlin/j2k/ast/Expressions.kt +++ b/j2k/src/org/jetbrains/kotlin/j2k/ast/Expressions.kt @@ -142,6 +142,16 @@ open class Operator(val operatorType: IElementType): Expression() { fun asString() = asString(operatorType) + fun acceptLineBreakBefore(): Boolean { + return when(operatorType) { + JavaTokenType.ANDAND, + JavaTokenType.OROR, + JavaTokenType.PLUS, + JavaTokenType.MINUS -> true + else -> false + } + } + private fun asString(tokenType: IElementType): String { return when(tokenType) { JavaTokenType.EQ -> "=" diff --git a/j2k/testData/fileOrElement/binaryExpression/orWithNewLine.java b/j2k/testData/fileOrElement/binaryExpression/orWithNewLine.java new file mode 100644 index 00000000000..f72139171e4 --- /dev/null +++ b/j2k/testData/fileOrElement/binaryExpression/orWithNewLine.java @@ -0,0 +1,46 @@ +public class A { + public static void main(String[] args) { + boolean ANDAND = true + && true + && true; + boolean OROR = true + || true + || true; + int PLUS = 1 + + 2 + + 3; + int MINUS = 1 + - 2 + - 3; + int ASTERISK = 1 + * 2 + * 3; + int DIV = 1 + + / 2 + + + / 3; + int PERC = 1 + % 2 + % 3; + int GTGT = 1 + << 2 + << 3; + int LTLT = 1 + >> 2 + >> 3; + int XOR = 1 + ^ 2 + ^ 3; + int AND = 1 + & 2 + & 3; + int OR = 1 + % 2 + % 3; + int GTGTGT = 1 + >>> 2 + >>> 3; + } +} \ No newline at end of file diff --git a/j2k/testData/fileOrElement/binaryExpression/orWithNewLine.kt b/j2k/testData/fileOrElement/binaryExpression/orWithNewLine.kt new file mode 100644 index 00000000000..df9d544295a --- /dev/null +++ b/j2k/testData/fileOrElement/binaryExpression/orWithNewLine.kt @@ -0,0 +1,25 @@ +object A { + @JvmStatic fun main(args: Array) { + val ANDAND = true + && true + && true + val OROR = true + || true + || true + val PLUS = 1 + +2 + +3 + val MINUS = 1 + -2 + -3 + val ASTERISK = 1 * 2 * 3 + val DIV = 1 / 2 / 3 + val PERC = 1 % 2 % 3 + val GTGT = 1 shl 2 shl 3 + val LTLT = 1 shr 2 shr 3 + val XOR = 1 xor 2 xor 3 + val AND = 1 and 2 and 3 + val OR = 1 % 2 % 3 + val GTGTGT = 1 ushr 2 ushr 3 + } +} \ No newline at end of file diff --git a/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterForWebDemoTestGenerated.java b/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterForWebDemoTestGenerated.java index 8f942414d4d..58b8044e915 100644 --- a/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterForWebDemoTestGenerated.java +++ b/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterForWebDemoTestGenerated.java @@ -538,6 +538,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo doTest(fileName); } + @TestMetadata("orWithNewLine.java") + public void testOrWithNewLine() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/binaryExpression/orWithNewLine.java"); + doTest(fileName); + } + @TestMetadata("plus.java") public void testPlus() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/binaryExpression/plus.java"); diff --git a/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterSingleFileTestGenerated.java b/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterSingleFileTestGenerated.java index 5cdd2f360d2..0dba621c6f1 100644 --- a/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterSingleFileTestGenerated.java +++ b/j2k/tests/org/jetbrains/kotlin/j2k/JavaToKotlinConverterSingleFileTestGenerated.java @@ -538,6 +538,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo doTest(fileName); } + @TestMetadata("orWithNewLine.java") + public void testOrWithNewLine() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/binaryExpression/orWithNewLine.java"); + doTest(fileName); + } + @TestMetadata("plus.java") public void testPlus() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/binaryExpression/plus.java");