Introduce LocalDescriptorResolver which handles differences in local declaration resolution between compiler and ide environments

This commit is contained in:
Pavel V. Talanov
2015-07-24 18:56:06 +03:00
parent 2ef5e03f27
commit 52bc84cb5f
13 changed files with 149 additions and 25 deletions
@@ -30,6 +30,7 @@ import org.jetbrains.kotlin.idea.framework.KotlinJavaScriptLibraryDetectionUtil
import org.jetbrains.kotlin.js.analyze.TopDownAnalyzerFacadeForJS
import org.jetbrains.kotlin.js.resolve.JsPlatform
import org.jetbrains.kotlin.resolve.BindingTraceContext
import org.jetbrains.kotlin.resolve.TargetEnvironment
import org.jetbrains.kotlin.resolve.lazy.ResolveSession
import org.jetbrains.kotlin.resolve.lazy.declarations.DeclarationProviderFactoryService
import org.jetbrains.kotlin.serialization.js.KotlinJavascriptSerializationUtil
@@ -48,7 +49,9 @@ public object JsAnalyzerFacade : AnalyzerFacade<JsResolverForModule, PlatformAna
moduleDescriptor: ModuleDescriptorImpl,
moduleContext: ModuleContext,
moduleContent: ModuleContent,
platformParameters: PlatformAnalysisParameters, resolverForProject: ResolverForProject<M, JsResolverForModule>
platformParameters: PlatformAnalysisParameters,
targetEnvironment: TargetEnvironment,
resolverForProject: ResolverForProject<M, JsResolverForModule>
): JsResolverForModule {
val (syntheticFiles, moduleContentScope) = moduleContent
val project = moduleContext.project
@@ -56,7 +59,7 @@ public object JsAnalyzerFacade : AnalyzerFacade<JsResolverForModule, PlatformAna
project, moduleContext.storageManager, syntheticFiles, if (moduleInfo.isLibrary) GlobalSearchScope.EMPTY_SCOPE else moduleContentScope
)
val resolveSession = createLazyResolveSession(moduleContext, declarationProviderFactory, BindingTraceContext(), JsPlatform)
val resolveSession = createLazyResolveSession(moduleContext, declarationProviderFactory, BindingTraceContext(), JsPlatform, targetEnvironment)
var packageFragmentProvider = resolveSession.getPackageFragmentProvider()
if (moduleInfo is LibraryInfo && KotlinJavaScriptLibraryDetectionUtil.isKotlinJavaScriptLibrary(moduleInfo.library)) {
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.analyzer.*
import org.jetbrains.kotlin.context.GlobalContextImpl
import org.jetbrains.kotlin.context.withProject
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.idea.project.IdeEnvironment
import org.jetbrains.kotlin.idea.project.ResolveSessionForBodies
import org.jetbrains.kotlin.load.java.structure.JavaClass
import org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl
@@ -63,7 +64,7 @@ fun createModuleResolverProvider(
val resolverForProject = analyzerFacade.setupResolverForProject(
globalContext.withProject(project), modulesToCreateResolversFor, modulesContent,
jvmPlatformParameters, delegateProvider.resolverForProject
jvmPlatformParameters, IdeEnvironment, delegateProvider.resolverForProject
)
return resolverForProject
}
@@ -0,0 +1,27 @@
/*
* Copyright 2010-2015 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.kotlin.idea.project
import org.jetbrains.kotlin.container.StorageComponentContainer
import org.jetbrains.kotlin.container.useImpl
import org.jetbrains.kotlin.resolve.TargetEnvironment
public object IdeEnvironment : TargetEnvironment("Ide") {
override fun configure(container: StorageComponentContainer) {
container.useImpl<LocalDescriptorResolverForIde>()
}
}
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2015 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.kotlin.idea.project
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.psi.JetDeclaration
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.BindingContextUtils
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.resolve.lazy.LocalDescriptorResolver
public class LocalDescriptorResolverForIde(
private val resolveElementCache: ResolveElementCache
): LocalDescriptorResolver {
override fun resolveLocalDeclaration(declaration: JetDeclaration): DeclarationDescriptor {
val context = resolveElementCache.resolveToElement(declaration, BodyResolveMode.FULL)
return BindingContextUtils.getNotNull(
context, BindingContext.DECLARATION_TO_DESCRIPTOR, declaration,
"Descriptor wasn't found for declaration $declaration\n${declaration.getElementTextWithContext()}"
)
}
}
@@ -76,14 +76,7 @@ public class ResolveSessionForBodies implements KotlinCodeAnalyzer {
@NotNull
@Override
public DeclarationDescriptor resolveToDescriptor(@NotNull JetDeclaration declaration) {
if (!JetPsiUtil.isLocal(declaration)) {
return resolveSession.resolveToDescriptor(declaration);
}
BindingContext context = resolveElementCache.resolveToElement(declaration, BodyResolveMode.FULL);
return BindingContextUtils.getNotNull(context, BindingContext.DECLARATION_TO_DESCRIPTOR, declaration,
"Descriptor wasn't found for declaration " + declaration.toString() + "\n" +
PsiUtilPackage.getElementTextWithContext(declaration));
return resolveSession.resolveToDescriptor(declaration);
}
@Override