1
1
package org.javacs.kt
2
2
3
3
import org.jetbrains.kotlin.com.intellij.codeInsight.NullableNotNullManager
4
+ import org.jetbrains.kotlin.com.intellij.lang.Language
4
5
import org.jetbrains.kotlin.com.intellij.openapi.Disposable
5
6
import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer
6
7
import org.jetbrains.kotlin.com.intellij.openapi.vfs.StandardFileSystems
7
8
import org.jetbrains.kotlin.com.intellij.openapi.vfs.VirtualFileManager
8
9
import org.jetbrains.kotlin.com.intellij.openapi.vfs.VirtualFileSystem
10
+ import org.jetbrains.kotlin.com.intellij.psi.PsiFile
9
11
import org.jetbrains.kotlin.com.intellij.psi.PsiFileFactory
10
12
import org.jetbrains.kotlin.com.intellij.mock.MockProject
11
13
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
@@ -244,26 +246,25 @@ class Compiler(classPath: Set<Path>, buildScriptClassPath: Set<Path> = emptySet(
244
246
buildScriptCompileEnvironment?.updateConfiguration(config)
245
247
}
246
248
247
- fun createFile (content : String , file : Path = Paths .get("dummy.virtual.kt"), kind : CompilationKind = CompilationKind .DEFAULT ): KtFile {
249
+ fun createPsiFile (content : String , file : Path = Paths .get("dummy.virtual.kt"), language : Language = KotlinLanguage . INSTANCE , kind : CompilationKind = CompilationKind .DEFAULT ): PsiFile {
248
250
assert (! content.contains(' \r ' ))
249
251
250
- val new = psiFileFactoryFor(kind).createFileFromText(file.toString(), KotlinLanguage . INSTANCE , content, true , false ) as KtFile
252
+ val new = psiFileFactoryFor(kind).createFileFromText(file.toString(), language , content, true , false )
251
253
assert (new.virtualFile != null )
252
254
253
255
return new
254
256
}
255
257
256
- fun createExpression (content : String , file : Path = Paths .get("dummy.virtual.kt"), kind : CompilationKind = CompilationKind .DEFAULT ): KtExpression {
257
- val property = parseDeclaration( " val x = $content " , file, kind) as KtProperty
258
+ fun createKtFile (content : String , file : Path = Paths .get("dummy.virtual.kt"), kind : CompilationKind = CompilationKind .DEFAULT ): KtFile =
259
+ createPsiFile(content, file, language = KotlinLanguage . INSTANCE , kind = kind) as KtFile
258
260
261
+ fun createKtExpression (content : String , file : Path = Paths .get("dummy.virtual.kt"), kind : CompilationKind = CompilationKind .DEFAULT ): KtExpression {
262
+ val property = createKtDeclaration(" val x = $content " , file, kind) as KtProperty
259
263
return property.initializer!!
260
264
}
261
265
262
- fun createDeclaration (content : String , file : Path = Paths .get("dummy.virtual.kt"), kind : CompilationKind = CompilationKind .DEFAULT ): KtDeclaration =
263
- parseDeclaration(content, file, kind)
264
-
265
- private fun parseDeclaration (content : String , file : Path , kind : CompilationKind = CompilationKind .DEFAULT ): KtDeclaration {
266
- val parse = createFile(content, file, kind)
266
+ fun createKtDeclaration (content : String , file : Path = Paths .get("dummy.virtual.kt"), kind : CompilationKind = CompilationKind .DEFAULT ): KtDeclaration {
267
+ val parse = createKtFile(content, file, kind)
267
268
val declarations = parse.declarations
268
269
269
270
assert (declarations.size == 1 ) { " ${declarations.size} declarations in $content " }
@@ -288,10 +289,10 @@ class Compiler(classPath: Set<Path>, buildScriptClassPath: Set<Path> = emptySet(
288
289
fun psiFileFactoryFor (kind : CompilationKind ): PsiFileFactory =
289
290
PsiFileFactory .getInstance(compileEnvironmentFor(kind).environment.project)
290
291
291
- fun compileFile (file : KtFile , sourcePath : Collection <KtFile >, kind : CompilationKind = CompilationKind .DEFAULT ): Pair <BindingContext , ComponentProvider > =
292
- compileFiles (listOf (file), sourcePath, kind)
292
+ fun compileKtFile (file : KtFile , sourcePath : Collection <KtFile >, kind : CompilationKind = CompilationKind .DEFAULT ): Pair <BindingContext , ComponentProvider > =
293
+ compileKtFiles (listOf (file), sourcePath, kind)
293
294
294
- fun compileFiles (files : Collection <KtFile >, sourcePath : Collection <KtFile >, kind : CompilationKind = CompilationKind .DEFAULT ): Pair <BindingContext , ComponentProvider > {
295
+ fun compileKtFiles (files : Collection <KtFile >, sourcePath : Collection <KtFile >, kind : CompilationKind = CompilationKind .DEFAULT ): Pair <BindingContext , ComponentProvider > {
295
296
if (kind == CompilationKind .BUILD_SCRIPT ) {
296
297
// Print the (legacy) script template used by the compiled Kotlin DSL build file
297
298
files.forEach { LOG .debug { " $it -> ScriptDefinition: ${it.findScriptDefinition()?.asLegacyOrNull<KotlinScriptDefinition >()?.template?.simpleName} " } }
@@ -306,7 +307,7 @@ class Compiler(classPath: Set<Path>, buildScriptClassPath: Set<Path> = emptySet(
306
307
}
307
308
}
308
309
309
- fun compileExpression (expression : KtExpression , scopeWithImports : LexicalScope , sourcePath : Collection <KtFile >, kind : CompilationKind = CompilationKind .DEFAULT ): Pair <BindingContext , ComponentProvider > {
310
+ fun compileKtExpression (expression : KtExpression , scopeWithImports : LexicalScope , sourcePath : Collection <KtFile >, kind : CompilationKind = CompilationKind .DEFAULT ): Pair <BindingContext , ComponentProvider > {
310
311
try {
311
312
// Use same lock as 'compileFile' to avoid concurrency issues such as #42
312
313
compileLock.withLock {
0 commit comments