Search in sources :

Example 31 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 32 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)

Example 33 with GrMethodCallExpression

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

the class GroovyRefactoringUtil method verifySafeCopyExpressionSubElement.

private static int verifySafeCopyExpressionSubElement(PsiElement element) {
    int result = RefactoringUtil.EXPR_COPY_SAFE;
    if (element == null)
        return result;
    if (element instanceof GrNamedElement) {
        return RefactoringUtil.EXPR_COPY_SAFE;
    }
    if (element instanceof GrMethodCallExpression) {
        result = RefactoringUtil.EXPR_COPY_UNSAFE;
    }
    if (element instanceof GrNewExpression) {
        return RefactoringUtil.EXPR_COPY_PROHIBITED;
    }
    if (element instanceof GrAssignmentExpression) {
        return RefactoringUtil.EXPR_COPY_PROHIBITED;
    }
    if (element instanceof GrClosableBlock) {
        return RefactoringUtil.EXPR_COPY_PROHIBITED;
    }
    if (isPlusPlusOrMinusMinus(element)) {
        return RefactoringUtil.EXPR_COPY_PROHIBITED;
    }
    PsiElement[] children = element.getChildren();
    for (PsiElement child : children) {
        int childResult = verifySafeCopyExpressionSubElement(child);
        result = Math.max(result, childResult);
    }
    return result;
}
Also used : GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 34 with GrMethodCallExpression

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

the class EachToForIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrMethodCallExpression expression = (GrMethodCallExpression) element;
    final GrClosableBlock block = expression.getClosureArguments()[0];
    final GrParameterList parameterList = block.getParameterList();
    final GrParameter[] parameters = parameterList.getParameters();
    String var;
    if (parameters.length == 1) {
        var = parameters[0].getText();
        var = StringUtil.replace(var, GrModifier.DEF, "");
    } else {
        var = "it";
    }
    final GrExpression invokedExpression = expression.getInvokedExpression();
    GrExpression qualifier = ((GrReferenceExpression) invokedExpression).getQualifierExpression();
    final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(element.getProject());
    if (qualifier == null) {
        qualifier = elementFactory.createExpressionFromText("this");
    }
    StringBuilder builder = new StringBuilder();
    builder.append("for (").append(var).append(" in ").append(qualifier.getText()).append(") {\n");
    String text = block.getText();
    final PsiElement blockArrow = block.getArrow();
    int index;
    if (blockArrow != null) {
        index = blockArrow.getStartOffsetInParent() + blockArrow.getTextLength();
    } else {
        index = 1;
    }
    while (index < text.length() && Character.isWhitespace(text.charAt(index))) index++;
    text = text.substring(index, text.length() - 1);
    builder.append(text);
    builder.append("}");
    final GrStatement statement = elementFactory.createStatementFromText(builder.toString());
    GrForStatement forStatement = (GrForStatement) expression.replaceWithStatement(statement);
    final GrForClause clause = forStatement.getClause();
    GrVariable variable = clause.getDeclaredVariable();
    forStatement = updateReturnStatements(forStatement);
    if (variable == null)
        return;
    if (ApplicationManager.getApplication().isUnitTestMode())
        return;
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    final Document doc = documentManager.getDocument(element.getContainingFile());
    if (doc == null)
        return;
    documentManager.doPostponedOperationsAndUnblockDocument(doc);
    editor.getCaretModel().moveToOffset(variable.getTextOffset());
    new VariableInplaceRenamer(variable, editor).performInplaceRename();
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) Document(com.intellij.openapi.editor.Document) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause) VariableInplaceRenamer(com.intellij.refactoring.rename.inplace.VariableInplaceRenamer) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 35 with GrMethodCallExpression

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

the class ExplicitClosureCallPredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof GrMethodCallExpression)) {
        return false;
    }
    final GrMethodCallExpression call = (GrMethodCallExpression) element;
    final GrExpression invokedExpression = call.getInvokedExpression();
    if (invokedExpression == null) {
        return false;
    }
    if (!(invokedExpression instanceof GrReferenceExpression)) {
        return false;
    }
    final GrReferenceExpression referenceExpression = (GrReferenceExpression) invokedExpression;
    final String name = referenceExpression.getReferenceName();
    if (!"call".equals(name)) {
        return false;
    }
    final GrExpression qualifier = referenceExpression.getQualifierExpression();
    if (qualifier == null) {
        return false;
    }
    final PsiType qualifierType = qualifier.getType();
    if (qualifierType == null) {
        return false;
    }
    if (!qualifierType.equalsToText(GroovyCommonClassNames.GROOVY_LANG_CLOSURE)) {
        return false;
    }
    return !ErrorUtil.containsError(element);
}
Also used : GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiType(com.intellij.psi.PsiType)

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