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());
}
}
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);
}
}
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()]);
}
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);
}
}
}
}
}
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);
}
Aggregations