[JS IR] Generate special region comments at the start and the end of each file block
It allows to fold these blocks and navigate to them in the IDEA. Also, this commit adds compiler configuration keys to control generating comments with paths for file blocks, and now generating such comments is enabled only in our tests.
This commit is contained in:
+54
-10
@@ -31,34 +31,52 @@ class IrModuleToJsTransformer(
|
||||
) {
|
||||
val moduleName = backendContext.configuration[CommonConfigurationKeys.MODULE_NAME]!!
|
||||
private val moduleKind = backendContext.configuration[JSConfigurationKeys.MODULE_KIND]!!
|
||||
private val generateRegionComments = backendContext.configuration.getBoolean(JSConfigurationKeys.GENERATE_REGION_COMMENTS)
|
||||
|
||||
private fun generateModuleBody(module: IrModuleFragment, context: JsGenerationContext): List<JsStatement> {
|
||||
val statements = mutableListOf<JsStatement>().also {
|
||||
if (!generateScriptModule) it += JsStringLiteral("use strict").makeStmt()
|
||||
}
|
||||
|
||||
val preDeclarationBlock = JsBlock()
|
||||
val postDeclarationBlock = JsBlock()
|
||||
val preDeclarationBlock = JsGlobalBlock()
|
||||
val postDeclarationBlock = JsGlobalBlock()
|
||||
|
||||
statements += preDeclarationBlock
|
||||
statements.addWithComment("block: pre-declaration", preDeclarationBlock)
|
||||
|
||||
val generateFilePaths = backendContext.configuration.getBoolean(JSConfigurationKeys.GENERATE_COMMENTS_WITH_FILE_PATH)
|
||||
|
||||
module.files.forEach {
|
||||
val fileStatements = it.accept(IrFileToJsTransformer(), context).statements
|
||||
if (fileStatements.isNotEmpty()) {
|
||||
statements.add(JsSingleLineComment("file: ${it.path}"))
|
||||
var startComment = ""
|
||||
|
||||
if (generateRegionComments) {
|
||||
startComment = "region "
|
||||
}
|
||||
|
||||
if (generateRegionComments || generateFilePaths) {
|
||||
startComment += "file: ${it.path}"
|
||||
}
|
||||
|
||||
if (startComment.isNotEmpty()) {
|
||||
statements.add(JsSingleLineComment(startComment))
|
||||
}
|
||||
|
||||
statements.addAll(fileStatements)
|
||||
statements.endRegion()
|
||||
}
|
||||
}
|
||||
|
||||
// sort member forwarding code
|
||||
processClassModels(context.staticContext.classModels, preDeclarationBlock, postDeclarationBlock)
|
||||
|
||||
statements += postDeclarationBlock
|
||||
statements += context.staticContext.initializerBlock
|
||||
statements.addWithComment("block: post-declaration", postDeclarationBlock.statements)
|
||||
statements.addWithComment("block: init", context.staticContext.initializerBlock.statements)
|
||||
|
||||
if (backendContext.hasTests) {
|
||||
statements.startRegion("block: tests")
|
||||
statements += JsInvocation(context.getNameForStaticFunction(backendContext.testContainer).makeRef()).makeStmt()
|
||||
statements.endRegion()
|
||||
}
|
||||
|
||||
return statements
|
||||
@@ -108,18 +126,18 @@ class IrModuleToJsTransformer(
|
||||
|
||||
if (generateScriptModule) {
|
||||
with(program.globalBlock) {
|
||||
statements += importStatements
|
||||
statements.addWithComment("block: imports", importStatements)
|
||||
statements += moduleBody
|
||||
statements += exportStatements
|
||||
statements.addWithComment("block: exports", exportStatements)
|
||||
}
|
||||
} else {
|
||||
with(rootFunction) {
|
||||
parameters += JsParameter(internalModuleName)
|
||||
parameters += importedJsModules.map { JsParameter(it.internalName) }
|
||||
with(body) {
|
||||
statements += importStatements
|
||||
statements.addWithComment("block: imports", importStatements)
|
||||
statements += moduleBody
|
||||
statements += exportStatements
|
||||
statements.addWithComment("block: exports", exportStatements)
|
||||
statements += generateCallToMain(rootContext)
|
||||
statements += JsReturn(internalModuleName.makeRef())
|
||||
}
|
||||
@@ -229,4 +247,30 @@ class IrModuleToJsTransformer(
|
||||
declarationHandler
|
||||
)
|
||||
}
|
||||
|
||||
private fun MutableList<JsStatement>.startRegion(description: String = "") {
|
||||
if (generateRegionComments) {
|
||||
this += JsSingleLineComment("region $description")
|
||||
}
|
||||
}
|
||||
|
||||
private fun MutableList<JsStatement>.endRegion() {
|
||||
if (generateRegionComments) {
|
||||
this += JsSingleLineComment("endregion")
|
||||
}
|
||||
}
|
||||
|
||||
private fun MutableList<JsStatement>.addWithComment(regionDescription: String = "", block: JsBlock) {
|
||||
startRegion(regionDescription)
|
||||
this += block
|
||||
endRegion()
|
||||
}
|
||||
|
||||
private fun MutableList<JsStatement>.addWithComment(regionDescription: String = "", statements: List<JsStatement>) {
|
||||
if (statements.isEmpty()) return
|
||||
|
||||
startRegion(regionDescription)
|
||||
this += statements
|
||||
endRegion()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user