Skip to content

Commit

Permalink
Merge pull request #267 from modelix/typescript-gen-identation
Browse files Browse the repository at this point in the history
fix(model-api-gen): indentation in generated TypeScript files
  • Loading branch information
slisson authored Oct 2, 2023
2 parents 9b7de09 + 9080f16 commit d29aa82
Showing 1 changed file with 41 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,44 @@ class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = Na
// TODO delete old files from previous generation
outputDir
.resolve(language.generatedClassName().simpleName + ".ts")
.writeText(generateLanguage(language))
.fixFormatAndWriteText(generateLanguage(language))

generateRegistry(languages)
}
}

private fun fixFormat(input: CharSequence): CharSequence {
val result = StringBuffer(input.length)
var indentLevel = 0
for (line in input.lineSequence()) {
val trimmed = line.trimStart()
if (trimmed.isEmpty()) continue
repeat(indentLevel - (if (trimmed.startsWith("}")) 1 else 0)) {
result.append(" ")
}
result.appendLine(trimmed)
indentLevel += line.count { it == '{' } - line.count { it == '}' }
}
return result
}

private fun Path.fixFormatAndWriteText(text: String) = writeText(fixFormat(text))

private fun generateRegistry(languages: ProcessedLanguageSet) {
outputDir.resolve("index.ts").writeText(
outputDir.resolve("index.ts").fixFormatAndWriteText(
"""
import { LanguageRegistry } from "@modelix/ts-model-api";
${languages.getLanguages().joinToString("\n") { """
import { ${it.simpleClassName()} } from "./${it.simpleClassName()}";
""".trimIndent()
"""
}}
export function registerLanguages() {
${languages.getLanguages().joinToString("\n") { """
LanguageRegistry.INSTANCE.register(${it.simpleClassName()}.INSTANCE);
""".trimIndent()
"""
}}
}
""".trimIndent(),
""",
)
}

Expand Down Expand Up @@ -84,7 +101,7 @@ class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = Na
${language.getConcepts().joinToString("\n") { concept ->
"""
this.nodeWrappers.set("${concept.uid}", (node: INodeJS) => new ${concept.nodeWrapperImplName()}(node))
""".trimIndent()
"""
}}
}
public getConcepts() {
Expand All @@ -93,7 +110,7 @@ class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = Na
}
${language.getConcepts().joinToString("\n") { generateConcept(it) }.replaceIndent(" ")}
""".trimIndent()
"""
}

private fun generateConcept(concept: ProcessedConcept): String {
Expand All @@ -108,7 +125,7 @@ class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = Na
public get $rawValueName(): string | undefined {
return this._node.getPropertyValue("${feature.originalName}")
}
""".trimIndent()
"""
val typedPropertyText = if (feature.type is PrimitivePropertyType) {
when ((feature.type as PrimitivePropertyType).primitive) {
Primitive.INT -> {
Expand All @@ -121,7 +138,7 @@ class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = Na
return str ? parseInt(str) : 0;
}
""".trimIndent()
"""
}
Primitive.BOOLEAN -> {
"""
Expand All @@ -132,25 +149,26 @@ class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = Na
return this.$rawValueName === "true";
}
""".trimIndent()
"""
}
Primitive.STRING -> """
Primitive.STRING ->
"""
public set ${feature.generatedName}(value: string) {
this.$rawValueName = value;
}
public get ${feature.generatedName}(): string {
return this.$rawValueName ?? "";
}
""".trimIndent()
"""
}
} else {
""
}
"""
$rawPropertyText
$typedPropertyText
""".trimIndent()
"""
}
is ProcessedReferenceLink -> {
val typeRef = feature.type.resolved
Expand All @@ -164,15 +182,15 @@ class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = Na
let target = this._node.getReferenceTargetNode("${feature.originalName}");
return target ? LanguageRegistry.INSTANCE.wrapNode(target) as $entityType : undefined;
}
""".trimIndent()
"""
}
is ProcessedChildLink -> {
val accessorClassName = if (feature.multiple) "ChildListAccessor" else "SingleChildAccessor"
val typeRef = feature.type.resolved
val languagePrefix = typeRef.languagePrefix(concept.language)
"""
public ${feature.generatedName}: $accessorClassName<$languagePrefix${typeRef.nodeWrapperInterfaceName()}> = new $accessorClassName(this._node, "${feature.originalName}")
""".trimIndent()
"""
}
else -> ""
}
Expand All @@ -182,26 +200,26 @@ class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = Na
is ProcessedProperty -> {
val rawPropertyText = """
${feature.rawValueName()}: string | undefined
""".trimIndent()
"""
val typedPropertyText = if (feature.type is PrimitivePropertyType) {
when ((feature.type as PrimitivePropertyType).primitive) {
Primitive.BOOLEAN -> {
"""
${feature.generatedName}: boolean
""".trimIndent()
"""
}
Primitive.INT -> {
"""
${feature.generatedName}: number
""".trimIndent()
"""
}
Primitive.STRING -> {
"""
${feature.generatedName}: string
""".trimIndent()
"""
}
}
} else {
Expand All @@ -210,7 +228,7 @@ class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = Na
"""
$rawPropertyText
$typedPropertyText
""".trimIndent()
"""
}
is ProcessedReferenceLink -> {
val typeRef = feature.type.resolved
Expand All @@ -219,13 +237,13 @@ class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = Na
"""
set ${feature.generatedName}(value: $entityType | undefined);
get ${feature.generatedName}(): $entityType | undefined;
""".trimIndent()
"""
}
is ProcessedChildLink -> {
val accessorClassName = if (feature.multiple) "ChildListAccessor" else "SingleChildAccessor"
"""
${feature.generatedName}: $accessorClassName<${feature.type.resolved.tsInterfaceRef(concept.language)}>
""".trimIndent()
"""
}
else -> ""
}
Expand Down Expand Up @@ -259,7 +277,7 @@ class TypescriptMMGenerator(val outputDir: Path, val nameConfig: NameConfig = Na
${featuresImpl.replaceIndent(" ")}
}
""".trimIndent()
"""
}

private fun ProcessedConcept.nodeWrapperInterfaceName() =
Expand Down

0 comments on commit d29aa82

Please sign in to comment.