Search in sources :

Example 6 with PsiStatement

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

the class JavaIfUnwrapper method doUnwrap.

@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
    PsiStatement then = ((PsiIfStatement) element).getThenBranch();
    context.extractFromBlockOrSingleStatement(then, element);
    context.delete(element);
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiIfStatement(com.intellij.psi.PsiIfStatement)

Example 7 with PsiStatement

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

the class JavaElseUnwrapperBase method doUnwrap.

@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
    PsiStatement elseBranch;
    if (isElseKeyword(element)) {
        elseBranch = ((PsiIfStatement) element.getParent()).getElseBranch();
    } else {
        elseBranch = (PsiStatement) element;
    }
    unwrapElseBranch(elseBranch, element.getParent(), context);
}
Also used : PsiStatement(com.intellij.psi.PsiStatement)

Example 8 with PsiStatement

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

the class BlockBraceFixer method apply.

@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
    if (psiElement instanceof PsiCodeBlock && afterUnmatchedBrace(editor, psiElement.getContainingFile().getFileType())) {
        PsiCodeBlock block = (PsiCodeBlock) psiElement;
        int stopOffset = block.getTextRange().getEndOffset();
        final PsiStatement[] statements = block.getStatements();
        if (statements.length > 0) {
            stopOffset = statements[0].getTextRange().getEndOffset();
        }
        editor.getDocument().insertString(stopOffset, "}");
    }
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiCodeBlock(com.intellij.psi.PsiCodeBlock)

Example 9 with PsiStatement

use of com.intellij.psi.PsiStatement 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 10 with PsiStatement

use of com.intellij.psi.PsiStatement 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)

Aggregations

PsiStatement (com.intellij.psi.PsiStatement)16 PsiElement (com.intellij.psi.PsiElement)9 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)7 PsiExpression (com.intellij.psi.PsiExpression)6 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)5 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)5 PsiField (com.intellij.psi.PsiField)5 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)5 PsiReference (com.intellij.psi.PsiReference)5 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)5 Nullable (com.android.annotations.Nullable)4 PsiIfStatement (com.intellij.psi.PsiIfStatement)4 PsiMethod (com.intellij.psi.PsiMethod)4 PsiParenthesizedExpression (com.intellij.psi.PsiParenthesizedExpression)4 PsiClass (com.intellij.psi.PsiClass)3 PsiConditionalExpression (com.intellij.psi.PsiConditionalExpression)3 PsiType (com.intellij.psi.PsiType)3 ResourceUrl (com.android.ide.common.resources.ResourceUrl)2 PsiArrayType (com.intellij.psi.PsiArrayType)2 PsiCodeBlock (com.intellij.psi.PsiCodeBlock)2