use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter 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.params.GrParameter in project intellij-community by JetBrains.
the class ConvertParameterToMapEntryIntention method getAppropriateParameter.
@Nullable
private static GrParameter getAppropriateParameter(final PsiElement element) {
if (element instanceof GrParameter) {
return (GrParameter) element;
}
if (element instanceof GrReferenceExpression) {
final GrReferenceExpression expr = (GrReferenceExpression) element;
final PsiElement resolved = expr.resolve();
LOG.assertTrue(resolved instanceof GrParameter);
return ((GrParameter) resolved);
}
LOG.error("Selected expression is not resolved to method/closure parameter");
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class ConvertClosureArgToItIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrClosableBlock closure = (GrClosableBlock) element;
final GrParameterList parameterList = closure.getParameterList();
final GrParameter parameter = parameterList.getParameters()[0];
final Set<GrReferenceExpression> referencesToChange = new HashSet<>();
final GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
super.visitReferenceExpression(referenceExpression);
if (!referenceExpression.getText().equals(parameter.getName())) {
return;
}
final PsiElement referent = referenceExpression.resolve();
if (parameter.equals(referent)) {
referencesToChange.add(referenceExpression);
}
}
};
closure.accept(visitor);
parameter.delete();
for (GrReferenceExpression referenceExpression : referencesToChange) {
PsiImplUtil.replaceExpression("it", referenceExpression);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class MyPredicate method getParameterByArgument.
@Nullable
private static GrParameter getParameterByArgument(GrExpression arg) {
PsiElement parent = PsiUtil.skipParentheses(arg.getParent(), true);
if (!(parent instanceof GrArgumentList))
return null;
final GrArgumentList argList = (GrArgumentList) parent;
parent = parent.getParent();
if (!(parent instanceof GrMethodCall))
return null;
final GrMethodCall methodCall = (GrMethodCall) parent;
final GrExpression expression = methodCall.getInvokedExpression();
if (!(expression instanceof GrReferenceExpression))
return null;
final GroovyResolveResult resolveResult = ((GrReferenceExpression) expression).advancedResolve();
if (resolveResult == null)
return null;
GrClosableBlock[] closures = methodCall.getClosureArguments();
final Map<GrExpression, Pair<PsiParameter, PsiType>> mapToParams = GrClosureSignatureUtil.mapArgumentsToParameters(resolveResult, arg, false, false, argList.getNamedArguments(), argList.getExpressionArguments(), closures);
if (mapToParams == null)
return null;
final Pair<PsiParameter, PsiType> parameterPair = mapToParams.get(arg);
final PsiParameter parameter = parameterPair == null ? null : parameterPair.getFirst();
return parameter instanceof GrParameter ? ((GrParameter) parameter) : null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GroovyInlineMethodUtil method replaceParametersWithArguments.
/**
* Inline method call's arguments as its parameters
*
* @param call method call
* @param method given method
*/
public static void replaceParametersWithArguments(GrCallExpression call, GrMethod method) throws IncorrectOperationException {
GrParameter[] parameters = method.getParameters();
if (parameters.length == 0)
return;
GrArgumentList argumentList = call.getArgumentList();
if (argumentList == null) {
setDefaultValuesToParameters(method, null, call);
return;
}
Project project = call.getProject();
final GroovyResolveResult resolveResult = call.advancedResolve();
GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, resolveResult.getSubstitutor());
if (signature == null) {
return;
}
GrClosureSignatureUtil.ArgInfo<PsiElement>[] infos = GrClosureSignatureUtil.mapParametersToArguments(signature, call.getNamedArguments(), call.getExpressionArguments(), call.getClosureArguments(), call, true, false);
if (infos == null)
return;
for (int i = 0; i < infos.length; i++) {
GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo = infos[i];
GrParameter parameter = parameters[i];
final GrExpression arg = inferArg(signature, parameters, parameter, argInfo, project);
if (arg != null) {
replaceAllOccurrencesWithExpression(method, call, arg, parameter);
}
}
}
Aggregations