Search in sources :

Example 6 with GrArgumentLabel

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

the class GrSortMapKeysIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    PsiElement parent = element.getParent();
    if (parent instanceof GrArgumentLabel) {
        PsiElement pparent = parent.getParent().getParent();
        if (pparent instanceof GrListOrMap && !ErrorUtil.containsError(pparent)) {
            GrListOrMap map = (GrListOrMap) pparent;
            if (map.getInitializers().length == 0) {
                GrNamedArgument[] namedArgs = map.getNamedArguments();
                if (isLiteralKeys(namedArgs)) {
                    GrListOrMap newMap = constructNewMap(namedArgs, project);
                    map.replace(newMap);
                }
            }
        }
    }
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) PsiElement(com.intellij.psi.PsiElement)

Example 7 with GrArgumentLabel

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

the class GroovyAnnotator method checkNamedArgs.

private void checkNamedArgs(GrNamedArgument[] namedArguments, boolean forArgList) {
    highlightNamedArgs(namedArguments);
    Set<Object> existingKeys = ContainerUtil.newHashSet();
    for (GrNamedArgument namedArgument : namedArguments) {
        GrArgumentLabel label = namedArgument.getLabel();
        Object value = PsiUtil.getLabelValue(label);
        if (value == null)
            continue;
        if (value == ObjectUtils.NULL)
            value = null;
        if (existingKeys.add(value))
            continue;
        if (forArgList) {
            myHolder.createErrorAnnotation(label, GroovyBundle.message("duplicated.named.parameter", String.valueOf(value)));
        } else {
            myHolder.createWarningAnnotation(label, GroovyBundle.message("duplicate.element.in.the.map", String.valueOf(value)));
        }
    }
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)

Example 8 with GrArgumentLabel

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

the class EquivalenceChecker method namedArgumentListsAreEquivalent.

private static boolean namedArgumentListsAreEquivalent(GrNamedArgument[] namedArgs1, GrNamedArgument[] namedArgs2) {
    if (namedArgs1.length != namedArgs2.length) {
        return false;
    }
    for (GrNamedArgument arg1 : namedArgs1) {
        final GrArgumentLabel label1 = arg1.getLabel();
        if (label1 == null) {
            return false;
        }
        final String name1 = label1.getName();
        boolean found = false;
        final GrExpression expression1 = arg1.getExpression();
        for (GrNamedArgument arg2 : namedArgs2) {
            final GrArgumentLabel label2 = arg2.getLabel();
            if (label2 == null) {
                return false;
            }
            final String name2 = label2.getName();
            final GrExpression expression2 = arg2.getExpression();
            if (name1 == null) {
                if (name2 == null && expressionsAreEquivalent(((GrExpression) label1.getNameElement()), (GrExpression) label2.getNameElement()) && expressionsAreEquivalent(expression1, expression2)) {
                    found = true;
                    break;
                }
            } else if (name1.equals(name2) && expressionsAreEquivalent(expression1, expression2)) {
                found = true;
                break;
            }
        }
        if (!found) {
            return false;
        }
    }
    return true;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)

Example 9 with GrArgumentLabel

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

the class PsiImplUtil method replaceExpression.

@Nullable
public static GrExpression replaceExpression(GrExpression oldExpr, GrExpression newExpr, boolean removeUnnecessaryParentheses) {
    PsiElement oldParent = oldExpr.getParent();
    if (oldParent == null)
        throw new PsiInvalidElementAccessException(oldExpr);
    if (!(oldExpr instanceof GrApplicationStatement)) {
        newExpr = ApplicationStatementUtil.convertToMethodCallExpression(newExpr);
    }
    // Remove unnecessary parentheses
    if (removeUnnecessaryParentheses && oldParent instanceof GrParenthesizedExpression && !(oldParent.getParent() instanceof GrArgumentLabel)) {
        return ((GrExpression) oldParent).replaceWithExpression(newExpr, true);
    }
    //regexes cannot be after identifier , try to replace it with simple string
    if (getRegexAtTheBeginning(newExpr) != null && isAfterIdentifier(oldExpr)) {
        final PsiElement copy = newExpr.copy();
        final GrLiteral regex = getRegexAtTheBeginning(copy);
        LOG.assertTrue(regex != null);
        final GrLiteral stringLiteral = GrStringUtil.createStringFromRegex(regex);
        if (regex == copy) {
            return oldExpr.replaceWithExpression(stringLiteral, removeUnnecessaryParentheses);
        } else {
            regex.replace(stringLiteral);
            return oldExpr.replaceWithExpression((GrExpression) copy, removeUnnecessaryParentheses);
        }
    }
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(oldExpr.getProject());
    if (oldParent instanceof GrStringInjection) {
        if (newExpr instanceof GrString || newExpr instanceof GrLiteral && ((GrLiteral) newExpr).getValue() instanceof String) {
            return GrStringUtil.replaceStringInjectionByLiteral((GrStringInjection) oldParent, (GrLiteral) newExpr);
        } else {
            GrClosableBlock block = factory.createClosureFromText("{foo}");
            oldParent.getNode().replaceChild(oldExpr.getNode(), block.getNode());
            GrStatement[] statements = block.getStatements();
            return ((GrExpression) statements[0]).replaceWithExpression(newExpr, removeUnnecessaryParentheses);
        }
    }
    if (PsiTreeUtil.getParentOfType(oldExpr, GrStringInjection.class, false, GrCodeBlock.class) != null) {
        final GrStringInjection stringInjection = PsiTreeUtil.getParentOfType(oldExpr, GrStringInjection.class);
        GrStringUtil.wrapInjection(stringInjection);
        assert stringInjection != null;
        final PsiElement replaced = oldExpr.replaceWithExpression(newExpr, removeUnnecessaryParentheses);
        return (GrExpression) replaced;
    }
    //check priorities    
    if (oldParent instanceof GrExpression && !(oldParent instanceof GrParenthesizedExpression)) {
        GrExpression addedParenth = checkPrecedence(newExpr, oldExpr) ? parenthesize(newExpr) : newExpr;
        if (newExpr != addedParenth) {
            return oldExpr.replaceWithExpression(addedParenth, removeUnnecessaryParentheses);
        }
    }
    //we should add the expression in arg list
    if (oldExpr instanceof GrClosableBlock && !(newExpr instanceof GrClosableBlock) && oldParent instanceof GrMethodCallExpression && ArrayUtil.contains(oldExpr, ((GrMethodCallExpression) oldParent).getClosureArguments())) {
        return ((GrMethodCallExpression) oldParent).replaceClosureArgument((GrClosableBlock) oldExpr, newExpr);
    }
    newExpr = (GrExpression) oldExpr.replace(newExpr);
    // to find target parenthesised expression.
    if (newExpr instanceof GrParenthesizedExpression && isFirstChild(newExpr)) {
        int parentCount = 0;
        PsiElement element = oldParent;
        while (element != null && !(element instanceof GrCommandArgumentList)) {
            if (element instanceof GrCodeBlock || element instanceof GrParenthesizedExpression)
                break;
            if (element instanceof PsiFile)
                break;
            final PsiElement parent = element.getParent();
            if (parent == null)
                break;
            if (!isFirstChild(element))
                break;
            element = parent;
            parentCount++;
        }
        if (element instanceof GrCommandArgumentList) {
            final GrCommandArgumentList commandArgList = (GrCommandArgumentList) element;
            final PsiElement parent = commandArgList.getParent();
            LOG.assertTrue(parent instanceof GrApplicationStatement);
            final GrMethodCall methodCall = factory.createMethodCallByAppCall((GrApplicationStatement) parent);
            final GrMethodCall newCall = (GrMethodCall) parent.replace(methodCall);
            PsiElement result = newCall.getArgumentList().getAllArguments()[0];
            for (int i = 0; i < parentCount; i++) {
                result = PsiUtil.skipWhitespacesAndComments(result.getFirstChild(), true);
            }
            LOG.assertTrue(result instanceof GrParenthesizedExpression);
            return (GrExpression) result;
        }
    }
    return newExpr;
}
Also used : GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with GrArgumentLabel

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

the class GppClosureParameterTypeProvider method getOverriddenMethodVariants.

@NotNull
public static List<Pair<PsiMethod, PsiSubstitutor>> getOverriddenMethodVariants(GrNamedArgument namedArgument) {
    final GrArgumentLabel label = namedArgument.getLabel();
    if (label == null) {
        return Collections.emptyList();
    }
    final String methodName = label.getName();
    if (methodName == null) {
        return Collections.emptyList();
    }
    final PsiElement map = namedArgument.getParent();
    if (map instanceof GrListOrMap && ((GrListOrMap) map).isMap()) {
        for (PsiType expected : GroovyExpectedTypesProvider.getDefaultExpectedTypes((GrExpression) map)) {
            if (expected instanceof PsiClassType) {
                final List<Pair<PsiMethod, PsiSubstitutor>> pairs = getMethodsToOverrideImplementInInheritor((PsiClassType) expected, false);
                return ContainerUtil.findAll(pairs, pair -> methodName.equals(pair.first.getName()));
            }
        }
    }
    return Collections.emptyList();
}
Also used : GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrArgumentLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel)13 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)8 PsiElement (com.intellij.psi.PsiElement)4 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)4 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)4 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)3 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)2 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)2 ASTNode (com.intellij.lang.ASTNode)1 Pair (com.intellij.openapi.util.Pair)1 PsiRecursiveElementWalkingVisitor (com.intellij.psi.PsiRecursiveElementWalkingVisitor)1 LightVariableBuilder (com.intellij.psi.impl.light.LightVariableBuilder)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 IElementType (com.intellij.psi.tree.IElementType)1 MethodSignature (com.intellij.psi.util.MethodSignature)1 ConflictsDialog (com.intellij.refactoring.ui.ConflictsDialog)1