Search in sources :

Example 91 with IElementType

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

the class ExpressionGenerator method visitBinaryExpression.

@Override
public void visitBinaryExpression(@NotNull GrBinaryExpression expression) {
    final GrExpression left = expression.getLeftOperand();
    GrExpression right = expression.getRightOperand();
    final PsiType ltype = left.getType();
    final PsiElement token = expression.getOperationToken();
    final IElementType op = expression.getOperationTokenType();
    if (op == GroovyTokenTypes.mREGEX_FIND) {
        builder.append(GroovyCommonClassNames.JAVA_UTIL_REGEX_PATTERN).append(".compile(");
        if (right != null) {
            right.accept(this);
        }
        builder.append(").matcher(");
        left.accept(this);
        builder.append(')');
        return;
    }
    if (op == GroovyTokenTypes.mREGEX_MATCH) {
        builder.append(GroovyCommonClassNames.JAVA_UTIL_REGEX_PATTERN).append(".matches(");
        if (right != null) {
            right.accept(this);
        }
        builder.append(", ");
        left.accept(this);
        builder.append(')');
        return;
    }
    if ((op == GroovyTokenTypes.mEQUAL || op == GroovyTokenTypes.mNOT_EQUAL) && (GrInspectionUtil.isNull(left) || right != null && GrInspectionUtil.isNull(right))) {
        writeSimpleBinaryExpression(token, left, right);
        return;
    }
    if (op == GroovyTokenTypes.kIN && right instanceof GrReferenceExpression && InheritanceUtil.isInheritor(right.getType(), CommonClassNames.JAVA_LANG_CLASS)) {
        final PsiType type = com.intellij.psi.util.PsiUtil.substituteTypeParameter(right.getType(), CommonClassNames.JAVA_LANG_CLASS, 0, true);
        writeInstanceof(left, type, expression);
        return;
    }
    if (shouldNotReplaceOperatorWithMethod(ltype, right, op)) {
        writeSimpleBinaryExpression(token, left, right);
        return;
    }
    final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(expression.multiResolve(false));
    final PsiElement resolved = resolveResult.getElement();
    if (resolved instanceof PsiMethod) {
        if (right == null) {
            right = factory.createExpressionFromText("null");
        }
        if (op == GroovyTokenTypes.mNOT_EQUAL && "equals".equals(((PsiMethod) resolved).getName())) {
            builder.append('!');
        }
        invokeMethodOn(((PsiMethod) resolved), left, new GrExpression[] { right }, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), expression);
        if (op == GroovyTokenTypes.mGE) {
            builder.append(" >= 0");
        } else if (op == GroovyTokenTypes.mGT) {
            builder.append(" > 0");
        } else if (op == GroovyTokenTypes.mLT) {
            builder.append(" < 0");
        } else if (op == GroovyTokenTypes.mLE)
            builder.append(" <= 0");
    } else {
        writeSimpleBinaryExpression(token, left, right);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 92 with IElementType

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

the class ExpressionGenerator method getRValue.

/**
   * returns rValue         for lValue =  expr
   * lValue+Rvalue  for lValue += rValue
   */
@Nullable
private GrExpression getRValue(GrAssignmentExpression expression) {
    GrExpression rValue = expression.getRValue();
    if (rValue == null)
        return null;
    GrExpression lValue = expression.getLValue();
    IElementType opToken = expression.getOperationTokenType();
    if (opToken == GroovyTokenTypes.mASSIGN)
        return rValue;
    Pair<String, IElementType> pair = GenerationUtil.getBinaryOperatorType(opToken);
    LOG.assertTrue(pair != null);
    return factory.createExpressionFromText(lValue.getText() + pair.getFirst() + rValue.getText(), expression);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) Nullable(org.jetbrains.annotations.Nullable)

Example 93 with IElementType

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

the class FormSourceCodeGenerator method lexemsEqual.

private static boolean lexemsEqual(final PsiClass classToBind, final PsiClass newClass) {
    Lexer oldTextLexer = JavaParserDefinition.createLexer(LanguageLevel.HIGHEST);
    Lexer newTextLexer = JavaParserDefinition.createLexer(LanguageLevel.HIGHEST);
    String oldBuffer = classToBind.getText();
    String newBuffer = newClass.getText();
    oldTextLexer.start(oldBuffer);
    newTextLexer.start(newBuffer);
    while (true) {
        IElementType oldLexem = oldTextLexer.getTokenType();
        IElementType newLexem = newTextLexer.getTokenType();
        if (oldLexem == null || newLexem == null) {
            // must terminate at the same time
            return oldLexem == null && newLexem == null;
        }
        if (oldLexem != newLexem) {
            return false;
        }
        if (oldLexem != TokenType.WHITE_SPACE && oldLexem != JavaDocElementType.DOC_COMMENT) {
            int oldStart = oldTextLexer.getTokenStart();
            int newStart = newTextLexer.getTokenStart();
            int oldLength = oldTextLexer.getTokenEnd() - oldTextLexer.getTokenStart();
            int newLength = newTextLexer.getTokenEnd() - newTextLexer.getTokenStart();
            if (oldLength != newLength) {
                return false;
            }
            for (int i = 0; i < oldLength; i++) {
                if (oldBuffer.charAt(oldStart + i) != newBuffer.charAt(newStart + i)) {
                    return false;
                }
            }
        }
        oldTextLexer.advance();
        newTextLexer.advance();
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Lexer(com.intellij.lexer.Lexer)

Example 94 with IElementType

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

the class CommandLineParserUtil method bound_argument.

static void bound_argument(@NotNull final PsiBuilder b, final int i) {
    final IElementType tokenType = b.getTokenType();
    final IElementType leftElement = b.rawLookup(-1);
    final IElementType rightElement = b.rawLookup(1);
    if (leftElement == null || TokenType.WHITE_SPACE.equals(leftElement)) {
        return;
    }
    /**
     * At '=' position: if no whitespace to left and right, we move to argument.
     * And we report error if whitespace to the left.
     */
    if (tokenType == CommandLineElementTypes.EQ) {
        if (leftElement.equals(CommandLineElementTypes.LONG_OPTION_NAME_TOKEN)) {
            if (rightElement == null || TokenType.WHITE_SPACE.equals(rightElement)) {
                b.error("Space between argument is its value is unexpected");
            }
            b.advanceLexer();
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType)

Example 95 with IElementType

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

the class RestLexerTest method doTest.

private static void doTest(String text, String... expected) throws IOException {
    _RestFlexLexer lexer = createLexer(text);
    for (String expectedTokenText : expected) {
        IElementType type = lexer.advance();
        if (type == null) {
            fail("Not enough tokens");
        }
        String tokenText = "[" + lexer.yytext() + ", " + type + "]";
        assertEquals(expectedTokenText, tokenText);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) com.jetbrains.rest.lexer._RestFlexLexer(com.jetbrains.rest.lexer._RestFlexLexer)

Aggregations

IElementType (com.intellij.psi.tree.IElementType)843 ASTNode (com.intellij.lang.ASTNode)127 PsiBuilder (com.intellij.lang.PsiBuilder)121 Nullable (org.jetbrains.annotations.Nullable)100 NotNull (org.jetbrains.annotations.NotNull)78 PsiElement (com.intellij.psi.PsiElement)77 TextRange (com.intellij.openapi.util.TextRange)43 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)26 ArrayList (java.util.ArrayList)22 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)22 Lexer (com.intellij.lexer.Lexer)17 GrBinaryExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)15 NonNls (org.jetbrains.annotations.NonNls)14 Document (com.intellij.openapi.editor.Document)13 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)13 LighterASTNode (com.intellij.lang.LighterASTNode)12 BracePair (com.intellij.lang.BracePair)10 Project (com.intellij.openapi.project.Project)9 PsiFile (com.intellij.psi.PsiFile)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9