use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class ConvertParameterToMapEntryIntention method analyzeForNamedArguments.
/**
* @param owner Method or closure
* @param occurrences references to owner
* @return true if there we use owner's first parameter as map, false if we need to add ne one as fist map
*/
private static FIRST_PARAMETER_KIND analyzeForNamedArguments(final GrParametersOwner owner, final Collection<PsiElement> occurrences) {
boolean thereAreNamedArguments = false;
for (PsiElement occurrence : occurrences) {
if (occurrence instanceof GrReferenceExpression && occurrence.getParent() instanceof GrCall) {
final GrCall call = (GrCall) occurrence.getParent();
final GrArgumentList args = call.getArgumentList();
if (args != null && args.getNamedArguments().length > 0) {
thereAreNamedArguments = true;
}
}
if (thereAreNamedArguments)
break;
}
if (thereAreNamedArguments) {
if (firstOwnerParameterMustBeMap(owner)) {
return FIRST_PARAMETER_KIND.MUST_BE_MAP;
}
return FIRST_PARAMETER_KIND.ERROR;
}
return FIRST_PARAMETER_KIND.IS_NOT_MAP;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression 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.expressions.GrReferenceExpression 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.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class MapKeysCompletionProvider method addCompletions.
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
PsiElement element = parameters.getPosition();
GrReferenceExpression expression = (GrReferenceExpression) element.getParent();
GrExpression qualifierExpression = expression.getQualifierExpression();
if (qualifierExpression == null)
return;
PsiType mapType = qualifierExpression.getType();
if (!InheritanceUtil.isInheritor(mapType, CommonClassNames.JAVA_UTIL_MAP)) {
return;
}
PsiElement resolve = null;
if (qualifierExpression instanceof GrMethodCall) {
resolve = ((GrMethodCall) qualifierExpression).resolveMethod();
} else if (qualifierExpression instanceof GrReferenceExpression) {
resolve = ((GrReferenceExpression) qualifierExpression).resolve();
}
for (GroovyMapContentProvider provider : GroovyMapContentProvider.EP_NAME.getExtensions()) {
GroovyMapCompletionUtil.addKeyVariants(provider, qualifierExpression, resolve, result);
}
if (mapType instanceof GrMapType) {
for (String key : ((GrMapType) mapType).getStringKeys()) {
LookupElement lookup = LookupElementBuilder.create(key);
lookup = PrioritizedLookupElement.withPriority(lookup, 1);
result.addElement(lookup);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class QualifiedMethodInsertHandler method handleInsert.
@Override
public void handleInsert(InsertionContext context, JavaGlobalMemberLookupElement item) {
GroovyInsertHandler.INSTANCE.handleInsert(context, item);
final PsiClass containingClass = item.getContainingClass();
context.getDocument().insertString(context.getStartOffset(), containingClass.getName() + ".");
PsiDocumentManager.getInstance(containingClass.getProject()).commitDocument(context.getDocument());
final GrReferenceExpression ref = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), GrReferenceExpression.class, false);
if (ref != null) {
ref.bindToElement(containingClass);
}
}
Aggregations