mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
SplitIfAction and ExtractIfConditionAction code unified
Fixes IDEA-152047 Better "Split into 2 if's" result for else-if cases
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
package com.intellij.codeInsight.intention.impl;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightBundle;
|
||||
import com.intellij.codeInsight.PsiEquivalenceUtil;
|
||||
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
@@ -14,18 +13,11 @@ import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.PsiExpressionTrimRenderer;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import com.siyeh.ig.psiutils.ControlFlowUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.intellij.util.ObjectUtils.tryCast;
|
||||
|
||||
public class ExtractIfConditionAction extends PsiElementBaseIntentionAction {
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) {
|
||||
@@ -100,15 +92,8 @@ public class ExtractIfConditionAction extends PsiElementBaseIntentionAction {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return create(
|
||||
factory,
|
||||
ifStatement.getThenBranch(), ifStatement.getElseBranch(),
|
||||
operand,
|
||||
removeOperand(factory, polyadicExpression, operand, tracker),
|
||||
polyadicExpression.getOperationTokenType(),
|
||||
tracker
|
||||
);
|
||||
PsiExpression leave = removeOperand(factory, polyadicExpression, operand, tracker);
|
||||
return SplitConditionUtil.create(factory, ifStatement, operand, leave, polyadicExpression.getOperationTokenType(), tracker);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -128,142 +113,6 @@ public class ExtractIfConditionAction extends PsiElementBaseIntentionAction {
|
||||
return factory.createExpressionFromText(sb.toString(), expression);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiStatement create(@NotNull PsiElementFactory factory,
|
||||
@Nullable PsiStatement thenBranch,
|
||||
@Nullable PsiStatement elseBranch,
|
||||
@NotNull PsiExpression extract,
|
||||
@NotNull PsiExpression leave,
|
||||
@NotNull IElementType operation,
|
||||
CommentTracker tracker) {
|
||||
if (thenBranch == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (operation == JavaTokenType.OROR) {
|
||||
return createOrOr(factory, thenBranch, elseBranch, extract, leave, tracker);
|
||||
}
|
||||
if (operation == JavaTokenType.ANDAND) {
|
||||
return createAndAnd(factory, thenBranch, elseBranch, extract, leave, tracker);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static PsiStatement createAndAnd(@NotNull PsiElementFactory factory,
|
||||
@NotNull PsiStatement thenBranch,
|
||||
@Nullable PsiStatement elseBranch,
|
||||
@NotNull PsiExpression extract,
|
||||
@NotNull PsiExpression leave,
|
||||
CommentTracker tracker) {
|
||||
List<String> elseChain = new ArrayList<>();
|
||||
boolean chainFinished = false;
|
||||
while (!chainFinished) {
|
||||
PsiIfStatement nextIf = tryCast(ControlFlowUtils.stripBraces(elseBranch), PsiIfStatement.class);
|
||||
if (nextIf == null) break;
|
||||
PsiExpression nextCondition = PsiUtil.skipParenthesizedExprDown(nextIf.getCondition());
|
||||
if (nextCondition == null) break;
|
||||
if (PsiEquivalenceUtil.areElementsEquivalent(extract, nextCondition) && nextIf.getThenBranch() != null) {
|
||||
elseChain.add(nextIf.getThenBranch().getText());
|
||||
chainFinished = true;
|
||||
} else {
|
||||
if (!(nextCondition instanceof PsiPolyadicExpression)) break;
|
||||
PsiPolyadicExpression nextPolyadic = (PsiPolyadicExpression)nextCondition;
|
||||
if (!nextPolyadic.getOperationTokenType().equals(JavaTokenType.ANDAND)) break;
|
||||
PsiExpression firstOperand = nextPolyadic.getOperands()[0];
|
||||
if (!PsiEquivalenceUtil.areElementsEquivalent(extract, firstOperand)) break;
|
||||
elseChain.add(
|
||||
createIfString(removeOperand(factory, nextPolyadic, firstOperand, tracker), nextIf.getThenBranch(), (PsiStatement)null, tracker));
|
||||
}
|
||||
elseBranch = nextIf.getElseBranch();
|
||||
}
|
||||
if (!chainFinished && elseBranch != null) {
|
||||
elseChain.add(elseBranch.getText());
|
||||
}
|
||||
String thenString;
|
||||
if (elseChain.isEmpty()) {
|
||||
thenString = createIfString(leave, thenBranch, (String)null, tracker);
|
||||
}
|
||||
else {
|
||||
thenString = "{" + createIfString(leave, thenBranch, String.join(" else ", elseChain), tracker) + "}";
|
||||
}
|
||||
String ifString = createIfString(extract, thenString, elseBranch, tracker);
|
||||
return factory.createStatementFromText(ifString, thenBranch);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static PsiStatement createOrOr(@NotNull PsiElementFactory factory,
|
||||
@NotNull PsiStatement thenBranch,
|
||||
@Nullable PsiStatement elseBranch,
|
||||
@NotNull PsiExpression extract,
|
||||
@NotNull PsiExpression leave, CommentTracker tracker) {
|
||||
|
||||
return factory.createStatementFromText(
|
||||
createIfString(extract, thenBranch,
|
||||
createIfString(leave, thenBranch, elseBranch, tracker),
|
||||
tracker
|
||||
),
|
||||
thenBranch
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String createIfString(@NotNull PsiExpression condition,
|
||||
@NotNull PsiStatement thenBranch,
|
||||
@Nullable PsiStatement elseBranch,
|
||||
CommentTracker tracker) {
|
||||
return createIfString(condition.getText(), toThenBranchString(tracker.markUnchanged(thenBranch)),
|
||||
toElseBranchString(elseBranch != null ? tracker.markUnchanged(elseBranch) : null, false));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String createIfString(@NotNull PsiExpression condition,
|
||||
@NotNull PsiStatement thenBranch,
|
||||
@Nullable String elseBranch,
|
||||
CommentTracker tracker) {
|
||||
return createIfString(tracker.text(condition), toThenBranchString(tracker.markUnchanged(thenBranch)), elseBranch);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String createIfString(@NotNull PsiExpression condition,
|
||||
@NotNull String thenBranch,
|
||||
@Nullable PsiStatement elseBranch,
|
||||
CommentTracker tracker) {
|
||||
return createIfString(tracker.text(condition), thenBranch,
|
||||
toElseBranchString(elseBranch != null ? tracker.markUnchanged(elseBranch) : null, true));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String createIfString(@NotNull String condition,
|
||||
@NotNull String thenBranch,
|
||||
@Nullable String elseBranch) {
|
||||
final String elsePart = elseBranch != null ? "\n else " + elseBranch : "";
|
||||
return "if (" + condition + ")\n" + thenBranch + elsePart;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String toThenBranchString(@NotNull PsiStatement statement) {
|
||||
if (!(statement instanceof PsiBlockStatement)) {
|
||||
return "{ " + statement.getText() + "\n }";
|
||||
}
|
||||
|
||||
return statement.getText();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String toElseBranchString(@Nullable PsiStatement statement, boolean skipElse) {
|
||||
if (statement == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (statement instanceof PsiBlockStatement || skipElse && statement instanceof PsiIfStatement) {
|
||||
return statement.getText();
|
||||
}
|
||||
|
||||
return "{ " + statement.getText() + "\n }";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiExpression findOperand(@NotNull PsiElement e, @NotNull PsiPolyadicExpression expression) {
|
||||
final TextRange elementTextRange = e.getTextRange();
|
||||
|
||||
@@ -15,9 +15,20 @@
|
||||
*/
|
||||
package com.intellij.codeInsight.intention.impl;
|
||||
|
||||
import com.intellij.codeInsight.PsiEquivalenceUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import com.siyeh.ig.psiutils.ControlFlowUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.intellij.util.ObjectUtils.tryCast;
|
||||
|
||||
public class SplitConditionUtil {
|
||||
public static PsiPolyadicExpression findCondition(PsiElement element) {
|
||||
@@ -44,12 +55,17 @@ public class SplitConditionUtil {
|
||||
return expression;
|
||||
}
|
||||
|
||||
public static PsiExpression getROperands(PsiPolyadicExpression expression, PsiJavaToken separator) throws IncorrectOperationException {
|
||||
public static PsiExpression getROperands(PsiPolyadicExpression expression, PsiJavaToken separator) {
|
||||
return getROperands(expression, separator, new CommentTracker());
|
||||
}
|
||||
|
||||
public static PsiExpression getROperands(PsiPolyadicExpression expression, PsiJavaToken separator, CommentTracker ct) {
|
||||
PsiElement next = PsiTreeUtil.skipWhitespacesAndCommentsForward(separator);
|
||||
final int offsetInParent;
|
||||
if (next == null) {
|
||||
offsetInParent = separator.getStartOffsetInParent() + separator.getTextLength();
|
||||
} else {
|
||||
ct.markRangeUnchanged(next, expression.getLastChild());
|
||||
offsetInParent = next.getStartOffsetInParent();
|
||||
}
|
||||
|
||||
@@ -58,15 +74,167 @@ public class SplitConditionUtil {
|
||||
return factory.createExpressionFromText(rOperands, expression.getParent());
|
||||
}
|
||||
|
||||
public static PsiExpression getLOperands(PsiPolyadicExpression expression, PsiJavaToken separator) throws IncorrectOperationException {
|
||||
public static PsiExpression getLOperands(PsiPolyadicExpression expression, PsiJavaToken separator) {
|
||||
return getLOperands(expression, separator, new CommentTracker());
|
||||
}
|
||||
|
||||
public static PsiExpression getLOperands(PsiPolyadicExpression expression, PsiJavaToken separator, CommentTracker ct) {
|
||||
PsiElement prev = separator;
|
||||
if (prev.getPrevSibling() instanceof PsiWhiteSpace) prev = prev.getPrevSibling();
|
||||
if (prev == null) {
|
||||
throw new IncorrectOperationException("Unable to split '"+expression.getText()+"' left to '"+separator+"' (offset "+separator.getStartOffsetInParent()+")");
|
||||
}
|
||||
ct.markRangeUnchanged(expression.getFirstChild(), prev.getPrevSibling());
|
||||
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(expression.getProject()).getElementFactory();
|
||||
String rOperands = expression.getText().substring(0, prev.getStartOffsetInParent());
|
||||
return factory.createExpressionFromText(rOperands, expression.getParent());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
static PsiIfStatement create(@NotNull PsiElementFactory factory,
|
||||
@NotNull PsiIfStatement ifStatement,
|
||||
@NotNull PsiExpression extract,
|
||||
@NotNull PsiExpression leave,
|
||||
@NotNull IElementType operation,
|
||||
CommentTracker tracker) {
|
||||
PsiStatement thenBranch = ifStatement.getThenBranch();
|
||||
if (thenBranch == null) {
|
||||
return null;
|
||||
}
|
||||
PsiStatement elseBranch = ifStatement.getElseBranch();
|
||||
|
||||
if (operation == JavaTokenType.OROR) {
|
||||
return createOrOr(factory, thenBranch, elseBranch, extract, leave, tracker);
|
||||
}
|
||||
if (operation == JavaTokenType.ANDAND) {
|
||||
return createAndAnd(factory, thenBranch, elseBranch, extract, leave, tracker);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static PsiIfStatement createAndAnd(@NotNull PsiElementFactory factory,
|
||||
@NotNull PsiStatement thenBranch,
|
||||
@Nullable PsiStatement elseBranch,
|
||||
@NotNull PsiExpression extract,
|
||||
@NotNull PsiExpression leave,
|
||||
CommentTracker tracker) {
|
||||
List<String> elseChain = new ArrayList<>();
|
||||
boolean chainFinished = false;
|
||||
while (!chainFinished) {
|
||||
PsiIfStatement nextIf = tryCast(ControlFlowUtils.stripBraces(elseBranch), PsiIfStatement.class);
|
||||
if (nextIf == null) break;
|
||||
PsiExpression nextCondition = PsiUtil.skipParenthesizedExprDown(nextIf.getCondition());
|
||||
if (nextCondition == null) break;
|
||||
if (PsiEquivalenceUtil.areElementsEquivalent(extract, nextCondition) && nextIf.getThenBranch() != null) {
|
||||
elseChain.add(nextIf.getThenBranch().getText());
|
||||
chainFinished = true;
|
||||
}
|
||||
else {
|
||||
if (!(nextCondition instanceof PsiPolyadicExpression)) break;
|
||||
PsiPolyadicExpression nextPolyadic = (PsiPolyadicExpression)nextCondition;
|
||||
if (!nextPolyadic.getOperationTokenType().equals(JavaTokenType.ANDAND)) break;
|
||||
PsiExpression[] nextOperands = nextPolyadic.getOperands();
|
||||
PsiExpression[] operands;
|
||||
if (extract instanceof PsiPolyadicExpression &&
|
||||
((PsiPolyadicExpression)extract).getOperationTokenType().equals(JavaTokenType.ANDAND)) {
|
||||
operands = ((PsiPolyadicExpression)extract).getOperands();
|
||||
}
|
||||
else {
|
||||
operands = new PsiExpression[]{extract};
|
||||
}
|
||||
if (nextOperands.length <= operands.length) break;
|
||||
for (int i = 0; i < operands.length; i++) {
|
||||
if (!PsiEquivalenceUtil.areElementsEquivalent(nextOperands[i], operands[i])) break;
|
||||
}
|
||||
PsiExpression nextExtracted =
|
||||
getROperands(nextPolyadic, nextPolyadic.getTokenBeforeOperand(nextOperands[operands.length]), tracker);
|
||||
elseChain.add(createIfString(nextExtracted, nextIf.getThenBranch(), (PsiStatement)null, tracker));
|
||||
}
|
||||
elseBranch = nextIf.getElseBranch();
|
||||
}
|
||||
if (!chainFinished && elseBranch != null) {
|
||||
elseChain.add(elseBranch.getText());
|
||||
}
|
||||
String thenString;
|
||||
if (elseChain.isEmpty()) {
|
||||
thenString = createIfString(leave, thenBranch, (String)null, tracker);
|
||||
}
|
||||
else {
|
||||
thenString = "{" + createIfString(leave, thenBranch, String.join(" else ", elseChain), tracker) + "}";
|
||||
}
|
||||
String ifString = createIfString(extract, thenString, elseBranch, tracker);
|
||||
return (PsiIfStatement)factory.createStatementFromText(ifString, thenBranch);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static PsiIfStatement createOrOr(@NotNull PsiElementFactory factory,
|
||||
@NotNull PsiStatement thenBranch,
|
||||
@Nullable PsiStatement elseBranch,
|
||||
@NotNull PsiExpression extract,
|
||||
@NotNull PsiExpression leave,
|
||||
CommentTracker tracker) {
|
||||
return (PsiIfStatement)factory.createStatementFromText(
|
||||
createIfString(extract, thenBranch, createIfString(leave, thenBranch, elseBranch, tracker), tracker), thenBranch);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String createIfString(@NotNull PsiExpression condition,
|
||||
@NotNull PsiStatement thenBranch,
|
||||
@Nullable PsiStatement elseBranch,
|
||||
CommentTracker tracker) {
|
||||
PsiExpression stripped = PsiUtil.skipParenthesizedExprDown(condition);
|
||||
return createIfString(tracker.text(stripped == null ? condition : stripped),
|
||||
toThenBranchString(tracker.markUnchanged(thenBranch)),
|
||||
toElseBranchString(elseBranch != null ? tracker.markUnchanged(elseBranch) : null, false));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String createIfString(@NotNull PsiExpression condition,
|
||||
@NotNull PsiStatement thenBranch,
|
||||
@Nullable String elseBranch,
|
||||
CommentTracker tracker) {
|
||||
PsiExpression stripped = PsiUtil.skipParenthesizedExprDown(condition);
|
||||
return createIfString(tracker.text(stripped == null ? condition : stripped),
|
||||
toThenBranchString(tracker.markUnchanged(thenBranch)), elseBranch);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String createIfString(@NotNull PsiExpression condition,
|
||||
@NotNull String thenBranch,
|
||||
@Nullable PsiStatement elseBranch,
|
||||
CommentTracker tracker) {
|
||||
PsiExpression stripped = PsiUtil.skipParenthesizedExprDown(condition);
|
||||
return createIfString(tracker.text(stripped == null ? condition : stripped),
|
||||
thenBranch, toElseBranchString(elseBranch != null ? tracker.markUnchanged(elseBranch) : null, true));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String createIfString(@NotNull String condition,
|
||||
@NotNull String thenBranch,
|
||||
@Nullable String elseBranch) {
|
||||
final String elsePart = elseBranch != null ? "\n else " + elseBranch : "";
|
||||
return "if (" + condition + ")\n" + thenBranch + elsePart;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String toThenBranchString(@NotNull PsiStatement statement) {
|
||||
if (!(statement instanceof PsiBlockStatement)) {
|
||||
return "{ " + statement.getText() + "\n }";
|
||||
}
|
||||
|
||||
return statement.getText();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String toElseBranchString(@Nullable PsiStatement statement, boolean skipElse) {
|
||||
if (statement == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (statement instanceof PsiBlockStatement || skipElse && statement instanceof PsiIfStatement) {
|
||||
return statement.getText();
|
||||
}
|
||||
|
||||
return "{ " + statement.getText() + "\n }";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,8 +25,8 @@ import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.refactoring.util.RefactoringUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import static com.intellij.codeInsight.intention.impl.SplitConditionUtil.getLOperands;
|
||||
@@ -68,64 +68,30 @@ public class SplitIfAction extends PsiElementBaseIntentionAction {
|
||||
|
||||
PsiPolyadicExpression expression = (PsiPolyadicExpression)token.getParent();
|
||||
PsiIfStatement ifStatement = PsiTreeUtil.getParentOfType(expression, PsiIfStatement.class);
|
||||
if (ifStatement == null) return;
|
||||
|
||||
LOG.assertTrue(PsiTreeUtil.isAncestor(ifStatement.getCondition(), expression, false));
|
||||
PsiExpression condition = ifStatement.getCondition();
|
||||
LOG.assertTrue(PsiTreeUtil.isAncestor(condition, expression, false));
|
||||
|
||||
if (token.getTokenType() == JavaTokenType.ANDAND) {
|
||||
doAndSplit(ifStatement, expression, token, editor);
|
||||
CommentTracker ct = new CommentTracker();
|
||||
PsiExpression lOperand = getLOperands(expression, token, ct);
|
||||
PsiExpression rOperand = getROperands(expression, token, ct);
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(expression.getProject()).getElementFactory();
|
||||
|
||||
PsiIfStatement replacement =
|
||||
SplitConditionUtil.create(factory, ifStatement, lOperand, rOperand, token.getTokenType(), ct);
|
||||
if (replacement == null) return;
|
||||
PsiElement result = ct.replaceAndRestoreComments(ifStatement, replacement);
|
||||
result = CodeStyleManager.getInstance(expression.getProject()).reformat(result);
|
||||
if (result instanceof PsiIfStatement) {
|
||||
PsiExpression resultCondition = ((PsiIfStatement)result).getCondition();
|
||||
if (resultCondition != null) {
|
||||
int offset = resultCondition.getTextOffset();
|
||||
|
||||
editor.getCaretModel().moveToOffset(offset);
|
||||
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
|
||||
editor.getSelectionModel().removeSelection();
|
||||
}
|
||||
}
|
||||
else if (token.getTokenType() == JavaTokenType.OROR) {
|
||||
doOrSplit(ifStatement, expression, token, editor);
|
||||
}
|
||||
}
|
||||
|
||||
private static void doAndSplit(PsiIfStatement ifStatement, PsiPolyadicExpression expression, PsiJavaToken token, Editor editor) throws IncorrectOperationException {
|
||||
PsiExpression lOperand = getLOperands(expression, token);
|
||||
PsiExpression rOperand = getROperands(expression, token);
|
||||
|
||||
PsiManager psiManager = ifStatement.getManager();
|
||||
PsiIfStatement subIf = (PsiIfStatement)ifStatement.copy();
|
||||
|
||||
subIf.getCondition().replace(RefactoringUtil.unparenthesizeExpression(rOperand));
|
||||
ifStatement.getCondition().replace(RefactoringUtil.unparenthesizeExpression(lOperand));
|
||||
|
||||
if (ifStatement.getThenBranch() instanceof PsiBlockStatement) {
|
||||
PsiBlockStatement blockStmt =
|
||||
(PsiBlockStatement)JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createStatementFromText("{}", null);
|
||||
blockStmt = (PsiBlockStatement)CodeStyleManager.getInstance(psiManager.getProject()).reformat(blockStmt);
|
||||
blockStmt = (PsiBlockStatement)ifStatement.getThenBranch().replace(blockStmt);
|
||||
blockStmt.getCodeBlock().add(subIf);
|
||||
}
|
||||
else {
|
||||
ifStatement.getThenBranch().replace(subIf);
|
||||
}
|
||||
|
||||
int offset1 = ifStatement.getCondition().getTextOffset();
|
||||
|
||||
editor.getCaretModel().moveToOffset(offset1);
|
||||
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
|
||||
editor.getSelectionModel().removeSelection();
|
||||
}
|
||||
|
||||
private static void doOrSplit(PsiIfStatement ifStatement, PsiPolyadicExpression expression, PsiJavaToken token, Editor editor) throws IncorrectOperationException {
|
||||
PsiExpression lOperand = getLOperands(expression, token);
|
||||
PsiExpression rOperand = getROperands(expression, token);
|
||||
|
||||
PsiIfStatement secondIf = (PsiIfStatement)ifStatement.copy();
|
||||
|
||||
PsiStatement elseBranch = ifStatement.getElseBranch();
|
||||
if (elseBranch != null) { elseBranch = (PsiStatement)elseBranch.copy(); }
|
||||
|
||||
ifStatement.getCondition().replace(RefactoringUtil.unparenthesizeExpression(lOperand));
|
||||
secondIf.getCondition().replace(RefactoringUtil.unparenthesizeExpression(rOperand));
|
||||
|
||||
ifStatement.setElseBranch(secondIf);
|
||||
if (elseBranch != null) { secondIf.setElseBranch(elseBranch); }
|
||||
|
||||
int offset1 = ifStatement.getCondition().getTextOffset();
|
||||
|
||||
editor.getCaretModel().moveToOffset(offset1);
|
||||
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
|
||||
editor.getSelectionModel().removeSelection();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
class C {
|
||||
void foo(boolean a, boolean b, boolean c) {
|
||||
if (a) {
|
||||
if (b) {
|
||||
System.out.println("ab");
|
||||
} else if (c) {
|
||||
System.out.println("ac");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
class C {
|
||||
void foo(boolean a, boolean b, boolean c, boolean d, boolean e) {
|
||||
/*c3*/
|
||||
/*c4*/
|
||||
if (a &&/*c1*/ b) {
|
||||
if (c &&/*c2*/ d) {
|
||||
System.out.println("abcd");
|
||||
} else if (e) {
|
||||
System.out.println("abe");
|
||||
} else {
|
||||
System.out.println("ab");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
class C {
|
||||
void foo(boolean a, boolean b, boolean c, boolean d, boolean e) {
|
||||
/*c3*/
|
||||
if (a &&/*c1*/ b) {
|
||||
if (c &&/*c2*/ d) {
|
||||
System.out.println("abcd");
|
||||
} else if (e) {
|
||||
System.out.println("abe");
|
||||
} else if (a && /*c4*/ c) {
|
||||
System.out.println("ac");
|
||||
}
|
||||
} else if (a && /*c4*/ c) {
|
||||
System.out.println("ac");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
class C {
|
||||
void foo() {
|
||||
//comment
|
||||
if (a) {
|
||||
call();
|
||||
} else if (b) {
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
class C {
|
||||
void foo() {
|
||||
//comment
|
||||
if (a /*inside*/ || c) {
|
||||
call();
|
||||
} else if (b) {
|
||||
call();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
public class SplitCondition {
|
||||
private static void appendString(StringBuilder builder, boolean condition) {
|
||||
if (condition) {
|
||||
if (builder.length() > 0) {
|
||||
}
|
||||
}
|
||||
if (condition) {
|
||||
if (builder.length() > 0) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
class C {
|
||||
void foo(boolean a, boolean b, boolean c) {
|
||||
if (a &<caret>& b) {
|
||||
System.out.println("ab");
|
||||
} else if(a && c) {
|
||||
System.out.println("ac");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
class C {
|
||||
void foo(boolean a, boolean b, boolean c, boolean d, boolean e) {
|
||||
if (a &&/*c1*/ b &<caret>& c &&/*c2*/ d) {
|
||||
System.out.println("abcd");
|
||||
} else if(a /*c3*/ && b && e) {
|
||||
System.out.println("abe");
|
||||
} else if(a && /*c4*/ b) {
|
||||
System.out.println("ab");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
class C {
|
||||
void foo(boolean a, boolean b, boolean c, boolean d, boolean e) {
|
||||
if (a &&/*c1*/ b &<caret>& c &&/*c2*/ d) {
|
||||
System.out.println("abcd");
|
||||
} else if(a /*c3*/ && b && e) {
|
||||
System.out.println("abe");
|
||||
} else if(a && /*c4*/ c) {
|
||||
System.out.println("ac");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
class C {
|
||||
void foo() {
|
||||
if (a |<caret>| //comment
|
||||
if (a |<caret>| //comment
|
||||
b) {
|
||||
call();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
class C {
|
||||
void foo() {
|
||||
if (a /*inside*/ || c |<caret>| //comment
|
||||
b) {
|
||||
call();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,10 +15,11 @@
|
||||
*/
|
||||
package com.intellij.java.codeInsight.intention;
|
||||
|
||||
import com.intellij.application.options.CodeStyle;
|
||||
import com.intellij.codeInsight.intention.impl.SplitIfAction;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||
|
||||
/**
|
||||
@@ -26,7 +27,7 @@ import com.intellij.testFramework.LightCodeInsightTestCase;
|
||||
*/
|
||||
public class SplitIfActionTest extends LightCodeInsightTestCase {
|
||||
public void test1() {
|
||||
CodeStyleSettingsManager.getSettings(getProject()).getCommonSettings(JavaLanguage.INSTANCE).ELSE_ON_NEW_LINE= true;
|
||||
CodeStyle.getSettings(getProject()).getCommonSettings(JavaLanguage.INSTANCE).ELSE_ON_NEW_LINE = true;
|
||||
doTest();
|
||||
}
|
||||
|
||||
@@ -58,12 +59,22 @@ public class SplitIfActionTest extends LightCodeInsightTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testCommentInside() { doTest(); }
|
||||
|
||||
public void testChain() { doTest(); }
|
||||
|
||||
public void testChain2() { doTest(); }
|
||||
|
||||
public void testChain3() { doTest(); }
|
||||
|
||||
public void testWithoutSpaces() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
configureByFile("/codeInsight/splitIfAction/before" + getTestName(false)+ ".java");
|
||||
CommonCodeStyleSettings settings = CodeStyle.getSettings(getFile()).getCommonSettings(JavaLanguage.INSTANCE);
|
||||
settings.IF_BRACE_FORCE = CommonCodeStyleSettings.FORCE_BRACES_IF_MULTILINE;
|
||||
perform();
|
||||
checkResultByFile("/codeInsight/splitIfAction/after" + getTestName(false) + ".java");
|
||||
}
|
||||
@@ -75,7 +86,7 @@ public class SplitIfActionTest extends LightCodeInsightTestCase {
|
||||
}
|
||||
|
||||
|
||||
private void perform() {
|
||||
private static void perform() {
|
||||
SplitIfAction action = new SplitIfAction();
|
||||
assertTrue(action.isAvailable(getProject(), getEditor(), getFile()));
|
||||
ApplicationManager.getApplication().runWriteAction(() -> action.invoke(getProject(), getEditor(), getFile()));
|
||||
|
||||
Reference in New Issue
Block a user