Replaced predicate strategy with priority-returning strategy.

This commit is contained in:
Evgeny Gerashchenko
2013-03-28 18:17:21 +04:00
parent 69c5411269
commit 2e678a94f1
2 changed files with 28 additions and 11 deletions
@@ -17,11 +17,11 @@
package org.jetbrains.jet.lang.resolve.calls.tasks;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.psi.JetPsiUtil;
import org.jetbrains.jet.lang.psi.JetReferenceExpression;
@@ -31,9 +31,12 @@ import java.util.Collection;
import java.util.List;
public class ResolutionTaskHolder<D extends CallableDescriptor, F extends D> {
private static final int MIN_PRIORITY = 0;
private static final int MAX_PRIORITY = 1;
private final JetReferenceExpression reference;
private final BasicCallResolutionContext basicCallResolutionContext;
private final Predicate<ResolutionCandidate<D>> visibleStrategy;
private final PriorityProvider<ResolutionCandidate<D>> priorityProvider;
private final boolean isSafeCall;
private final Collection<Collection<ResolutionCandidate<D>>> localExtensions = Sets.newLinkedHashSet();
@@ -44,10 +47,11 @@ public class ResolutionTaskHolder<D extends CallableDescriptor, F extends D> {
public ResolutionTaskHolder(@NotNull JetReferenceExpression reference,
@NotNull BasicCallResolutionContext basicCallResolutionContext,
@NotNull Predicate<ResolutionCandidate<D>> visibleStrategy) {
@NotNull PriorityProvider<ResolutionCandidate<D>> priorityProvider
) {
this.reference = reference;
this.basicCallResolutionContext = basicCallResolutionContext;
this.visibleStrategy = visibleStrategy;
this.priorityProvider = priorityProvider;
this.isSafeCall = JetPsiUtil.isSafeCall(basicCallResolutionContext.call);
}
@@ -93,9 +97,15 @@ public class ResolutionTaskHolder<D extends CallableDescriptor, F extends D> {
}
candidateList.addAll(nonLocalExtensions);
for (Predicate<ResolutionCandidate<D>> visibilityStrategy : Lists.newArrayList(visibleStrategy, Predicates.not(visibleStrategy))) {
for (int priority = MAX_PRIORITY; priority >= MIN_PRIORITY; priority--) {
final int finalPriority = priority;
for (Collection<ResolutionCandidate<D>> candidates : candidateList) {
Collection<ResolutionCandidate<D>> filteredCandidates = Collections2.filter(candidates, visibilityStrategy);
Collection<ResolutionCandidate<D>> filteredCandidates = Collections2.filter(candidates, new Predicate<ResolutionCandidate<D>>() {
@Override
public boolean apply(@Nullable ResolutionCandidate<D> input) {
return finalPriority == priorityProvider.getPriority(input);
}
});
if (!filteredCandidates.isEmpty()) {
tasks.add(new ResolutionTask<D, F>(filteredCandidates, reference, basicCallResolutionContext));
}
@@ -104,4 +114,8 @@ public class ResolutionTaskHolder<D extends CallableDescriptor, F extends D> {
}
return tasks;
}
public interface PriorityProvider<D> {
int getPriority(D candidate);
}
}
@@ -16,7 +16,6 @@
package org.jetbrains.jet.lang.resolve.calls.tasks;
import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import com.intellij.openapi.progress.ProgressIndicatorProvider;
import org.jetbrains.annotations.NotNull;
@@ -26,8 +25,8 @@ import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetReferenceExpression;
import org.jetbrains.jet.lang.psi.JetSuperExpression;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.calls.context.BasicCallResolutionContext;
import org.jetbrains.jet.lang.resolve.calls.autocasts.AutoCastServiceImpl;
import org.jetbrains.jet.lang.resolve.calls.context.BasicCallResolutionContext;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.JetScopeUtils;
@@ -78,7 +77,7 @@ public class TaskPrioritizer {
public static <D extends CallableDescriptor, F extends D> List<ResolutionTask<D, F>> computePrioritizedTasks(@NotNull final BasicCallResolutionContext context, @NotNull Name name,
@NotNull JetReferenceExpression functionReference, @NotNull List<CallableDescriptorCollector<? extends D>> callableDescriptorCollectors) {
ReceiverValue explicitReceiver = context.call.getExplicitReceiver();
final JetScope scope;
JetScope scope;
if (explicitReceiver.exists() && explicitReceiver.getType() instanceof NamespaceType) {
// Receiver is a namespace
scope = explicitReceiver.getType().getMemberScope();
@@ -87,9 +86,13 @@ public class TaskPrioritizer {
else {
scope = context.scope;
}
Predicate<ResolutionCandidate<D>> visibleStrategy = new Predicate<ResolutionCandidate<D>>() {
ResolutionTaskHolder.PriorityProvider<ResolutionCandidate<D>> visibleStrategy = new ResolutionTaskHolder.PriorityProvider<ResolutionCandidate<D>>() {
@Override
public boolean apply(@Nullable ResolutionCandidate<D> call) {
public int getPriority(ResolutionCandidate<D> call) {
return isVisible(call) ? 1 : 0;
}
private boolean isVisible(ResolutionCandidate<D> call) {
if (call == null) return false;
D candidateDescriptor = call.getDescriptor();
if (ErrorUtils.isError(candidateDescriptor)) return true;