aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlya Ryzhenkov <orangy@jetbrains.com>2014-07-11 15:28:02 +0400
committerIlya Ryzhenkov <orangy@jetbrains.com>2014-07-11 15:28:02 +0400
commitbdbdbd8986c0b16da69a4beced3cfd274dfd9086 (patch)
tree67d840925df998309ccd83e1a3252269ab57077b
parent7f6961a9de7d4b42e07b3f1fbd71e3bfb2200f9a (diff)
downloaddokka-bdbdbd8986c0b16da69a4beced3cfd274dfd9086.tar.gz
dokka-bdbdbd8986c0b16da69a4beced3cfd274dfd9086.tar.bz2
dokka-bdbdbd8986c0b16da69a4beced3cfd274dfd9086.zip
Primitive API for analysing JetFile in BindingContext.
-rw-r--r--.idea/dictionaries/orangy.xml7
-rw-r--r--dokka.iml4
-rw-r--r--src/CompilerAPI.kt58
-rw-r--r--src/DokkaContext.kt58
-rw-r--r--src/main.kt92
-rw-r--r--test/data/discovery.kt0
-rw-r--r--test/src/DiscoveryTest.kt2
7 files changed, 151 insertions, 70 deletions
diff --git a/.idea/dictionaries/orangy.xml b/.idea/dictionaries/orangy.xml
new file mode 100644
index 00000000..8ff96307
--- /dev/null
+++ b/.idea/dictionaries/orangy.xml
@@ -0,0 +1,7 @@
+<component name="ProjectDictionaryState">
+ <dictionary name="orangy">
+ <words>
+ <w>dokka</w>
+ </words>
+ </dictionary>
+</component> \ No newline at end of file
diff --git a/dokka.iml b/dokka.iml
index 91a990ac..6db7ee33 100644
--- a/dokka.iml
+++ b/dokka.iml
@@ -3,8 +3,8 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
- <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" packagePrefix="jetbrains.dokka" />
- <sourceFolder url="file://$MODULE_DIR$/test/src" isTestSource="true" />
+ <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" packagePrefix="com.jetbrains.dokka" />
+ <sourceFolder url="file://$MODULE_DIR$/test/src" isTestSource="true" packagePrefix="com.jetbrains.dokka.tests" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
diff --git a/src/CompilerAPI.kt b/src/CompilerAPI.kt
new file mode 100644
index 00000000..b68c0354
--- /dev/null
+++ b/src/CompilerAPI.kt
@@ -0,0 +1,58 @@
+package com.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.*
+
+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())
+ 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<PsiFile>(),
+ 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<JetFile>, analyser: () -> AnalyzeExhaust) = analyzeAndReport(analyser, files) \ No newline at end of file
diff --git a/src/DokkaContext.kt b/src/DokkaContext.kt
new file mode 100644
index 00000000..5154c237
--- /dev/null
+++ b/src/DokkaContext.kt
@@ -0,0 +1,58 @@
+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<T>(analyser: (JetCoreEnvironment, BindingContext) -> T) {
+ val environment = JetCoreEnvironment.createForProduction(this, configuration)
+ val result = environment.analyze(messageCollector)
+ analyser(environment, result)
+ }
+
+ public fun analyze<T>(analyser: (BindingContext) -> T) {
+ analyze { environment, context ->
+ analyser(context)
+ }
+ }
+
+ public fun analyzeFiles<T>(analyser: (BindingContext, JetFile) -> T) {
+ analyze { environment, context ->
+ for (file in environment.getSourceFiles())
+ analyser(context, file)
+ }
+ }
+
+ public val classpath: List<File>
+ get() = configuration.get(JVMConfigurationKeys.CLASSPATH_KEY) ?: listOf()
+
+ public fun addClasspath(list: List<File>) {
+ configuration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, list)
+ }
+
+ public val sources: List<String>
+ get() = configuration.get(CommonConfigurationKeys.SOURCE_ROOTS_KEY) ?: listOf()
+ public fun addSources(list: List<String>) {
+ 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 7edaf43d..7669af82 100644
--- a/src/main.kt
+++ b/src/main.kt
@@ -4,86 +4,42 @@ 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.cli.jvm.*
-import org.jetbrains.jet.config.*
-import org.jetbrains.jet.cli.jvm.compiler.*
-import org.jetbrains.jet.cli.common.*
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.resolve.BindingContext
+import org.jetbrains.jet.lang.psi.JetFile
public fun main(args: Array<String>) {
+ val dokka = DokkaContext(MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR)
+ dokka.addClasspath(getClasspath(PathUtil.getKotlinPathsForCompiler()))
val arguments = K2JVMCompilerArguments()
- arguments.freeArgs = Args.parse(arguments, args)
+ val sources: List<String> = Args.parse(arguments, args) ?: listOf()
+ dokka.addSources(sources)
- val rootDisposable = Disposer.newDisposable()
+ println("Dokka is preparing sources and libraries...")
+ println("Sources: ${dokka.sources.join()}")
+ println("Classpath: ${dokka.classpath.joinToString()}")
- val messageCollector = MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR
- val configuration = CompilerConfiguration()
+ println()
- val paths = PathUtil.getKotlinPathsForCompiler()
-
- configuration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, getClasspath(paths, arguments))
- configuration.addAll(CommonConfigurationKeys.SOURCE_ROOTS_KEY, arguments.freeArgs ?: listOf())
- configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector)
-
- val environment = JetCoreEnvironment.createForProduction(rootDisposable, configuration)
- val context = environment.analyze(messageCollector)
- rootDisposable.dispose()
-}
-
-private fun getClasspath(paths: KotlinPaths, arguments: K2JVMCompilerArguments): MutableList<File> {
- val classpath = arrayListOf<File>()
- classpath.addAll(PathUtil.getJdkClassesRoots())
- classpath.add(paths.getRuntimePath())
- val classPath = arguments.classpath
- if (classPath != null) {
- for (element in classPath.split(File.pathSeparatorChar)) {
- classpath.add(File(element))
- }
+ dokka.analyzeFiles { context, file ->
+ println("Processing: ${file.getName()}")
+ println()
+ analyseFile(context, file)
}
- return classpath
-}
-private fun getAnnotationsPath(paths: KotlinPaths, arguments: K2JVMCompilerArguments): MutableList<File> {
- val annotationsPath = arrayListOf<File>()
- annotationsPath.add(paths.getJdkAnnotationsPath())
- val annotationPaths = arguments.annotations
- if (annotationPaths != null) {
- for (element in annotationPaths.split(File.pathSeparatorChar)) {
- annotationsPath.add(File(element))
- }
- }
- return annotationsPath
+ Disposer.dispose(dokka)
}
-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<PsiFile>(),
- sharedModule,
- compilerConfiguration.get(JVMConfigurationKeys.MODULE_IDS),
- compilerConfiguration.get(JVMConfigurationKeys.INCREMENTAL_CACHE_BASE_DIR))
+fun analyseFile(context: BindingContext, file: JetFile) {
+ val packageFragment = context.get(BindingContext.FILE_TO_PACKAGE_FRAGMENT, file)
+ if (packageFragment == null) {
+ println("PackageFragment is null")
+ return
}
- val exhaust = analyzerWithCompilerReport.getAnalyzeExhaust()
- assert(exhaust != null) { "AnalyzeExhaust should be non-null, compiling: " + sourceFiles }
-
- return exhaust!!.getBindingContext()
+ println("Package: ${packageFragment}")
+ for (descriptor in packageFragment.getMemberScope().getAllDescriptors()) {
+ println("Member: ${descriptor}")
+ }
}
-
-fun AnalyzerWithCompilerReport.analyzeAndReport(files: List<JetFile>, analyser: () -> AnalyzeExhaust) = analyzeAndReport(analyser, files) \ No newline at end of file
diff --git a/test/data/discovery.kt b/test/data/discovery.kt
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/data/discovery.kt
diff --git a/test/src/DiscoveryTest.kt b/test/src/DiscoveryTest.kt
new file mode 100644
index 00000000..71a8aa4f
--- /dev/null
+++ b/test/src/DiscoveryTest.kt
@@ -0,0 +1,2 @@
+public class DiscoveryTest {
+} \ No newline at end of file