toolisticon/kotlin-code-generation

Control line breaks/line length

Closed this issue · 2 comments

Scenario

Example: GeneratedAnnotation ... it is implemented as single line output, but depending on the values (name, comments), the generated code breaks.

Wanted Behaviour

Control line breaks ... if the generated ocde is implemented as a single line, keep it single lined.

Possible Workarounds

  • kotlin poet documents how to use a "dot" instead of a "space" to force non breaking lines ... but it might be easier if we could just configure the max line length, see kotlin poet discussions

Ok, kotlin poet does not provide this, but using an additional formatter might help, such as ktfmt or klint.

In fact ktfmt is quite simple to use: Formatter.format(Formatter.KOTLINLANG_FORMAT.copy(maxWidth = 240, blockIndent = 2, continuationIndent = 2), spec.code )

<dependency>
      <groupId>com.facebook</groupId>
      <artifactId>ktfmt</artifactId>
      <version>0.52</version>
    </dependency>

Note: probably best solved in the lib that uses this generator, otherwise we would have to deal with dependencies and flavors (ktfmt, klint, ....)