public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiJavaToken)) { return false; } final PsiJavaToken token = (PsiJavaToken)element; if (token.getTokenType() != JavaTokenType.IF_KEYWORD) { return false; } final PsiElement parent = element.getParent(); if (!(parent instanceof PsiIfStatement)) { return false; } final PsiIfStatement statement = (PsiIfStatement)parent; if (ErrorUtil.containsError(statement)) { return false; } return SwitchUtils.getSwitchExpression(statement, 0) != null; }
@NotNull public PsiElementPredicate getElementPredicate() { return new PsiElementPredicate() { public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiJavaToken)) { return false; } final PsiJavaToken token = (PsiJavaToken)element; final PsiElement parent = token.getParent(); if (!(parent instanceof PsiIfStatement)) { return false; } return TrivialIfInspection.isTrivial((PsiIfStatement)parent); } }; }
@Override public void visitIfStatement(@NotNull PsiIfStatement statement) { super.visitIfStatement(statement); final PsiElement parent = statement.getParent(); if (parent instanceof PsiIfStatement) { final PsiIfStatement parentStatement = (PsiIfStatement)parent; final PsiStatement elseBranch = parentStatement.getElseBranch(); if (statement.equals(elseBranch)) { return; } } final int branchCount = calculateBranchCount(statement); if (branchCount <= m_limit) { return; } registerStatementError(statement, Integer.valueOf(branchCount)); }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiJavaToken)) { return false; } final PsiJavaToken token = (PsiJavaToken)element; if (token.getTokenType() != JavaTokenType.IF_KEYWORD) { return false; } final PsiElement parent = element.getParent(); if (!(parent instanceof PsiIfStatement)) { return false; } final PsiIfStatement statement = (PsiIfStatement)parent; if (ErrorUtil.containsError(statement)) { return false; } return SwitchUtils.getSwitchExpression(statement) != null; }
private static void replaceSimplifiableAssignment( PsiIfStatement statement) throws IncorrectOperationException { final PsiExpression condition = statement.getCondition(); if (condition == null) { return; } final String conditionText = condition.getText(); final PsiStatement thenBranch = statement.getThenBranch(); final PsiExpressionStatement assignmentStatement = (PsiExpressionStatement) ControlFlowUtils.stripBraces(thenBranch); final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)assignmentStatement.getExpression(); final PsiJavaToken operator = assignmentExpression.getOperationSign(); final String operand = operator.getText(); final PsiExpression lhs = assignmentExpression.getLExpression(); final String lhsText = lhs.getText(); replaceStatement(statement, lhsText + operand + conditionText + ';'); }
private static void replaceSimplifiableImplicitReturnNegated( PsiIfStatement statement) throws IncorrectOperationException { final PsiExpression condition = statement.getCondition(); if (condition == null) { return; } final String conditionText = BoolUtils.getNegatedExpressionText(condition); final PsiElement nextStatement = PsiTreeUtil.skipSiblingsForward(statement, PsiWhiteSpace.class); if (nextStatement == null) { return; } @NonNls final String newStatement = "return " + conditionText + ';'; replaceStatement(statement, newStatement); deleteElement(nextStatement); }
private static void replaceSimplifiableAssignmentNegated( PsiIfStatement statement) throws IncorrectOperationException { final PsiExpression condition = statement.getCondition(); if (condition == null) { return; } final String conditionText = BoolUtils.getNegatedExpressionText(condition); final PsiStatement thenBranch = statement.getThenBranch(); final PsiExpressionStatement assignmentStatement = (PsiExpressionStatement) ControlFlowUtils.stripBraces(thenBranch); final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) assignmentStatement.getExpression(); final PsiJavaToken operator = assignmentExpression.getOperationSign(); final String operand = operator.getText(); final PsiExpression lhs = assignmentExpression.getLExpression(); final String lhsText = lhs.getText(); replaceStatement(statement, lhsText + operand + conditionText + ';'); }
public static boolean isSimplifiableImplicitReturn( PsiIfStatement ifStatement) { if (ifStatement.getElseBranch() != null) { return false; } PsiStatement thenBranch = ifStatement.getThenBranch(); thenBranch = ControlFlowUtils.stripBraces(thenBranch); final PsiElement nextStatement = PsiTreeUtil.skipSiblingsForward(ifStatement, PsiWhiteSpace.class); if (!(nextStatement instanceof PsiStatement)) { return false; } final PsiStatement elseBranch = (PsiStatement)nextStatement; return ConditionalUtils.isReturn(thenBranch, PsiKeyword.TRUE) && ConditionalUtils.isReturn(elseBranch, PsiKeyword.FALSE); }
public static boolean isSimplifiableImplicitReturnNegated( PsiIfStatement ifStatement) { if (ifStatement.getElseBranch() != null) { return false; } PsiStatement thenBranch = ifStatement.getThenBranch(); thenBranch = ControlFlowUtils.stripBraces(thenBranch); final PsiElement nextStatement = PsiTreeUtil.skipSiblingsForward(ifStatement, PsiWhiteSpace.class); if (!(nextStatement instanceof PsiStatement)) { return false; } final PsiStatement elseBranch = (PsiStatement)nextStatement; return ConditionalUtils.isReturn(thenBranch, PsiKeyword.FALSE) && ConditionalUtils.isReturn(elseBranch, PsiKeyword.TRUE); }
@Override public List<Class<? extends PsiElement>> getApplicablePsiTypes() { return Arrays.asList( PsiClass.class, PsiMethod.class, PsiIfStatement.class); }
@Override public void collectElementsToIgnore(PsiElement element, Set<PsiElement> result) { PsiElement parent = element.getParent(); while (parent instanceof PsiIfStatement) { result.add(parent); parent = parent.getParent(); } }
@Override protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException { PsiStatement then = ((PsiIfStatement)element).getThenBranch(); context.extractFromBlockOrSingleStatement(then, element); context.delete(element); }
@Override protected void unwrapElseBranch(PsiStatement branch, PsiElement parent, Context context) throws IncorrectOperationException { // if we have 'else if' then we have to extract statements from the 'if' branch if (branch instanceof PsiIfStatement) { branch = ((PsiIfStatement)branch).getThenBranch(); } parent = findTopmostIfStatement(parent); context.extractFromBlockOrSingleStatement(branch, parent); context.delete(parent); }
@Override public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) { List<TextRange> result = new ArrayList<TextRange>(); result.addAll(expandToWholeLine(editorText, e.getTextRange(), false)); PsiIfStatement statement = (PsiIfStatement)e; final PsiKeyword elseKeyword = statement.getElseElement(); if (elseKeyword != null) { final PsiStatement then = statement.getThenBranch(); if (then != null) { final TextRange thenRange = new TextRange(statement.getTextRange().getStartOffset(), then.getTextRange().getEndOffset()); if (thenRange.contains(cursorOffset)) { result.addAll(expandToWholeLine(editorText, thenRange, false)); } } result.addAll(expandToWholeLine(editorText, new TextRange(elseKeyword.getTextRange().getStartOffset(), statement.getTextRange().getEndOffset()), false)); final PsiStatement branch = statement.getElseBranch(); if (branch instanceof PsiIfStatement) { PsiIfStatement elseIf = (PsiIfStatement)branch; final PsiKeyword element = elseIf.getElseElement(); if (element != null) { final PsiStatement elseThen = elseIf.getThenBranch(); if (elseThen != null) { result.addAll(expandToWholeLine(editorText, new TextRange(elseKeyword.getTextRange().getStartOffset(), elseThen.getTextRange().getEndOffset()), false)); } } } } return result; }
@Override public void visitIfStatement(@NotNull PsiIfStatement statement) { super.visitIfStatement(statement); if (ControlFlowUtils.isElseIf(statement)) { return; } final int branchCount = calculateBranchCount(statement); if (branchCount <= m_limit) { return; } registerStatementError(statement, Integer.valueOf(branchCount)); }
private int calculateBranchCount(PsiIfStatement statement) { final PsiStatement branch = statement.getElseBranch(); if (branch == null) { return 1; } if (!(branch instanceof PsiIfStatement)) { return 2; } return 1 + calculateBranchCount((PsiIfStatement)branch); }
public void processIntention(PsiElement element) throws IncorrectOperationException { final PsiJavaToken token = (PsiJavaToken)element; final PsiIfStatement parentStatement = (PsiIfStatement)token.getParent(); if (parentStatement == null) { return; } final PsiStatement elseBranch = parentStatement.getElseBranch(); if (elseBranch == null) { return; } final String newStatement = '{' + elseBranch.getText() + '}'; PsiReplacementUtil.replaceStatement(elseBranch, newStatement); }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiJavaToken)) { return false; } final PsiJavaToken token = (PsiJavaToken)element; final PsiElement parent = token.getParent(); if (!(parent instanceof PsiIfStatement)) { return false; } final PsiIfStatement ifStatement = (PsiIfStatement)parent; if (ErrorUtil.containsError(ifStatement)) { return false; } PsiStatement thenBranch = ifStatement.getThenBranch(); thenBranch = ControlFlowUtils.stripBraces(thenBranch); PsiStatement elseBranch = ifStatement.getElseBranch(); elseBranch = ControlFlowUtils.stripBraces(elseBranch); if (thenBranch == null) { return false; } if (elseBranch != null) { return false; } if (!(thenBranch instanceof PsiIfStatement)) { return false; } final PsiIfStatement childIfStatement = (PsiIfStatement)thenBranch; return childIfStatement.getElseBranch() == null; }
@Override public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) { List<TextRange> result = new ArrayList<TextRange>(); result.addAll(expandToWholeLine(editorText, e.getTextRange(), false)); PsiIfStatement statement = (PsiIfStatement)e; final PsiKeyword elseKeyword = statement.getElseElement(); if (elseKeyword != null) { result.addAll(expandToWholeLine(editorText, new TextRange(elseKeyword.getTextRange().getStartOffset(), statement.getTextRange().getEndOffset()), false)); final PsiStatement branch = statement.getElseBranch(); if (branch instanceof PsiIfStatement) { PsiIfStatement elseIf = (PsiIfStatement)branch; final PsiKeyword element = elseIf.getElseElement(); if (element != null) { result.addAll(expandToWholeLine(editorText, new TextRange(elseKeyword.getTextRange().getStartOffset(), elseIf.getThenBranch().getTextRange().getEndOffset()), false)); } } } return result; }
public void processIntention(PsiElement element) throws IncorrectOperationException { final PsiJavaToken token = (PsiJavaToken)element; final PsiIfStatement parentStatement = (PsiIfStatement)token.getParent(); if (parentStatement == null) { return; } final PsiStatement elseBranch = parentStatement.getElseBranch(); if (elseBranch == null) { return; } final String newStatement = '{' + elseBranch.getText() + '}'; replaceStatement(newStatement, elseBranch); }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiJavaToken)) { return false; } final PsiJavaToken token = (PsiJavaToken)element; final PsiElement parent = token.getParent(); if (!(parent instanceof PsiIfStatement)) { return false; } final PsiIfStatement ifStatement = (PsiIfStatement)parent; if (ErrorUtil.containsError(ifStatement)) { return false; } PsiStatement thenBranch = ifStatement.getThenBranch(); thenBranch = ConditionalUtils.stripBraces(thenBranch); PsiStatement elseBranch = ifStatement.getElseBranch(); elseBranch = ConditionalUtils.stripBraces(elseBranch); if (thenBranch == null) { return false; } if (elseBranch != null) { return false; } if (!(thenBranch instanceof PsiIfStatement)) { return false; } final PsiIfStatement childIfStatement = (PsiIfStatement)thenBranch; return childIfStatement.getElseBranch() == null; }
public DeleteSideEffectsAwareFix(PsiExpressionStatement statement) { myPointer = SmartPointerManager.getInstance(statement.getProject()).createSmartPsiElementPointer(statement); PsiExpression expression = statement.getExpression(); List<PsiExpression> sideEffects = SideEffectChecker.extractSideEffectExpressions(expression); if(sideEffects.isEmpty()) { myMessage = JavaQuickFixBundle.message("delete.element.fix.text"); } else if(sideEffects.size() == 1 && sideEffects.get(0) == PsiUtil.skipParenthesizedExprDown(expression)) { // "Remove unnecessary parentheses" action is already present which will do the same myMessage = ""; } else { PsiStatement[] statements = StatementExtractor.generateStatements(sideEffects, expression); if(statements.length == 1) { if(statements[0] instanceof PsiIfStatement) { myMessage = JavaQuickFixBundle.message("extract.side.effects.convert.to.if"); } else { myMessage = JavaQuickFixBundle.message("extract.side.effects.single"); } } else { myMessage = JavaQuickFixBundle.message("extract.side.effects.multiple"); } } }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if(psiElement instanceof PsiIfStatement) { final Document doc = editor.getDocument(); final PsiIfStatement ifStatement = (PsiIfStatement) psiElement; final PsiJavaToken rParen = ifStatement.getRParenth(); final PsiJavaToken lParen = ifStatement.getLParenth(); final PsiExpression condition = ifStatement.getCondition(); if(condition == null) { if(lParen == null || rParen == null) { int stopOffset = doc.getLineEndOffset(doc.getLineNumber(ifStatement.getTextRange().getStartOffset())); final PsiStatement then = ifStatement.getThenBranch(); if(then != null) { stopOffset = Math.min(stopOffset, then.getTextRange().getStartOffset()); } stopOffset = Math.min(stopOffset, ifStatement.getTextRange().getEndOffset()); doc.replaceString(ifStatement.getTextRange().getStartOffset(), stopOffset, "if ()"); processor.registerUnresolvedError(ifStatement.getTextRange().getStartOffset() + "if (".length()); } else { processor.registerUnresolvedError(lParen.getTextRange().getEndOffset()); } } else if(rParen == null) { doc.insertString(condition.getTextRange().getEndOffset(), ")"); } } }
public static boolean isMergableExplicitIf(PsiElement element) { if (!(element instanceof PsiJavaToken)) { return false; } final PsiJavaToken token = (PsiJavaToken)element; final PsiElement parent = token.getParent(); if (!(parent instanceof PsiIfStatement)) { return false; } final PsiIfStatement ifStatement = (PsiIfStatement)parent; final PsiStatement thenBranch = ifStatement.getThenBranch(); final PsiStatement elseBranch = ifStatement.getElseBranch(); if (thenBranch == null) { return false; } if (elseBranch == null) { return false; } if (!(elseBranch instanceof PsiIfStatement)) { return false; } if (ErrorUtil.containsError(ifStatement)) { return false; } final PsiIfStatement childIfStatement = (PsiIfStatement)elseBranch; final PsiStatement childThenBranch = childIfStatement.getThenBranch(); return EquivalenceChecker.statementsAreEquivalent(thenBranch, childThenBranch); }
private static boolean isMergableImplicitIf(PsiElement element) { if (!(element instanceof PsiJavaToken)) { return false; } final PsiJavaToken token = (PsiJavaToken)element; final PsiElement parent = token.getParent(); if (!(parent instanceof PsiIfStatement)) { return false; } final PsiIfStatement ifStatement = (PsiIfStatement)parent; final PsiStatement thenBranch = ifStatement.getThenBranch(); if (thenBranch == null) { return false; } final PsiStatement elseBranch = ifStatement.getElseBranch(); if (elseBranch != null) { return false; } if (ControlFlowUtils.statementMayCompleteNormally(thenBranch)) { return false; } final PsiElement nextStatement = PsiTreeUtil.skipSiblingsForward(ifStatement, PsiWhiteSpace.class); if (!(nextStatement instanceof PsiIfStatement)) { return false; } final PsiIfStatement nextIfStatement = (PsiIfStatement)nextStatement; final PsiStatement nextThenBranch = nextIfStatement.getThenBranch(); return EquivalenceChecker.statementsAreEquivalent(thenBranch, nextThenBranch); }
private void checkIfStatementWithoutElseBranch( PsiIfStatement ifStatement) { final PsiStatement thenBranch = ifStatement.getThenBranch(); if (ControlFlowUtils.statementMayCompleteNormally(thenBranch)) { return; } PsiStatement nextStatement = getNextStatement(ifStatement); if (thenBranch instanceof PsiBlockStatement) { final PsiBlockStatement blockStatement = (PsiBlockStatement)thenBranch; final PsiCodeBlock codeBlock = blockStatement.getCodeBlock(); final PsiStatement[] statements = codeBlock.getStatements(); final PsiStatement lastStatement = statements[statements.length - 1]; for (PsiStatement statement : statements) { if (nextStatement == null) { if (statement == lastStatement && statement instanceof PsiReturnStatement) { final PsiReturnStatement returnStatement = (PsiReturnStatement)statement; if (returnStatement.getReturnValue() == null) { registerStatementError(ifStatement); } } return; } else if (!EquivalenceChecker.getCanonicalPsiEquivalence().statementsAreEquivalent( statement, nextStatement)) { return; } nextStatement = getNextStatement(nextStatement); } } else if (!EquivalenceChecker.getCanonicalPsiEquivalence().statementsAreEquivalent( thenBranch, nextStatement)) { return; } registerStatementError(ifStatement); }
@Nullable private static PsiStatement getNextStatement(PsiStatement statement) { PsiStatement nextStatement = PsiTreeUtil.getNextSiblingOfType(statement, PsiStatement.class); while (nextStatement == null) { //noinspection AssignmentToMethodParameter statement = PsiTreeUtil.getParentOfType(statement, PsiStatement.class); if (statement == null) { return null; } if (statement instanceof PsiLoopStatement) { // return in a loop statement is not the same as continuing // looping. return statement; } nextStatement = PsiTreeUtil.getNextSiblingOfType(statement, PsiStatement.class); if (nextStatement == null) { continue; } final PsiElement statementParent = statement.getParent(); if (!(statementParent instanceof PsiIfStatement)) { continue; } // nextStatement should not be the else part of an if statement final PsiElement nextStatementParent = nextStatement.getParent(); if (statementParent.equals(nextStatementParent)) { nextStatement = null; } } return nextStatement; }