Minimal refactoring that eliminates importing Java's root namespace by default.
All tests pass. Some beautifying needed #KT-1477 Fixed
This commit is contained in:
@@ -53,4 +53,13 @@ public class DefaultModuleConfiguration implements ModuleConfiguration {
|
||||
public void extendNamespaceScope(@NotNull BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public NamespaceDescriptor getTopLevelNamespace(@NotNull String shortName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAllTopLevelNamespacesTo(@NotNull Collection<? super NamespaceDescriptor> topLevelNamespaces) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.jet.lang;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetImportDirective;
|
||||
import org.jetbrains.jet.lang.resolve.BindingTrace;
|
||||
@@ -36,6 +37,15 @@ public interface ModuleConfiguration {
|
||||
@Override
|
||||
public void extendNamespaceScope(@NotNull BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public NamespaceDescriptor getTopLevelNamespace(@NotNull String shortName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAllTopLevelNamespacesTo(@NotNull Collection<? super NamespaceDescriptor> topLevelNamespaces) {
|
||||
}
|
||||
};
|
||||
|
||||
void addDefaultImports(@NotNull WritableScope rootScope, @NotNull Collection<JetImportDirective> directives);
|
||||
@@ -45,4 +55,10 @@ public interface ModuleConfiguration {
|
||||
* This method is called every time a namespace descriptor is created. Use it to add extra descriptors to the namespace, e.g. merge a Java package with a Kotlin one
|
||||
*/
|
||||
void extendNamespaceScope(@NotNull BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope);
|
||||
|
||||
/** This method is called only if no namespace with the same short name is declared in the module itself, or to merge namespaces */
|
||||
@Nullable
|
||||
NamespaceDescriptor getTopLevelNamespace(@NotNull String shortName);
|
||||
|
||||
void addAllTopLevelNamespacesTo(@NotNull Collection<? super NamespaceDescriptor> topLevelNamespaces);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.jet.lang.resolve;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
@@ -32,6 +33,7 @@ import org.jetbrains.jet.lang.diagnostics.DiagnosticHolder;
|
||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.JetScopeAdapter;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
|
||||
import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl;
|
||||
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
|
||||
@@ -96,7 +98,7 @@ public class AnalyzingUtils {
|
||||
|
||||
public static BindingContext analyzeFilesWithGivenTrace(
|
||||
@NotNull Project project,
|
||||
@NotNull ModuleConfiguration configuration,
|
||||
@NotNull final ModuleConfiguration configuration,
|
||||
@NotNull Collection<JetFile> files,
|
||||
@NotNull Predicate<PsiFile> filesToAnalyzeCompletely,
|
||||
@NotNull JetControlFlowDataTraceFactory flowDataTraceFactory,
|
||||
@@ -112,46 +114,71 @@ public class AnalyzingUtils {
|
||||
scope.importScope(libraryScope);
|
||||
scope.changeLockLevel(WritableScope.LockLevel.BOTH);
|
||||
|
||||
TopDownAnalyzer.process(project, bindingTraceContext, scope, new NamespaceLike.Adapter(owner) {
|
||||
final Map<String, NamespaceDescriptorImpl> declaredNamespaces = Maps.newHashMap();
|
||||
|
||||
private Map<String, NamespaceDescriptorImpl> declaredNamespaces = Maps.newHashMap();
|
||||
TopDownAnalyzer.process(project, bindingTraceContext,
|
||||
new JetScopeAdapter(scope) {
|
||||
@Override
|
||||
public NamespaceDescriptor getNamespace(@NotNull String name) {
|
||||
NamespaceDescriptor topLevelNamespace = declaredNamespaces.get(name);
|
||||
if (topLevelNamespace != null) {
|
||||
return topLevelNamespace;
|
||||
}
|
||||
NamespaceDescriptor topLevelNamespaceFromConfiguration = configuration.getTopLevelNamespace(name);
|
||||
if (topLevelNamespaceFromConfiguration != null) {
|
||||
return topLevelNamespaceFromConfiguration;
|
||||
}
|
||||
return super.getNamespace(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NamespaceDescriptorImpl getNamespace(String name) {
|
||||
return declaredNamespaces.get(name);
|
||||
}
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<DeclarationDescriptor> getAllDescriptors() {
|
||||
List<DeclarationDescriptor> allDescriptors = Lists.newArrayList(super.getAllDescriptors());
|
||||
allDescriptors.addAll(declaredNamespaces.values());
|
||||
configuration.addAllTopLevelNamespacesTo(allDescriptors);
|
||||
return allDescriptors;
|
||||
}
|
||||
},
|
||||
new NamespaceLike.Adapter(owner) {
|
||||
|
||||
@Override
|
||||
public void addNamespace(@NotNull NamespaceDescriptor namespaceDescriptor) {
|
||||
scope.addNamespace(namespaceDescriptor);
|
||||
declaredNamespaces.put(namespaceDescriptor.getName(), (NamespaceDescriptorImpl) namespaceDescriptor);
|
||||
}
|
||||
@Override
|
||||
public NamespaceDescriptorImpl getNamespace(String name) {
|
||||
return declaredNamespaces.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addClassifierDescriptor(@NotNull MutableClassDescriptorLite classDescriptor) {
|
||||
throw new IllegalStateException("A class shouldn't sit right under a module: " + classDescriptor);
|
||||
}
|
||||
@Override
|
||||
public void addNamespace(@NotNull NamespaceDescriptor namespaceDescriptor) {
|
||||
scope.addNamespace(namespaceDescriptor);
|
||||
declaredNamespaces.put(namespaceDescriptor.getName(), (NamespaceDescriptorImpl) namespaceDescriptor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addObjectDescriptor(@NotNull MutableClassDescriptorLite objectDescriptor) {
|
||||
throw new IllegalStateException("An object shouldn't sit right under a module: " + objectDescriptor);
|
||||
}
|
||||
@Override
|
||||
public void addClassifierDescriptor(@NotNull MutableClassDescriptorLite classDescriptor) {
|
||||
throw new IllegalStateException("A class shouldn't sit right under a module: " + classDescriptor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFunctionDescriptor(@NotNull SimpleFunctionDescriptor functionDescriptor) {
|
||||
throw new IllegalStateException("A function shouldn't sit right under a module: " + functionDescriptor);
|
||||
}
|
||||
@Override
|
||||
public void addObjectDescriptor(@NotNull MutableClassDescriptorLite objectDescriptor) {
|
||||
throw new IllegalStateException("An object shouldn't sit right under a module: " + objectDescriptor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPropertyDescriptor(@NotNull PropertyDescriptor propertyDescriptor) {
|
||||
throw new IllegalStateException("A property shouldn't sit right under a module: " + propertyDescriptor);
|
||||
}
|
||||
@Override
|
||||
public void addFunctionDescriptor(@NotNull SimpleFunctionDescriptor functionDescriptor) {
|
||||
throw new IllegalStateException("A function shouldn't sit right under a module: " + functionDescriptor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassObjectStatus setClassObjectDescriptor(@NotNull MutableClassDescriptorLite classObjectDescriptor) {
|
||||
throw new IllegalStateException("Must be guaranteed not to happen by the parser");
|
||||
}
|
||||
}, files, filesToAnalyzeCompletely, flowDataTraceFactory, configuration);
|
||||
@Override
|
||||
public void addPropertyDescriptor(@NotNull PropertyDescriptor propertyDescriptor) {
|
||||
throw new IllegalStateException("A property shouldn't sit right under a module: " + propertyDescriptor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassObjectStatus setClassObjectDescriptor(@NotNull MutableClassDescriptorLite classObjectDescriptor) {
|
||||
throw new IllegalStateException("Must be guaranteed not to happen by the parser");
|
||||
}
|
||||
},
|
||||
files, filesToAnalyzeCompletely, flowDataTraceFactory, configuration);
|
||||
|
||||
return bindingTraceContext.getBindingContext();
|
||||
}
|
||||
|
||||
@@ -144,6 +144,8 @@ public interface BindingContext {
|
||||
return declarationDescriptor.getOriginal();
|
||||
}
|
||||
};
|
||||
WritableSlice<JetFile, NamespaceDescriptor> FILE_TO_NAMESPACE = Slices.createSimpleSlice();
|
||||
|
||||
ReadOnlySlice<DeclarationDescriptor, PsiElement> DESCRIPTOR_TO_DECLARATION = Slices.<DeclarationDescriptor, PsiElement>sliceBuilder().setKeyNormalizer(DECLARATION_DESCRIPTOR_NORMALIZER).build();
|
||||
|
||||
WritableSlice<PsiElement, NamespaceDescriptor> NAMESPACE = Slices.<PsiElement, NamespaceDescriptor>sliceBuilder().setOpposite((WritableSlice) DESCRIPTOR_TO_DECLARATION).build();
|
||||
|
||||
@@ -167,7 +167,7 @@ public class TopDownAnalyzer {
|
||||
|
||||
public static void processObject(
|
||||
@NotNull Project project,
|
||||
@NotNull BindingTrace trace,
|
||||
@NotNull final BindingTrace trace,
|
||||
@NotNull JetScope outerScope,
|
||||
@NotNull DeclarationDescriptor containingDeclaration,
|
||||
@NotNull JetObjectDeclaration object) {
|
||||
@@ -175,6 +175,9 @@ public class TopDownAnalyzer {
|
||||
|
||||
@Override
|
||||
public NamespaceDescriptorImpl getNamespace(String name) {
|
||||
if (name == "") {
|
||||
return (NamespaceDescriptorImpl) trace.get(BindingContext.FQNAME_TO_NAMESPACE_DESCRIPTOR, "");
|
||||
}
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
@@ -84,6 +84,9 @@ public class TypeHierarchyResolver {
|
||||
|
||||
|
||||
public void process(@NotNull JetScope outerScope, @NotNull NamespaceLike owner, @NotNull Collection<? extends PsiElement> declarations) {
|
||||
// Create the default package
|
||||
createNamespaceDescriptorIfNeeded(null, owner, "");
|
||||
|
||||
collectNamespacesAndClassifiers(outerScope, outerScope, owner, declarations); // namespaceScopes, classes
|
||||
|
||||
importsResolver.processTypeImports();
|
||||
@@ -228,7 +231,6 @@ public class TypeHierarchyResolver {
|
||||
}
|
||||
|
||||
private NamespaceDescriptorImpl createNamespaceDescriptorPathIfNeeded(JetFile file, NamespaceLike owner, JetScope outerScope) {
|
||||
|
||||
NamespaceLike currentOwner = owner;
|
||||
|
||||
for (JetSimpleNameExpression nameExpression : file.getNamespaceHeader().getParentNamespaceNames()) {
|
||||
@@ -248,13 +250,10 @@ public class TypeHierarchyResolver {
|
||||
context.getTrace().record(RESOLUTION_SCOPE, file.getNamespaceHeader(), outerScope);
|
||||
|
||||
return createNamespaceDescriptorIfNeeded(file, currentOwner, name);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private NamespaceDescriptorImpl createNamespaceDescriptorIfNeeded(@Nullable JetFile file, @NotNull NamespaceLike owner, String name) {
|
||||
|
||||
NamespaceDescriptorImpl namespaceDescriptor = owner.getNamespace(name);
|
||||
if (namespaceDescriptor == null) {
|
||||
namespaceDescriptor = new NamespaceDescriptorImpl(
|
||||
@@ -273,6 +272,10 @@ public class TypeHierarchyResolver {
|
||||
}
|
||||
}
|
||||
|
||||
if (file != null) {
|
||||
context.getTrace().record(BindingContext.FILE_TO_NAMESPACE, file, namespaceDescriptor);
|
||||
}
|
||||
|
||||
return namespaceDescriptor;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user