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.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.jetbrains.jet.lang.resolve.scopes.JetScopeSelectorUtil.*;
|
||||||
|
|
||||||
public class ChainedScope implements JetScope {
|
public class ChainedScope implements JetScope {
|
||||||
private final DeclarationDescriptor containingDeclaration;
|
private final DeclarationDescriptor containingDeclaration;
|
||||||
private final String debugName;
|
private final String debugName;
|
||||||
@@ -46,57 +48,29 @@ public class ChainedScope implements JetScope {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ClassifierDescriptor getClassifier(@NotNull Name name) {
|
public ClassifierDescriptor getClassifier(@NotNull Name name) {
|
||||||
for (JetScope scope : scopeChain) {
|
return getFirstMatch(scopeChain, name, CLASSIFIER_DESCRIPTOR_SCOPE_SELECTOR);
|
||||||
ClassifierDescriptor classifier = scope.getClassifier(name);
|
|
||||||
if (classifier != null) return classifier;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PackageViewDescriptor getPackage(@NotNull Name name) {
|
public PackageViewDescriptor getPackage(@NotNull Name name) {
|
||||||
for (JetScope jetScope : scopeChain) {
|
return getFirstMatch(scopeChain, name, PACKAGE_SCOPE_SELECTOR);
|
||||||
PackageViewDescriptor aPackage = jetScope.getPackage(name);
|
|
||||||
if (aPackage != null) {
|
|
||||||
return aPackage;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public Set<VariableDescriptor> getProperties(@NotNull Name name) {
|
public Set<VariableDescriptor> getProperties(@NotNull Name name) {
|
||||||
Set<VariableDescriptor> properties = Sets.newLinkedHashSet();
|
return getFromAllScopes(scopeChain, name, NAMED_PROPERTIES_SCOPE_SELECTOR);
|
||||||
for (JetScope jetScope : scopeChain) {
|
|
||||||
properties.addAll(jetScope.getProperties(name));
|
|
||||||
}
|
|
||||||
return properties;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public VariableDescriptor getLocalVariable(@NotNull Name name) {
|
public VariableDescriptor getLocalVariable(@NotNull Name name) {
|
||||||
for (JetScope jetScope : scopeChain) {
|
return getFirstMatch(scopeChain, name, VARIABLE_DESCRIPTOR_SCOPE_SELECTOR);
|
||||||
VariableDescriptor variable = jetScope.getLocalVariable(name);
|
|
||||||
if (variable != null) {
|
|
||||||
return variable;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public Set<FunctionDescriptor> getFunctions(@NotNull Name name) {
|
public Set<FunctionDescriptor> getFunctions(@NotNull Name name) {
|
||||||
if (scopeChain.length == 0) {
|
return getFromAllScopes(scopeChain, name, NAMED_FUNCTION_SCOPE_SELECTOR);
|
||||||
return Collections.emptySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
Set<FunctionDescriptor> result = Sets.newLinkedHashSet();
|
|
||||||
for (JetScope jetScope : scopeChain) {
|
|
||||||
result.addAll(jetScope.getFunctions(name));
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import org.jetbrains.jet.lang.descriptors.*;
|
|||||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class JetScopeSelectorUtil {
|
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 =
|
public static final ScopeByNameMultiSelector<FunctionDescriptor> NAMED_FUNCTION_SCOPE_SELECTOR =
|
||||||
new ScopeByNameMultiSelector<FunctionDescriptor>() {
|
new ScopeByNameMultiSelector<FunctionDescriptor>() {
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -110,4 +120,38 @@ public class JetScopeSelectorUtil {
|
|||||||
return scope.getAllDescriptors();
|
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