mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-02-05 08:06:56 +07:00
Java: more pattern variables in "invert if"
GitOrigin-RevId: c86f2d885c2a1f2578bc6b8cc7b8fccbdfcc7e32
This commit is contained in:
committed by
intellij-monorepo-bot
parent
e44fb03466
commit
f2b4d42e14
@@ -47,11 +47,11 @@ public final class InvertIfConditionAction extends PsiUpdateModCommandAction<Psi
|
||||
.filter(PsiPrefixExpression.class)
|
||||
.filter(prefixExpr -> PsiUtil.skipParenthesizedExprDown(prefixExpr.getOperand()) == null)
|
||||
.first() != null) return false;
|
||||
if (element instanceof PsiKeyword) {
|
||||
if (element instanceof PsiKeyword keyword) {
|
||||
if (element.getParent() != ifStatement) {
|
||||
return false;
|
||||
}
|
||||
final IElementType tokenType = ((PsiKeyword)element).getTokenType();
|
||||
final IElementType tokenType = keyword.getTokenType();
|
||||
if (tokenType != JavaTokenType.IF_KEYWORD && tokenType != JavaTokenType.ELSE_KEYWORD) {
|
||||
return false;
|
||||
}
|
||||
@@ -141,7 +141,7 @@ public final class InvertIfConditionAction extends PsiUpdateModCommandAction<Psi
|
||||
|
||||
if (emptyBlock(((PsiBlockStatement)elseBranch).getCodeBlock())) {
|
||||
PsiElement parent = ifStatement.getParent();
|
||||
if (parent instanceof PsiIfStatement && ((PsiIfStatement)parent).getElseBranch() != null) {
|
||||
if (parent instanceof PsiIfStatement parentIf && parentIf.getElseBranch() != null) {
|
||||
ifStatement = (PsiIfStatement)wrapWithCodeBlock(ifStatement);
|
||||
}
|
||||
new CommentTracker().deleteAndRestoreComments(Objects.requireNonNull(ifStatement.getElseBranch()));
|
||||
@@ -163,9 +163,9 @@ public final class InvertIfConditionAction extends PsiUpdateModCommandAction<Psi
|
||||
|
||||
private static PsiElement findCodeBlock(PsiIfStatement ifStatement) {
|
||||
PsiElement e = PsiTreeUtil.getParentOfType(ifStatement, PsiMethod.class, PsiClassInitializer.class, PsiLambdaExpression.class);
|
||||
if (e instanceof PsiMethod) return ((PsiMethod) e).getBody();
|
||||
if (e instanceof PsiLambdaExpression) return ((PsiLambdaExpression)e).getBody();
|
||||
if (e instanceof PsiClassInitializer) return ((PsiClassInitializer) e).getBody();
|
||||
if (e instanceof PsiMethod method) return method.getBody();
|
||||
if (e instanceof PsiLambdaExpression lambdaExpression) return lambdaExpression.getBody();
|
||||
if (e instanceof PsiClassInitializer classInitializer) return classInitializer.getBody();
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -237,7 +237,7 @@ public final class InvertIfConditionAction extends PsiUpdateModCommandAction<Psi
|
||||
PsiElement element = flow.getElement(endOffset);
|
||||
while (element != null && !(element instanceof PsiStatement)) element = element.getParent();
|
||||
|
||||
if (element != null && element.getParent() instanceof PsiForStatement && ((PsiForStatement)element.getParent()).getUpdate() == element ||
|
||||
if (element != null && element.getParent() instanceof PsiForStatement forStatement && forStatement.getUpdate() == element ||
|
||||
element instanceof PsiWhileStatement && flow.getStartOffset(element) == endOffset && PsiTreeUtil.isAncestor(element, ifStatement, true) ||
|
||||
element instanceof PsiForeachStatement && flow.getStartOffset(element) + 1 == endOffset) {
|
||||
PsiStatement statement = factory.createStatementFromText("continue;", ifStatement);
|
||||
@@ -317,8 +317,8 @@ public final class InvertIfConditionAction extends PsiUpdateModCommandAction<Psi
|
||||
final PsiLoopStatement loopStmt = PsiTreeUtil.getParentOfType(ifStatement, PsiLoopStatement.class);
|
||||
if (loopStmt != null) {
|
||||
final PsiStatement body = loopStmt.getBody();
|
||||
if (body instanceof PsiBlockStatement) {
|
||||
final PsiStatement[] statements = ((PsiBlockStatement)body).getCodeBlock().getStatements();
|
||||
if (body instanceof PsiBlockStatement blockStatement) {
|
||||
final PsiStatement[] statements = blockStatement.getCodeBlock().getStatements();
|
||||
if (statements.length > 0 && !PsiTreeUtil.isAncestor(statements[statements.length - 1], ifStatement, false) &&
|
||||
ArrayUtilRt.find(statements, ifStatement) < 0) {
|
||||
ifStatement.setElseBranch(ct.markUnchanged(thenBranch));
|
||||
@@ -328,15 +328,15 @@ public final class InvertIfConditionAction extends PsiUpdateModCommandAction<Psi
|
||||
}
|
||||
if ((thenBranch instanceof PsiContinueStatement continueStatement &&
|
||||
(continueStatement.getLabelIdentifier() == null || theSameLabel(continueStatement.getLabelIdentifier(), loopStmt))) ||
|
||||
(thenBranch instanceof PsiReturnStatement && ((PsiReturnStatement)thenBranch).getReturnValue() == null)) {
|
||||
(thenBranch instanceof PsiReturnStatement returnStatement && returnStatement.getReturnValue() == null)) {
|
||||
PsiStatement elseBranch = ifStatement.getElseBranch();
|
||||
if (elseBranch != null) {
|
||||
elseBranch.delete();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (thenBranch instanceof PsiBlockStatement) {
|
||||
PsiStatement[] statements = ((PsiBlockStatement) thenBranch).getCodeBlock().getStatements();
|
||||
if (thenBranch instanceof PsiBlockStatement blockStatement) {
|
||||
PsiStatement[] statements = blockStatement.getCodeBlock().getStatements();
|
||||
if (statements.length > 0 && statements[statements.length - 1] instanceof PsiContinueStatement) {
|
||||
new CommentTracker().deleteAndRestoreComments(statements[statements.length - 1]);
|
||||
}
|
||||
@@ -427,8 +427,8 @@ public final class InvertIfConditionAction extends PsiUpdateModCommandAction<Psi
|
||||
if (endOffset == -1) {
|
||||
endOffset = controlFlow.getSize();
|
||||
}
|
||||
while (endOffset < instructions.size() && instructions.get(endOffset) instanceof GoToInstruction &&
|
||||
!((GoToInstruction) instructions.get(endOffset)).isReturn &&
|
||||
while (endOffset < instructions.size() &&
|
||||
instructions.get(endOffset) instanceof GoToInstruction gotoInstruction && !gotoInstruction.isReturn &&
|
||||
!(controlFlow.getElement(endOffset) instanceof PsiYieldStatement) &&
|
||||
!(controlFlow.getElement(endOffset) instanceof PsiBreakStatement) &&
|
||||
!(controlFlow.getElement(endOffset) instanceof PsiContinueStatement)) {
|
||||
|
||||
Reference in New Issue
Block a user