Search in sources :

Example 6 with GrCallExpression

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

the class InvertIfIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    PsiElement parent = element.getParent();
    if (!"if".equals(element.getText()) || !(parent instanceof GrIfStatement)) {
        throw new IncorrectOperationException("Not invoked on an if");
    }
    GrIfStatement parentIf = (GrIfStatement) parent;
    GroovyPsiElementFactory groovyPsiElementFactory = GroovyPsiElementFactory.getInstance(project);
    GrExpression condition = parentIf.getCondition();
    if (condition == null) {
        throw new IncorrectOperationException("Invoked on an if with empty condition");
    }
    GrExpression negatedCondition = null;
    if (condition instanceof GrUnaryExpression) {
        GrUnaryExpression unaryCondition = (GrUnaryExpression) condition;
        if ("!".equals(unaryCondition.getOperationToken().getText())) {
            negatedCondition = stripParenthesis(unaryCondition.getOperand());
        }
    }
    if (negatedCondition == null) {
        // Now check whether this is a simple expression
        condition = stripParenthesis(condition);
        String negatedExpressionText;
        if (condition instanceof GrCallExpression || condition instanceof GrReferenceExpression) {
            negatedExpressionText = "!" + condition.getText();
        } else {
            negatedExpressionText = "!(" + condition.getText() + ")";
        }
        negatedCondition = groovyPsiElementFactory.createExpressionFromText(negatedExpressionText, parentIf);
    }
    GrStatement thenBranch = parentIf.getThenBranch();
    final boolean thenIsNotEmpty = isNotEmpty(thenBranch);
    String newIfText = "if (" + negatedCondition.getText() + ") {}";
    if (thenIsNotEmpty) {
        newIfText += " else {}";
    }
    GrIfStatement newIf = (GrIfStatement) groovyPsiElementFactory.createStatementFromText(newIfText, parentIf.getContext());
    generateElseBranchTextAndRemoveTailStatements(parentIf, newIf);
    if (thenIsNotEmpty) {
        final GrStatement elseBranch = newIf.getElseBranch();
        assert elseBranch != null;
        elseBranch.replaceWithStatement(thenBranch);
    }
    parentIf.replace(newIf);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) GrUnaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 7 with GrCallExpression

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

the class GroovyIntroduceParameterObjectDelegate method createNewParameterInitializerAtCallSite.

@Override
public PsiElement createNewParameterInitializerAtCallSite(PsiElement callExpression, IntroduceParameterObjectClassDescriptor descriptor, List<? extends ParameterInfo> oldMethodParameters, Object substitutor) {
    if (callExpression instanceof GrCallExpression) {
        final GrArgumentList list = ((GrCallExpression) callExpression).getArgumentList();
        if (list == null) {
            return null;
        }
        final GrExpression[] args = list.getExpressionArguments();
        final String qualifiedName = StringUtil.getQualifiedName(descriptor.getPackageName(), descriptor.getClassName());
        String newExpression = "new " + qualifiedName + '(' + JavaIntroduceParameterObjectDelegate.getMergedArgs(descriptor, oldMethodParameters, args) + ')';
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(callExpression.getProject());
        return factory.createExpressionFromText(newExpression, callExpression);
    }
    return null;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 8 with GrCallExpression

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

the class GroovyMethodInliner method getConflicts.

@Override
@Nullable
public MultiMap<PsiElement, String> getConflicts(@NotNull PsiReference reference, @NotNull PsiElement referenced) {
    PsiElement element = reference.getElement();
    if (!(element instanceof GrExpression && element.getParent() instanceof GrCallExpression)) {
        final MultiMap<PsiElement, String> map = new MultiMap<>();
        map.putValue(element, GroovyRefactoringBundle.message("cannot.inline.reference.0", element.getText()));
        return map;
    }
    GrCallExpression call = (GrCallExpression) element.getParent();
    Collection<GroovyInlineMethodUtil.ReferenceExpressionInfo> infos = GroovyInlineMethodUtil.collectReferenceInfo(myMethod);
    return collectConflicts(call, infos);
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with GrCallExpression

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

the class MultipleRepositoryUrlsInspection method findUrlCallExpressions.

@NotNull
static List<GrCallExpression> findUrlCallExpressions(@NotNull GrClosableBlock closure) {
    GrCallExpression[] applicationStatements = PsiTreeUtil.getChildrenOfType(closure, GrCallExpression.class);
    if (applicationStatements == null)
        return Collections.emptyList();
    List<GrCallExpression> statements = ContainerUtil.newArrayList();
    for (GrCallExpression statement : applicationStatements) {
        GrReferenceExpression[] referenceExpressions = PsiTreeUtil.getChildrenOfType(statement, GrReferenceExpression.class);
        if (referenceExpressions == null)
            continue;
        for (GrReferenceExpression expression : referenceExpressions) {
            String expressionText = expression.getText();
            if ("url".equals(expressionText) || "setUrl".equals(expressionText)) {
                statements.add(statement);
            }
        }
    }
    return statements;
}
Also used : GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression)9 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)5 PsiElement (com.intellij.psi.PsiElement)3 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)3 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)3 Project (com.intellij.openapi.project.Project)2 Nullable (org.jetbrains.annotations.Nullable)2 Editor (com.intellij.openapi.editor.Editor)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 TextRange (com.intellij.openapi.util.TextRange)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 MultiMap (com.intellij.util.containers.MultiMap)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)1 GrIfStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement)1 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)1 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)1 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)1