Search in sources :

Example 41 with GrArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project android by JetBrains.

the class GradleSettingsFile method addModule.

/**
   * Adds a reference to the module to the settings file, if there is not already one. The module path must be colon separated, with a
   * leading colon, e.g. ":project:subproject". Must be run inside a write action.
   *
   * If the file does not match the default module location, this method will override the location.
   */
public void addModule(@NotNull String modulePath, @NotNull File location) {
    checkInitialized();
    commitDocumentChanges();
    for (GrMethodCall includeStatement : getMethodCalls(myGroovyFile, INCLUDE_METHOD)) {
        for (GrLiteral lit : getLiteralArguments(includeStatement)) {
            if (modulePath.equals(lit.getValue())) {
                return;
            }
        }
    }
    GrMethodCall includeStatement = getMethodCall(myGroovyFile, INCLUDE_METHOD);
    if (includeStatement != null) {
        GrArgumentList argList = includeStatement.getArgumentList();
        GrLiteral literal = GroovyPsiElementFactory.getInstance(myProject).createLiteralFromValue(modulePath);
        argList.addAfter(literal, argList.getLastChild());
    } else {
        GrStatement statement = GroovyPsiElementFactory.getInstance(myProject).createStatementFromText(INCLUDE_METHOD + " '" + modulePath + "'");
        myGroovyFile.add(statement);
    }
    // We get location relative to this file parent
    VirtualFile parent = getFile().getParent();
    File defaultLocation = GradleUtil.getModuleDefaultPath(parent, modulePath);
    if (!FileUtil.filesEqual(defaultLocation, location)) {
        final String path;
        File parentFile = VfsUtilCore.virtualToIoFile(parent);
        if (FileUtil.isAncestor(parentFile, location, true)) {
            path = PathUtil.toSystemIndependentName(FileUtil.getRelativePath(parentFile, location));
        } else {
            path = PathUtil.toSystemIndependentName(location.getAbsolutePath());
        }
        String locationAssignment = String.format(CUSTOM_LOCATION_FORMAT, modulePath, path);
        GrStatement locationStatement = GroovyPsiElementFactory.getInstance(myProject).createStatementFromText(locationAssignment);
        myGroovyFile.add(locationStatement);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 42 with GrArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project android by JetBrains.

the class GradleDslElement method deleteIfEmpty.

protected static void deleteIfEmpty(@Nullable PsiElement element) {
    if (element == null || !element.isValid()) {
        return;
    }
    PsiElement parent = element.getParent();
    if (element instanceof GrAssignmentExpression) {
        if (((GrAssignmentExpression) element).getRValue() == null) {
            element.delete();
        }
    } else if (element instanceof GrApplicationStatement) {
        if (((GrApplicationStatement) element).getArgumentList() == null) {
            element.delete();
        }
    } else if (element instanceof GrClosableBlock) {
        final Boolean[] isEmpty = new Boolean[] { true };
        ((GrClosableBlock) element).acceptChildren(new GroovyElementVisitor() {

            @Override
            public void visitElement(GroovyPsiElement child) {
                if (child instanceof GrParameterList) {
                    if (((GrParameterList) child).getParameters().length == 0) {
                        // Ignore the empty parameter list.
                        return;
                    }
                }
                isEmpty[0] = false;
            }
        });
        if (isEmpty[0]) {
            element.delete();
        }
    } else if (element instanceof GrMethodCallExpression) {
        GrMethodCallExpression call = ((GrMethodCallExpression) element);
        GrArgumentList argumentList;
        try {
            argumentList = call.getArgumentList();
        } catch (AssertionError e) {
            // We will get this exception if the argument list is already deleted.
            argumentList = null;
        }
        GrClosableBlock[] closureArguments = call.getClosureArguments();
        if ((argumentList == null || argumentList.getAllArguments().length == 0) && closureArguments.length == 0) {
            element.delete();
        }
    } else if (element instanceof GrCommandArgumentList) {
        GrCommandArgumentList commandArgumentList = (GrCommandArgumentList) element;
        if (commandArgumentList.getAllArguments().length == 0) {
            commandArgumentList.delete();
        }
    } else if (element instanceof GrListOrMap) {
        GrListOrMap listOrMap = (GrListOrMap) element;
        if ((listOrMap.isMap() && listOrMap.getNamedArguments().length == 0) || (!listOrMap.isMap() && listOrMap.getInitializers().length == 0)) {
            listOrMap.delete();
        }
    }
    if (!element.isValid()) {
        // If this element is deleted, also delete the parent if it is empty.
        deleteIfEmpty(parent);
    }
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) GroovyElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 43 with GrArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project android by JetBrains.

the class GradleDslExpressionList method createNamedArgumentList.

@Nullable
private GroovyPsiElement createNamedArgumentList() {
    assert myParent instanceof GradleDslExpressionMap;
    GroovyPsiElement parentPsiElement = myParent.create();
    if (parentPsiElement == null) {
        return null;
    }
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentPsiElement.getProject());
    GrExpression expressionFromText = factory.createExpressionFromText("[]");
    if (expressionFromText instanceof GrListOrMap) {
        // Elements need to be added to the list before adding the list to the named argument.
        GrListOrMap list = (GrListOrMap) expressionFromText;
        for (GradleDslExpression expression : myToBeAddedExpressions) {
            expression.setPsiElement(list);
            expression.applyChanges();
            myExpressions.add(expression);
        }
        myToBeAddedExpressions.clear();
    }
    GrNamedArgument namedArgument = factory.createNamedArgument(myName, expressionFromText);
    PsiElement added;
    if (parentPsiElement instanceof GrArgumentList) {
        added = ((GrArgumentList) parentPsiElement).addNamedArgument(namedArgument);
    } else {
        added = parentPsiElement.addAfter(namedArgument, parentPsiElement.getLastChild());
    }
    if (added instanceof GrNamedArgument) {
        GrNamedArgument addedNameArgument = (GrNamedArgument) added;
        setPsiElement(addedNameArgument.getExpression());
        return getPsiElement();
    }
    return null;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 44 with GrArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project android by JetBrains.

the class GradleDslExpressionMap method create.

@Override
@Nullable
public GroovyPsiElement create() {
    GroovyPsiElement psiElement = super.create();
    if (psiElement == null) {
        return null;
    }
    if (psiElement instanceof GrListOrMap || psiElement instanceof GrArgumentList) {
        return psiElement;
    }
    if (psiElement instanceof GrApplicationStatement) {
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(psiElement.getProject());
        GrArgumentList argumentList = factory.createArgumentListFromText("xyz");
        // Workaround to get an empty argument list.
        argumentList.getFirstChild().delete();
        PsiElement added = psiElement.addAfter(argumentList, psiElement.getLastChild());
        if (added instanceof GrArgumentList) {
            GrArgumentList addedArgumentList = (GrArgumentList) added;
            setPsiElement(addedArgumentList);
            return addedArgumentList;
        }
    }
    return null;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 45 with GrArgumentList

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList in project intellij-community by JetBrains.

the class GrIntroduceClosureParameterProcessor method processExternalUsage.

private static void processExternalUsage(UsageInfo usage, GrIntroduceParameterSettings settings, PsiElement expression) {
    final PsiElement element = usage.getElement();
    GrCall callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
    if (callExpression == null) {
        final PsiElement parent = element.getParent();
        if (parent instanceof GrReferenceExpression && element == ((GrReferenceExpression) parent).getQualifier() && "call".equals(((GrReferenceExpression) parent).getReferenceName())) {
            callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(parent);
        }
    }
    if (callExpression == null)
        return;
    //check for x.getFoo()(args)
    if (callExpression instanceof GrMethodCall) {
        final GrExpression invoked = ((GrMethodCall) callExpression).getInvokedExpression();
        if (invoked instanceof GrReferenceExpression) {
            final GroovyResolveResult result = ((GrReferenceExpression) invoked).advancedResolve();
            final PsiElement resolved = result.getElement();
            if (resolved instanceof GrAccessorMethod && !result.isInvokedOnProperty()) {
                PsiElement actualCallExpression = callExpression.getParent();
                if (actualCallExpression instanceof GrCall) {
                    callExpression = (GrCall) actualCallExpression;
                }
            }
        }
    }
    GrArgumentList argList = callExpression.getArgumentList();
    LOG.assertTrue(argList != null);
    GrExpression[] oldArgs = argList.getExpressionArguments();
    GrClosableBlock toReplaceIn = (GrClosableBlock) settings.getToReplaceIn();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
    final GrExpression anchor = getAnchorForArgument(oldArgs, toReplaceIn.isVarArgs(), toReplaceIn.getParameterList());
    GrClosureSignature signature = GrClosureSignatureUtil.createSignature(callExpression);
    if (signature == null)
        signature = GrClosureSignatureUtil.createSignature(toReplaceIn);
    final GrClosureSignatureUtil.ArgInfo<PsiElement>[] actualArgs = GrClosureSignatureUtil.mapParametersToArguments(signature, callExpression.getNamedArguments(), callExpression.getExpressionArguments(), callExpression.getClosureArguments(), callExpression, true, true);
    if (PsiTreeUtil.isAncestor(toReplaceIn, callExpression, false)) {
        argList.addAfter(factory.createExpressionFromText(settings.getName()), anchor);
    } else {
        PsiElement initializer = ExpressionConverter.getExpression(expression, GroovyLanguage.INSTANCE, settings.getProject());
        LOG.assertTrue(initializer instanceof GrExpression);
        GrExpression newArg = GroovyIntroduceParameterUtil.addClosureToCall(initializer, argList);
        if (newArg == null) {
            final PsiElement dummy = argList.addAfter(factory.createExpressionFromText("1"), anchor);
            newArg = ((GrExpression) dummy).replaceWithExpression((GrExpression) initializer, true);
        }
        new OldReferencesResolver(callExpression, newArg, toReplaceIn, settings.replaceFieldsWithGetters(), initializer, signature, actualArgs, toReplaceIn.getParameters()).resolve();
        ChangeContextUtil.clearContextInfo(initializer);
        //newarg can be replaced by OldReferenceResolve
        if (newArg.isValid()) {
            JavaCodeStyleManager.getInstance(newArg.getProject()).shortenClassReferences(newArg);
            CodeStyleManager.getInstance(settings.getProject()).reformat(newArg);
        }
    }
    if (actualArgs == null) {
        GroovyIntroduceParameterUtil.removeParamsFromUnresolvedCall(callExpression, toReplaceIn.getParameters(), settings.parametersToRemove());
    } else {
        GroovyIntroduceParameterUtil.removeParametersFromCall(actualArgs, settings.parametersToRemove());
    }
    if (argList.getAllArguments().length == 0 && PsiImplUtil.hasClosureArguments(callExpression)) {
        final GrArgumentList emptyArgList = ((GrMethodCallExpression) factory.createExpressionFromText("foo{}")).getArgumentList();
        argList.replace(emptyArgList);
    }
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) OldReferencesResolver(org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.OldReferencesResolver)

Aggregations

GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)80 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)26 PsiElement (com.intellij.psi.PsiElement)21 Nullable (org.jetbrains.annotations.Nullable)20 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)18 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)15 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)14 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)14 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)14 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)14 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)12 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)9 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)8 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)8 NotNull (org.jetbrains.annotations.NotNull)7 GrCommandArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList)6 GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)6 GrGdkMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod)6 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)6 ASTNode (com.intellij.lang.ASTNode)5