smart enter to wrap its single then/else branch into code blocks (IDEA-155280)

This commit is contained in:
peter
2016-04-29 19:35:30 +02:00
parent 5131d6fadb
commit 4e3139ffda
6 changed files with 63 additions and 21 deletions

View File

@@ -19,6 +19,8 @@ import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author max
@@ -30,37 +32,37 @@ public class MissingIfBranchesFixer implements Fixer {
PsiIfStatement ifStatement = (PsiIfStatement) psiElement;
final Document doc = editor.getDocument();
final PsiStatement elseBranch = ifStatement.getElseBranch();
final PsiKeyword elseElement = ifStatement.getElseElement();
if (elseElement != null && (elseBranch == null || !(elseBranch instanceof PsiBlockStatement) &&
startLine(doc, elseBranch) > startLine(doc, elseElement))) {
doc.insertString(elseElement.getTextRange().getEndOffset(), "{}");
if (elseElement != null) {
handleBranch(doc, ifStatement, elseElement, ifStatement.getElseBranch());
}
PsiElement thenBranch = ifStatement.getThenBranch();
if (thenBranch instanceof PsiBlockStatement) return;
boolean transformingOneLiner = false;
if (thenBranch != null && startLine(doc, thenBranch) == startLine(doc, ifStatement)) {
if (ifStatement.getCondition() != null) {
return;
}
transformingOneLiner = true;
}
final PsiJavaToken rParenth = ifStatement.getRParenth();
PsiJavaToken rParenth = ifStatement.getRParenth();
assert rParenth != null;
handleBranch(doc, ifStatement, rParenth, ifStatement.getThenBranch());
}
if (elseBranch == null && !transformingOneLiner || thenBranch == null) {
doc.insertString(rParenth.getTextRange().getEndOffset(), "{}");
private static void handleBranch(@NotNull Document doc, @NotNull PsiIfStatement ifStatement, @NotNull PsiElement beforeBranch, @Nullable PsiStatement branch) {
if (branch instanceof PsiBlockStatement) return;
boolean transformingOneLiner = branch != null && (startLine(doc, beforeBranch) == startLine(doc, branch) ||
startCol(doc, ifStatement) < startCol(doc, branch));
if (!transformingOneLiner) {
doc.insertString(beforeBranch.getTextRange().getEndOffset(), "{}");
}
else {
doc.insertString(rParenth.getTextRange().getEndOffset(), "{");
doc.insertString(thenBranch.getTextRange().getEndOffset() + 1, "}");
doc.insertString(beforeBranch.getTextRange().getEndOffset(), "{");
doc.insertString(branch.getTextRange().getEndOffset() + 1, "}");
}
}
private static int startLine(Document doc, PsiElement psiElement) {
private static int startLine(Document doc, @NotNull PsiElement psiElement) {
return doc.getLineNumber(psiElement.getTextRange().getStartOffset());
}
private static int startCol(Document doc, @NotNull PsiElement psiElement) {
int offset = psiElement.getTextRange().getStartOffset();
return offset - doc.getLineStartOffset(doc.getLineNumber(offset));
}
}

View File

@@ -0,0 +1,9 @@
class Foo {
{
if (t != true<caret>)
logger.error("SimulationClient.run error: strategy () still running. please stop it before restart.");
else
proxy.startStopStrategy("strategyName", true);
}
}

View File

@@ -0,0 +1,11 @@
class Foo {
{
if (t != true) {
<caret>
logger.error("SimulationClient.run error: strategy () still running. please stop it before restart.");
} else {
proxy.startStopStrategy("strategyName", true);
}
}
}

View File

@@ -0,0 +1,7 @@
class Foo {
{
if (t != true<caret>)
logger.error("SimulationClient.run error: strategy () still running. please stop it before restart.");
}
}

View File

@@ -0,0 +1,9 @@
class Foo {
{
if (t != true) {
<caret>
logger.error("SimulationClient.run error: strategy () still running. please stop it before restart.");
}
}
}

View File

@@ -43,6 +43,10 @@ public class CompleteStatementTest extends EditorActionTestCase {
public void testCompleteIfStatementGoesToThen() throws Exception { doTest(); }
public void testAddBracesToIfAndElse() { doTest(); }
public void testAddBracesToIfThenOneLiner() { doTest(); }
public void testCompleteIfKeywordStatementGoesToThen() throws Exception { doTest(); }
public void testIndentation() throws Exception { doTest(); }