use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument in project intellij-community by JetBrains.
the class ExpressionGenerator method generateMethodCall.
private void generateMethodCall(GrMethodCall methodCallExpression) {
final GrExpression invoked = methodCallExpression.getInvokedExpression();
final GrExpression[] exprs = methodCallExpression.getExpressionArguments();
final GrNamedArgument[] namedArgs = methodCallExpression.getNamedArguments();
final GrClosableBlock[] clArgs = methodCallExpression.getClosureArguments();
if (invoked instanceof GrReferenceExpression) {
final GroovyResolveResult resolveResult = ((GrReferenceExpression) invoked).advancedResolve();
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiMethod) {
//todo replace null-qualifier with this-reference
final GrExpression qualifier = ((GrReferenceExpression) invoked).getQualifier();
invokeMethodOn(((PsiMethod) resolved), qualifier, exprs, namedArgs, clArgs, resolveResult.getSubstitutor(), methodCallExpression);
return;
} else if (resolved == null) {
final GrExpression qualifier = ((GrReferenceExpression) invoked).getQualifier();
final GrExpression[] args = generateArgsForInvokeMethod(((GrReferenceExpression) invoked).getReferenceName(), exprs, namedArgs, clArgs, methodCallExpression);
GenerationUtil.invokeMethodByName(qualifier, "invokeMethod", args, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, this, methodCallExpression);
return;
}
}
GenerationUtil.invokeMethodByName(invoked, "call", exprs, namedArgs, clArgs, this, methodCallExpression);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument in project intellij-community by JetBrains.
the class ConvertParameterToMapEntryIntention method performRefactoring.
private static void performRefactoring(final PsiElement element, final GrParametersOwner owner, final Collection<PsiElement> occurrences, final boolean createNewFirstParam, @Nullable final String mapParamName, final boolean specifyMapType) {
final GrParameter param = getAppropriateParameter(element);
assert param != null;
final String paramName = param.getName();
final String mapName = createNewFirstParam ? mapParamName : getFirstParameter(owner).getName();
final Project project = element.getProject();
final Runnable runnable = () -> {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final GrParameterList list = owner.getParameterList();
assert list != null;
final int index = list.getParameterNumber(param);
if (!createNewFirstParam && index <= 0) {
// bad undo
return;
}
//final List<GrCall> calls = getCallOccurrences(occurrences);
try {
for (PsiElement occurrence : occurrences) {
GrReferenceExpression refExpr = null;
GroovyResolveResult resolveResult = null;
boolean isExplicitGetterCall = false;
if (occurrence instanceof GrReferenceExpression) {
final PsiElement parent = occurrence.getParent();
if (parent instanceof GrCall) {
refExpr = (GrReferenceExpression) occurrence;
resolveResult = refExpr.advancedResolve();
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertyGetter(((PsiMethod) resolved)) && //check for explicit getter call
((PsiMethod) resolved).getName().equals(refExpr.getReferenceName())) {
isExplicitGetterCall = true;
}
} else if (parent instanceof GrReferenceExpression) {
resolveResult = ((GrReferenceExpression) parent).advancedResolve();
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiMethod && "call".equals(((PsiMethod) resolved).getName())) {
refExpr = (GrReferenceExpression) parent;
}
}
}
if (refExpr == null)
continue;
final GrClosureSignature signature = generateSignature(owner, refExpr);
if (signature == null)
continue;
GrCall call;
if (isExplicitGetterCall) {
PsiElement parent = refExpr.getParent();
LOG.assertTrue(parent instanceof GrCall);
parent = parent.getParent();
if (parent instanceof GrReferenceExpression && "call".equals(((GrReferenceExpression) parent).getReferenceName())) {
parent = parent.getParent();
}
if (parent instanceof GrCall) {
call = (GrCall) parent;
} else {
continue;
}
} else {
call = (GrCall) refExpr.getParent();
}
if (resolveResult.isInvokedOnProperty()) {
final PsiElement parent = call.getParent();
if (parent instanceof GrCall) {
call = (GrCall) parent;
} else if (parent instanceof GrReferenceExpression && parent.getParent() instanceof GrCall) {
final PsiElement resolved = ((GrReferenceExpression) parent).resolve();
if (resolved instanceof PsiMethod && "call".equals(((PsiMethod) resolved).getName())) {
call = (GrCall) parent.getParent();
} else {
continue;
}
}
}
final GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, call);
if (argInfos == null)
continue;
final GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo = argInfos[index];
final GrNamedArgument namedArg;
if (argInfo.isMultiArg) {
if (argInfo.args.isEmpty())
continue;
String arg = "[" + StringUtil.join(ContainerUtil.map(argInfo.args, element1 -> element1.getText()), ", ") + "]";
for (PsiElement psiElement : argInfo.args) {
psiElement.delete();
}
namedArg = factory.createNamedArgument(paramName, factory.createExpressionFromText(arg));
} else {
if (argInfo.args.isEmpty())
continue;
final PsiElement argument = argInfo.args.iterator().next();
assert argument instanceof GrExpression;
namedArg = factory.createNamedArgument(paramName, (GrExpression) argument);
argument.delete();
}
call.addNamedArgument(namedArg);
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
//Replace of occurrences of old parameter in closure/method
final Collection<PsiReference> references = ReferencesSearch.search(param).findAll();
for (PsiReference ref : references) {
final PsiElement elt = ref.getElement();
if (elt instanceof GrReferenceExpression) {
GrReferenceExpression expr = (GrReferenceExpression) elt;
final GrExpression newExpr = factory.createExpressionFromText(mapName + "." + paramName);
expr.replaceWithExpression(newExpr, true);
}
}
//Add new map parameter to closure/method if it's necessary
if (createNewFirstParam) {
try {
final GrParameter newParam = factory.createParameter(mapName, specifyMapType ? MAP_TYPE_TEXT : "", null);
list.addAfter(newParam, null);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
//Eliminate obsolete parameter from parameter list
param.delete();
};
CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(runnable), REFACTORING_NAME, null);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument 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.GrNamedArgument 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.GrNamedArgument in project intellij-community by JetBrains.
the class GrSortMapKeysIntention method constructNewMap.
@NotNull
private static GrListOrMap constructNewMap(@NotNull GrNamedArgument[] args, Project project) {
StringBuilder builder = new StringBuilder();
builder.append("[");
Arrays.sort(args, (o1, o2) -> {
final String l1 = o1.getLabelName();
final String l2 = o2.getLabelName();
assert l1 != null && l2 != null;
return l1.compareTo(l2);
});
for (GrNamedArgument arg : args) {
builder.append(arg.getText()).append(",\n");
}
builder.replace(builder.length() - 2, builder.length(), "]");
return (GrListOrMap) GroovyPsiElementFactory.getInstance(project).createExpressionFromText(builder);
}
Aggregations