Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding in-place formatted code blocks #55

Open
vlsi opened this issue Feb 2, 2020 · 4 comments
Open

Consider adding in-place formatted code blocks #55

vlsi opened this issue Feb 2, 2020 · 4 comments

Comments

@vlsi
Copy link

vlsi commented Feb 2, 2020

See square/javapoet#761 (comment)

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
Copy link
Owner

tieskedh commented Feb 4, 2020

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
Copy link
Author

vlsi commented Feb 4, 2020

Ok. Let me try that.

However, I think it won't be added to KotlinPoet judging by square/kotlinpoet#496 (comment)

@vlsi
Copy link
Author

vlsi commented Feb 9, 2020

As you can see, square/kotlinpoet#877 was closed, so WDYT regarding extended blocks in KotlinPoetDSL?

@tieskedh
Copy link
Owner

tieskedh commented Feb 9, 2020

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants