aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/AnalysisEnvironment.kt (renamed from src/DokkaContext.kt)31
-rw-r--r--src/CompilerAPI.kt11
-rw-r--r--src/main.kt37
-rw-r--r--test/data/function.kt2
-rw-r--r--test/playground.kt44
5 files changed, 90 insertions, 35 deletions
diff --git a/src/DokkaContext.kt b/src/AnalysisEnvironment.kt
index 5154c237..13316419 100644
--- a/src/DokkaContext.kt
+++ b/src/AnalysisEnvironment.kt
@@ -12,30 +12,33 @@ 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()
+public class AnalysisEnvironment(val messageCollector: MessageCollector, body: AnalysisEnvironment.()->Unit = {}) : Disposable {
+ val configuration = CompilerConfiguration();
- ;
{
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector)
+ body()
}
- private fun analyze<T>(analyser: (JetCoreEnvironment, BindingContext) -> T) {
+ private fun processContext<T>(processor: (JetCoreEnvironment, BindingContext) -> T): T {
val environment = JetCoreEnvironment.createForProduction(this, configuration)
val result = environment.analyze(messageCollector)
- analyser(environment, result)
+ return processor(environment, result)
}
- public fun analyze<T>(analyser: (BindingContext) -> T) {
- analyze { environment, context ->
- analyser(context)
+ public fun processContext<T>(processor: (BindingContext) -> T): T {
+ return processContext { environment, context -> processor(context) }
+ }
+
+ public fun streamFiles<T>(processor: (BindingContext, JetFile) -> T): Stream<T> {
+ return processContext { environment, context ->
+ environment.getSourceFiles().stream().map { file -> processor(context, file) }
}
}
- public fun analyzeFiles<T>(analyser: (BindingContext, JetFile) -> T) {
- analyze { environment, context ->
- for (file in environment.getSourceFiles())
- analyser(context, file)
+ public fun processFiles<T>(processor: (BindingContext, JetFile) -> T): List<T> {
+ return processContext { environment, context ->
+ environment.getSourceFiles().map { file -> processor(context, file) }
}
}
@@ -46,6 +49,10 @@ public class DokkaContext(val messageCollector: MessageCollector) : Disposable {
configuration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, list)
}
+ public fun addClasspath(file: File) {
+ configuration.add(JVMConfigurationKeys.CLASSPATH_KEY, file)
+ }
+
public val sources: List<String>
get() = configuration.get(CommonConfigurationKeys.SOURCE_ROOTS_KEY) ?: listOf()
public fun addSources(list: List<String>) {
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<File> {
- val classpath = arrayListOf<File>()
- classpath.addAll(PathUtil.getJdkClassesRoots())
- classpath.add(paths.getRuntimePath())
- return classpath
-}
-
private fun getAnnotationsPath(paths: KotlinPaths, arguments: K2JVMCompilerArguments): MutableList<File> {
val annotationsPath = arrayListOf<File>()
annotationsPath.add(paths.getJdkAnnotationsPath())
@@ -55,4 +48,6 @@ private fun JetCoreEnvironment.analyze(messageCollector: MessageCollector): Bind
return exhaust!!.getBindingContext()
}
-fun AnalyzerWithCompilerReport.analyzeAndReport(files: List<JetFile>, analyser: () -> AnalyzeExhaust) = analyzeAndReport(analyser, files) \ No newline at end of file
+fun AnalyzerWithCompilerReport.analyzeAndReport(files: List<JetFile>, analyser: () -> AnalyzeExhaust) = analyzeAndReport(analyser, files)
+
+fun BindingContext.getPackageFragment(file: JetFile) = get(BindingContext.FILE_TO_PACKAGE_FRAGMENT, 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<String>) {
- val dokka = DokkaContext(MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR)
- dokka.addClasspath(getClasspath(PathUtil.getKotlinPathsForCompiler()))
- val arguments = K2JVMCompilerArguments()
- val sources: List<String> = Args.parse(arguments, args) ?: listOf()
- dokka.addSources(sources)
+ val compilerArguments = K2JVMCompilerArguments()
+ val sources: List<String> = 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