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

Comments outside of code block #1862

Open
adrianimboden opened this issue Mar 12, 2024 · 1 comment
Open

Comments outside of code block #1862

adrianimboden opened this issue Mar 12, 2024 · 1 comment

Comments

@adrianimboden
Copy link

Is your feature request related to a problem? Please describe.
IntelliJ (and probably other editors) support editor-fold comments to collapse stuff that probably is not so interesting to look at to get an overview: https://www.jetbrains.com/guide/java/tips/editor-fold/

In our code, we have generated data classes where when we go into the generated code, we are only really interested in the primary constructor parameter list, to see whats inside the dataclass. All the other stuff (annotations, additional constructors, etc. are not really interesting to look at when everything runs smoothly).

In our case, we have the primary constructor private to prevent code to use non-named parameter constructor calls.

Example:

//<editor-fold desc="not so interesting to look at, please focus on the members">
@JsonSerialize(...)
@JsonDeserialize(...)
@ApiModel
...
//</editor-fold>
public data class MyGeneratedDataStructure private constructor(
  public val member1: Boolean,
  public val member2: Boolean,
  public val member3: Boolean,
  public val member4: String,
  public val member5: OtherGeneratedStructure,
  //...
) {
//<editor-fold desc="not so interesting to look at, please focus on the members">
  public constructor(
    vararg nothing: PleaseDoNotUseNonNamedParameters,
    ... dozens of parameters
  ) : this(
   ... dozens of parameters
) {
  ... hundreds of lines of parameters
  }
//</editor-fold>
}

public data class OtherGeneratedStructure ...

This would collapse in the IDE like this:
image

constructors cannot be extension methods, otherwise I would move the clutter into an extension method to keep the data classes as clean as possible. Also annotations can be quite extensive and they cannot be moved somewhere else, as far as I know

Describe the solution you'd like
Something like this would be nice:

FileSpec
 .builder("com.example.model", "models")
 .addType(
  TypeSpec
   .classBuilder("MyGeneratedDataStructure")
   .addAnnotation(AnnotationSpec
    .builder(...)
    .addToFoldGroup("not so interesting to look at, please focus on the members")
    .build()
   )
   .primaryConstructor(
    FunSpec
     .constructorBuilder()
     .addCode(...)
     .addToFoldGroup("not so interesting to look at, please focus on the members")
     .build()
   )
   .build(),
 )
 .build()

Describe alternatives you've considered

  • Adding the comments as a second step outside of kotlinpoet. This would be rather cumbersome tough
  • moving the stuff out into a separate file (this works for all the other stuff except constructors and annotations)
@JakeWharton
Copy link
Member

This came up when we were contributing to SwiftPoet, too.

Part of the problem is that we reserve the right to re-arrange the order of members added to a type. Additionally, we might collapse a member into a primary constructor, such as when it forms a primary constructor property. I don't have a good way to reconcile this behavior with regions.

For non-collapsing members we could group by region and then perform our ordering. But that also might change the initialization order of default values and break compilation.

I don't see a clear path to supporting this, unfortunately.

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