KT-816 Add std namespace in to default imports

This commit is contained in:
svtk
2011-12-15 16:24:47 +04:00
parent cbe6dcc8c2
commit 5c819cb77e
8 changed files with 95 additions and 34 deletions
@@ -12,6 +12,8 @@ import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.PsiModificationTracker;
import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.StandardConfiguration;
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils;
import org.jetbrains.jet.lang.diagnostics.Errors;
@@ -80,12 +82,15 @@ public class AnalyzerFacade {
public static BindingContext analyzeNamespacesWithJavaIntegration(Project project, Collection<? extends JetDeclaration> declarations, Predicate<PsiFile> filesToAnalyzeCompletely, JetControlFlowDataTraceFactory flowDataTraceFactory) {
BindingTraceContext bindingTraceContext = new BindingTraceContext();
return AnalyzingUtils.getInstance().analyzeNamespacesWithGivenTrace(
JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(project);
return AnalyzingUtils.analyzeNamespacesWithGivenTrace(
project,
JavaBridgeConfiguration.createJavaBridgeConfiguration(project, bindingTraceContext),
JavaBridgeConfiguration.createJavaBridgeConfiguration(project, bindingTraceContext, StandardConfiguration.createStandardConfiguration(project, semanticServices)),
declarations,
filesToAnalyzeCompletely,
flowDataTraceFactory, bindingTraceContext);
flowDataTraceFactory,
bindingTraceContext,
semanticServices);
}
public static BindingContext shallowAnalyzeFiles(Collection<PsiFile> files) {
@@ -14,24 +14,28 @@ import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
*/
public class JavaBridgeConfiguration implements Configuration {
public static Configuration createJavaBridgeConfiguration(@NotNull Project project, @NotNull BindingTrace trace) {
return new JavaBridgeConfiguration(project, trace);
public static Configuration createJavaBridgeConfiguration(@NotNull Project project, @NotNull BindingTrace trace, Configuration delegateConfiguration) {
return new JavaBridgeConfiguration(project, trace, delegateConfiguration);
}
private final JavaSemanticServices javaSemanticServices;
private final Configuration delegateConfiguration;
private JavaBridgeConfiguration(Project project, BindingTrace trace) {
private JavaBridgeConfiguration(Project project, BindingTrace trace, Configuration delegateConfiguration) {
this.javaSemanticServices = new JavaSemanticServices(project, JetSemanticServices.createSemanticServices(project), trace);
this.delegateConfiguration = delegateConfiguration;
}
@Override
public void addDefaultImports(BindingTrace trace, WritableScope rootScope) {
public void addDefaultImports(@NotNull BindingTrace trace, @NotNull WritableScope rootScope) {
rootScope.importScope(new JavaPackageScope("", null, javaSemanticServices));
rootScope.importScope(new JavaPackageScope("java.lang", null, javaSemanticServices));
delegateConfiguration.addDefaultImports(trace, rootScope);
}
@Override
public void extendNamespaceScope(BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope) {
public void extendNamespaceScope(@NotNull BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope) {
namespaceMemberScope.importScope(new JavaPackageScope(DescriptorUtils.getFQName(namespaceDescriptor), namespaceDescriptor, javaSemanticServices));
delegateConfiguration.extendNamespaceScope(trace, namespaceDescriptor, namespaceMemberScope);
}
}
@@ -11,19 +11,19 @@ import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
public interface Configuration {
Configuration EMPTY = new Configuration() {
@Override
public void addDefaultImports(BindingTrace trace, WritableScope rootScope) {
public void addDefaultImports(@NotNull BindingTrace trace, @NotNull WritableScope rootScope) {
}
@Override
public void extendNamespaceScope(BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope) {
public void extendNamespaceScope(@NotNull BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope) {
}
};
void addDefaultImports(BindingTrace trace, WritableScope rootScope);
void addDefaultImports(@NotNull BindingTrace trace, @NotNull WritableScope rootScope);
/**
*
* 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(BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope);
void extendNamespaceScope(@NotNull BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope);
}
@@ -0,0 +1,42 @@
package org.jetbrains.jet.lang;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.psi.JetImportDirective;
import org.jetbrains.jet.lang.psi.JetPsiFactory;
import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.ImportsResolver;
import org.jetbrains.jet.lang.resolve.TemporaryBindingTrace;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
/**
* @author svtk
*/
public class StandardConfiguration implements Configuration {
private Project project;
private JetSemanticServices services;
public static StandardConfiguration createStandardConfiguration(Project project, JetSemanticServices services) {
return new StandardConfiguration(services, project);
}
private StandardConfiguration(JetSemanticServices services, Project project) {
this.services = services;
this.project = project;
}
@Override
public void addDefaultImports(@NotNull BindingTrace trace, @NotNull WritableScope rootScope) {
TemporaryBindingTrace temporaryTrace = TemporaryBindingTrace.create(trace);
JetImportDirective importDirective = JetPsiFactory.createImportDirective(project, "std.*");
if (ImportsResolver.importNamespace(importDirective, rootScope, temporaryTrace, services)) {
temporaryTrace.commit();
}
}
@Override
public void extendNamespaceScope(@NotNull BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope) {
}
}
@@ -31,12 +31,6 @@ import java.util.Map;
*/
public class AnalyzingUtils {
private static final AnalyzingUtils INSTANCE = new AnalyzingUtils();
public static AnalyzingUtils getInstance() {
return INSTANCE;
}
public static void checkForSyntacticErrors(@NotNull PsiElement root) {
root.acceptChildren(new PsiElementVisitor() {
@Override
@@ -75,19 +69,25 @@ public class AnalyzingUtils {
// --------------------------------------------------------------------------------------------------------------------------
public BindingContext analyzeNamespaces(
public static BindingContext analyzeNamespaces(
@NotNull Project project,
@NotNull Configuration configuration,
@NotNull Collection<? extends JetDeclaration> declarations,
@NotNull Predicate<PsiFile> filesToAnalyzeCompletely,
@NotNull JetControlFlowDataTraceFactory flowDataTraceFactory) {
@NotNull JetControlFlowDataTraceFactory flowDataTraceFactory,
@NotNull JetSemanticServices semanticServices) {
BindingTraceContext bindingTraceContext = new BindingTraceContext();
return analyzeNamespacesWithGivenTrace(project, configuration, declarations, filesToAnalyzeCompletely, flowDataTraceFactory, bindingTraceContext);
return analyzeNamespacesWithGivenTrace(project, configuration, declarations, filesToAnalyzeCompletely, flowDataTraceFactory, bindingTraceContext, semanticServices);
}
public BindingContext analyzeNamespacesWithGivenTrace(Project project, Configuration configuration, Collection<? extends JetDeclaration> declarations, Predicate<PsiFile> filesToAnalyzeCompletely, JetControlFlowDataTraceFactory flowDataTraceFactory, BindingTraceContext bindingTraceContext) {
JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(project);
public static BindingContext analyzeNamespacesWithGivenTrace(
@NotNull Project project,
@NotNull Configuration configuration,
@NotNull Collection<? extends JetDeclaration> declarations,
@NotNull Predicate<PsiFile> filesToAnalyzeCompletely,
@NotNull JetControlFlowDataTraceFactory flowDataTraceFactory,
@NotNull BindingTraceContext bindingTraceContext,
@NotNull JetSemanticServices semanticServices) {
JetScope libraryScope = semanticServices.getStandardLibrary().getLibraryScope();
ModuleDescriptor owner = new ModuleDescriptor("<module>");
@@ -1,6 +1,7 @@
package org.jetbrains.jet.lang.resolve;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.psi.*;
@@ -40,14 +41,7 @@ public class ImportsResolver {
continue;
}
if (importDirective.isAllUnder()) {
JetExpression importedReference = importDirective.getImportedReference();
if (importedReference != null) {
ExpressionTypingServices typeInferrerServices = context.getSemanticServices().getTypeInferrerServices(context.getTrace());
JetType type = typeInferrerServices.getTypeWithNamespaces(namespaceScope, importedReference);
if (type != null) {
namespaceScope.importScope(type.getMemberScope());
}
}
importNamespace(importDirective, namespaceScope, context.getTrace(), context.getSemanticServices());
}
else {
ClassifierDescriptor classifierDescriptor = null;
@@ -105,4 +99,17 @@ public class ImportsResolver {
}
}
}
public static boolean importNamespace(JetImportDirective importDirective, WritableScope namespaceScope, @NotNull BindingTrace trace, @NotNull JetSemanticServices services) {
JetExpression importedReference = importDirective.getImportedReference();
if (importedReference != null) {
ExpressionTypingServices typeInferrerServices = services.getTypeInferrerServices(trace);
JetType type = typeInferrerServices.getTypeWithNamespaces(namespaceScope, importedReference);
if (type != null) {
namespaceScope.importScope(type.getMemberScope());
return true;
}
}
return false;
}
}
@@ -91,7 +91,9 @@ public class TopDownAnalyzer {
public static void processStandardLibraryNamespace(
@NotNull JetSemanticServices semanticServices,
@NotNull BindingTrace trace,
@NotNull WritableScope outerScope, @NotNull NamespaceDescriptorImpl standardLibraryNamespace, @NotNull JetNamespace namespace) {
@NotNull WritableScope outerScope,
@NotNull NamespaceDescriptorImpl standardLibraryNamespace,
@NotNull JetNamespace namespace) {
TopDownAnalysisContext context = new TopDownAnalysisContext(semanticServices, trace, Predicates.<PsiFile>alwaysTrue(), Configuration.EMPTY, false);
context.getNamespaceScopes().put(namespace, standardLibraryNamespace.getMemberScope());
context.getNamespaceDescriptors().put(namespace, standardLibraryNamespace);
@@ -10,6 +10,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetLiteFixture;
import org.jetbrains.jet.JetTestCaseBuilder;
import org.jetbrains.jet.lang.Configuration;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils;
import org.jetbrains.jet.lang.psi.JetDeclaration;
@@ -94,7 +95,7 @@ public class JetDiagnosticsTest extends JetLiteFixture {
bindingContext = AnalyzerFacade.analyzeNamespacesWithJavaIntegration(getProject(), namespaces, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY);
}
else {
bindingContext = AnalyzingUtils.getInstance().analyzeNamespaces(getProject(), Configuration.EMPTY, namespaces, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY);
bindingContext = AnalyzingUtils.analyzeNamespaces(getProject(), Configuration.EMPTY, namespaces, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY, JetSemanticServices.createSemanticServices(getProject()));
}
StringBuilder actualText = new StringBuilder();