Skip to content

Commit b533dc4

Browse files
committed
Support indexation with compiler plugin
1 parent 69cdecd commit b533dc4

File tree

12 files changed

+99
-40
lines changed

12 files changed

+99
-40
lines changed

dependencies/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,20 @@ dependencies {
8383
kotlinJsDependency("org.jetbrains.kotlin:kotlin-stdlib-js:$kotlinVersion")
8484
kotlinJsDependency("org.jetbrains.kotlin:kotlin-dom-api-compat:$kotlinVersion")
8585
kotlinWasmDependency("org.jetbrains.kotlin:kotlin-stdlib-wasm-js:$kotlinVersion")
86+
87+
// compose
8688
kotlinWasmDependency("org.jetbrains.compose.runtime:runtime:1.6.0-alpha01")
8789
kotlinWasmDependency("org.jetbrains.compose.ui:ui:1.6.0-alpha01")
8890
kotlinWasmDependency("org.jetbrains.compose.animation:animation:1.6.0-alpha01")
91+
kotlinWasmDependency("org.jetbrains.compose.animation:animation-graphics:1.6.0-alpha01")
8992
kotlinWasmDependency("org.jetbrains.compose.foundation:foundation:1.6.0-alpha01")
9093
kotlinWasmDependency("org.jetbrains.compose.material:material:1.6.0-alpha01")
9194
kotlinWasmDependency("org.jetbrains.compose.components:components-resources:1.6.0-alpha01")
9295

9396
kotlinJsDependency("org.jetbrains.compose.runtime:runtime:1.6.0-alpha01")
9497
kotlinJsDependency("org.jetbrains.compose.ui:ui:1.6.0-alpha01")
9598
kotlinJsDependency("org.jetbrains.compose.animation:animation:1.6.0-alpha01")
99+
kotlinJsDependency("org.jetbrains.compose.animation:animation-graphics:1.6.0-alpha01")
96100
kotlinJsDependency("org.jetbrains.compose.foundation:foundation:1.6.0-alpha01")
97101
kotlinJsDependency("org.jetbrains.compose.material:material:1.6.0-alpha01")
98102
kotlinJsDependency("org.jetbrains.compose.components:components-resources:1.6.0-alpha01")

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ systemProp.policy=executor.policy
55
systemProp.indexes=indexes.json
66
systemProp.indexesJs=indexesJs.json
77
systemProp.indexesWasm=indexesWasm.json
8+
systemProp.indexesComposeWasm=indexesComposeWasm.json

indexation/src/main/kotlin/Main.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ fun main(args: Array<String>) {
1313
WebIndexationBuilder(
1414
kotlinEnvironment = kotlinEnvironment,
1515
configuration = kotlinEnvironment.jsConfiguration,
16-
libraries = kotlinEnvironment.JS_LIBRARIES
16+
libraries = kotlinEnvironment.JS_LIBRARIES,
17+
compilerPlugins = false
1718
).writeIndexesToFile(outputPathJs)
1819

1920
WebIndexationBuilder(
2021
kotlinEnvironment = kotlinEnvironment,
2122
configuration = kotlinEnvironment.wasmConfiguration,
22-
libraries = kotlinEnvironment.WASM_LIBRARIES
23+
libraries = kotlinEnvironment.WASM_LIBRARIES,
24+
compilerPlugins = true
2325
).writeIndexesToFile(outputPathWasm)
2426
}

indexation/src/main/kotlin/WebIndexationBuilder.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package indexation
33
import model.ImportInfo
44
import component.KotlinEnvironment
55
import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport
6+
import org.jetbrains.kotlin.cli.jvm.plugins.PluginCliParser
67
import org.jetbrains.kotlin.config.CompilerConfiguration
78
import org.jetbrains.kotlin.ir.backend.js.prepareAnalyzedSourceModule
89
import org.jetbrains.kotlin.library.impl.isKotlinLibrary
@@ -11,13 +12,22 @@ import java.io.File
1112
class WebIndexationBuilder(
1213
private val kotlinEnvironment: KotlinEnvironment,
1314
private val configuration: CompilerConfiguration,
14-
private val libraries: List<String>
15+
private val libraries: List<String>,
16+
private val compilerPlugins: Boolean
1517
): IndexationBuilder() {
1618

1719
override fun getAllIndexes(): List<ImportInfo> =
1820
kotlinEnvironment.environment { coreEnvironment ->
1921
val project = coreEnvironment.project
2022

23+
if (compilerPlugins) {
24+
PluginCliParser.loadPluginsSafe(
25+
kotlinEnvironment.COMPILER_PLUGINS,
26+
kotlinEnvironment.compilerPluginOptions,
27+
emptyList(),
28+
configuration
29+
)
30+
}
2131
val sourceModule = prepareAnalyzedSourceModule(
2232
project,
2333
coreEnvironment.getSourceFiles(),

src/main/kotlin/com/compiler/server/compiler/components/CompletionProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class CompletionProvider(
204204
val analysis = when {
205205
projectType.isJvmRelated() -> errorAnalyzer.analysisOf(files, coreEnvironment)
206206
projectType.isJsRelated() -> errorAnalyzer.analyzeFileForJs(files, coreEnvironment)
207-
projectType == ProjectType.WASM -> errorAnalyzer.analyzeFileForWasm(files, coreEnvironment)
207+
projectType.isWasmRelated() -> errorAnalyzer.analyzeFileForWasm(files, coreEnvironment)
208208
else -> throw IllegalArgumentException("Unknown project type $projectType")
209209
}
210210
return with(analysis) {

src/main/kotlin/com/compiler/server/compiler/components/ErrorAnalyzer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ErrorAnalyzer(
6161
val analysis = when {
6262
projectType.isJvmRelated() -> analysisOf(files, coreEnvironment)
6363
projectType.isJsRelated() -> analyzeFileForJs(files, coreEnvironment)
64-
projectType == ProjectType.WASM -> analyzeFileForWasm(files, coreEnvironment)
64+
projectType.isWasmRelated() -> analyzeFileForWasm(files, coreEnvironment)
6565
else -> throw IllegalArgumentException("Unknown platform: $projectType")
6666
}
6767
return ErrorsAndAnalysis(

src/main/kotlin/com/compiler/server/compiler/components/IndexationProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ class IndexationProvider(
3535
fun hasIndexes(projectType: ProjectType) = when {
3636
projectType.isJsRelated() -> jsIndexes != null
3737
projectType.isJvmRelated() -> jvmIndexes != null
38-
projectType == ProjectType.WASM -> wasmIndexes != null
38+
projectType.isWasmRelated() -> wasmIndexes != null
3939
else -> throw IllegalArgumentException("Platform $projectType not found")
4040
}
4141

4242
fun getClassesByName(name: String, projectType: ProjectType): List<ImportInfo>? {
4343
val indexes = when {
4444
projectType.isJsRelated() -> jsIndexes
4545
projectType.isJvmRelated() -> jvmIndexes
46-
projectType == ProjectType.WASM -> wasmIndexes
46+
projectType.isWasmRelated() -> wasmIndexes
4747
else -> throw IllegalArgumentException("Platform $projectType not found")
4848
}
4949
return indexes?.get(name)

src/main/kotlin/com/compiler/server/compiler/components/KotlinToJSTranslator.kt

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ class KotlinToJSTranslator(
3131
fun translateJs(
3232
files: List<KtFile>,
3333
arguments: List<String>,
34-
translate: (List<KtFile>, List<String>) -> CompilationResult<String>
34+
compilerPlugins: Boolean,
35+
translate: (List<KtFile>, List<String>, Boolean) -> CompilationResult<String>
3536
): TranslationJSResult = try {
36-
val compilationResult = translate(files, arguments)
37+
val compilationResult = translate(files, arguments, compilerPlugins)
3738
val jsCode = when (compilationResult) {
3839
is Compiled<String> -> compilationResult.result
3940
is NotCompiled -> null
@@ -45,10 +46,12 @@ class KotlinToJSTranslator(
4546

4647
fun translateWasm(
4748
files: List<KtFile>,
48-
translate: (List<KtFile>) -> CompilationResult<WasmTranslationSuccessfulOutput>
49+
debugInfo: Boolean,
50+
compilerPlugins: Boolean,
51+
translate: (List<KtFile>, Boolean) -> CompilationResult<WasmTranslationSuccessfulOutput>
4952
): TranslationResultWithJsCode {
5053
return try {
51-
val compilationResult = translate(files)
54+
val compilationResult = translate(files, compilerPlugins)
5255
val wasmCompilationOutput = when (compilationResult) {
5356
is Compiled<WasmTranslationSuccessfulOutput> -> compilationResult.result
5457
is NotCompiled -> return TranslationJSResult(compilerDiagnostics = compilationResult.compilerDiagnostics)
@@ -58,14 +61,14 @@ class KotlinToJSTranslator(
5861
jsInstantiated = wasmCompilationOutput.jsInstantiated,
5962
compilerDiagnostics = compilationResult.compilerDiagnostics,
6063
wasm = wasmCompilationOutput.wasm,
61-
wat = wasmCompilationOutput.wat
64+
wat = if (debugInfo) wasmCompilationOutput.wat else null
6265
)
6366
} catch (e: Exception) {
6467
TranslationJSResult(exception = e.toExceptionDescriptor())
6568
}
6669
}
6770

68-
fun doTranslateWithIr(files: List<KtFile>, arguments: List<String>): CompilationResult<String> =
71+
fun doTranslateWithIr(files: List<KtFile>, arguments: List<String>, compilerPlugins: Boolean): CompilationResult<String> =
6972
usingTempDirectory { inputDir ->
7073
val moduleName = "moduleId"
7174
usingTempDirectory { outputDir ->
@@ -79,11 +82,13 @@ class KotlinToJSTranslator(
7982
"-libraries=${kotlinEnvironment.JS_LIBRARIES.joinToString(PATH_SEPARATOR)}",
8083
"-ir-output-dir=$klibPath",
8184
"-ir-output-name=$moduleName",
82-
) + kotlinEnvironment.COMPILER_PLUGINS.map {
83-
"-Xplugin=$it"
84-
} + kotlinEnvironment.compilerPluginOptions.map {
85-
"-P=$it"
86-
}
85+
) + if (compilerPlugins) {
86+
kotlinEnvironment.COMPILER_PLUGINS.map {
87+
"-Xplugin=$it"
88+
} + kotlinEnvironment.compilerPluginOptions.map {
89+
"-P=$it"
90+
}
91+
} else emptyList()
8792
k2JsIrCompiler.tryCompilation(inputDir, ioFiles, filePaths + additionalCompilerArgumentsForKLib)
8893
.flatMap {
8994
k2JsIrCompiler.tryCompilation(inputDir, ioFiles, listOf(
@@ -113,7 +118,7 @@ class KotlinToJSTranslator(
113118
}
114119

115120

116-
fun doTranslateWithWasm(files: List<KtFile>): CompilationResult<WasmTranslationSuccessfulOutput> =
121+
fun doTranslateWithWasm(files: List<KtFile>, compilerPlugins: Boolean): CompilationResult<WasmTranslationSuccessfulOutput> =
117122
usingTempDirectory { inputDir ->
118123
val moduleName = "moduleId"
119124
usingTempDirectory { outputDir ->
@@ -127,11 +132,13 @@ class KotlinToJSTranslator(
127132
"-libraries=${kotlinEnvironment.WASM_LIBRARIES.joinToString(PATH_SEPARATOR)}",
128133
"-ir-output-dir=$klibPath",
129134
"-ir-output-name=$moduleName",
130-
) + kotlinEnvironment.COMPILER_PLUGINS.map {
131-
"-Xplugin=$it"
132-
} + kotlinEnvironment.compilerPluginOptions.map {
133-
"-P=$it"
134-
}
135+
) + if (compilerPlugins) {
136+
kotlinEnvironment.COMPILER_PLUGINS.map {
137+
"-Xplugin=$it"
138+
} + kotlinEnvironment.compilerPluginOptions.map {
139+
"-P=$it"
140+
}
141+
} else emptyList()
135142

136143
k2JsIrCompiler.tryCompilation(inputDir, ioFiles, filePaths + additionalCompilerArgumentsForKLib)
137144
.flatMap {

src/main/kotlin/com/compiler/server/controllers/CompilerRestController.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ class CompilerRestController(private val kotlinProjectExecutor: KotlinProjectExe
2121
@PostMapping("/translate")
2222
fun translateKotlinProjectEndpoint(
2323
@RequestBody project: Project,
24-
@RequestParam(defaultValue = "js") compiler: String
24+
@RequestParam(defaultValue = "js") compiler: String,
25+
@RequestParam(defaultValue = "false") compilerPlugins: Boolean,
26+
@RequestParam(defaultValue = "false") debugInfo: Boolean
2527
): TranslationResultWithJsCode {
2628
return when (KotlinTranslatableCompiler.valueOf(compiler.uppercase())) {
27-
KotlinTranslatableCompiler.JS -> kotlinProjectExecutor.convertToJsIr(project)
28-
KotlinTranslatableCompiler.WASM -> kotlinProjectExecutor.convertToWasm(project)
29+
KotlinTranslatableCompiler.JS -> kotlinProjectExecutor.convertToJsIr(project, compilerPlugins)
30+
KotlinTranslatableCompiler.WASM -> kotlinProjectExecutor.convertToWasm(project, debugInfo, compilerPlugins)
2931
}
3032
}
3133

src/main/kotlin/com/compiler/server/controllers/KotlinPlaygroundRestController.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,16 @@ class KotlinPlaygroundRestController(private val kotlinProjectExecutor: KotlinPr
4141
when (project.confType) {
4242
ProjectType.JAVA -> kotlinProjectExecutor.run(project)
4343
ProjectType.JS -> throw LegacyJsException()
44-
ProjectType.JS_IR, ProjectType.CANVAS -> kotlinProjectExecutor.convertToJsIr(project)
45-
ProjectType.WASM -> kotlinProjectExecutor.convertToWasm(project)
44+
ProjectType.JS_IR, ProjectType.CANVAS ->
45+
kotlinProjectExecutor.convertToJsIr(
46+
project,
47+
compilerPlugins = false
48+
)
49+
ProjectType.WASM, ProjectType.COMPOSE_WASM -> kotlinProjectExecutor.convertToWasm(
50+
project,
51+
debugInfo = false,
52+
compilerPlugins = false
53+
)
4654
ProjectType.JUNIT -> kotlinProjectExecutor.test(project)
4755
}
4856
}

0 commit comments

Comments
 (0)