use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.
the class GrReassignedLocalVarsChecker method isReassignedVarImpl.
private static boolean isReassignedVarImpl(@NotNull final GrVariable resolved) {
final GrControlFlowOwner variableScope = PsiTreeUtil.getParentOfType(resolved, GrCodeBlock.class, GroovyFile.class);
if (variableScope == null)
return false;
final String name = resolved.getName();
final Ref<Boolean> isReassigned = Ref.create(false);
for (PsiElement scope = resolved.getParent().getNextSibling(); scope != null; scope = scope.getNextSibling()) {
if (scope instanceof GroovyPsiElement) {
((GroovyPsiElement) scope).accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
if (getUsedVarsInsideBlock(closure).contains(name)) {
isReassigned.set(true);
}
}
@Override
public void visitElement(@NotNull GroovyPsiElement element) {
if (isReassigned.get())
return;
super.visitElement(element);
}
});
if (isReassigned.get())
break;
}
}
return isReassigned.get();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock 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.blocks.GrClosableBlock 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;
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.
the class ConvertJavaStyleArrayIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
if (!(element instanceof GrMethodCallExpression))
return false;
final GrExpression expression = ((GrMethodCallExpression) element).getInvokedExpression();
if (!(expression instanceof GrNewExpression))
return false;
if (((GrNewExpression) expression).getArrayCount() == 0)
return false;
if (!((GrMethodCallExpression) element).getArgumentList().getText().trim().isEmpty())
return false;
final GrClosableBlock[] closureArguments = ((GrMethodCallExpression) element).getClosureArguments();
if (closureArguments.length != 1)
return false;
final GrClosableBlock block = closureArguments[0];
if (block.getLBrace() == null || block.getRBrace() == null)
return false;
return true;
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.
the class GrNamedArgumentSearchVisitor method find.
public static Map<String, NamedArgumentDescriptor> find(GrVariable variable) {
final GrExpression initializerGroovy = variable.getInitializerGroovy();
if (!(initializerGroovy instanceof GrClosableBlock)) {
return Collections.emptyMap();
}
final GrClosableBlock closure = (GrClosableBlock) initializerGroovy;
final GrParameter[] parameters = closure.getAllParameters();
if (parameters.length == 0)
return Collections.emptyMap();
GrParameter parameter = parameters[0];
GrNamedArgumentSearchVisitor visitor = new GrNamedArgumentSearchVisitor(parameter.getName());
closure.accept(visitor);
return visitor.getResult();
}
Aggregations