Replace triple booleans with enums

This commit is contained in:
Alexey Sedunov
2013-05-31 19:19:57 +04:00
parent 5345d0a0d5
commit 2cc163cd90
@@ -54,7 +54,7 @@ public class JetExpressionMover extends AbstractJetUpDownMover {
return file.findElementAt(lineStartOffset + lineText.indexOf('}')); return file.findElementAt(lineStartOffset + lineText.indexOf('}'));
} }
private static boolean checkForMovableDownClosingBrace( private static BraceStatus checkForMovableDownClosingBrace(
@NotNull PsiElement closingBrace, @NotNull PsiElement closingBrace,
@NotNull PsiElement block, @NotNull PsiElement block,
@NotNull Editor editor, @NotNull Editor editor,
@@ -78,7 +78,7 @@ public class JetExpressionMover extends AbstractJetUpDownMover {
} }
while (current != null && !(PsiTreeUtil.instanceOf(current, BLOCKLIKE_ELEMENT_CLASSES))); while (current != null && !(PsiTreeUtil.instanceOf(current, BLOCKLIKE_ELEMENT_CLASSES)));
if (nextExpression == null) return false; if (nextExpression == null) return BraceStatus.NOT_MOVABLE;
Document doc = editor.getDocument(); Document doc = editor.getDocument();
@@ -86,10 +86,10 @@ public class JetExpressionMover extends AbstractJetUpDownMover {
info.toMove2 = new LineRange(nextElement, nextExpression); info.toMove2 = new LineRange(nextElement, nextExpression);
info.indentSource = true; info.indentSource = true;
return true; return BraceStatus.MOVABLE;
} }
private static boolean checkForMovableUpClosingBrace( private static BraceStatus checkForMovableUpClosingBrace(
@NotNull PsiElement closingBrace, @NotNull PsiElement closingBrace,
PsiElement block, PsiElement block,
@NotNull Editor editor, @NotNull Editor editor,
@@ -97,7 +97,7 @@ public class JetExpressionMover extends AbstractJetUpDownMover {
) { ) {
//noinspection unchecked //noinspection unchecked
PsiElement prev = JetPsiUtil.getLastChildByType(block, JetExpression.class); PsiElement prev = JetPsiUtil.getLastChildByType(block, JetExpression.class);
if (prev == null) return false; if (prev == null) return BraceStatus.NOT_MOVABLE;
Document doc = editor.getDocument(); Document doc = editor.getDocument();
@@ -105,35 +105,42 @@ public class JetExpressionMover extends AbstractJetUpDownMover {
info.toMove2 = new LineRange(prev, prev, doc); info.toMove2 = new LineRange(prev, prev, doc);
info.indentSource = true; info.indentSource = true;
return true; return BraceStatus.MOVABLE;
}
private static enum BraceStatus {
NOT_FOUND,
MOVABLE,
NOT_MOVABLE
} }
// Returns null if standalone closing brace is not found // Returns null if standalone closing brace is not found
private static Boolean checkForMovableClosingBrace( private static BraceStatus checkForMovableClosingBrace(
@NotNull Editor editor, @NotNull Editor editor,
@NotNull PsiFile file, @NotNull PsiFile file,
@NotNull MoveInfo info, @NotNull MoveInfo info,
boolean down boolean down
) { ) {
PsiElement closingBrace = getStandaloneClosingBrace(file, editor); PsiElement closingBrace = getStandaloneClosingBrace(file, editor);
if (closingBrace == null) return null; if (closingBrace == null) return BraceStatus.NOT_FOUND;
PsiElement blockLikeElement = closingBrace.getParent(); PsiElement blockLikeElement = closingBrace.getParent();
if (!(blockLikeElement instanceof JetBlockExpression)) return false; if (!(blockLikeElement instanceof JetBlockExpression)) return BraceStatus.NOT_MOVABLE;
if (blockLikeElement.getParent() instanceof JetWhenEntry) return false; if (blockLikeElement.getParent() instanceof JetWhenEntry) return BraceStatus.NOT_MOVABLE;
PsiElement enclosingExpression = PsiTreeUtil.getParentOfType(blockLikeElement, JetExpression.class); PsiElement enclosingExpression = PsiTreeUtil.getParentOfType(blockLikeElement, JetExpression.class);
if (enclosingExpression instanceof JetDoWhileExpression) return false; if (enclosingExpression instanceof JetDoWhileExpression) return BraceStatus.NOT_MOVABLE;
if (enclosingExpression instanceof JetIfExpression) { if (enclosingExpression instanceof JetIfExpression) {
JetIfExpression ifExpression = (JetIfExpression) enclosingExpression; JetIfExpression ifExpression = (JetIfExpression) enclosingExpression;
if (blockLikeElement == ifExpression.getThen() && ifExpression.getElse() != null) return false; if (blockLikeElement == ifExpression.getThen() && ifExpression.getElse() != null) return BraceStatus.NOT_MOVABLE;
} }
return down ? checkForMovableDownClosingBrace(closingBrace, blockLikeElement, editor, info) : return down
checkForMovableUpClosingBrace(closingBrace, blockLikeElement, editor, info); ? checkForMovableDownClosingBrace(closingBrace, blockLikeElement, editor, info)
: checkForMovableUpClosingBrace(closingBrace, blockLikeElement, editor, info);
} }
@Nullable @Nullable
@@ -296,28 +303,32 @@ public class JetExpressionMover extends AbstractJetUpDownMover {
return PsiTreeUtil.getNonStrictParentOfType(element, MOVABLE_ELEMENT_CLASSES); return PsiTreeUtil.getNonStrictParentOfType(element, MOVABLE_ELEMENT_CLASSES);
} }
// return true to forbid the move private static enum MoveStatus {
// return false to permit the move DEFAULT,
// return null to fall back to default line mover behavior FORBIDDEN,
private static Boolean isForbiddenMove(@NotNull PsiElement element, boolean down) { PERMITTED
}
private static MoveStatus getMoveStatus(@NotNull PsiElement element, boolean down) {
if (element instanceof JetParameter) { if (element instanceof JetParameter) {
PsiElement sibling = getSiblingOfType(element, down, element.getClass()); PsiElement sibling = getSiblingOfType(element, down, element.getClass());
return (sibling != null) ? null : true; return (sibling != null) ? MoveStatus.DEFAULT : MoveStatus.FORBIDDEN;
} }
return false; return MoveStatus.PERMITTED;
} }
@Override @Override
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) { public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
if (!super.checkAvailable(editor, file, info, down)) return false; if (!super.checkAvailable(editor, file, info, down)) return false;
Boolean closingBraceWin = checkForMovableClosingBrace(editor, file, info, down); switch (checkForMovableClosingBrace(editor, file, info, down)) {
if (closingBraceWin != null) { case NOT_MOVABLE: {
if (!closingBraceWin) {
info.toMove2 = null; info.toMove2 = null;
return true;
} }
return true; case MOVABLE: return true;
default: break;
} }
LineRange oldRange = info.toMove; LineRange oldRange = info.toMove;
@@ -331,14 +342,14 @@ public class JetExpressionMover extends AbstractJetUpDownMover {
if (firstElement == null || lastElement == null) return false; if (firstElement == null || lastElement == null) return false;
Boolean forbidFirst = isForbiddenMove(firstElement, down); MoveStatus firstMoveStatus = getMoveStatus(firstElement, down);
Boolean forbidLast = isForbiddenMove(lastElement, down); MoveStatus lastMoveStatus = getMoveStatus(lastElement, down);
if (forbidFirst == null || forbidLast == null) { if (firstMoveStatus == MoveStatus.DEFAULT || lastMoveStatus == MoveStatus.DEFAULT) {
return true; return true;
} }
if (forbidFirst || forbidLast) { if (firstMoveStatus == MoveStatus.FORBIDDEN || lastMoveStatus == MoveStatus.FORBIDDEN) {
info.toMove2 = null; info.toMove2 = null;
return true; return true;
} }