@@ -152,18 +152,31 @@ private class CompilationEnvironment(
152
152
}
153
153
}
154
154
155
+ /* *
156
+ * Determines the compilation environment used
157
+ * by the compiler (and thus the class path).
158
+ */
159
+ enum class CompilationKind {
160
+ /* * Uses the default class path. */
161
+ DEFAULT ,
162
+ /* * Uses the Kotlin DSL class path if available. */
163
+ BUILD_SCRIPT
164
+ }
165
+
155
166
/* *
156
167
* Incrementally compiles files and expressions.
157
168
* The basic strategy for compiling one file at-a-time is outlined in OneFilePerformance.
158
169
*/
159
- class Compiler (classPath : Set <Path >) : Closeable {
170
+ class Compiler (classPath : Set <Path >, buildScriptClassPath : Set < Path > = emptySet() ) : Closeable {
160
171
private var closed = false
161
172
private val localFileSystem: VirtualFileSystem
162
- private val compileEnvironment = CompilationEnvironment (classPath)
173
+
174
+ private val defaultCompileEnvironment = CompilationEnvironment (classPath)
175
+ private val buildScriptCompileEnvironment = buildScriptClassPath.takeIf { it.isNotEmpty() }?.let (::CompilationEnvironment )
163
176
private val compileLock = ReentrantLock () // TODO: Lock at file-level
164
177
165
178
val psiFileFactory
166
- get() = PsiFileFactory .getInstance(compileEnvironment .environment.project)
179
+ get() = PsiFileFactory .getInstance(defaultCompileEnvironment .environment.project)
167
180
168
181
companion object {
169
182
init {
@@ -180,10 +193,11 @@ class Compiler(classPath: Set<Path>) : Closeable {
180
193
* configuration (which is a class from this project).
181
194
*/
182
195
fun updateConfiguration (config : CompilerConfiguration ) {
183
- compileEnvironment.updateConfiguration(config)
196
+ defaultCompileEnvironment.updateConfiguration(config)
197
+ buildScriptCompileEnvironment?.updateConfiguration(config)
184
198
}
185
199
186
- fun createFile (content : String , file : Path = Paths .get("dummy.virtual.kts ")): KtFile {
200
+ fun createFile (content : String , file : Path = Paths .get("dummy.virtual.kt ")): KtFile {
187
201
assert (! content.contains(' \r ' ))
188
202
189
203
val new = psiFileFactory.createFileFromText(file.toString(), KotlinLanguage .INSTANCE , content, true , false ) as KtFile
@@ -192,13 +206,13 @@ class Compiler(classPath: Set<Path>) : Closeable {
192
206
return new
193
207
}
194
208
195
- fun createExpression (content : String , file : Path = Paths .get("dummy.virtual.kts ")): KtExpression {
209
+ fun createExpression (content : String , file : Path = Paths .get("dummy.virtual.kt ")): KtExpression {
196
210
val property = parseDeclaration(" val x = $content " , file) as KtProperty
197
211
198
212
return property.initializer!!
199
213
}
200
214
201
- fun createDeclaration (content : String , file : Path = Paths .get("dummy.virtual.kts ")): KtDeclaration =
215
+ fun createDeclaration (content : String , file : Path = Paths .get("dummy.virtual.kt ")): KtDeclaration =
202
216
parseDeclaration(content, file)
203
217
204
218
private fun parseDeclaration (content : String , file : Path ): KtDeclaration {
@@ -219,24 +233,29 @@ class Compiler(classPath: Set<Path>) : Closeable {
219
233
else return onlyDeclaration
220
234
}
221
235
222
- fun compileFile (file : KtFile , sourcePath : Collection <KtFile >): Pair <BindingContext , ComponentProvider > =
223
- compileFiles(listOf (file), sourcePath)
236
+ private fun compileEnvironmentFor (kind : CompilationKind ): CompilationEnvironment = when (kind) {
237
+ CompilationKind .DEFAULT -> defaultCompileEnvironment
238
+ CompilationKind .BUILD_SCRIPT -> buildScriptCompileEnvironment ? : defaultCompileEnvironment
239
+ }
240
+
241
+ fun compileFile (file : KtFile , sourcePath : Collection <KtFile >, kind : CompilationKind = CompilationKind .DEFAULT ): Pair <BindingContext , ComponentProvider > =
242
+ compileFiles(listOf (file), sourcePath, kind)
224
243
225
- fun compileFiles (files : Collection <KtFile >, sourcePath : Collection <KtFile >): Pair <BindingContext , ComponentProvider > {
244
+ fun compileFiles (files : Collection <KtFile >, sourcePath : Collection <KtFile >, kind : CompilationKind = CompilationKind . DEFAULT ): Pair <BindingContext , ComponentProvider > {
226
245
compileLock.withLock {
227
- val (container, trace) = compileEnvironment .createContainer(sourcePath)
246
+ val (container, trace) = compileEnvironmentFor(kind) .createContainer(sourcePath)
228
247
val topDownAnalyzer = container.get<LazyTopDownAnalyzer >()
229
248
topDownAnalyzer.analyzeDeclarations(TopLevelDeclarations , files)
230
249
231
250
return Pair (trace.bindingContext, container)
232
251
}
233
252
}
234
253
235
- fun compileExpression (expression : KtExpression , scopeWithImports : LexicalScope , sourcePath : Collection <KtFile >): Pair <BindingContext , ComponentProvider > {
254
+ fun compileExpression (expression : KtExpression , scopeWithImports : LexicalScope , sourcePath : Collection <KtFile >, kind : CompilationKind = CompilationKind . DEFAULT ): Pair <BindingContext , ComponentProvider > {
236
255
try {
237
256
// Use same lock as 'compileFile' to avoid concurrency issues such as #42
238
257
compileLock.withLock {
239
- val (container, trace) = compileEnvironment .createContainer(sourcePath)
258
+ val (container, trace) = compileEnvironmentFor(kind) .createContainer(sourcePath)
240
259
val incrementalCompiler = container.get<ExpressionTypingServices >()
241
260
incrementalCompiler.getTypeInfo(
242
261
scopeWithImports,
@@ -255,7 +274,8 @@ class Compiler(classPath: Set<Path>) : Closeable {
255
274
256
275
override fun close () {
257
276
if (! closed) {
258
- compileEnvironment.close()
277
+ defaultCompileEnvironment.close()
278
+ buildScriptCompileEnvironment?.close()
259
279
closed = true
260
280
} else {
261
281
LOG .warn(" Compiler is already closed!" )
0 commit comments