Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Add ability to insert comment inside class scope #887

Closed
wants to merge 1 commit into from

Conversation

Scottmitch
Copy link

Motivation:
When generating code it maybe useful to insert
comments as anchors for post processing and further
extension by other code generation tools. However
this is not easy to achieve with the existing API.

Modifications:

  • Add a TypeSpec.addRawInitializerBlock method which
    is treated the same as addInitializerBlock without
    type restrictions and additional scope block.

Motivation:
When generating code it maybe useful to insert
comments as anchors for post processing and further
extension by other code generation tools. However
this is not easy to achieve with the existing API.

Modifications:
- Add a `TypeSpec.addRawInitializerBlock` method which
  is treated the same as `addInitializerBlock` without
  type restrictions and additional scope block.
@Scottmitch
Copy link
Author

Scottmitch commented Apr 29, 2022

apple/servicetalk#2160 -> to work around not being able to insert comments outside pre-defined scopes we have to do regex on the resulting generated code string. please advise if there is a more preferred way todo this, and if not this PR provides a way. There is somewhat of an escape hatch but the users of javapoet can already generate invalid code in other scenarios so I think something similar would be reasonable.

@@ -626,6 +626,11 @@ public Builder addInitializerBlock(CodeBlock block) {
return this;
}

public Builder addRawInitializerBlock(CodeBlock block) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

happy to change the name if maintainers prefer (e.g. addRawCodeBlock as suggested by @bondolo)

@Scottmitch
Copy link
Author

Scottmitch commented May 26, 2022

any thoughts/feedback from maintainers on this (@sullis, @dsinghvi)?

@Scottmitch
Copy link
Author

closing in favor of #967 (more constrained approach)

@Scottmitch Scottmitch closed this May 7, 2023
@Scottmitch Scottmitch deleted the raw_init branch August 7, 2023 15:00
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant