From 877b9f2d996ac64de167c2a8f369ced1cebd4d18 Mon Sep 17 00:00:00 2001 From: Lingzhang Date: Thu, 13 Mar 2014 21:26:03 -0400 Subject: [PATCH] KT4385 Bug Fix for Unnecessary Parentheses in WhenToIfIntention --- .../jet/lang/psi/JetPsiUnparsingUtils.java | 3 ++- .../whenToIf/whenWithDotQualifiedExpression.kt | 13 +++++++++++++ .../whenWithDotQualifiedExpression.kt.after | 11 +++++++++++ .../intentions/CodeTransformationTestGenerated.java | 5 +++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 idea/testData/intentions/branched/ifWhen/whenToIf/whenWithDotQualifiedExpression.kt create mode 100644 idea/testData/intentions/branched/ifWhen/whenToIf/whenWithDotQualifiedExpression.kt.after diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUnparsingUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUnparsingUtils.java index 1172d51f842..cfa0dcf4052 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUnparsingUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiUnparsingUtils.java @@ -53,7 +53,8 @@ public class JetPsiUnparsingUtils { return (expression instanceof JetParenthesizedExpression || expression instanceof JetConstantExpression || - expression instanceof JetSimpleNameExpression) + expression instanceof JetSimpleNameExpression || + expression instanceof JetDotQualifiedExpression) ? text : "(" + text + ")"; } diff --git a/idea/testData/intentions/branched/ifWhen/whenToIf/whenWithDotQualifiedExpression.kt b/idea/testData/intentions/branched/ifWhen/whenToIf/whenWithDotQualifiedExpression.kt new file mode 100644 index 00000000000..807fda71d73 --- /dev/null +++ b/idea/testData/intentions/branched/ifWhen/whenToIf/whenWithDotQualifiedExpression.kt @@ -0,0 +1,13 @@ +class G { + fun cat(x: Int, y: Int): Int { + return x + y + } +} + +fun test(x: Int, y: Int): String { + when (G.cat(x, y)) { + 1 -> return "one" + 2 -> return "two" + else -> return "big" + } +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifWhen/whenToIf/whenWithDotQualifiedExpression.kt.after b/idea/testData/intentions/branched/ifWhen/whenToIf/whenWithDotQualifiedExpression.kt.after new file mode 100644 index 00000000000..1e222021460 --- /dev/null +++ b/idea/testData/intentions/branched/ifWhen/whenToIf/whenWithDotQualifiedExpression.kt.after @@ -0,0 +1,11 @@ +class G { + fun cat(x: Int, y: Int): Int { + return x + y + } +} + +fun test(x: Int, y: Int): String { + if (G.cat(x, y) == 1) return "one" + else if (G.cat(x, y) == 2) return "two" + else return "big" +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java index 44901d93781..e9e326f63a4 100644 --- a/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/intentions/CodeTransformationTestGenerated.java @@ -469,6 +469,11 @@ public class CodeTransformationTestGenerated extends AbstractCodeTransformationT JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/intentions/branched/ifWhen/whenToIf"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("whenWithDotQualifiedExpression.kt") + public void testWhenWithDotQualifiedExpression() throws Exception { + doTestWhenToIf("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithDotQualifiedExpression.kt"); + } + @TestMetadata("whenWithEqualityTests.kt") public void testWhenWithEqualityTests() throws Exception { doTestWhenToIf("idea/testData/intentions/branched/ifWhen/whenToIf/whenWithEqualityTests.kt");