diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Analysis/AnalysisEnvironment.kt | 111 | ||||
-rw-r--r-- | src/Analysis/CompilerAPI.kt | 9 | ||||
-rw-r--r-- | src/Analysis/CoreProjectFileIndex.kt | 97 | ||||
-rw-r--r-- | src/Kotlin/ContentBuilder.kt | 4 | ||||
-rw-r--r-- | src/Kotlin/DocumentationBuilder.kt | 8 | ||||
-rw-r--r-- | src/main.kt | 17 |
6 files changed, 174 insertions, 72 deletions
diff --git a/src/Analysis/AnalysisEnvironment.kt b/src/Analysis/AnalysisEnvironment.kt index 1813a4b0..1d0631bb 100644 --- a/src/Analysis/AnalysisEnvironment.kt +++ b/src/Analysis/AnalysisEnvironment.kt @@ -1,17 +1,31 @@ package org.jetbrains.dokka -import org.jetbrains.kotlin.cli.common.messages.* -import com.intellij.openapi.* -import org.jetbrains.kotlin.cli.jvm.compiler.* -import org.jetbrains.kotlin.resolve.* -import org.jetbrains.kotlin.psi.* -import java.io.File +import com.intellij.core.CoreApplicationEnvironment +import com.intellij.core.CoreModuleManager +import com.intellij.mock.MockComponentManager +import com.intellij.openapi.Disposable +import com.intellij.openapi.module.Module +import com.intellij.openapi.module.ModuleManager +import com.intellij.openapi.roots.ContentIterator +import com.intellij.openapi.roots.OrderEntry +import com.intellij.openapi.roots.ProjectFileIndex +import com.intellij.openapi.util.Disposer +import com.intellij.openapi.vfs.VirtualFile +import org.jetbrains.jps.model.module.JpsModuleSourceRootType +import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys +import org.jetbrains.kotlin.cli.common.messages.MessageCollector +import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles +import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment +import org.jetbrains.kotlin.cli.jvm.config.addJavaSourceRoot +import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoot +import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots +import org.jetbrains.kotlin.cli.jvm.config.jvmClasspathRoots import org.jetbrains.kotlin.config.* -import org.jetbrains.kotlin.cli.common.* -import org.jetbrains.kotlin.cli.jvm.* -import com.intellij.openapi.util.* -import org.jetbrains.kotlin.descriptors.ModuleDescriptor +import org.jetbrains.kotlin.idea.caches.resolve.KotlinCacheService +import org.jetbrains.kotlin.idea.caches.resolve.LibraryModificationTracker +import org.jetbrains.kotlin.idea.caches.resolve.ResolutionFacade import org.jetbrains.kotlin.resolve.lazy.ResolveSession +import java.io.File /** * Kotlin as a service entry point @@ -33,65 +47,41 @@ public class AnalysisEnvironment(val messageCollector: MessageCollector, body: A * Executes [processor] when analysis is complete. * $processor: function to receive compiler environment, module and context for symbol resolution */ - public fun withContext<T>(processor: (JetCoreEnvironment, ResolveSession) -> T): T { - val environment = JetCoreEnvironment.createForProduction(this, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES) - val resolveSession = environment.analyze() - if (environment.getSourceFiles().isNotEmpty()) { - resolveSession.forceResolveAll() - } - return processor(environment, resolveSession) - } + public fun withContext<T>(processor: (KotlinCoreEnvironment, ResolutionFacade, ResolveSession) -> T): T { + val environment = KotlinCoreEnvironment.createForProduction(this, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES) + val projectComponentManager = environment.project as MockComponentManager - /** - * Executes [processor] when analysis is complete. - * $processor: function to receive compiler module and context for symbol resolution - */ - public fun withContext<T>(processor: (ResolveSession) -> T): T { - return withContext { environment, session -> processor(session) } - } + val moduleManager = CoreModuleManager(environment.project, this) + CoreApplicationEnvironment.registerComponentInstance(projectComponentManager.getPicoContainer(), + javaClass<ModuleManager>(), moduleManager) - /** - * Streams files into [processor] and returns a stream of its results - * $processor: function to receive context for symbol resolution and file for processing - */ - public fun streamFiles<T>(processor: (ResolveSession, JetFile) -> T): Stream<T> { - return withContext { environment, session -> - environment.getSourceFiles().stream().map { file -> processor(session, file) } - } - } + projectComponentManager.registerService(javaClass<ProjectFileIndex>(), + CoreProjectFileIndex()) + projectComponentManager.registerService(javaClass<LibraryModificationTracker>(), + LibraryModificationTracker(environment.project)) + projectComponentManager.registerService(javaClass<KotlinCacheService>(), + KotlinCacheService(environment.project)) - /** - * Runs [processor] for each file and collects its results into single list - * $processor: function to receive context for symbol resolution and file for processing - */ - public fun processFiles<T>(processor: (ResolveSession, JetFile) -> T): List<T> { - return withContext { environment, session -> - environment.getSourceFiles().map { file -> processor(session, file) } - } - } - /** - * Runs [processor] for each file and collects its results into single list - * $processor: is a function to receive context for symbol resolution and file for processing - */ - public fun processFilesFlat<T>(processor: (ResolveSession, JetFile) -> List<T>): List<T> { - return withContext { environment, session -> - environment.getSourceFiles().flatMap { file -> processor(session, file) } - } + 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() + return processor(environment, facade, resolveSession) } /** * Classpath for this environment. */ public val classpath: List<File> - get() = configuration.get(JVMConfigurationKeys.CLASSPATH_KEY) ?: listOf() + get() = configuration.jvmClasspathRoots /** * Adds list of paths to classpath. * $paths: collection of files to add */ public fun addClasspath(paths: List<File>) { - configuration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, paths) + configuration.addJvmClasspathRoots(paths) } /** @@ -99,21 +89,30 @@ public class AnalysisEnvironment(val messageCollector: MessageCollector, body: A * $path: path to add */ public fun addClasspath(path: File) { - configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, path) + configuration.addJvmClasspathRoot(path) } /** * List of source roots for this environment. */ public val sources: List<String> - get() = configuration.get(CommonConfigurationKeys.SOURCE_ROOTS_KEY) ?: listOf() + get() = configuration.get(CommonConfigurationKeys.CONTENT_ROOTS) + ?.filterIsInstance<KotlinSourceRoot>() + ?.map { it.path } ?: emptyList() /** * Adds list of paths to source roots. * $list: collection of files to add */ public fun addSources(list: List<String>) { - configuration.addAll(CommonConfigurationKeys.SOURCE_ROOTS_KEY, list) + list.forEach { + val file = File(it) + if (file.extension == "java") { + configuration.addJavaSourceRoot(file) + } else { + configuration.addKotlinSourceRoot(it) + } + } } /** diff --git a/src/Analysis/CompilerAPI.kt b/src/Analysis/CompilerAPI.kt index abba0afa..8b877204 100644 --- a/src/Analysis/CompilerAPI.kt +++ b/src/Analysis/CompilerAPI.kt @@ -10,12 +10,12 @@ import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.resolve.scopes.* import org.jetbrains.kotlin.context.GlobalContext import com.intellij.psi.search.GlobalSearchScope +import org.jetbrains.kotlin.context.ProjectContext import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.lazy.ResolveSession -fun JetCoreEnvironment.analyze(): ResolveSession { - val globalContext = GlobalContext() - val project = getProject() +fun KotlinCoreEnvironment.analyze(): ResolveSession { + val projectContext = ProjectContext(project) val sourceFiles = getSourceFiles() val module = object : ModuleInfo { @@ -23,8 +23,7 @@ fun JetCoreEnvironment.analyze(): ResolveSession { override fun dependencies(): List<ModuleInfo> = listOf(this) } val resolverForProject = JvmAnalyzerFacade.setupResolverForProject( - globalContext, - project, + projectContext, listOf(module), { ModuleContent(sourceFiles, GlobalSearchScope.allScope(project)) }, JvmPlatformParameters { module } diff --git a/src/Analysis/CoreProjectFileIndex.kt b/src/Analysis/CoreProjectFileIndex.kt new file mode 100644 index 00000000..916eab73 --- /dev/null +++ b/src/Analysis/CoreProjectFileIndex.kt @@ -0,0 +1,97 @@ +package org.jetbrains.dokka + +import com.intellij.openapi.module.Module +import com.intellij.openapi.roots.ContentIterator +import com.intellij.openapi.roots.OrderEntry +import com.intellij.openapi.roots.ProjectFileIndex +import com.intellij.openapi.vfs.VirtualFile +import org.jetbrains.jps.model.module.JpsModuleSourceRootType + +/** + * Workaround for the lack of ability to create a ProjectFileIndex implementation using only + * classes from projectModel-{api,impl}. + */ +class CoreProjectFileIndex(): ProjectFileIndex { + override fun getContentRootForFile(p0: VirtualFile): VirtualFile? { + throw UnsupportedOperationException() + } + + override fun getContentRootForFile(p0: VirtualFile, p1: Boolean): VirtualFile? { + throw UnsupportedOperationException() + } + + override fun getPackageNameByDirectory(p0: VirtualFile): String? { + throw UnsupportedOperationException() + } + + override fun isInLibrarySource(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun getClassRootForFile(p0: VirtualFile): VirtualFile? { + throw UnsupportedOperationException() + } + + override fun getOrderEntriesForFile(p0: VirtualFile): List<OrderEntry> = emptyList() + + override fun isInLibraryClasses(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun isExcluded(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun getSourceRootForFile(p0: VirtualFile): VirtualFile? { + throw UnsupportedOperationException() + } + + override fun isUnderIgnored(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun isLibraryClassFile(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun getModuleForFile(p0: VirtualFile): Module? = null + + override fun getModuleForFile(p0: VirtualFile, p1: Boolean): Module? { + throw UnsupportedOperationException() + } + + override fun isInSource(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun isIgnored(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun isContentSourceFile(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun isInSourceContent(p0: VirtualFile): Boolean = false + + override fun iterateContent(p0: ContentIterator): Boolean { + throw UnsupportedOperationException() + } + + override fun isInContent(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun iterateContentUnderDirectory(p0: VirtualFile, p1: ContentIterator): Boolean { + throw UnsupportedOperationException() + } + + override fun isInTestSourceContent(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun isUnderSourceRootOfType(p0: VirtualFile, p1: MutableSet<out JpsModuleSourceRootType<*>>): Boolean { + throw UnsupportedOperationException() + } +} + diff --git a/src/Kotlin/ContentBuilder.kt b/src/Kotlin/ContentBuilder.kt index f454c8fe..3dd5c00e 100644 --- a/src/Kotlin/ContentBuilder.kt +++ b/src/Kotlin/ContentBuilder.kt @@ -120,7 +120,7 @@ fun DocumentationBuilder.functionBody(descriptor: DeclarationDescriptor, functio logger.warn("Missing function name in @sample in ${descriptor.signature()}") return ContentBlockCode().let() { it.append(ContentText("Missing function name in @sample")); it } } - val scope = getResolutionScope(session, descriptor) + val scope = getResolutionScope(resolutionFacade, descriptor) val rootPackage = session.getModuleDescriptor().getPackage(FqName.ROOT)!! val rootScope = rootPackage.getMemberScope() val symbol = resolveInScope(functionName, scope) ?: resolveInScope(functionName, rootScope) @@ -169,7 +169,7 @@ private fun DocumentationBuilder.resolveInScope(functionName: String, scope: Jet currentScope = if (partSymbol is ClassDescriptor) partSymbol.getDefaultType().getMemberScope() else - getResolutionScope(session, partSymbol) + getResolutionScope(resolutionFacade, partSymbol) symbol = partSymbol } diff --git a/src/Kotlin/DocumentationBuilder.kt b/src/Kotlin/DocumentationBuilder.kt index 8910c3e3..02ced7a4 100644 --- a/src/Kotlin/DocumentationBuilder.kt +++ b/src/Kotlin/DocumentationBuilder.kt @@ -8,6 +8,7 @@ import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotated import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor import org.jetbrains.kotlin.descriptors.impl.EnumEntrySyntheticClassDescriptor +import org.jetbrains.kotlin.idea.caches.resolve.ResolutionFacade import org.jetbrains.kotlin.idea.kdoc.KDocFinder import org.jetbrains.kotlin.idea.kdoc.resolveKDocLink import org.jetbrains.kotlin.kdoc.psi.impl.KDocSection @@ -37,7 +38,8 @@ private fun isSamePackage(descriptor1: DeclarationDescriptor, descriptor2: Decla return package1 != null && package2 != null && package1.fqName == package2.fqName } -class DocumentationBuilder(val session: ResolveSession, +class DocumentationBuilder(val resolutionFacade: ResolutionFacade, + val session: ResolveSession, val options: DocumentationOptions, val refGraph: NodeReferenceGraph, val logger: DokkaLogger) { @@ -172,7 +174,7 @@ class DocumentationBuilder(val session: ResolveSession, fun resolveContentLink(descriptor: DeclarationDescriptor, href: String): ContentBlock { val symbol = try { - val symbols = resolveKDocLink(session, descriptor, null, href.split('.').toList()) + val symbols = resolveKDocLink(resolutionFacade, descriptor, null, href.split('.').toList()) findTargetSymbol(symbols) } catch(e: Exception) { null @@ -425,7 +427,7 @@ class DocumentationBuilder(val session: ResolveSession, fun ClassDescriptor.build(): DocumentationNode { val kind = when (getKind()) { ClassKind.OBJECT -> Kind.Object - ClassKind.TRAIT -> Kind.Interface + ClassKind.INTERFACE -> Kind.Interface ClassKind.ENUM_CLASS -> Kind.Enum ClassKind.ANNOTATION_CLASS -> Kind.AnnotationClass ClassKind.ENUM_ENTRY -> Kind.EnumItem diff --git a/src/main.kt b/src/main.kt index aa3da881..4e2d2b11 100644 --- a/src/main.kt +++ b/src/main.kt @@ -12,7 +12,8 @@ import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.cli.common.messages.MessageCollector import org.jetbrains.kotlin.cli.common.messages.MessageRenderer -import org.jetbrains.kotlin.cli.jvm.compiler.JetCoreEnvironment +import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment +import org.jetbrains.kotlin.cli.jvm.config.JavaSourceRoot import org.jetbrains.kotlin.config.CommonConfigurationKeys import org.jetbrains.kotlin.utils.PathUtil import java.io.File @@ -215,12 +216,12 @@ fun buildDocumentationModule(environment: AnalysisEnvironment, includes: List<String> = listOf(), filesToDocumentFilter: (PsiFile) -> Boolean = { file -> true }, logger: DokkaLogger): DocumentationModule { - val documentation = environment.withContext { environment, session -> + val documentation = environment.withContext { environment, resolutionFacade, session -> val fragmentFiles = environment.getSourceFiles().filter(filesToDocumentFilter) val fragments = fragmentFiles.map { session.getPackageFragment(it.getPackageFqName()) }.filterNotNull().distinct() val refGraph = NodeReferenceGraph() - val documentationBuilder = DocumentationBuilder(session, options, refGraph, logger) + val documentationBuilder = DocumentationBuilder(resolutionFacade, session, options, refGraph, logger) val packageDocs = PackageDocs(documentationBuilder, fragments.firstOrNull(), logger) for (include in includes) { packageDocs.parse(include) @@ -244,15 +245,19 @@ fun buildDocumentationModule(environment: AnalysisEnvironment, } -fun JetCoreEnvironment.getJavaSourceFiles(): List<PsiJavaFile> { - val sourceRoots = getConfiguration().getList(CommonConfigurationKeys.SOURCE_ROOTS_KEY).map { File(it) } +fun KotlinCoreEnvironment.getJavaSourceFiles(): List<PsiJavaFile> { + val sourceRoots = configuration.get(CommonConfigurationKeys.CONTENT_ROOTS) + ?.filterIsInstance<JavaSourceRoot>() + ?.map { it.file } + ?: listOf() + val result = arrayListOf<PsiJavaFile>() val localFileSystem = VirtualFileManager.getInstance().getFileSystem("file") sourceRoots.forEach { sourceRoot -> sourceRoot.getAbsoluteFile().recurse { val vFile = localFileSystem.findFileByPath(it.path) if (vFile != null) { - val psiFile = PsiManager.getInstance(getProject()).findFile(vFile) + val psiFile = PsiManager.getInstance(project).findFile(vFile) if (psiFile is PsiJavaFile) { result.add(psiFile) } |