Search in sources :

Example 1 with GrArgumentLabel

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

the class ExpressionGenerator method generateMapElementInsertions.

private void generateMapElementInsertions(GrListOrMap listOrMap, String varName) {
    for (GrNamedArgument arg : listOrMap.getNamedArguments()) {
        StringBuilder insertion = new StringBuilder();
        insertion.append(varName).append(".put(");
        final String stringKey = arg.getLabelName();
        if (stringKey != null) {
            insertion.append('"').append(stringKey).append('"');
        } else {
            final GrArgumentLabel label = arg.getLabel();
            final GrExpression expression = label == null ? null : label.getExpression();
            if (expression != null) {
                expression.accept(new ExpressionGenerator(insertion, context));
            } else {
            //todo should we generate an exception?
            }
        }
        insertion.append(", ");
        final GrExpression expression = arg.getExpression();
        if (expression != null) {
            expression.accept(new ExpressionGenerator(insertion, context));
        } else {
        //todo should we generate an exception?
        }
        insertion.append(");");
        context.myStatements.add(insertion.toString());
    }
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)

Example 2 with GrArgumentLabel

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

the class GantMemberContributor method processDynamicElements.

@Override
public void processDynamicElements(@NotNull PsiType qualifierType, PsiClass aClass, @NotNull PsiScopeProcessor processor, @NotNull PsiElement place, @NotNull ResolveState state) {
    if (aClass != null && ClassUtil.getSuperClassesWithCache(aClass).containsKey("groovy.util.AntBuilder")) {
        processAntTasks(processor, place, state);
        return;
    }
    if (!(place instanceof GrReferenceExpression) || ((GrReferenceExpression) place).isQualified()) {
        return;
    }
    GrClosableBlock closure = PsiTreeUtil.getContextOfType(place, GrClosableBlock.class, true);
    boolean antTasksProcessed = false;
    while (closure != null) {
        final PsiElement parent = closure.getParent();
        if (parent instanceof GrMethodCall) {
            final PsiMethod method = ((GrMethodCall) parent).resolveMethod();
            if (method instanceof AntBuilderMethod) {
                antTasksProcessed = true;
                if (!processAntTasks(processor, place, state)) {
                    return;
                }
                if (!((AntBuilderMethod) method).processNestedElements(processor)) {
                    return;
                }
                break;
            }
        }
        closure = PsiTreeUtil.getContextOfType(closure, GrClosableBlock.class, true);
    }
    // ------- gant-specific
    PsiFile file = place.getContainingFile();
    if (file == null || !GroovyScriptUtil.isSpecificScriptFile(file, GantScriptType.INSTANCE)) {
        return;
    }
    if (aClass instanceof GroovyScriptClass) {
        for (GrArgumentLabel label : GantUtils.getScriptTargets((GroovyFile) file)) {
            final String targetName = label.getName();
            if (targetName != null) {
                final PsiNamedElement variable = new LightVariableBuilder(targetName, GroovyCommonClassNames.GROOVY_LANG_CLOSURE, label).setBaseIcon(JetgroovyIcons.Groovy.Gant_target);
                if (!ResolveUtil.processElement(processor, variable, state)) {
                    return;
                }
            }
        }
    }
    if (!antTasksProcessed) {
        processAntTasks(processor, place, state);
    }
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) LightVariableBuilder(com.intellij.psi.impl.light.LightVariableBuilder) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)

Example 3 with GrArgumentLabel

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

the class GantUtils method getScriptTargets.

public static GrArgumentLabel[] getScriptTargets(GroovyFile file) {
    ArrayList<GrArgumentLabel> labels = new ArrayList<>();
    for (PsiElement child : file.getChildren()) {
        if (child instanceof GrMethodCallExpression) {
            GrMethodCallExpression call = (GrMethodCallExpression) child;
            GrNamedArgument[] arguments = call.getNamedArguments();
            if (arguments.length == 1) {
                GrArgumentLabel label = arguments[0].getLabel();
                if (label != null && isPlainIdentifier(label)) {
                    labels.add(label);
                }
            }
        }
    }
    return labels.toArray(new GrArgumentLabel[labels.size()]);
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement)

Example 4 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 5 with GrArgumentLabel

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

the class ConvertClosureToMethodIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final GrField field;
    if (element.getParent() instanceof GrField) {
        field = (GrField) element.getParent();
    } else {
        final PsiReference ref = element.getReference();
        LOG.assertTrue(ref != null);
        PsiElement resolved = ref.resolve();
        if (resolved instanceof GrAccessorMethod) {
            resolved = ((GrAccessorMethod) resolved).getProperty();
        }
        LOG.assertTrue(resolved instanceof GrField);
        field = (GrField) resolved;
    }
    final HashSet<PsiReference> usages = new HashSet<>();
    usages.addAll(ReferencesSearch.search(field).findAll());
    final GrAccessorMethod[] getters = field.getGetters();
    for (GrAccessorMethod getter : getters) {
        usages.addAll(MethodReferencesSearch.search(getter).findAll());
    }
    final GrAccessorMethod setter = field.getSetter();
    if (setter != null) {
        usages.addAll(MethodReferencesSearch.search(setter).findAll());
    }
    final String fieldName = field.getName();
    LOG.assertTrue(fieldName != null);
    final Collection<PsiElement> fieldUsages = new HashSet<>();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    for (PsiReference usage : usages) {
        final PsiElement psiElement = usage.getElement();
        if (PsiUtil.isMethodUsage(psiElement))
            continue;
        if (!GroovyLanguage.INSTANCE.equals(psiElement.getLanguage())) {
            conflicts.putValue(psiElement, GroovyIntentionsBundle.message("closure.is.accessed.outside.of.groovy", fieldName));
        } else {
            if (psiElement instanceof GrReferenceExpression) {
                fieldUsages.add(psiElement);
                if (PsiUtil.isAccessedForWriting((GrExpression) psiElement)) {
                    conflicts.putValue(psiElement, GroovyIntentionsBundle.message("write.access.to.closure.variable", fieldName));
                }
            } else if (psiElement instanceof GrArgumentLabel) {
                conflicts.putValue(psiElement, GroovyIntentionsBundle.message("field.is.used.in.argument.label", fieldName));
            }
        }
    }
    final PsiClass containingClass = field.getContainingClass();
    final GrExpression initializer = field.getInitializerGroovy();
    LOG.assertTrue(initializer != null);
    final PsiType type = initializer.getType();
    LOG.assertTrue(type instanceof GrClosureType);
    final GrSignature signature = ((GrClosureType) type).getSignature();
    final List<MethodSignature> signatures = GrClosureSignatureUtil.generateAllMethodSignaturesBySignature(fieldName, signature);
    for (MethodSignature s : signatures) {
        final PsiMethod method = MethodSignatureUtil.findMethodBySignature(containingClass, s, true);
        if (method != null) {
            conflicts.putValue(method, GroovyIntentionsBundle.message("method.with.signature.already.exists", GroovyPresentationUtil.getSignaturePresentation(s)));
        }
    }
    if (!conflicts.isEmpty()) {
        final ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts, () -> execute(field, fieldUsages));
        conflictsDialog.show();
        if (conflictsDialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
            return;
    }
    execute(field, fieldUsages);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) MethodSignature(com.intellij.psi.util.MethodSignature) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) MultiMap(com.intellij.util.containers.MultiMap) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) GrSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrSignature) HashSet(com.intellij.util.containers.HashSet) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)

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