diff --git a/idea/src/org/jetbrains/kotlin/idea/joinLines/JoinBlockIntoSingleStatementHandler.kt b/idea/src/org/jetbrains/kotlin/idea/joinLines/JoinBlockIntoSingleStatementHandler.kt index b0eda87fbd2..e8028632a6d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/joinLines/JoinBlockIntoSingleStatementHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/joinLines/JoinBlockIntoSingleStatementHandler.kt @@ -42,17 +42,28 @@ class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate { // handle nested if's val pparent = parent.parent - if (pparent is KtIfExpression && block == pparent.then && statement is KtIfExpression && statement.`else` == null) { - // if outer if has else-branch and inner does not have it, do not remove braces otherwise else-branch will belong to different if! - if (pparent.`else` != null) return -1 + if (pparent is KtIfExpression) { + if (block == pparent.then && statement is KtIfExpression && statement.`else` == null) { + // if outer if has else-branch and inner does not have it, do not remove braces otherwise else-branch will belong to different if! + if (pparent.`else` != null) return -1 - return MergeIfsIntention.applyTo(pparent) + return MergeIfsIntention.applyTo(pparent) + } + + if (block == pparent.`else`) { + val ifParent = pparent.parent + if (!( + ifParent is KtBlockExpression || + ifParent is KtDeclaration || + KtPsiUtil.isAssignment(ifParent))) { + return -1 + } + } } val newStatement = block.replace(statement) return newStatement.textRange!!.startOffset } - override fun tryJoinLines(document: Document, file: PsiFile, start: Int, end: Int) - = - 1 + override fun tryJoinLines(document: Document, file: PsiFile, start: Int, end: Int) = -1 } diff --git a/idea/testData/joinLines/removeBraces/IfWithElseBlockInAssignment.kt b/idea/testData/joinLines/removeBraces/IfWithElseBlockInAssignment.kt new file mode 100644 index 00000000000..d09cf759466 --- /dev/null +++ b/idea/testData/joinLines/removeBraces/IfWithElseBlockInAssignment.kt @@ -0,0 +1,8 @@ +fun test() { + val t = 1 + val f = 2 + val a: Int + a = if (true) t else { + f + } +} \ No newline at end of file diff --git a/idea/testData/joinLines/removeBraces/IfWithElseBlockInAssignment.kt.after b/idea/testData/joinLines/removeBraces/IfWithElseBlockInAssignment.kt.after new file mode 100644 index 00000000000..c926b1b74cc --- /dev/null +++ b/idea/testData/joinLines/removeBraces/IfWithElseBlockInAssignment.kt.after @@ -0,0 +1,6 @@ +fun test() { + val t = 1 + val f = 2 + val a: Int + a = if (true) t else f +} \ No newline at end of file diff --git a/idea/testData/joinLines/removeBraces/IfWithPostfixAfterElseBlock.kt b/idea/testData/joinLines/removeBraces/IfWithPostfixAfterElseBlock.kt new file mode 100644 index 00000000000..d1e6c94363d --- /dev/null +++ b/idea/testData/joinLines/removeBraces/IfWithPostfixAfterElseBlock.kt @@ -0,0 +1,8 @@ +fun test() { + val t = 1 + val f = 2 + val a: Int + a = if (true) t else { + f + }!! +} \ No newline at end of file diff --git a/idea/testData/joinLines/removeBraces/IfWithPostfixAfterElseBlock.kt.after b/idea/testData/joinLines/removeBraces/IfWithPostfixAfterElseBlock.kt.after new file mode 100644 index 00000000000..47f291c8ddc --- /dev/null +++ b/idea/testData/joinLines/removeBraces/IfWithPostfixAfterElseBlock.kt.after @@ -0,0 +1,7 @@ +fun test() { + val t = 1 + val f = 2 + val a: Int + a = if (true) t else { f + }!! +} \ No newline at end of file diff --git a/idea/testData/joinLines/removeBraces/IfWithQualifiedAfterElseBlock.kt b/idea/testData/joinLines/removeBraces/IfWithQualifiedAfterElseBlock.kt new file mode 100644 index 00000000000..72074828f31 --- /dev/null +++ b/idea/testData/joinLines/removeBraces/IfWithQualifiedAfterElseBlock.kt @@ -0,0 +1,6 @@ +fun test() { + if (smth) list1 else { + list2 + }.add(newItem) +} + diff --git a/idea/testData/joinLines/removeBraces/IfWithQualifiedAfterElseBlock.kt.after b/idea/testData/joinLines/removeBraces/IfWithQualifiedAfterElseBlock.kt.after new file mode 100644 index 00000000000..512fc6e6e3c --- /dev/null +++ b/idea/testData/joinLines/removeBraces/IfWithQualifiedAfterElseBlock.kt.after @@ -0,0 +1,5 @@ +fun test() { + if (smth) list1 else { list2 + }.add(newItem) +} + diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/declarations/AbstractJoinLinesTest.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/declarations/AbstractJoinLinesTest.java index 6fc8c6b21aa..9c2c9baa303 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/declarations/AbstractJoinLinesTest.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/declarations/AbstractJoinLinesTest.java @@ -19,15 +19,25 @@ package org.jetbrains.kotlin.idea.intentions.declarations; import com.intellij.codeInsight.editorActions.JoinLinesHandler; import com.intellij.openapi.projectRoots.JavaSdk; import com.intellij.openapi.projectRoots.Sdk; +import com.intellij.rt.execution.junit.FileComparisonFailure; import com.intellij.testFramework.LightCodeInsightTestCase; import org.apache.commons.lang.SystemUtils; import org.jetbrains.annotations.NotNull; +import org.jetbrains.kotlin.test.KotlinTestUtils; + +import java.io.File; public abstract class AbstractJoinLinesTest extends LightCodeInsightTestCase { public void doTest(@NotNull String path) throws Exception { configureByFile(path); new JoinLinesHandler(null).execute(getEditor(), getCurrentEditorDataContext()); - checkResultByFile(path + ".after"); + String afterFilePath = path + ".after"; + try { + checkResultByFile(afterFilePath); + } + catch (FileComparisonFailure e) { + KotlinTestUtils.assertEqualsToFile(new File(afterFilePath), getEditor()); + } } @NotNull diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/declarations/JoinLinesTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/declarations/JoinLinesTestGenerated.java index 39e072eba2b..93a40aff339 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/declarations/JoinLinesTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/declarations/JoinLinesTestGenerated.java @@ -260,6 +260,24 @@ public class JoinLinesTestGenerated extends AbstractJoinLinesTest { doTest(fileName); } + @TestMetadata("IfWithElseBlockInAssignment.kt") + public void testIfWithElseBlockInAssignment() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/IfWithElseBlockInAssignment.kt"); + doTest(fileName); + } + + @TestMetadata("IfWithPostfixAfterElseBlock.kt") + public void testIfWithPostfixAfterElseBlock() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/IfWithPostfixAfterElseBlock.kt"); + doTest(fileName); + } + + @TestMetadata("IfWithQualifiedAfterElseBlock.kt") + public void testIfWithQualifiedAfterElseBlock() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/IfWithQualifiedAfterElseBlock.kt"); + doTest(fileName); + } + @TestMetadata("LambdaBody.kt") public void testLambdaBody() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/LambdaBody.kt");