Search in sources :

Example 11 with PsiJavaToken

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

the class TypeCastSelectioner 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));
    PsiTypeCastExpression expression = (PsiTypeCastExpression) e;
    PsiElement[] children = expression.getChildren();
    PsiElement lParen = null;
    PsiElement rParen = null;
    for (PsiElement child : children) {
        if (child instanceof PsiJavaToken) {
            PsiJavaToken token = (PsiJavaToken) child;
            if (token.getTokenType() == JavaTokenType.LPARENTH)
                lParen = token;
            if (token.getTokenType() == JavaTokenType.RPARENTH)
                rParen = token;
        }
    }
    if (lParen != null && rParen != null) {
        result.addAll(expandToWholeLine(editorText, new TextRange(lParen.getTextRange().getStartOffset(), rParen.getTextRange().getEndOffset()), false));
    }
    return result;
}
Also used : PsiJavaToken(com.intellij.psi.PsiJavaToken) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) PsiTypeCastExpression(com.intellij.psi.PsiTypeCastExpression) PsiElement(com.intellij.psi.PsiElement)

Example 12 with PsiJavaToken

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

the class MissingSwitchBodyFixer method apply.

@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
    if (!(psiElement instanceof PsiSwitchStatement))
        return;
    PsiSwitchStatement switchStatement = (PsiSwitchStatement) psiElement;
    final Document doc = editor.getDocument();
    final PsiCodeBlock body = switchStatement.getBody();
    if (body != null)
        return;
    final PsiJavaToken rParenth = switchStatement.getRParenth();
    assert rParenth != null;
    doc.insertString(rParenth.getTextRange().getEndOffset(), "{}");
}
Also used : PsiCodeBlock(com.intellij.psi.PsiCodeBlock) PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiSwitchStatement(com.intellij.psi.PsiSwitchStatement) Document(com.intellij.openapi.editor.Document)

Example 13 with PsiJavaToken

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

the class ConstantSubexpressionIntention method getTextForElement.

@Override
protected String getTextForElement(PsiElement element) {
    final PsiJavaToken token;
    if (element instanceof PsiJavaToken) {
        token = (PsiJavaToken) element;
    } else {
        final PsiElement prevSibling = element.getPrevSibling();
        if (prevSibling instanceof PsiJavaToken) {
            token = (PsiJavaToken) prevSibling;
        } else {
            throw new AssertionError();
        }
    }
    final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression) element.getParent();
    final PsiPolyadicExpression subexpression = ConstantSubexpressionPredicate.getSubexpression(polyadicExpression, token);
    final String text = HighlightUtil.getPresentableText(subexpression);
    return IntentionPowerPackBundle.message("constant.expression.intention.name", text);
}
Also used : PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression) PsiElement(com.intellij.psi.PsiElement)

Example 14 with PsiJavaToken

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

the class NegateComparisonIntention method getTextForElement.

public String getTextForElement(PsiElement element) {
    String operatorText = "";
    String negatedOperatorText = "";
    final PsiBinaryExpression exp = (PsiBinaryExpression) element;
    if (exp != null) {
        final PsiJavaToken sign = exp.getOperationSign();
        operatorText = sign.getText();
        negatedOperatorText = ComparisonUtils.getNegatedComparison(sign.getTokenType());
    }
    if (operatorText.equals(negatedOperatorText)) {
        return IntentionPowerPackBundle.message("negate.comparison.intention.name", operatorText);
    } else {
        return IntentionPowerPackBundle.message("negate.comparison.intention.name1", operatorText, negatedOperatorText);
    }
}
Also used : PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiBinaryExpression(com.intellij.psi.PsiBinaryExpression)

Example 15 with PsiJavaToken

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

the class ExpressionPredicate method satisfiedBy.

public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof PsiJavaToken)) {
        return false;
    }
    final PsiElement parent = element.getParent();
    if (!(parent instanceof PsiPolyadicExpression)) {
        return false;
    }
    final PsiPolyadicExpression expression = (PsiPolyadicExpression) parent;
    final PsiExpression[] operands = expression.getOperands();
    if (operands.length < 2) {
        return false;
    }
    PsiExpression prevOperand = null;
    for (PsiExpression operand : operands) {
        final PsiJavaToken token = expression.getTokenBeforeOperand(operand);
        if (element == token) {
            if (prevOperand == null || operand.getText().equals(prevOperand.getText())) {
                return false;
            }
            break;
        }
        prevOperand = operand;
    }
    return !ComparisonUtils.isComparison(expression);
}
Also used : PsiJavaToken(com.intellij.psi.PsiJavaToken) PsiExpression(com.intellij.psi.PsiExpression) PsiPolyadicExpression(com.intellij.psi.PsiPolyadicExpression) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiJavaToken (com.intellij.psi.PsiJavaToken)19 PsiElement (com.intellij.psi.PsiElement)12 PsiPolyadicExpression (com.intellij.psi.PsiPolyadicExpression)5 PsiExpression (com.intellij.psi.PsiExpression)4 PsiIfStatement (com.intellij.psi.PsiIfStatement)4 IElementType (com.intellij.psi.tree.IElementType)4 Document (com.intellij.openapi.editor.Document)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiBinaryExpression (com.intellij.psi.PsiBinaryExpression)2 PsiForStatement (com.intellij.psi.PsiForStatement)2 PsiStatement (com.intellij.psi.PsiStatement)2 PsiWhileStatement (com.intellij.psi.PsiWhileStatement)2 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)1 PsiBlockStatement (com.intellij.psi.PsiBlockStatement)1 PsiCodeBlock (com.intellij.psi.PsiCodeBlock)1 PsiDoWhileStatement (com.intellij.psi.PsiDoWhileStatement)1 PsiForeachStatement (com.intellij.psi.PsiForeachStatement)1 PsiSwitchStatement (com.intellij.psi.PsiSwitchStatement)1 PsiTypeCastExpression (com.intellij.psi.PsiTypeCastExpression)1 PsiElementPredicate (com.siyeh.ipp.base.PsiElementPredicate)1