From 044308ba60a0d4462ccb7388f16ad17a31d7450d Mon Sep 17 00:00:00 2001 From: Ilya Ryzhenkov Date: Fri, 11 Jul 2014 20:49:04 +0400 Subject: Complete package migration and move files into folders. --- src/AnalysesEnvironment.kt | 71 ------------- src/Analysis/AnalysisEnvironment.kt | 70 +++++++++++++ src/Analysis/CommentsAPI.kt | 30 ++++++ src/Analysis/CompilerAPI.kt | 62 +++++++++++ src/Analysis/PsiAPI.kt | 19 ++++ src/CommentsAPI.kt | 30 ------ src/CompilerAPI.kt | 62 ----------- src/Documentation/DocumentationBuilder.kt | 73 +++++++++++++ src/Documentation/DocumentationBuildingVisitor.kt | 121 ++++++++++++++++++++++ src/Documentation/DocumentationModel.kt | 67 ++++++++++++ src/DocumentationBuilder.kt | 73 ------------- src/DocumentationBuildingVisitor.kt | 121 ---------------------- src/DocumentationModel.kt | 67 ------------ src/PsiAPI.kt | 19 ---- src/main.kt | 10 +- 15 files changed, 447 insertions(+), 448 deletions(-) delete mode 100644 src/AnalysesEnvironment.kt create mode 100644 src/Analysis/AnalysisEnvironment.kt create mode 100644 src/Analysis/CommentsAPI.kt create mode 100644 src/Analysis/CompilerAPI.kt create mode 100644 src/Analysis/PsiAPI.kt delete mode 100644 src/CommentsAPI.kt delete mode 100644 src/CompilerAPI.kt create mode 100644 src/Documentation/DocumentationBuilder.kt create mode 100644 src/Documentation/DocumentationBuildingVisitor.kt create mode 100644 src/Documentation/DocumentationModel.kt delete mode 100644 src/DocumentationBuilder.kt delete mode 100644 src/DocumentationBuildingVisitor.kt delete mode 100644 src/DocumentationModel.kt delete mode 100644 src/PsiAPI.kt (limited to 'src') diff --git a/src/AnalysesEnvironment.kt b/src/AnalysesEnvironment.kt deleted file mode 100644 index 7cd95feb..00000000 --- a/src/AnalysesEnvironment.kt +++ /dev/null @@ -1,71 +0,0 @@ -package org.jetbrains.dokka - -import com.intellij.openapi.* -import org.jetbrains.jet.cli.common.messages.* -import org.jetbrains.jet.cli.common.* -import org.jetbrains.jet.cli.jvm.compiler.* -import java.io.* -import org.jetbrains.jet.cli.jvm.* -import org.jetbrains.jet.config.* -import com.intellij.openapi.util.* -import org.jetbrains.jet.lang.resolve.* -import org.jetbrains.jet.lang.psi.* -import kotlin.platform.* - -public class AnalysesEnvironment(val messageCollector: MessageCollector, body: AnalysesEnvironment.() -> Unit = {}) : Disposable { - val configuration = CompilerConfiguration(); - - { - configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector) - body() - } - - private fun withContext(processor: (JetCoreEnvironment, BindingContext) -> T): T { - val environment = JetCoreEnvironment.createForProduction(this, configuration) - val result = environment.analyze(messageCollector) - return processor(environment, result) - } - - public fun withContext(processor: (BindingContext) -> T): T { - return withContext { environment, context -> processor(context) } - } - - public fun streamFiles(processor: (BindingContext, JetFile) -> T): Stream { - return withContext { environment, context -> - environment.getSourceFiles().stream().map { file -> processor(context, file) } - } - } - - public fun processFiles(processor: (BindingContext, JetFile) -> T): List { - return withContext { environment, context -> - environment.getSourceFiles().map { file -> processor(context, file) } - } - } - - public fun processFilesFlat(processor: (BindingContext, JetFile) -> List): List { - return withContext { environment, context -> - environment.getSourceFiles().flatMap { file -> processor(context, file) } - } - } - - public val classpath: List - get() = configuration.get(JVMConfigurationKeys.CLASSPATH_KEY) ?: listOf() - - public fun addClasspath(list: List) { - configuration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, list) - } - - public fun addClasspath(file: File) { - configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, file) - } - - public val sources: List - get() = configuration.get(CommonConfigurationKeys.SOURCE_ROOTS_KEY) ?: listOf() - public fun addSources(list: List) { - configuration.addAll(CommonConfigurationKeys.SOURCE_ROOTS_KEY, list) - } - - public override fun dispose() { - Disposer.dispose(this) - } -} \ No newline at end of file diff --git a/src/Analysis/AnalysisEnvironment.kt b/src/Analysis/AnalysisEnvironment.kt new file mode 100644 index 00000000..346367ee --- /dev/null +++ b/src/Analysis/AnalysisEnvironment.kt @@ -0,0 +1,70 @@ +package org.jetbrains.dokka + +import org.jetbrains.jet.cli.common.messages.* +import com.intellij.openapi.* +import org.jetbrains.jet.cli.jvm.compiler.* +import org.jetbrains.jet.lang.resolve.* +import org.jetbrains.jet.lang.psi.* +import java.io.File +import org.jetbrains.jet.config.* +import org.jetbrains.jet.cli.common.* +import org.jetbrains.jet.cli.jvm.* +import com.intellij.openapi.util.* + +public class AnalysisEnvironment(val messageCollector: MessageCollector, body: AnalysisEnvironment.() -> Unit = {}) : Disposable { + val configuration = CompilerConfiguration(); + + { + configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector) + body() + } + + private fun withContext(processor: (JetCoreEnvironment, BindingContext) -> T): T { + val environment = JetCoreEnvironment.createForProduction(this, configuration) + val result = environment.analyze(messageCollector) + return processor(environment, result) + } + + public fun withContext(processor: (BindingContext) -> T): T { + return withContext { environment, context -> processor(context) } + } + + public fun streamFiles(processor: (BindingContext, JetFile) -> T): Stream { + return withContext { environment, context -> + environment.getSourceFiles().stream().map { file -> processor(context, file) } + } + } + + public fun processFiles(processor: (BindingContext, JetFile) -> T): List { + return withContext { environment, context -> + environment.getSourceFiles().map { file -> processor(context, file) } + } + } + + public fun processFilesFlat(processor: (BindingContext, JetFile) -> List): List { + return withContext { environment, context -> + environment.getSourceFiles().flatMap { file -> processor(context, file) } + } + } + + public val classpath: List + get() = configuration.get(JVMConfigurationKeys.CLASSPATH_KEY) ?: listOf() + + public fun addClasspath(list: List) { + configuration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, list) + } + + public fun addClasspath(file: File) { + configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, file) + } + + public val sources: List + get() = configuration.get(CommonConfigurationKeys.SOURCE_ROOTS_KEY) ?: listOf() + public fun addSources(list: List) { + configuration.addAll(CommonConfigurationKeys.SOURCE_ROOTS_KEY, list) + } + + public override fun dispose() { + Disposer.dispose(this) + } +} \ No newline at end of file diff --git a/src/Analysis/CommentsAPI.kt b/src/Analysis/CommentsAPI.kt new file mode 100644 index 00000000..a32ee734 --- /dev/null +++ b/src/Analysis/CommentsAPI.kt @@ -0,0 +1,30 @@ +package org.jetbrains.dokka + +import org.jetbrains.jet.lang.descriptors.* +import org.jetbrains.jet.lang.resolve.* +import org.jetbrains.jet.kdoc.psi.api.* +import org.jetbrains.jet.lang.psi.* + +fun BindingContext.getDocumentation(descriptor: DeclarationDescriptor): KDoc? { + val psiElement = DescriptorToSourceUtils.descriptorToDeclaration(descriptor) + if (psiElement == null) + throw IllegalArgumentException("$descriptor doesn't have connection to source code, is it synthetic?") + + return psiElement.previousSiblings().takeWhile { it !is JetDeclaration }.firstOrNull { it is KDoc } as KDoc? +} + +fun KDoc?.extractText(): String { + if (this == null) + return "" + val text = getText() + if (text == null) + return "" + val lines = text.replace("\r", "").split("\n") + return lines.map { + it.dropWhile { java.lang.Character.isWhitespace(it) } + .dropWhile { it == '/' } + .dropWhile { it == '*' } + .dropWhile { it == '/' } + .dropWhile { java.lang.Character.isWhitespace(it) } + }.filter { it.any() }.join("\n") +} \ No newline at end of file diff --git a/src/Analysis/CompilerAPI.kt b/src/Analysis/CompilerAPI.kt new file mode 100644 index 00000000..bf77a7d4 --- /dev/null +++ b/src/Analysis/CompilerAPI.kt @@ -0,0 +1,62 @@ +package org.jetbrains.dokka + +import org.jetbrains.jet.cli.common.arguments.* +import org.jetbrains.jet.cli.common.messages.* +import org.jetbrains.jet.cli.jvm.* +import org.jetbrains.jet.cli.jvm.compiler.* +import org.jetbrains.jet.utils.* +import java.io.* +import org.jetbrains.jet.lang.resolve.java.* +import com.google.common.base.* +import com.intellij.psi.* +import org.jetbrains.jet.lang.resolve.* +import org.jetbrains.jet.lang.psi.* +import org.jetbrains.jet.analyzer.* +import org.jetbrains.jet.lang.descriptors.* + +private fun getAnnotationsPath(paths: KotlinPaths, arguments: K2JVMCompilerArguments): MutableList { + val annotationsPath = arrayListOf() + annotationsPath.add(paths.getJdkAnnotationsPath()) + val annotationPaths = arguments.annotations + if (annotationPaths != null) { + for (element in annotationPaths.split(File.pathSeparatorChar)) { + annotationsPath.add(File(element)) + } + } + return annotationsPath +} + +fun JetCoreEnvironment.analyze(messageCollector: MessageCollector): BindingContext { + val project = getProject() + val sourceFiles = getSourceFiles() + + val analyzerWithCompilerReport = AnalyzerWithCompilerReport(messageCollector) + analyzerWithCompilerReport.analyzeAndReport(sourceFiles) { + val support = CliLightClassGenerationSupport.getInstanceForCli(project)!! + val sharedTrace = support.getTrace() + val sharedModule = support.getModule() + val compilerConfiguration = getConfiguration()!! + AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(project, sourceFiles, sharedTrace, + Predicates.alwaysTrue(), + sharedModule, + compilerConfiguration.get(JVMConfigurationKeys.MODULE_IDS), + compilerConfiguration.get(JVMConfigurationKeys.INCREMENTAL_CACHE_BASE_DIR)) + } + + val exhaust = analyzerWithCompilerReport.getAnalyzeExhaust() + assert(exhaust != null) { "AnalyzeExhaust should be non-null, compiling: " + sourceFiles } + + return exhaust!!.getBindingContext() +} + +fun AnalyzerWithCompilerReport.analyzeAndReport(files: List, analyser: () -> AnalyzeExhaust) = analyzeAndReport(analyser, files) + +fun BindingContext.getPackageFragment(file: JetFile) = get(BindingContext.FILE_TO_PACKAGE_FRAGMENT, file) + +fun DeclarationDescriptor.isUserCode() = + when (this) { + is PackageFragmentDescriptor -> false + is PropertyAccessorDescriptor -> !isDefault() + is CallableMemberDescriptor -> getKind() == CallableMemberDescriptor.Kind.DECLARATION + else -> true + } diff --git a/src/Analysis/PsiAPI.kt b/src/Analysis/PsiAPI.kt new file mode 100644 index 00000000..2282cd1d --- /dev/null +++ b/src/Analysis/PsiAPI.kt @@ -0,0 +1,19 @@ +package org.jetbrains.dokka + +import com.intellij.psi.* +import kotlin.support.* + +fun PsiElement.previousSiblings(): Stream { + var element: PsiElement? = this + return object : Stream { + override fun iterator(): Iterator = object : AbstractIterator() { + override fun computeNext() { + element = element?.getPrevSibling() + if (element == null) + done() + else + setNext(element!!) + } + } + } +} diff --git a/src/CommentsAPI.kt b/src/CommentsAPI.kt deleted file mode 100644 index 2e574d23..00000000 --- a/src/CommentsAPI.kt +++ /dev/null @@ -1,30 +0,0 @@ -package org.jetbrains.dokka - -import org.jetbrains.jet.lang.descriptors.* -import org.jetbrains.jet.lang.resolve.* -import org.jetbrains.jet.kdoc.psi.api.* -import org.jetbrains.jet.lang.psi.JetDeclaration - -fun BindingContext.getDocumentation(descriptor: DeclarationDescriptor): KDoc? { - val psiElement = DescriptorToSourceUtils.descriptorToDeclaration(descriptor) - if (psiElement == null) - throw IllegalArgumentException("$descriptor doesn't have connection to source code, is it synthetic?") - - return psiElement.previousSiblings().takeWhile { it !is JetDeclaration }.firstOrNull { it is KDoc } as KDoc? -} - -fun KDoc?.extractText(): String { - if (this == null) - return "" - val text = getText() - if (text == null) - return "" - val lines = text.replace("\r", "").split("\n") - return lines.map { - it.dropWhile { java.lang.Character.isWhitespace(it) } - .dropWhile { it == '/' } - .dropWhile { it == '*' } - .dropWhile { it == '/' } - .dropWhile { java.lang.Character.isWhitespace(it) } - }.filter { it.any() }.join("\n") -} \ No newline at end of file diff --git a/src/CompilerAPI.kt b/src/CompilerAPI.kt deleted file mode 100644 index f3c801a7..00000000 --- a/src/CompilerAPI.kt +++ /dev/null @@ -1,62 +0,0 @@ -package org.jetbrains.dokka - -import org.jetbrains.jet.cli.common.arguments.* -import org.jetbrains.jet.cli.common.messages.* -import org.jetbrains.jet.cli.jvm.* -import org.jetbrains.jet.cli.jvm.compiler.* -import org.jetbrains.jet.utils.* -import java.io.* -import org.jetbrains.jet.lang.resolve.java.* -import com.google.common.base.* -import com.intellij.psi.* -import org.jetbrains.jet.lang.resolve.* -import org.jetbrains.jet.lang.psi.* -import org.jetbrains.jet.analyzer.* -import org.jetbrains.jet.lang.descriptors.* - -private fun getAnnotationsPath(paths: KotlinPaths, arguments: K2JVMCompilerArguments): MutableList { - val annotationsPath = arrayListOf() - annotationsPath.add(paths.getJdkAnnotationsPath()) - val annotationPaths = arguments.annotations - if (annotationPaths != null) { - for (element in annotationPaths.split(File.pathSeparatorChar)) { - annotationsPath.add(File(element)) - } - } - return annotationsPath -} - -private fun JetCoreEnvironment.analyze(messageCollector: MessageCollector): BindingContext { - val project = getProject() - val sourceFiles = getSourceFiles() - - val analyzerWithCompilerReport = AnalyzerWithCompilerReport(messageCollector) - analyzerWithCompilerReport.analyzeAndReport(sourceFiles) { - val support = CliLightClassGenerationSupport.getInstanceForCli(project)!! - val sharedTrace = support.getTrace() - val sharedModule = support.getModule() - val compilerConfiguration = getConfiguration()!! - AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(project, sourceFiles, sharedTrace, - Predicates.alwaysTrue(), - sharedModule, - compilerConfiguration.get(JVMConfigurationKeys.MODULE_IDS), - compilerConfiguration.get(JVMConfigurationKeys.INCREMENTAL_CACHE_BASE_DIR)) - } - - val exhaust = analyzerWithCompilerReport.getAnalyzeExhaust() - assert(exhaust != null) { "AnalyzeExhaust should be non-null, compiling: " + sourceFiles } - - return exhaust!!.getBindingContext() -} - -fun AnalyzerWithCompilerReport.analyzeAndReport(files: List, analyser: () -> AnalyzeExhaust) = analyzeAndReport(analyser, files) - -fun BindingContext.getPackageFragment(file: JetFile) = get(BindingContext.FILE_TO_PACKAGE_FRAGMENT, file) - -fun DeclarationDescriptor.isUserCode() = - when (this) { - is PackageFragmentDescriptor -> false - is PropertyAccessorDescriptor -> !isDefault() - is CallableMemberDescriptor -> getKind() == CallableMemberDescriptor.Kind.DECLARATION - else -> true - } diff --git a/src/Documentation/DocumentationBuilder.kt b/src/Documentation/DocumentationBuilder.kt new file mode 100644 index 00000000..77f74eb2 --- /dev/null +++ b/src/Documentation/DocumentationBuilder.kt @@ -0,0 +1,73 @@ +package org.jetbrains.dokka + +import org.jetbrains.jet.lang.resolve.* +import org.jetbrains.jet.lang.psi.* +import org.jetbrains.jet.lang.descriptors.* +import org.jetbrains.jet.lang.descriptors.impl.* + +fun BindingContext.createDocumentation(file: JetFile): DocumentationModel { + val model = DocumentationModel() + val packageFragment = getPackageFragment(file) + if (packageFragment == null) throw IllegalArgumentException("File $file should have package fragment") + + val visitor = DocumentationBuilderVisitor(this) + visitDescriptor(packageFragment, model, visitor) + + return model +} + +class DocumentationBuilderVisitor(val context: BindingContext) : DeclarationDescriptorVisitorEmptyBodies() { + + override fun visitDeclarationDescriptor(descriptor: DeclarationDescriptor?, data: DocumentationNode?): DocumentationNode? { + val doc = context.getDocumentation(descriptor!!).extractText() + val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Unknown) + data?.addReferenceTo(node, DocumentationReferenceKind.Member) + return node + } + + override fun visitValueParameterDescriptor(descriptor: ValueParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { + val doc = context.getDocumentation(descriptor!!).extractText() + val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Parameter) + data?.addReferenceTo(node, DocumentationReferenceKind.Detail) + return node + } + + override fun visitClassDescriptor(descriptor: ClassDescriptor?, data: DocumentationNode?): DocumentationNode? { + val doc = context.getDocumentation(descriptor!!).extractText() + val node = DocumentationNode(descriptor.getName().asString(), doc, + when (descriptor.getKind()) { + ClassKind.OBJECT -> DocumentationNodeKind.Object + else -> DocumentationNodeKind.Class + } + ) + data?.addReferenceTo(node, DocumentationReferenceKind.Member) + return node + } + + override fun visitFunctionDescriptor(descriptor: FunctionDescriptor?, data: DocumentationNode?): DocumentationNode? { + val doc = context.getDocumentation(descriptor!!).extractText() + val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Function) + data?.addReferenceTo(node, DocumentationReferenceKind.Member) + return node + } + + override fun visitPropertyDescriptor(descriptor: PropertyDescriptor?, data: DocumentationNode?): DocumentationNode? { + val doc = context.getDocumentation(descriptor!!).extractText() + val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Property) + data?.addReferenceTo(node, DocumentationReferenceKind.Member) + return node + } + + override fun visitConstructorDescriptor(descriptor: ConstructorDescriptor?, data: DocumentationNode?): DocumentationNode? { + val doc = context.getDocumentation(descriptor!!).extractText() + val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Constructor) + data?.addReferenceTo(node, DocumentationReferenceKind.Member) + return node + } + + override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = DocumentationNode(descriptor!!.fqName.asString(), "", DocumentationNodeKind.Package) + data?.addReferenceTo(node, DocumentationReferenceKind.Member) + return node + } +} diff --git a/src/Documentation/DocumentationBuildingVisitor.kt b/src/Documentation/DocumentationBuildingVisitor.kt new file mode 100644 index 00000000..e7b3fc09 --- /dev/null +++ b/src/Documentation/DocumentationBuildingVisitor.kt @@ -0,0 +1,121 @@ +package org.jetbrains.dokka + +import org.jetbrains.jet.lang.descriptors.* +import org.jetbrains.jet.lang.resolve.name.FqName + +class DocumentationBuildingVisitor(private val worker: DeclarationDescriptorVisitor) +: DeclarationDescriptorVisitor { + + private fun visitChildren(descriptors: Collection, data: DocumentationNode) { + for (descriptor in descriptors) { + if (descriptor.isUserCode()) + descriptor.accept(this, data) + } + } + + private fun visitChild(descriptor: DeclarationDescriptor?, data: DocumentationNode) { + if (descriptor != null && descriptor.isUserCode()) + descriptor.accept(this, data) + } + + private fun createDocumentation(descriptor: DeclarationDescriptor, data: DocumentationNode): DocumentationNode { + return descriptor.accept(worker, data) + } + + private fun processCallable(descriptor: CallableDescriptor, data: DocumentationNode): DocumentationNode { + val node = createDocumentation(descriptor, data) + visitChildren(descriptor.getTypeParameters(), node) + visitChild(descriptor.getReceiverParameter(), node) + visitChildren(descriptor.getValueParameters(), node) + return node + } + + public override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = createDocumentation(descriptor!!, data!!) + visitChildren(descriptor.getMemberScope().getAllDescriptors(), node) + return node + } + + public override fun visitPackageViewDescriptor(descriptor: PackageViewDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = createDocumentation(descriptor!!, data!!) + visitChildren(descriptor.getMemberScope().getAllDescriptors(), node) + return node + } + + public override fun visitVariableDescriptor(descriptor: VariableDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = processCallable(descriptor!!, data!!) + return node + } + + public override fun visitPropertyDescriptor(descriptor: PropertyDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = processCallable(descriptor!!, data!!) + visitChild(descriptor.getGetter(), node) + visitChild(descriptor.getSetter(), node) + return node + } + + public override fun visitFunctionDescriptor(descriptor: FunctionDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = processCallable(descriptor!!, data!!) + return node + } + + public override fun visitTypeParameterDescriptor(descriptor: TypeParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = createDocumentation(descriptor!!, data!!) + return node + } + + public override fun visitClassDescriptor(descriptor: ClassDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = createDocumentation(descriptor!!, data!!) + if (descriptor.getKind() != ClassKind.OBJECT) { + // do not go inside object for class object and constructors, they are generated + visitChildren(descriptor.getTypeConstructor().getParameters(), node) + visitChildren(descriptor.getConstructors(), node) + visitChild(descriptor.getClassObjectDescriptor(), node) + } + val members = descriptor.getDefaultType().getMemberScope().getAllDescriptors().filter { + it !is CallableMemberDescriptor || it.isUserCode() + } + visitChildren(members, node) + return node + } + + public override fun visitModuleDeclaration(descriptor: ModuleDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = createDocumentation(descriptor!!, data!!) + visitChild(descriptor.getPackage(FqName.ROOT), node) + return node + } + + public override fun visitConstructorDescriptor(constructorDescriptor: ConstructorDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = visitFunctionDescriptor(constructorDescriptor, data) + return node + } + + public override fun visitScriptDescriptor(scriptDescriptor: ScriptDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = visitClassDescriptor(scriptDescriptor!!.getClassDescriptor(), data) + return node + } + + public override fun visitValueParameterDescriptor(descriptor: ValueParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = visitVariableDescriptor(descriptor, data) + return node + } + + public override fun visitPropertyGetterDescriptor(descriptor: PropertyGetterDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = visitFunctionDescriptor(descriptor, data) + return node + } + + public override fun visitPropertySetterDescriptor(descriptor: PropertySetterDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = visitFunctionDescriptor(descriptor, data) + return node + } + + public override fun visitReceiverParameterDescriptor(descriptor: ReceiverParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = createDocumentation(descriptor!!, data!!) + return node + } +} + +public fun visitDescriptor(descriptor: DeclarationDescriptor, data: DocumentationNode, visitor: DeclarationDescriptorVisitor): DocumentationNode { + return descriptor.accept(DocumentationBuildingVisitor(visitor), data) +} diff --git a/src/Documentation/DocumentationModel.kt b/src/Documentation/DocumentationModel.kt new file mode 100644 index 00000000..0113eb18 --- /dev/null +++ b/src/Documentation/DocumentationModel.kt @@ -0,0 +1,67 @@ +package org.jetbrains.dokka + +import org.jetbrains.jet.lang.resolve.BindingContext +import org.jetbrains.jet.lang.psi.JetFile + +public enum class DocumentationNodeKind { + Unknown + + Package + Class + Object + Constructor + Function + Property + Parameter + TypeParameter + Exception + + Page + Model +} + +public enum class DocumentationReferenceKind { + Member + Detail + Owner + Link + Override +} + +public open class DocumentationNode(val name: String, val doc: String, val kind: DocumentationNodeKind) { + private val references = arrayListOf() + + public val owner: DocumentationNode + get() = references(DocumentationReferenceKind.Owner).single().to + public val details: List + get() = references(DocumentationReferenceKind.Detail).map { it.to } + public val members: List + get() = references(DocumentationReferenceKind.Member).map { it.to } + public val links: List + get() = references(DocumentationReferenceKind.Link).map { it.to } + + // TODO: Should we allow node mutation? Model merge will copy by ref, so references are transparent, which could nice + public fun addReferenceTo(to: DocumentationNode, kind: DocumentationReferenceKind) { + references.add(DocumentationReference(this, to, kind)) + } + + public fun addAllReferencesFrom(other: DocumentationNode) { + references.addAll(other.references) + } + + public fun references(kind: DocumentationReferenceKind): List = references.filter { it.kind == kind } +} + +public class DocumentationModel : DocumentationNode("model", "", DocumentationNodeKind.Model) { + fun merge(other: DocumentationModel): DocumentationModel { + val model = DocumentationModel() + model.addAllReferencesFrom(other) + model.addAllReferencesFrom(this) + return model + } + + public val nodes: List + get() = members +} + +public data class DocumentationReference(val from: DocumentationNode, val to: DocumentationNode, val kind: DocumentationReferenceKind) diff --git a/src/DocumentationBuilder.kt b/src/DocumentationBuilder.kt deleted file mode 100644 index 77f74eb2..00000000 --- a/src/DocumentationBuilder.kt +++ /dev/null @@ -1,73 +0,0 @@ -package org.jetbrains.dokka - -import org.jetbrains.jet.lang.resolve.* -import org.jetbrains.jet.lang.psi.* -import org.jetbrains.jet.lang.descriptors.* -import org.jetbrains.jet.lang.descriptors.impl.* - -fun BindingContext.createDocumentation(file: JetFile): DocumentationModel { - val model = DocumentationModel() - val packageFragment = getPackageFragment(file) - if (packageFragment == null) throw IllegalArgumentException("File $file should have package fragment") - - val visitor = DocumentationBuilderVisitor(this) - visitDescriptor(packageFragment, model, visitor) - - return model -} - -class DocumentationBuilderVisitor(val context: BindingContext) : DeclarationDescriptorVisitorEmptyBodies() { - - override fun visitDeclarationDescriptor(descriptor: DeclarationDescriptor?, data: DocumentationNode?): DocumentationNode? { - val doc = context.getDocumentation(descriptor!!).extractText() - val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Unknown) - data?.addReferenceTo(node, DocumentationReferenceKind.Member) - return node - } - - override fun visitValueParameterDescriptor(descriptor: ValueParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { - val doc = context.getDocumentation(descriptor!!).extractText() - val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Parameter) - data?.addReferenceTo(node, DocumentationReferenceKind.Detail) - return node - } - - override fun visitClassDescriptor(descriptor: ClassDescriptor?, data: DocumentationNode?): DocumentationNode? { - val doc = context.getDocumentation(descriptor!!).extractText() - val node = DocumentationNode(descriptor.getName().asString(), doc, - when (descriptor.getKind()) { - ClassKind.OBJECT -> DocumentationNodeKind.Object - else -> DocumentationNodeKind.Class - } - ) - data?.addReferenceTo(node, DocumentationReferenceKind.Member) - return node - } - - override fun visitFunctionDescriptor(descriptor: FunctionDescriptor?, data: DocumentationNode?): DocumentationNode? { - val doc = context.getDocumentation(descriptor!!).extractText() - val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Function) - data?.addReferenceTo(node, DocumentationReferenceKind.Member) - return node - } - - override fun visitPropertyDescriptor(descriptor: PropertyDescriptor?, data: DocumentationNode?): DocumentationNode? { - val doc = context.getDocumentation(descriptor!!).extractText() - val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Property) - data?.addReferenceTo(node, DocumentationReferenceKind.Member) - return node - } - - override fun visitConstructorDescriptor(descriptor: ConstructorDescriptor?, data: DocumentationNode?): DocumentationNode? { - val doc = context.getDocumentation(descriptor!!).extractText() - val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Constructor) - data?.addReferenceTo(node, DocumentationReferenceKind.Member) - return node - } - - override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = DocumentationNode(descriptor!!.fqName.asString(), "", DocumentationNodeKind.Package) - data?.addReferenceTo(node, DocumentationReferenceKind.Member) - return node - } -} diff --git a/src/DocumentationBuildingVisitor.kt b/src/DocumentationBuildingVisitor.kt deleted file mode 100644 index e7b3fc09..00000000 --- a/src/DocumentationBuildingVisitor.kt +++ /dev/null @@ -1,121 +0,0 @@ -package org.jetbrains.dokka - -import org.jetbrains.jet.lang.descriptors.* -import org.jetbrains.jet.lang.resolve.name.FqName - -class DocumentationBuildingVisitor(private val worker: DeclarationDescriptorVisitor) -: DeclarationDescriptorVisitor { - - private fun visitChildren(descriptors: Collection, data: DocumentationNode) { - for (descriptor in descriptors) { - if (descriptor.isUserCode()) - descriptor.accept(this, data) - } - } - - private fun visitChild(descriptor: DeclarationDescriptor?, data: DocumentationNode) { - if (descriptor != null && descriptor.isUserCode()) - descriptor.accept(this, data) - } - - private fun createDocumentation(descriptor: DeclarationDescriptor, data: DocumentationNode): DocumentationNode { - return descriptor.accept(worker, data) - } - - private fun processCallable(descriptor: CallableDescriptor, data: DocumentationNode): DocumentationNode { - val node = createDocumentation(descriptor, data) - visitChildren(descriptor.getTypeParameters(), node) - visitChild(descriptor.getReceiverParameter(), node) - visitChildren(descriptor.getValueParameters(), node) - return node - } - - public override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = createDocumentation(descriptor!!, data!!) - visitChildren(descriptor.getMemberScope().getAllDescriptors(), node) - return node - } - - public override fun visitPackageViewDescriptor(descriptor: PackageViewDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = createDocumentation(descriptor!!, data!!) - visitChildren(descriptor.getMemberScope().getAllDescriptors(), node) - return node - } - - public override fun visitVariableDescriptor(descriptor: VariableDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = processCallable(descriptor!!, data!!) - return node - } - - public override fun visitPropertyDescriptor(descriptor: PropertyDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = processCallable(descriptor!!, data!!) - visitChild(descriptor.getGetter(), node) - visitChild(descriptor.getSetter(), node) - return node - } - - public override fun visitFunctionDescriptor(descriptor: FunctionDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = processCallable(descriptor!!, data!!) - return node - } - - public override fun visitTypeParameterDescriptor(descriptor: TypeParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = createDocumentation(descriptor!!, data!!) - return node - } - - public override fun visitClassDescriptor(descriptor: ClassDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = createDocumentation(descriptor!!, data!!) - if (descriptor.getKind() != ClassKind.OBJECT) { - // do not go inside object for class object and constructors, they are generated - visitChildren(descriptor.getTypeConstructor().getParameters(), node) - visitChildren(descriptor.getConstructors(), node) - visitChild(descriptor.getClassObjectDescriptor(), node) - } - val members = descriptor.getDefaultType().getMemberScope().getAllDescriptors().filter { - it !is CallableMemberDescriptor || it.isUserCode() - } - visitChildren(members, node) - return node - } - - public override fun visitModuleDeclaration(descriptor: ModuleDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = createDocumentation(descriptor!!, data!!) - visitChild(descriptor.getPackage(FqName.ROOT), node) - return node - } - - public override fun visitConstructorDescriptor(constructorDescriptor: ConstructorDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = visitFunctionDescriptor(constructorDescriptor, data) - return node - } - - public override fun visitScriptDescriptor(scriptDescriptor: ScriptDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = visitClassDescriptor(scriptDescriptor!!.getClassDescriptor(), data) - return node - } - - public override fun visitValueParameterDescriptor(descriptor: ValueParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = visitVariableDescriptor(descriptor, data) - return node - } - - public override fun visitPropertyGetterDescriptor(descriptor: PropertyGetterDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = visitFunctionDescriptor(descriptor, data) - return node - } - - public override fun visitPropertySetterDescriptor(descriptor: PropertySetterDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = visitFunctionDescriptor(descriptor, data) - return node - } - - public override fun visitReceiverParameterDescriptor(descriptor: ReceiverParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = createDocumentation(descriptor!!, data!!) - return node - } -} - -public fun visitDescriptor(descriptor: DeclarationDescriptor, data: DocumentationNode, visitor: DeclarationDescriptorVisitor): DocumentationNode { - return descriptor.accept(DocumentationBuildingVisitor(visitor), data) -} diff --git a/src/DocumentationModel.kt b/src/DocumentationModel.kt deleted file mode 100644 index 0113eb18..00000000 --- a/src/DocumentationModel.kt +++ /dev/null @@ -1,67 +0,0 @@ -package org.jetbrains.dokka - -import org.jetbrains.jet.lang.resolve.BindingContext -import org.jetbrains.jet.lang.psi.JetFile - -public enum class DocumentationNodeKind { - Unknown - - Package - Class - Object - Constructor - Function - Property - Parameter - TypeParameter - Exception - - Page - Model -} - -public enum class DocumentationReferenceKind { - Member - Detail - Owner - Link - Override -} - -public open class DocumentationNode(val name: String, val doc: String, val kind: DocumentationNodeKind) { - private val references = arrayListOf() - - public val owner: DocumentationNode - get() = references(DocumentationReferenceKind.Owner).single().to - public val details: List - get() = references(DocumentationReferenceKind.Detail).map { it.to } - public val members: List - get() = references(DocumentationReferenceKind.Member).map { it.to } - public val links: List - get() = references(DocumentationReferenceKind.Link).map { it.to } - - // TODO: Should we allow node mutation? Model merge will copy by ref, so references are transparent, which could nice - public fun addReferenceTo(to: DocumentationNode, kind: DocumentationReferenceKind) { - references.add(DocumentationReference(this, to, kind)) - } - - public fun addAllReferencesFrom(other: DocumentationNode) { - references.addAll(other.references) - } - - public fun references(kind: DocumentationReferenceKind): List = references.filter { it.kind == kind } -} - -public class DocumentationModel : DocumentationNode("model", "", DocumentationNodeKind.Model) { - fun merge(other: DocumentationModel): DocumentationModel { - val model = DocumentationModel() - model.addAllReferencesFrom(other) - model.addAllReferencesFrom(this) - return model - } - - public val nodes: List - get() = members -} - -public data class DocumentationReference(val from: DocumentationNode, val to: DocumentationNode, val kind: DocumentationReferenceKind) diff --git a/src/PsiAPI.kt b/src/PsiAPI.kt deleted file mode 100644 index dc59b0f1..00000000 --- a/src/PsiAPI.kt +++ /dev/null @@ -1,19 +0,0 @@ -package org.jetbrains.dokka - -import com.intellij.psi.PsiElement -import kotlin.support.AbstractIterator - -fun PsiElement.previousSiblings(): Stream { - var element: PsiElement? = this - return object : Stream { - override fun iterator(): Iterator = object : AbstractIterator() { - override fun computeNext() { - element = element?.getPrevSibling() - if (element == null) - done() - else - setNext(element!!) - } - } - } -} diff --git a/src/main.kt b/src/main.kt index 67f37197..e385da3f 100644 --- a/src/main.kt +++ b/src/main.kt @@ -13,11 +13,11 @@ public fun main(args: Array) { val compilerArguments = K2JVMCompilerArguments() val sources: List = Args.parse(compilerArguments, args) ?: listOf() - val environment = AnalysesEnvironment(MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR) { -/* - addClasspath(PathUtil.getJdkClassesRoots()) - addClasspath(PathUtil.getKotlinPathsForCompiler().getRuntimePath()) -*/ + val environment = AnalysisEnvironment(MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR) { + /* + addClasspath(PathUtil.getJdkClassesRoots()) + addClasspath(PathUtil.getKotlinPathsForCompiler().getRuntimePath()) + */ addSources(sources) } -- cgit