casid / jte

Secure and speedy templates for Java and Kotlin.
https://jte.gg
Apache License 2.0
748 stars 56 forks source link

Keep indent for @template.call #270

Closed hellozyemlya closed 1 year ago

hellozyemlya commented 1 year ago

For example, following template:

            run {
                if(sourceCompound.contains("${prop.propertyName}")) {
                    @template.funcCall(
                        prop.nbtReadFunc,
                        @`sourceCompound`,
                        @`"${prop.propertyName}"`
                    )
                } else {
                    null
                }
            },

resulting in generated content like:

        run {
            if(sourceCompound.contains("stringValue")) {
readStringFrom(
sourceCompound,
"stringValue",
)
            } else {
                null
            }
        },

but expected output is:

        run {
            if(sourceCompound.contains("stringValue")) {
                readStringFrom(
                   sourceCompound,
                   "stringValue",
                )
            } else {
                null
            }
        },
hellozyemlya commented 1 year ago

Hm. Seems like there is no such feature(and I doubt which template engine has such feature).

And I guess it can be tricky to implement. An easy solution I can see, is to extend render method to accept indent, and put this indent after each \r\n(maybe skipping some parts of \r\n like in\r\n\r\n`, but passing this argument requires calculation distance to the last \r\n symbol in currently written output.

One of the tricky scenarios:

helloworld(@template.someTemplate(...))

Make indent for whole someTemplate equals to len("helloworld(")?

Maybe extend @template and make some kind of @templateKeepIndent variant, that will remember own indent and render underlying content with same indent?

casid commented 1 year ago

Hey @hellozyemlya,

thanks for using jte and for reporting this issue.

jte is mainly used to generate HTML pages, where indentation is not as relevant as in this code-generation example. As you already pointed out, this is rather complex to implement. In fact, this would need to be implemented at runtime instead of compile time, since templates can be called from various places.

Maybe another library would be suited better for generating such code files?

hellozyemlya commented 1 year ago

@casid I see, thanks for reply. Maybe as an author of jte, you know a good candidates for template library that can support this?

casid commented 1 year ago

Kotlin Poet might fit your usecase pretty well.