use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner in project intellij-community by JetBrains.
the class ConvertParameterToMapEntryIntention method collectOwnerOccurrences.
private static boolean collectOwnerOccurrences(final Project project, final GrParametersOwner owner, final Collection<PsiElement> occurrences) {
final PsiElement namedElem = getReferencedElement(owner);
if (namedElem == null)
return true;
final Ref<Boolean> result = new Ref<>(true);
final Task task = new Task.Modal(project, GroovyIntentionsBundle.message("find.method.ro.closure.usages.0", owner instanceof GrClosableBlock ? CLOSURE_CAPTION : METHOD_CAPTION), true) {
@Override
public void run(@NotNull final ProgressIndicator indicator) {
final Collection<PsiReference> references = Collections.synchronizedSet(new HashSet<PsiReference>());
final Processor<PsiReference> consumer = psiReference -> {
references.add(psiReference);
return true;
};
ReferencesSearch.search(namedElem).forEach(consumer);
boolean isProperty = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
@Override
public Boolean compute() {
return namedElem instanceof GrField && ((GrField) namedElem).isProperty();
}
});
if (isProperty) {
final GrAccessorMethod[] getters = ApplicationManager.getApplication().runReadAction(new Computable<GrAccessorMethod[]>() {
@Override
public GrAccessorMethod[] compute() {
return ((GrField) namedElem).getGetters();
}
});
for (GrAccessorMethod getter : getters) {
MethodReferencesSearch.search(getter).forEach(consumer);
}
}
for (final PsiReference reference : references) {
ApplicationManager.getApplication().runReadAction(() -> {
final PsiElement element = reference.getElement();
if (element != null) {
occurrences.add(element);
}
});
}
}
@Override
public void onCancel() {
result.set(false);
}
@Override
public void onThrowable(@NotNull Throwable error) {
super.onThrowable(error);
result.set(false);
}
@Override
public void onSuccess() {
result.set(true);
}
};
ProgressManager.getInstance().run(task);
return result.get().booleanValue();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner in project intellij-community by JetBrains.
the class ConvertParameterToMapEntryIntention method processIntention.
@Override
protected void processIntention(@NotNull final PsiElement element, @NotNull final Project project, Editor editor) throws IncorrectOperationException {
// Method or closure to be refactored
final GrParametersOwner owner = PsiTreeUtil.getParentOfType(element, GrParametersOwner.class);
final Collection<PsiElement> occurrences = new ArrayList<>();
// Find all referenced expressions
final boolean success = collectOwnerOccurrences(project, owner, occurrences);
if (!success)
return;
// Checking for Groovy files only
final boolean isClosure = owner instanceof GrClosableBlock;
if (!checkOwnerOccurrences(project, occurrences, isClosure))
return;
// To add or not to add new parameter for map entries
final GrParameter firstParam = getFirstParameter(owner);
switch(analyzeForNamedArguments(owner, occurrences)) {
case ERROR:
{
final GrNamedElement namedElement = getReferencedElement(owner);
LOG.assertTrue(namedElement != null);
final String msg = GroovyIntentionsBundle.message("wrong.first.parameter.type", isClosure ? CLOSURE_CAPTION_CAP : METHOD_CAPTION_CAP, namedElement.getName(), firstParam.getName());
showErrorMessage(msg, project);
return;
}
case MUST_BE_MAP:
{
if (firstParam == getAppropriateParameter(element)) {
final String msg = GroovyIntentionsBundle.message("convert.cannot.itself");
showErrorMessage(msg, project);
return;
}
performRefactoring(element, owner, occurrences, false, null, false);
break;
}
case IS_NOT_MAP:
{
if (!ApplicationManager.getApplication().isUnitTestMode()) {
final String[] possibleNames = generateValidNames(MY_POSSIBLE_NAMES, firstParam);
final GroovyMapParameterDialog dialog = new GroovyMapParameterDialog(project, possibleNames, true) {
@Override
protected void doOKAction() {
String name = getEnteredName();
MultiMap<PsiElement, String> conflicts = new MultiMap<>();
assert name != null;
GroovyValidationUtil.validateNewParameterName(firstParam, conflicts, name);
if (isClosure) {
findClosureConflictUsages(conflicts, occurrences);
}
if (reportConflicts(conflicts, project)) {
performRefactoring(element, owner, occurrences, createNewFirst(), name, specifyTypeExplicitly());
}
super.doOKAction();
}
};
dialog.show();
} else {
//todo add statictics manager
performRefactoring(element, owner, occurrences, true, (new GroovyValidationUtil.ParameterNameSuggester("attrs", firstParam)).generateName(), true);
}
break;
}
}
}
Aggregations