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

CodeBlock.builder() is missing addComment() method #1690

Open
HoffiMuc opened this issue Sep 6, 2023 · 3 comments
Open

CodeBlock.builder() is missing addComment() method #1690

HoffiMuc opened this issue Sep 6, 2023 · 3 comments

Comments

@HoffiMuc
Copy link

HoffiMuc commented Sep 6, 2023

Why does CodeBlock.builder() not have addComment() method like e.g. FunSpec.builder() ?

@Egorand
Copy link
Collaborator

Egorand commented Sep 6, 2023

What would be a use-case for it? A comment is a CodeBlock.

@HoffiMuc
Copy link
Author

HoffiMuc commented Sep 6, 2023

I am forced to do (as I am in a sub-function that only has a CodeBlock.builder() instance and not its FunSpec.builder()

codeBlockBuilder.addStatement("// not yet implemented %L PoetType of %T", prop.name(), prop.poetType)

as there just is missing a addComment(...) method on CodeBlock.builder()

codeBlockBuilder.addComment("not yet implemented %L PoetType of %T", prop.name(), prop.poetType)
//              | missing  |

but with addStatement("// ...")
if the comment-line is too long and a line break happens, the generated code obviously doesn't compile anymore.

@Egorand
Copy link
Collaborator

Egorand commented Sep 6, 2023

Yeah, I think that makes sense. I originally felt like "comment" is a concept on a higher level than "CodeBlock", and there are many things we represent with CodeBlocks which don't support comments, but that said, we do have API like beginControlFlow(), which definitely can't be used everywhere a CodeBlock can. Do you want to send a PR?

Meanwhile you can copy what FunSpec.Builder.addComment() does to ensure there are no line breaks inside of a comment:

public fun addComment(format: String, vararg args: Any): Builder = apply {
  body.add("//·${format.replace(' ', '·')}\n", *args)
}

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

No branches or pull requests

2 participants