From a24ea15f02cdb3c7bfeb90a0e0fde850b2853b6b Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Tue, 8 Sep 2015 14:22:11 +0200 Subject: somehow make Dokka work with new compiler API --- src/Analysis/AnalysisEnvironment.kt | 74 +++++++++++++++++++++++++++++++++++-- src/Analysis/CompilerAPI.kt | 34 ----------------- 2 files changed, 71 insertions(+), 37 deletions(-) delete mode 100644 src/Analysis/CompilerAPI.kt (limited to 'src/Analysis') diff --git a/src/Analysis/AnalysisEnvironment.kt b/src/Analysis/AnalysisEnvironment.kt index 8fdb0b51..060a3170 100644 --- a/src/Analysis/AnalysisEnvironment.kt +++ b/src/Analysis/AnalysisEnvironment.kt @@ -5,8 +5,15 @@ import com.intellij.core.CoreModuleManager import com.intellij.mock.MockComponentManager import com.intellij.openapi.Disposable import com.intellij.openapi.module.ModuleManager +import com.intellij.openapi.project.Project import com.intellij.openapi.roots.ProjectFileIndex import com.intellij.openapi.util.Disposer +import com.intellij.psi.PsiElement +import com.intellij.psi.search.GlobalSearchScope +import org.jetbrains.kotlin.analyzer.AnalysisResult +import org.jetbrains.kotlin.analyzer.ModuleContent +import org.jetbrains.kotlin.analyzer.ModuleInfo +import org.jetbrains.kotlin.analyzer.ResolverForModule import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys import org.jetbrains.kotlin.cli.common.messages.MessageCollector import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles @@ -19,10 +26,23 @@ import org.jetbrains.kotlin.config.CommonConfigurationKeys import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.config.ContentRoot import org.jetbrains.kotlin.config.KotlinSourceRoot +import org.jetbrains.kotlin.container.get +import org.jetbrains.kotlin.container.getService +import org.jetbrains.kotlin.context.ProjectContext +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.descriptors.ModuleDescriptor import org.jetbrains.kotlin.idea.caches.resolve.KotlinCacheService import org.jetbrains.kotlin.idea.caches.resolve.KotlinOutOfBlockCompletionModificationTracker import org.jetbrains.kotlin.idea.caches.resolve.LibraryModificationTracker import org.jetbrains.kotlin.idea.resolve.ResolutionFacade +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.psi.JetDeclaration +import org.jetbrains.kotlin.psi.JetElement +import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.CompilerEnvironment +import org.jetbrains.kotlin.resolve.jvm.JvmAnalyzerFacade +import org.jetbrains.kotlin.resolve.jvm.JvmPlatformParameters +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.resolve.lazy.ResolveSession import java.io.File @@ -63,10 +83,24 @@ public class AnalysisEnvironment(val messageCollector: MessageCollector, body: A projectComponentManager.registerService(KotlinOutOfBlockCompletionModificationTracker::class.java, KotlinOutOfBlockCompletionModificationTracker()) + val projectContext = ProjectContext(environment.project) val sourceFiles = environment.getSourceFiles() - val facade = KotlinCacheService.getInstance(environment.project).getResolutionFacade(sourceFiles) - // TODO get rid of resolveSession once we have all necessary APIs in ResolutionFacade - val resolveSession = environment.analyze() + + val module = object : ModuleInfo { + override val name: Name = Name.special("") + override fun dependencies(): List = listOf(this) + } + val resolverForProject = JvmAnalyzerFacade.setupResolverForProject( + projectContext, + listOf(module), + { ModuleContent(sourceFiles, GlobalSearchScope.allScope(environment.project)) }, + JvmPlatformParameters { module }, + CompilerEnvironment + ) + + val resolverForModule = resolverForProject.resolverForModule(module) + val resolveSession = resolverForModule.componentProvider.get() + val facade = DokkaResolutionFacade(environment.project, resolverForProject.descriptorForModule(module), resolverForModule) return processor(environment, facade, resolveSession) } @@ -126,3 +160,37 @@ public fun contentRootFromPath(path: String): ContentRoot { val file = File(path) return if (file.extension == "java") JavaSourceRoot(file) else KotlinSourceRoot(path) } + + +class DokkaResolutionFacade(override val project: Project, + override val moduleDescriptor: ModuleDescriptor, + val resolverForModule: ResolverForModule) : ResolutionFacade { + + override fun analyze(element: JetElement, bodyResolveMode: BodyResolveMode): BindingContext { + throw UnsupportedOperationException() + } + + override fun analyzeFullyAndGetResult(elements: Collection): AnalysisResult { + throw UnsupportedOperationException() + } + + override fun getFrontendService(element: PsiElement, serviceClass: Class): T { + throw UnsupportedOperationException() + } + + override fun getFrontendService(serviceClass: Class): T { + return resolverForModule.componentProvider.getService(serviceClass) + } + + override fun getFrontendService(moduleDescriptor: ModuleDescriptor, serviceClass: Class): T { + throw UnsupportedOperationException() + } + + override fun getIdeService(serviceClass: Class): T { + throw UnsupportedOperationException() + } + + override fun resolveToDescriptor(declaration: JetDeclaration): DeclarationDescriptor { + throw UnsupportedOperationException() + } +} diff --git a/src/Analysis/CompilerAPI.kt b/src/Analysis/CompilerAPI.kt deleted file mode 100644 index f4382bbb..00000000 --- a/src/Analysis/CompilerAPI.kt +++ /dev/null @@ -1,34 +0,0 @@ -package org.jetbrains.dokka - -import com.intellij.psi.search.GlobalSearchScope -import org.jetbrains.kotlin.analyzer.ModuleContent -import org.jetbrains.kotlin.analyzer.ModuleInfo -import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider -import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.jetbrains.kotlin.container.get -import org.jetbrains.kotlin.context.ProjectContext -import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.resolve.CompilerEnvironment -import org.jetbrains.kotlin.resolve.jvm.JvmAnalyzerFacade -import org.jetbrains.kotlin.resolve.jvm.JvmPlatformParameters -import org.jetbrains.kotlin.resolve.lazy.ResolveSession - -fun KotlinCoreEnvironment.analyze(): ResolveSession { - val projectContext = ProjectContext(project) - val sourceFiles = getSourceFiles() - - val module = object : ModuleInfo { - override val name: Name = Name.special("") - override fun dependencies(): List = listOf(this) - } - val resolverForProject = JvmAnalyzerFacade.setupResolverForProject( - projectContext, - listOf(module), - { ModuleContent(sourceFiles, GlobalSearchScope.allScope(project)) }, - JvmPlatformParameters { module }, - CompilerEnvironment, - packagePartProviderFactory = { module, content -> JvmPackagePartProvider(this) } - ) - - return resolverForProject.resolverForModule(module).componentProvider.get() -} -- cgit