Search in sources :

Example 6 with PsiIfStatement

use of com.intellij.psi.PsiIfStatement in project intellij-community by JetBrains.

the class JavaElseUnwrapperBase method collectElementsToIgnore.

@Override
public void collectElementsToIgnore(PsiElement element, Set<PsiElement> result) {
    PsiElement parent = element.getParent();
    while (parent instanceof PsiIfStatement) {
        result.add(parent);
        parent = parent.getParent();
    }
}
Also used : PsiIfStatement(com.intellij.psi.PsiIfStatement) PsiElement(com.intellij.psi.PsiElement)

Example 7 with PsiIfStatement

use of com.intellij.psi.PsiIfStatement in project intellij-community by JetBrains.

the class IfStatementSelectioner method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    List<TextRange> result = new ArrayList<>();
    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;
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiIfStatement(com.intellij.psi.PsiIfStatement) ArrayList(java.util.ArrayList) PsiKeyword(com.intellij.psi.PsiKeyword) TextRange(com.intellij.openapi.util.TextRange)

Example 8 with PsiIfStatement

use of com.intellij.psi.PsiIfStatement in project intellij-community by JetBrains.

the class MergeIfAndPredicate method satisfiedBy.

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;
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiIfStatement(com.intellij.psi.PsiIfStatement) PsiElement(com.intellij.psi.PsiElement)

Example 9 with PsiIfStatement

use of com.intellij.psi.PsiIfStatement in project intellij-community by JetBrains.

the class SplitElseIfIntention method processIntention.

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);
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiIfStatement(com.intellij.psi.PsiIfStatement)

Example 10 with PsiIfStatement

use of com.intellij.psi.PsiIfStatement in project intellij-community by JetBrains.

the class ReplaceIfWithSwitchIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element) {
    final PsiElement parent = element.getParent();
    if (!(parent instanceof PsiIfStatement)) {
        return;
    }
    final PsiIfStatement ifStatement = (PsiIfStatement) parent;
    IfCanBeSwitchInspection.replaceIfWithSwitch(ifStatement);
}
Also used : PsiIfStatement(com.intellij.psi.PsiIfStatement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiIfStatement (com.intellij.psi.PsiIfStatement)13 PsiElement (com.intellij.psi.PsiElement)7 PsiJavaToken (com.intellij.psi.PsiJavaToken)4 PsiStatement (com.intellij.psi.PsiStatement)4 Encapsulator (il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.Encapsulator)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 TextRange (com.intellij.openapi.util.TextRange)1 PsiConditionalExpression (com.intellij.psi.PsiConditionalExpression)1 PsiExpression (com.intellij.psi.PsiExpression)1 PsiFile (com.intellij.psi.PsiFile)1 PsiKeyword (com.intellij.psi.PsiKeyword)1 PsiElementPredicate (com.siyeh.ipp.base.PsiElementPredicate)1 Wrapper (il.org.spartan.Leonidas.auxilary_layer.Wrapper)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1