From ba96795f4eff615578f72c29a2f29b7111c8de8c Mon Sep 17 00:00:00 2001 From: Ilya Ryzhenkov Date: Fri, 11 Jul 2014 15:49:47 +0400 Subject: Improving API, setting up playground, preparing for processing data. --- src/AnalysisEnvironment.kt | 65 ++++++++++++++++++++++++++++++++++++++++++++++ src/CompilerAPI.kt | 11 +++----- src/DokkaContext.kt | 58 ----------------------------------------- src/main.kt | 37 ++++++++++++++++---------- test/data/function.kt | 2 -- test/playground.kt | 44 +++++++++++++++++++++++++++++++ 6 files changed, 136 insertions(+), 81 deletions(-) create mode 100644 src/AnalysisEnvironment.kt delete mode 100644 src/DokkaContext.kt delete mode 100644 test/data/function.kt create mode 100644 test/playground.kt diff --git a/src/AnalysisEnvironment.kt b/src/AnalysisEnvironment.kt new file mode 100644 index 00000000..13316419 --- /dev/null +++ b/src/AnalysisEnvironment.kt @@ -0,0 +1,65 @@ +package com.jetbrains.dokka + +import com.intellij.openapi.Disposable +import org.jetbrains.jet.config.CompilerConfiguration +import org.jetbrains.jet.cli.common.messages.MessageCollector +import org.jetbrains.jet.cli.common.CLIConfigurationKeys +import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment +import java.io.File +import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys +import org.jetbrains.jet.config.CommonConfigurationKeys +import com.intellij.openapi.util.Disposer +import org.jetbrains.jet.lang.resolve.BindingContext +import org.jetbrains.jet.lang.psi.JetFile + +public class AnalysisEnvironment(val messageCollector: MessageCollector, body: AnalysisEnvironment.()->Unit = {}) : Disposable { + val configuration = CompilerConfiguration(); + + { + configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector) + body() + } + + private fun processContext(processor: (JetCoreEnvironment, BindingContext) -> T): T { + val environment = JetCoreEnvironment.createForProduction(this, configuration) + val result = environment.analyze(messageCollector) + return processor(environment, result) + } + + public fun processContext(processor: (BindingContext) -> T): T { + return processContext { environment, context -> processor(context) } + } + + public fun streamFiles(processor: (BindingContext, JetFile) -> T): Stream { + return processContext { environment, context -> + environment.getSourceFiles().stream().map { file -> processor(context, file) } + } + } + + public fun processFiles(processor: (BindingContext, JetFile) -> T): List { + return processContext { environment, context -> + environment.getSourceFiles().map { 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/CompilerAPI.kt b/src/CompilerAPI.kt index b68c0354..756cd915 100644 --- a/src/CompilerAPI.kt +++ b/src/CompilerAPI.kt @@ -13,13 +13,6 @@ import org.jetbrains.jet.lang.resolve.* import org.jetbrains.jet.lang.psi.* import org.jetbrains.jet.analyzer.* -private fun getClasspath(paths: KotlinPaths): MutableList { - val classpath = arrayListOf() - classpath.addAll(PathUtil.getJdkClassesRoots()) - classpath.add(paths.getRuntimePath()) - return classpath -} - private fun getAnnotationsPath(paths: KotlinPaths, arguments: K2JVMCompilerArguments): MutableList { val annotationsPath = arrayListOf() annotationsPath.add(paths.getJdkAnnotationsPath()) @@ -55,4 +48,6 @@ private fun JetCoreEnvironment.analyze(messageCollector: MessageCollector): Bind return exhaust!!.getBindingContext() } -fun AnalyzerWithCompilerReport.analyzeAndReport(files: List, analyser: () -> AnalyzeExhaust) = analyzeAndReport(analyser, files) \ No newline at end of file +fun AnalyzerWithCompilerReport.analyzeAndReport(files: List, analyser: () -> AnalyzeExhaust) = analyzeAndReport(analyser, files) + +fun BindingContext.getPackageFragment(file: JetFile) = get(BindingContext.FILE_TO_PACKAGE_FRAGMENT, file) diff --git a/src/DokkaContext.kt b/src/DokkaContext.kt deleted file mode 100644 index 5154c237..00000000 --- a/src/DokkaContext.kt +++ /dev/null @@ -1,58 +0,0 @@ -package com.jetbrains.dokka - -import com.intellij.openapi.Disposable -import org.jetbrains.jet.config.CompilerConfiguration -import org.jetbrains.jet.cli.common.messages.MessageCollector -import org.jetbrains.jet.cli.common.CLIConfigurationKeys -import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment -import java.io.File -import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys -import org.jetbrains.jet.config.CommonConfigurationKeys -import com.intellij.openapi.util.Disposer -import org.jetbrains.jet.lang.resolve.BindingContext -import org.jetbrains.jet.lang.psi.JetFile - -public class DokkaContext(val messageCollector: MessageCollector) : Disposable { - val configuration = CompilerConfiguration() - - ; - { - configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector) - } - - private fun analyze(analyser: (JetCoreEnvironment, BindingContext) -> T) { - val environment = JetCoreEnvironment.createForProduction(this, configuration) - val result = environment.analyze(messageCollector) - analyser(environment, result) - } - - public fun analyze(analyser: (BindingContext) -> T) { - analyze { environment, context -> - analyser(context) - } - } - - public fun analyzeFiles(analyser: (BindingContext, JetFile) -> T) { - analyze { environment, context -> - for (file in environment.getSourceFiles()) - analyser(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 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/main.kt b/src/main.kt index 7669af82..8b550999 100644 --- a/src/main.kt +++ b/src/main.kt @@ -1,38 +1,49 @@ package com.jetbrains.dokka -import org.jetbrains.jet.cli.common.arguments.* import com.sampullara.cli.* import com.intellij.openapi.util.* import org.jetbrains.jet.cli.common.messages.* import org.jetbrains.jet.utils.* import org.jetbrains.jet.lang.resolve.BindingContext import org.jetbrains.jet.lang.psi.JetFile +import org.jetbrains.jet.cli.common.arguments.K2JVMCompilerArguments public fun main(args: Array) { - val dokka = DokkaContext(MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR) - dokka.addClasspath(getClasspath(PathUtil.getKotlinPathsForCompiler())) - val arguments = K2JVMCompilerArguments() - val sources: List = Args.parse(arguments, args) ?: listOf() - dokka.addSources(sources) + val compilerArguments = K2JVMCompilerArguments() + val sources: List = Args.parse(compilerArguments, args) ?: listOf() + + val environment = AnalysisEnvironment(MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR) { + addClasspath(PathUtil.getJdkClassesRoots()) + addClasspath(PathUtil.getKotlinPathsForCompiler().getRuntimePath()) + + addSources(sources) + } println("Dokka is preparing sources and libraries...") - println("Sources: ${dokka.sources.join()}") - println("Classpath: ${dokka.classpath.joinToString()}") + println("Sources: ${environment.sources.join()}") + println("Classpath: ${environment.classpath.joinToString()}") println() - dokka.analyzeFiles { context, file -> + val results = environment.processFiles { context, file -> println("Processing: ${file.getName()}") println() - analyseFile(context, file) + context.analyseFile(file) } - Disposer.dispose(dokka) + println() + println("Results:") + results.forEach { + println(it) + } + + Disposer.dispose(environment) } -fun analyseFile(context: BindingContext, file: JetFile) { - val packageFragment = context.get(BindingContext.FILE_TO_PACKAGE_FRAGMENT, file) + +fun BindingContext.analyseFile(file: JetFile) { + val packageFragment = getPackageFragment(file) if (packageFragment == null) { println("PackageFragment is null") return diff --git a/test/data/function.kt b/test/data/function.kt deleted file mode 100644 index facbf677..00000000 --- a/test/data/function.kt +++ /dev/null @@ -1,2 +0,0 @@ - -fun fn() {} \ No newline at end of file diff --git a/test/playground.kt b/test/playground.kt new file mode 100644 index 00000000..136995d1 --- /dev/null +++ b/test/playground.kt @@ -0,0 +1,44 @@ +// this file is not included in sources or tests, you can play with it for debug purposes +// Console run configuration will analyse it and provide lots of debug output + +fun topLevelFunction() { +} + +val topLevelConstantValue = "Hello" + +val topLevelValue : String + get() = "Bye bye" + +var topLevelVariable : String + get() = "Modify me!" + set(value) { + } + +class Class { + fun memberFunction() { + } + +} + +object Object { + fun objectFunction() { + } +} + +class OuterClass { + + class NestedClass { + fun nestedClassFunction() { + } + } + + inner class InnerClass { + fun innerClassFunction() { + } + } + + object NestedObject { + fun nestedObjectFunction() { + } + } +} \ No newline at end of file -- cgit