// Returns null if standalone closing brace is not found private static BraceStatus checkForMovableClosingBrace( @NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) { PsiElement closingBrace = getStandaloneClosingBrace(file, editor); if (closingBrace == null) return BraceStatus.NOT_FOUND; PsiElement blockLikeElement = closingBrace.getParent(); if (!(blockLikeElement instanceof JetBlockExpression)) return BraceStatus.NOT_MOVABLE; PsiElement blockParent = blockLikeElement.getParent(); if (blockParent instanceof JetWhenEntry) return BraceStatus.NOT_FOUND; if (PsiTreeUtil.instanceOf(blockParent, FUNCTIONLIKE_ELEMENT_CLASSES)) return BraceStatus.NOT_FOUND; PsiElement enclosingExpression = PsiTreeUtil.getParentOfType(blockLikeElement, JetExpression.class); if (enclosingExpression instanceof JetDoWhileExpression) return BraceStatus.NOT_MOVABLE; if (enclosingExpression instanceof JetIfExpression) { JetIfExpression ifExpression = (JetIfExpression) enclosingExpression; if (blockLikeElement == ifExpression.getThen() && ifExpression.getElse() != null) return BraceStatus.NOT_MOVABLE; } return down ? checkForMovableDownClosingBrace(closingBrace, blockLikeElement, editor, info) : checkForMovableUpClosingBrace(closingBrace, blockLikeElement, editor, info); }
@Nullable public static <T extends PsiElement> T getDirectParentOfTypeForBlock( @NotNull JetBlockExpression block, @NotNull Class<T> aClass) { T parent = PsiTreeUtil.getParentOfType(block, aClass); if (parent instanceof JetIfExpression) { JetIfExpression ifExpression = (JetIfExpression) parent; if (ifExpression.getElse() == block || ifExpression.getThen() == block) { return parent; } } if (parent instanceof JetWhenExpression) { JetWhenExpression whenExpression = (JetWhenExpression) parent; for (JetWhenEntry whenEntry : whenExpression.getEntries()) { if (whenEntry.getExpression() == block) { return parent; } } } if (parent instanceof JetFunctionLiteral) { JetFunctionLiteral functionLiteral = (JetFunctionLiteral) parent; if (functionLiteral.getBodyExpression() == block) { return parent; } } if (parent instanceof JetTryExpression) { JetTryExpression tryExpression = (JetTryExpression) parent; if (tryExpression.getTryBlock() == block) { return parent; } for (JetCatchClause clause : tryExpression.getCatchClauses()) { if (clause.getCatchBody() == block) { return parent; } } } return null; }