Don't remove else block if it participate in other expression (KT-18309)
#KT-18309 Fixed
This commit is contained in:
+17
-6
@@ -42,17 +42,28 @@ class JoinBlockIntoSingleStatementHandler : JoinRawLinesHandlerDelegate {
|
|||||||
|
|
||||||
// handle nested if's
|
// handle nested if's
|
||||||
val pparent = parent.parent
|
val pparent = parent.parent
|
||||||
if (pparent is KtIfExpression && block == pparent.then && statement is KtIfExpression && statement.`else` == null) {
|
if (pparent is KtIfExpression) {
|
||||||
// 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 (block == pparent.then && statement is KtIfExpression && statement.`else` == null) {
|
||||||
if (pparent.`else` != null) return -1
|
// 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)
|
val newStatement = block.replace(statement)
|
||||||
return newStatement.textRange!!.startOffset
|
return newStatement.textRange!!.startOffset
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun tryJoinLines(document: Document, file: PsiFile, start: Int, end: Int)
|
override fun tryJoinLines(document: Document, file: PsiFile, start: Int, end: Int) = -1
|
||||||
= - 1
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
fun test() {
|
||||||
|
val t = 1
|
||||||
|
val f = 2
|
||||||
|
val a: Int
|
||||||
|
a = if (true) t else {<caret>
|
||||||
|
f
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
fun test() {
|
||||||
|
val t = 1
|
||||||
|
val f = 2
|
||||||
|
val a: Int
|
||||||
|
a = if (true) t else <caret>f
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
fun test() {
|
||||||
|
val t = 1
|
||||||
|
val f = 2
|
||||||
|
val a: Int
|
||||||
|
a = if (true) t else {<caret>
|
||||||
|
f
|
||||||
|
}!!
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
fun test() {
|
||||||
|
val t = 1
|
||||||
|
val f = 2
|
||||||
|
val a: Int
|
||||||
|
a = if (true) t else {<caret> f
|
||||||
|
}!!
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
fun test() {
|
||||||
|
if (smth) list1 else {<caret>
|
||||||
|
list2
|
||||||
|
}.add(newItem)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
fun test() {
|
||||||
|
if (smth) list1 else {<caret> list2
|
||||||
|
}.add(newItem)
|
||||||
|
}
|
||||||
|
|
||||||
+11
-1
@@ -19,15 +19,25 @@ package org.jetbrains.kotlin.idea.intentions.declarations;
|
|||||||
import com.intellij.codeInsight.editorActions.JoinLinesHandler;
|
import com.intellij.codeInsight.editorActions.JoinLinesHandler;
|
||||||
import com.intellij.openapi.projectRoots.JavaSdk;
|
import com.intellij.openapi.projectRoots.JavaSdk;
|
||||||
import com.intellij.openapi.projectRoots.Sdk;
|
import com.intellij.openapi.projectRoots.Sdk;
|
||||||
|
import com.intellij.rt.execution.junit.FileComparisonFailure;
|
||||||
import com.intellij.testFramework.LightCodeInsightTestCase;
|
import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||||
import org.apache.commons.lang.SystemUtils;
|
import org.apache.commons.lang.SystemUtils;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
public abstract class AbstractJoinLinesTest extends LightCodeInsightTestCase {
|
public abstract class AbstractJoinLinesTest extends LightCodeInsightTestCase {
|
||||||
public void doTest(@NotNull String path) throws Exception {
|
public void doTest(@NotNull String path) throws Exception {
|
||||||
configureByFile(path);
|
configureByFile(path);
|
||||||
new JoinLinesHandler(null).execute(getEditor(), getCurrentEditorDataContext());
|
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
|
@NotNull
|
||||||
|
|||||||
+18
@@ -260,6 +260,24 @@ public class JoinLinesTestGenerated extends AbstractJoinLinesTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("LambdaBody.kt")
|
||||||
public void testLambdaBody() throws Exception {
|
public void testLambdaBody() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/LambdaBody.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/joinLines/removeBraces/LambdaBody.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user