From 6f47555d703fef0844cb6b2433a8222316194da3 Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Wed, 4 Jul 2012 17:15:54 +0400 Subject: [PATCH] Move JsConfiguration class to top level --- .../k2js/analyze/AnalyzerFacadeForJS.java | 60 ---------- .../k2js/analyze/JsConfiguration.java | 104 ++++++++++++++++++ 2 files changed, 104 insertions(+), 60 deletions(-) create mode 100644 js/js.translator/src/org/jetbrains/k2js/analyze/JsConfiguration.java diff --git a/js/js.translator/src/org/jetbrains/k2js/analyze/AnalyzerFacadeForJS.java b/js/js.translator/src/org/jetbrains/k2js/analyze/AnalyzerFacadeForJS.java index cc1ce4e3b43..471221e5cfb 100644 --- a/js/js.translator/src/org/jetbrains/k2js/analyze/AnalyzerFacadeForJS.java +++ b/js/js.translator/src/org/jetbrains/k2js/analyze/AnalyzerFacadeForJS.java @@ -25,37 +25,21 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.analyzer.AnalyzeExhaust; import org.jetbrains.jet.analyzer.AnalyzerFacadeForEverything; import org.jetbrains.jet.di.InjectorForTopDownAnalyzerForJs; -import org.jetbrains.jet.lang.DefaultModuleConfiguration; -import org.jetbrains.jet.lang.ModuleConfiguration; import org.jetbrains.jet.lang.descriptors.ModuleDescriptor; -import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor; import org.jetbrains.jet.lang.psi.JetFile; -import org.jetbrains.jet.lang.psi.JetImportDirective; -import org.jetbrains.jet.lang.psi.JetPsiFactory; import org.jetbrains.jet.lang.resolve.*; -import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.Name; -import org.jetbrains.jet.lang.resolve.scopes.WritableScope; -import org.jetbrains.jet.lang.types.lang.JetStandardClasses; import org.jetbrains.k2js.config.Config; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; -import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isRootNamespace; - /** * @author Pavel Talanov */ public final class AnalyzerFacadeForJS { - @NotNull - public static final List DEFAULT_IMPORT_PATHS = Arrays.asList(new ImportPath("js.*"), new ImportPath("java.lang.*"), - new ImportPath(JetStandardClasses.STANDARD_CLASSES_FQNAME, true), - new ImportPath("kotlin.*")); - private AnalyzerFacadeForJS() { } @@ -148,48 +132,4 @@ public final class AnalyzerFacadeForJS { } }; } - - private static final class JsConfiguration implements ModuleConfiguration { - - @NotNull - private final Project project; - @Nullable - private final BindingContext contextToBaseOn; - - private JsConfiguration(@NotNull Project project, @Nullable BindingContext contextToBaseOn) { - this.project = project; - this.contextToBaseOn = contextToBaseOn; - } - - @Override - public void addDefaultImports(@NotNull Collection directives) { - for (ImportPath path : DEFAULT_IMPORT_PATHS) { - directives.add(JetPsiFactory.createImportDirective(project, path)); - } - } - - @Override - public void extendNamespaceScope(@NotNull BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, - @NotNull WritableScope namespaceMemberScope) { - DefaultModuleConfiguration.createStandardConfiguration(project, true) - .extendNamespaceScope(trace, namespaceDescriptor, namespaceMemberScope); - if (contextToBaseOn == null) { - return; - } - if (isNamespaceImportedByDefault(namespaceDescriptor) || isRootNamespace(namespaceDescriptor)) { - FqName descriptorName = DescriptorUtils.getFQName(namespaceDescriptor).toSafe(); - NamespaceDescriptor alreadyAnalyzedNamespace = contextToBaseOn.get(BindingContext.FQNAME_TO_NAMESPACE_DESCRIPTOR, descriptorName); - namespaceMemberScope.importScope(alreadyAnalyzedNamespace.getMemberScope()); - } - } - - private static boolean isNamespaceImportedByDefault(@NotNull NamespaceDescriptor namespaceDescriptor) { - for (ImportPath path : DEFAULT_IMPORT_PATHS) { - if (path.fqnPart().equals(DescriptorUtils.getFQName(namespaceDescriptor).toSafe())) { - return true; - } - } - return false; - } - } } diff --git a/js/js.translator/src/org/jetbrains/k2js/analyze/JsConfiguration.java b/js/js.translator/src/org/jetbrains/k2js/analyze/JsConfiguration.java new file mode 100644 index 00000000000..622f495f250 --- /dev/null +++ b/js/js.translator/src/org/jetbrains/k2js/analyze/JsConfiguration.java @@ -0,0 +1,104 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.k2js.analyze; + +import com.intellij.openapi.project.Project; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.DefaultModuleConfiguration; +import org.jetbrains.jet.lang.ModuleConfiguration; +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.BindingContext; +import org.jetbrains.jet.lang.resolve.BindingTrace; +import org.jetbrains.jet.lang.resolve.DescriptorUtils; +import org.jetbrains.jet.lang.resolve.ImportPath; +import org.jetbrains.jet.lang.resolve.name.FqName; +import org.jetbrains.jet.lang.resolve.scopes.WritableScope; +import org.jetbrains.jet.lang.types.lang.JetStandardClasses; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isRootNamespace; + +/** +* @author Pavel Talanov +*/ +public final class JsConfiguration implements ModuleConfiguration { + + @NotNull + private static final List DEFAULT_IMPORT_PATHS = Arrays.asList(new ImportPath("js.*"), new ImportPath("java.lang.*"), + new ImportPath(JetStandardClasses.STANDARD_CLASSES_FQNAME, + true), + new ImportPath("kotlin.*")); + @NotNull + private final Project project; + /* + * Adds a possibility to inject some preanalyzed files to speed up tests. + * */ + @Nullable + private final BindingContext preanalyzedContext; + + JsConfiguration(@NotNull Project project, @Nullable BindingContext preanalyzedContext) { + this.project = project; + this.preanalyzedContext = preanalyzedContext; + } + + @Override + public void addDefaultImports(@NotNull Collection directives) { + for (ImportPath path : DEFAULT_IMPORT_PATHS) { + directives.add(JetPsiFactory.createImportDirective(project, path)); + } + } + + @Override + public void extendNamespaceScope(@NotNull BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, + @NotNull WritableScope namespaceMemberScope) { + DefaultModuleConfiguration.createStandardConfiguration(project, true) + .extendNamespaceScope(trace, namespaceDescriptor, namespaceMemberScope); + if (hasPreanalyzedContextForTests()) { + extendScopeWithPreAnalyzedContextForTests(namespaceDescriptor, namespaceMemberScope); + } + } + + private boolean hasPreanalyzedContextForTests() { + return preanalyzedContext != null; + } + + /*NOTE: this code is wrong. Check it if you have tests failing for frontend reasons*/ + @SuppressWarnings("ConstantConditions") + private void extendScopeWithPreAnalyzedContextForTests(@NotNull NamespaceDescriptor namespaceDescriptor, + @NotNull WritableScope namespaceMemberScope) { + if (isNamespaceImportedByDefault(namespaceDescriptor) || isRootNamespace(namespaceDescriptor)) { + FqName descriptorName = DescriptorUtils.getFQName(namespaceDescriptor).toSafe(); + NamespaceDescriptor alreadyAnalyzedNamespace = preanalyzedContext.get(BindingContext.FQNAME_TO_NAMESPACE_DESCRIPTOR, descriptorName); + namespaceMemberScope.importScope(alreadyAnalyzedNamespace.getMemberScope()); + } + } + + private static boolean isNamespaceImportedByDefault(@NotNull NamespaceDescriptor namespaceDescriptor) { + for (ImportPath path : DEFAULT_IMPORT_PATHS) { + if (path.fqnPart().equals(DescriptorUtils.getFQName(namespaceDescriptor).toSafe())) { + return true; + } + } + return false; + } +}