Search in sources :

Example 1 with GrCallExpression

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

the class GroovyInlineMethodUtil method inlineMethodSettings.

@Nullable
public static InlineHandler.Settings inlineMethodSettings(GrMethod method, Editor editor, boolean invokedOnReference) {
    final Project project = method.getProject();
    if (method.isConstructor()) {
        String message = GroovyRefactoringBundle.message("refactoring.cannot.be.applied.to.constructors", REFACTORING_NAME);
        showErrorMessage(message, project, editor);
        return InlineHandler.Settings.CANNOT_INLINE_SETTINGS;
    }
    if (invokedOnReference) {
        PsiReference reference = editor != null ? TargetElementUtil.findReference(editor, editor.getCaretModel().getOffset()) : null;
        if (reference == null)
            return InlineHandler.Settings.CANNOT_INLINE_SETTINGS;
        PsiElement element = reference.getElement();
        if (!(element instanceof GrExpression && element.getParent() instanceof GrCallExpression)) {
            String message = GroovyRefactoringBundle.message("refactoring.is.available.only.for.method.calls", REFACTORING_NAME);
            showErrorMessage(message, project, editor);
            return InlineHandler.Settings.CANNOT_INLINE_SETTINGS;
        }
        GrCallExpression call = (GrCallExpression) element.getParent();
        if (PsiTreeUtil.getParentOfType(element, GrParameter.class) != null) {
            String message = GroovyRefactoringBundle.message("refactoring.is.not.supported.in.parameter.initializers", REFACTORING_NAME);
            showErrorMessage(message, project, editor);
            return InlineHandler.Settings.CANNOT_INLINE_SETTINGS;
        }
        GroovyRefactoringUtil.highlightOccurrences(project, editor, new GrExpression[] { call });
        if (hasBadReturns(method) && !isTailMethodCall(call)) {
            String message = GroovyRefactoringBundle.message("refactoring.is.not.supported.when.return.statement.interrupts.the.execution.flow", REFACTORING_NAME);
            showErrorMessage(message, project, editor);
            return InlineHandler.Settings.CANNOT_INLINE_SETTINGS;
        }
    } else {
        if (hasBadReturns(method)) {
            String message = GroovyRefactoringBundle.message("refactoring.is.not.supported.when.return.statement.interrupts.the.execution.flow", REFACTORING_NAME);
            showErrorMessage(message, project, editor);
            return InlineHandler.Settings.CANNOT_INLINE_SETTINGS;
        }
    }
    if (method.getBlock() == null) {
        String message = method.hasModifierProperty(PsiModifier.ABSTRACT) ? GroovyRefactoringBundle.message("refactoring.cannot.be.applied.to.abstract.methods", REFACTORING_NAME) : GroovyRefactoringBundle.message("refactoring.cannot.be.applied.no.sources.attached", REFACTORING_NAME);
        showErrorMessage(message, project, editor);
        return InlineHandler.Settings.CANNOT_INLINE_SETTINGS;
    }
    return inlineMethodDialogResult(method, project, invokedOnReference);
}
Also used : Project(com.intellij.openapi.project.Project) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) Nullable(org.jetbrains.annotations.Nullable)

Example 2 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 inlineUsage.

@Override
public void inlineUsage(@NotNull UsageInfo usage, @NotNull PsiElement referenced) {
    PsiElement element = usage.getElement();
    if (!(element instanceof GrExpression && element.getParent() instanceof GrCallExpression))
        return;
    final Editor editor = getCurrentEditorIfApplicable(element);
    GrCallExpression call = (GrCallExpression) element.getParent();
    RangeMarker marker = inlineReferenceImpl(call, myMethod, isOnExpressionOrReturnPlace(call), GroovyInlineMethodUtil.isTailMethodCall(call), editor);
    // highlight replaced result
    if (marker != null) {
        Project project = referenced.getProject();
        TextRange range = TextRange.create(marker);
        GroovyRefactoringUtil.highlightOccurrencesByRanges(project, editor, new TextRange[] { range });
        WindowManager.getInstance().getStatusBar(project).setInfo(GroovyRefactoringBundle.message("press.escape.to.remove.the.highlighting"));
        if (editor != null) {
            editor.getCaretModel().moveToOffset(marker.getEndOffset());
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) Editor(com.intellij.openapi.editor.Editor)

Example 3 with GrCallExpression

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

the class GradleFilePsiMerger method mergePsi.

private static void mergePsi(@NotNull PsiElement fromRoot, @NotNull PsiElement toRoot, @NotNull Project project, @Nullable String supportLibVersionFilter) {
    Set<PsiElement> destinationChildren = new HashSet<>();
    destinationChildren.addAll(Arrays.asList(toRoot.getChildren()));
    // If both toRoot and fromRoot are call expressions
    if (toRoot instanceof GrCallExpression && fromRoot instanceof GrCallExpression) {
        PsiElement[] fromArguments = fromRoot.getLastChild().getChildren();
        PsiElement[] toArguments = toRoot.getLastChild().getChildren();
        // and both have only one argument and that argument is a literal
        if (toArguments.length == 1 && fromArguments.length == 1 && toArguments[0] instanceof GrLiteral && fromArguments[0] instanceof GrLiteral) {
            // End this branch by replacing the old literal with the new
            toArguments[0].replace(fromArguments[0]);
            return;
        }
    }
    // Do an element-wise (disregarding order) child comparison
    for (PsiElement child : fromRoot.getChildren()) {
        PsiElement destination = findEquivalentElement(destinationChildren, child);
        if (destination == null) {
            if (destinationChildren.isEmpty()) {
                toRoot.add(child);
            } else {
                toRoot.addBefore(child, toRoot.getLastChild());
            }
        // And we're done for this branch
        } else if (child.getFirstChild() != null && child.getFirstChild().getText().equalsIgnoreCase(GradleFileMergers.DEPENDENCIES) && destination.getFirstChild() != null && destination.getFirstChild().getText().equalsIgnoreCase(GradleFileMergers.DEPENDENCIES)) {
            // Special case dependencies
            // The last child of the dependencies method call is the closable block
            mergeDependencies(child.getLastChild(), destination.getLastChild(), project, supportLibVersionFilter);
        } else {
            mergePsi(child, destination, project, supportLibVersionFilter);
        }
    }
}
Also used : GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) PsiElement(com.intellij.psi.PsiElement)

Example 4 with GrCallExpression

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

the class MultipleRepositoryUrlsFix method doFix.

@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
    List<GrCallExpression> statements = MultipleRepositoryUrlsInspection.findUrlCallExpressions(myClosure);
    if (statements.size() <= 1)
        return;
    statements.remove(0);
    List<PsiElement> elements = new ArrayList<>(statements);
    for (GrCallExpression statement : statements) {
        PsiElement newLineCandidate = statement.getNextSibling();
        if (PsiUtil.isNewLine(newLineCandidate)) {
            elements.add(newLineCandidate);
        }
    }
    myClosure.removeElements(elements.toArray(new PsiElement[elements.size()]));
    GrClosableBlock closableBlock = PsiTreeUtil.getParentOfType(myClosure, GrClosableBlock.class);
    if (closableBlock == null)
        return;
    GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(project);
    for (GrCallExpression statement : statements) {
        closableBlock.addStatementBefore(elementFactory.createStatementFromText(myRepoType + '{' + statement.getText() + '}'), null);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) ArrayList(java.util.ArrayList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) PsiElement(com.intellij.psi.PsiElement)

Example 5 with GrCallExpression

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

the class ResolveUtil method getCallVariants.

public static GroovyResolveResult[] getCallVariants(GroovyPsiElement place) {
    final PsiElement parent = place.getParent();
    GroovyResolveResult[] variants = GroovyResolveResult.EMPTY_ARRAY;
    if (parent instanceof GrCallExpression) {
        variants = ((GrCallExpression) parent).getCallVariants(place instanceof GrExpression ? (GrExpression) place : null);
    } else if (parent instanceof GrConstructorInvocation) {
        final PsiClass clazz = ((GrConstructorInvocation) parent).getDelegatedClass();
        if (clazz != null) {
            final PsiMethod[] constructors = clazz.getConstructors();
            variants = getConstructorResolveResult(constructors, place);
        }
    } else if (parent instanceof GrAnonymousClassDefinition) {
        final PsiElement element = ((GrAnonymousClassDefinition) parent).getBaseClassReferenceGroovy().resolve();
        if (element instanceof PsiClass) {
            final PsiMethod[] constructors = ((PsiClass) element).getConstructors();
            variants = getConstructorResolveResult(constructors, place);
        }
    } else if (place instanceof GrReferenceExpression) {
        variants = ((GrReferenceExpression) place).getSameNameVariants();
    }
    return variants;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

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