tieskedh / KotlinPoetDSL

A DSL for KotlinPoet
https://kotlinpoetdsl.devhaan.nl
MIT License
27 stars 0 forks source link

Consider adding in-place formatted code blocks #55

Open vlsi opened 4 years ago

vlsi commented 4 years ago

See https://github.com/square/javapoet/issues/761#issuecomment-581057940

For instance:

val hello = """Hello, "world", \n test"""
val j = FieldSpec.builder(Int::class.typeName, "j").build()

val codeBlock = cb { "println(${hello.S} + ${j.N})" }

println(codeBlock.toString())

==>

println("Hello, \"world\", \\n test" + j)
tieskedh commented 4 years ago

If I'm right, this code is about adding extension-properties to Strings and Specs, such that it will be easier to compose the codeblock. If this is the case, I think this it is more useful if you would add it to KotlinPoet. When this is added, it automatically works in KotlinPoetDSL.

vlsi commented 4 years ago

Ok. Let me try that.

However, I think it won't be added to KotlinPoet judging by https://github.com/square/kotlinpoet/pull/496#issuecomment-441340885

vlsi commented 4 years ago

As you can see, https://github.com/square/kotlinpoet/issues/877 was closed, so WDYT regarding extended blocks in KotlinPoetDSL?

tieskedh commented 4 years ago

I personally would have something more reflection-like... Don't know how, but something more like the following:

val j = "j" valOf Int::class
val hello = strVal("""Hello, "world", \n test""")
cb {
     addLazy(::println, hello+j)
}

fun <R> CodeBlock.addLazy(func : KFunction1<String, R>, stringValue : StringValue){
    add(func.name+"("+stringValue+")")
}

But I have to investigate it more...