Search in sources :

Example 16 with GrMethodCallExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project android by JetBrains.

the class GradleEditorValueExtractor method visitReferenceExpression.

@Override
public void visitReferenceExpression(GrReferenceExpression expression) {
    if (expression.getParent() instanceof GrMethodCallExpression) {
        // This is a reference expression which points to the method name. We're not interested in it, so, just return.
        return;
    }
    GrExpression qualifier = expression.getQualifierExpression();
    if (qualifier == null) {
        myContext.addCachedVariable(expression.getText(), expression.getTextRange());
        return;
    }
    myContext.rememberVariableQualifier(qualifier.getText());
    PsiElement dotToken = expression.getDotToken();
    if (dotToken == null) {
        return;
    }
    ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.findSingle(GroovyLanguage.INSTANCE);
    for (PsiElement e = dotToken.getNextSibling(); e != null; e = e.getNextSibling()) {
        ASTNode node = e.getNode();
        if (node == null) {
            if (e instanceof PsiWhiteSpace) {
                continue;
            }
            e.accept(myVisitor);
            return;
        }
        IElementType type = node.getElementType();
        if (type == GroovyTokenTypes.mIDENT) {
            myContext.addCachedVariable(e.getText(), e.getTextRange());
        } else if (parserDefinition.getWhitespaceTokens().contains(type) || parserDefinition.getCommentTokens().contains(type)) {
            continue;
        } else {
            e.accept(myVisitor);
        }
        return;
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ParserDefinition(com.intellij.lang.ParserDefinition) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) ASTNode(com.intellij.lang.ASTNode) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 17 with GrMethodCallExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project android by JetBrains.

the class GradleDslParser method getExpressionElement.

@Nullable
private static GradleDslExpression getExpressionElement(@NotNull GradleDslElement parentElement, @NotNull GroovyPsiElement psiElement, @NotNull String propertyName, @NotNull GrExpression propertyExpression) {
    if (propertyExpression instanceof GrLiteral) {
        // ex: compileSdkVersion 23 or compileSdkVersion = "android-23"
        return new GradleDslLiteral(parentElement, psiElement, propertyName, (GrLiteral) propertyExpression);
    }
    if (propertyExpression instanceof GrReferenceExpression) {
        // ex: compileSdkVersion SDK_VERSION or sourceCompatibility = VERSION_1_5
        return new GradleDslReference(parentElement, psiElement, propertyName, (GrReferenceExpression) propertyExpression);
    }
    if (propertyExpression instanceof GrMethodCallExpression) {
        // ex: compile project("someProject")
        GrMethodCallExpression methodCall = (GrMethodCallExpression) propertyExpression;
        GrReferenceExpression callReferenceExpression = getChildOfType(methodCall, GrReferenceExpression.class);
        if (callReferenceExpression != null) {
            String referenceName = callReferenceExpression.getText();
            if (!isEmpty(referenceName)) {
                GrArgumentList argumentList = methodCall.getArgumentList();
                if (argumentList.getAllArguments().length > 0) {
                    return getMethodCall(parentElement, methodCall, referenceName, argumentList);
                } else {
                    return new GradleDslMethodCall(parentElement, methodCall, referenceName);
                }
            }
        }
    }
    if (propertyExpression instanceof GrNewExpression) {
        GrNewExpression newExpression = (GrNewExpression) propertyExpression;
        GrCodeReferenceElement referenceElement = newExpression.getReferenceElement();
        if (referenceElement != null) {
            String objectName = referenceElement.getText();
            if (!isEmpty(objectName)) {
                GrArgumentList argumentList = newExpression.getArgumentList();
                if (argumentList != null) {
                    if (argumentList.getAllArguments().length > 0) {
                        return getNewExpression(parentElement, newExpression, objectName, argumentList);
                    }
                }
            }
        }
    }
    return null;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with GrMethodCallExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project android by JetBrains.

the class GradleDslMethodCall method create.

@Override
@Nullable
public GroovyPsiElement create() {
    GroovyPsiElement psiElement = getPsiElement();
    if (psiElement != null) {
        return psiElement;
    }
    if (myParent == null) {
        return null;
    }
    GroovyPsiElement parentPsiElement = myParent.create();
    if (parentPsiElement == null) {
        return null;
    }
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentPsiElement.getProject());
    String statementText = (myStatementName != null ? myStatementName + " " : "") + myName + "()";
    GrStatement statement = factory.createStatementFromText(statementText);
    PsiElement addedElement = parentPsiElement.addBefore(statement, parentPsiElement.getLastChild());
    if (addedElement instanceof GrApplicationStatement) {
        GrExpression[] expressionArguments = ((GrApplicationStatement) addedElement).getArgumentList().getExpressionArguments();
        if (expressionArguments.length == 1 && expressionArguments[0] instanceof GrMethodCallExpression) {
            setPsiElement(expressionArguments[0]);
            return getPsiElement();
        }
    }
    if (addedElement instanceof GrMethodCallExpression) {
        setPsiElement((GrMethodCallExpression) addedElement);
        return getPsiElement();
    }
    return null;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with GrMethodCallExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.

the class ChangeToOperatorInspection method buildVisitor.

@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitMethodCallExpression(@NotNull GrMethodCallExpression methodCall) {
            final String methodName = getMethodName(methodCall);
            if (methodName == null)
                return;
            Transformation transformation = TRANSFORMATIONS.get(methodName);
            if (transformation == null)
                return;
            PsiElement highlightElement = getHighlightElement(methodCall);
            if (highlightElement == null)
                return;
            if (transformation.couldApply(methodCall, getOptions())) {
                registerError(highlightElement, message("replace.with.operator.message", methodName), new LocalQuickFix[] { getFix(transformation, methodName) }, GENERIC_ERROR_OR_WARNING);
            }
        }
    };
}
Also used : Transformation(org.jetbrains.plugins.groovy.codeInspection.changeToOperator.transformations.Transformation) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with GrMethodCallExpression

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression in project intellij-community by JetBrains.

the class GroovyNameSuggestionUtil method generateNameByExpr.

private static void generateNameByExpr(GrExpression expr, Set<String> possibleNames, NameValidator validator, boolean forStaticVariable) {
    if (expr instanceof GrReferenceExpression && ((GrReferenceExpression) expr).getReferenceName() != null) {
        if (PsiUtil.isThisReference(expr)) {
            possibleNames.add(validator.validateName("thisInstance", true));
        }
        if (PsiUtil.isSuperReference(expr)) {
            possibleNames.add(validator.validateName("superInstance", true));
        }
        GrReferenceExpression refExpr = (GrReferenceExpression) expr;
        String name = refExpr.getReferenceName();
        if (name != null && name.toUpperCase().equals(name)) {
            possibleNames.add(validator.validateName(name.toLowerCase(), true));
        } else {
            generateCamelNames(possibleNames, validator, name);
        }
        if (expr.getText().equals(name)) {
            possibleNames.remove(name);
        }
    }
    if (expr instanceof GrMethodCallExpression) {
        generateNameByExpr(((GrMethodCallExpression) expr).getInvokedExpression(), possibleNames, validator, forStaticVariable);
    }
    if (expr instanceof GrLiteral) {
        final Object value = ((GrLiteral) expr).getValue();
        if (value instanceof String) {
            generateNameByString(possibleNames, (String) value, validator, forStaticVariable, expr.getProject());
        }
    }
}
Also used : GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)48 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)22 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)17 PsiElement (com.intellij.psi.PsiElement)16 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)14 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)11 Nullable (org.jetbrains.annotations.Nullable)8 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)8 NotNull (org.jetbrains.annotations.NotNull)5 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)5 TextRange (com.intellij.openapi.util.TextRange)4 PsiType (com.intellij.psi.PsiType)4 IElementType (com.intellij.psi.tree.IElementType)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 NonNls (org.jetbrains.annotations.NonNls)4 ASTNode (com.intellij.lang.ASTNode)3 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)3 GrCommandArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList)3