aboutsummaryrefslogtreecommitdiff
path: root/src/Analysis/AnalysisEnvironment.kt
blob: 346367eebb94c7715f46447d9d673ebfb2f8ffe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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<T>(processor: (JetCoreEnvironment, BindingContext) -> T): T {
        val environment = JetCoreEnvironment.createForProduction(this, configuration)
        val result = environment.analyze(messageCollector)
        return processor(environment, result)
    }

    public fun withContext<T>(processor: (BindingContext) -> T): T {
        return withContext { environment, context -> processor(context) }
    }

    public fun streamFiles<T>(processor: (BindingContext, JetFile) -> T): Stream<T> {
        return withContext { environment, context ->
            environment.getSourceFiles().stream().map { file -> processor(context, file) }
        }
    }

    public fun processFiles<T>(processor: (BindingContext, JetFile) -> T): List<T> {
        return withContext { environment, context ->
            environment.getSourceFiles().map { file -> processor(context, file) }
        }
    }

    public fun processFilesFlat<T>(processor: (BindingContext, JetFile) -> List<T>): List<T> {
        return withContext { environment, context ->
            environment.getSourceFiles().flatMap { file -> processor(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 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>) {
        configuration.addAll(CommonConfigurationKeys.SOURCE_ROOTS_KEY, list)
    }

    public override fun dispose() {
        Disposer.dispose(this)
    }
}