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
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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.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
|
||||
|
||||
+18
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user