Refactoring: use selectors for chain scope behaviour implementation
This commit is contained in:
@@ -30,6 +30,8 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.jetbrains.jet.lang.resolve.scopes.JetScopeSelectorUtil.*;
|
||||
|
||||
public class ChainedScope implements JetScope {
|
||||
private final DeclarationDescriptor containingDeclaration;
|
||||
private final String debugName;
|
||||
@@ -46,57 +48,29 @@ public class ChainedScope implements JetScope {
|
||||
|
||||
@Override
|
||||
public ClassifierDescriptor getClassifier(@NotNull Name name) {
|
||||
for (JetScope scope : scopeChain) {
|
||||
ClassifierDescriptor classifier = scope.getClassifier(name);
|
||||
if (classifier != null) return classifier;
|
||||
}
|
||||
return null;
|
||||
return getFirstMatch(scopeChain, name, CLASSIFIER_DESCRIPTOR_SCOPE_SELECTOR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PackageViewDescriptor getPackage(@NotNull Name name) {
|
||||
for (JetScope jetScope : scopeChain) {
|
||||
PackageViewDescriptor aPackage = jetScope.getPackage(name);
|
||||
if (aPackage != null) {
|
||||
return aPackage;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return getFirstMatch(scopeChain, name, PACKAGE_SCOPE_SELECTOR);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Set<VariableDescriptor> getProperties(@NotNull Name name) {
|
||||
Set<VariableDescriptor> properties = Sets.newLinkedHashSet();
|
||||
for (JetScope jetScope : scopeChain) {
|
||||
properties.addAll(jetScope.getProperties(name));
|
||||
}
|
||||
return properties;
|
||||
return getFromAllScopes(scopeChain, name, NAMED_PROPERTIES_SCOPE_SELECTOR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public VariableDescriptor getLocalVariable(@NotNull Name name) {
|
||||
for (JetScope jetScope : scopeChain) {
|
||||
VariableDescriptor variable = jetScope.getLocalVariable(name);
|
||||
if (variable != null) {
|
||||
return variable;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return getFirstMatch(scopeChain, name, VARIABLE_DESCRIPTOR_SCOPE_SELECTOR);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Set<FunctionDescriptor> getFunctions(@NotNull Name name) {
|
||||
if (scopeChain.length == 0) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
Set<FunctionDescriptor> result = Sets.newLinkedHashSet();
|
||||
for (JetScope jetScope : scopeChain) {
|
||||
result.addAll(jetScope.getFunctions(name));
|
||||
}
|
||||
return result;
|
||||
return getFromAllScopes(scopeChain, name, NAMED_FUNCTION_SCOPE_SELECTOR);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
public class JetScopeSelectorUtil {
|
||||
@@ -84,6 +85,15 @@ public class JetScopeSelectorUtil {
|
||||
}
|
||||
};
|
||||
|
||||
public static final ScopeByNameSelector<VariableDescriptor> VARIABLE_DESCRIPTOR_SCOPE_SELECTOR =
|
||||
new ScopeByNameSelector<VariableDescriptor>() {
|
||||
@Nullable
|
||||
@Override
|
||||
public VariableDescriptor get(@NotNull JetScope scope, @NotNull Name name) {
|
||||
return scope.getLocalVariable(name);
|
||||
}
|
||||
};
|
||||
|
||||
public static final ScopeByNameMultiSelector<FunctionDescriptor> NAMED_FUNCTION_SCOPE_SELECTOR =
|
||||
new ScopeByNameMultiSelector<FunctionDescriptor>() {
|
||||
@NotNull
|
||||
@@ -110,4 +120,38 @@ public class JetScopeSelectorUtil {
|
||||
return scope.getAllDescriptors();
|
||||
}
|
||||
};
|
||||
|
||||
@Nullable
|
||||
public static <D extends DeclarationDescriptor> D getFirstMatch(
|
||||
@NotNull JetScope[] scopes,
|
||||
@NotNull Name name,
|
||||
@NotNull ScopeByNameSelector<D> descriptorSelector
|
||||
) {
|
||||
for (JetScope scope : scopes) {
|
||||
D descriptor = descriptorSelector.get(scope, name);
|
||||
|
||||
if (descriptor != null) {
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static <D extends DeclarationDescriptor> Set<D> getFromAllScopes(
|
||||
@NotNull JetScope[] scopes,
|
||||
@NotNull Name name,
|
||||
@NotNull ScopeByNameMultiSelector<D> descriptorsSelector
|
||||
) {
|
||||
if (scopes.length == 0) return Collections.emptySet();
|
||||
|
||||
Set<D> descriptors = Sets.newLinkedHashSet();
|
||||
|
||||
for (JetScope jetScope : scopes) {
|
||||
descriptors.addAll(descriptorsSelector.get(jetScope, name));
|
||||
}
|
||||
|
||||
return descriptors;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user