ktfmt icon indicating copy to clipboard operation
ktfmt copied to clipboard

KtFmt infers with KotlinX Knit

Open nomisRev opened this issue 4 years ago • 5 comments
trafficstars

When using KtFmt (Google style) together with KotlinX Knit tool it reports incorrectly formatted code for markers for Knit inside KDocs.

Execution failed for task ':spotlessKotlinCheck'.
> The following files had format violations:
      src/main/kotlin/com/github/nomisRev/kafka/AdminSettings.kt
          @@ -54,8 +54,7 @@
           ·*···}
           ·*·}
           ·*·```
          -·*·<!---·KNIT·example-admin-01.kt·-->
          -·*·<!---·TEST·lines.isNotEmpty()·-->
          +·*·<!---·KNIT·example-admin-01.kt·-->·<!---·TEST·lines.isNotEmpty()·-->
           ·*/
           public·fun·Admin(settings:·AdminSettings):·Admin·=·Admin.create(settings.properties())
           
  Run './gradlew :spotlessApply' to fix these violations.

Using KtFmt 0.30.0 through Spotless Gradle plugin.

nomisRev avatar Nov 22 '21 17:11 nomisRev

Thanks for the report. Does Knit require these two <!--- --> to be in separate lines?

cgrushko avatar Jun 09 '22 09:06 cgrushko

@cgrushko yes

Directives in markdown files must always start at the beginning of the line and have the following general format for single-line directives:

<!--- <directive> [<parameters>] -->

or the following format for multi-line directives:

<!--- <directive> [<parameters>] 
<text> 
-->

JavierSegoviaCordoba avatar Jun 13 '22 11:06 JavierSegoviaCordoba

Is there any plan to address this?

What about an option to disable formatting comments/docs?

JavierSegoviaCordoba avatar Sep 28 '22 12:09 JavierSegoviaCordoba

We're planning to integrate a new kdoc formatting engine: https://github.com/facebook/ktfmt/issues/339 IIRC we checked that it solves this issue specifically.

cgrushko avatar Sep 28 '22 12:09 cgrushko

@cgrushko I think there was some regression here. When currently running KtFmt over my project it completely changes how my KDoc is formatted breaking Knit support completely.

I.e.

 * <!--- INCLUDE
 * import arrow.core.raise.ensure
 * import arrow.exact.Exact
 * import arrow.exact.ExactError
 * import arrow.exact.exact

becoming

 * <!--- INCLUDE
 * import arrow.core.raise.ensure import arrow.exact.Exact import arrow.exact.ExactEither import
 * arrow.exact.ExactError import arrow.exact.exact import arrow.exact.exactEither

Also @see seems to be moved inside of a ``kotlin code-fence. Sadly this prevents us from using KtFmt on all my OSS projects again 😞

nomisRev avatar May 09 '23 13:05 nomisRev

v0.47 formats like this

/*
<!---·KNIT·example-admin-01.kt·-->
<!---·TEST·lines.isNotEmpty()·-->

 * <!--- INCLUDE
 * import arrow.core.raise.ensure
 * import arrow.exact.Exact
 * import arrow.exact.ExactError
 * import arrow.exact.exact
 * -->
*/

Looks fixed to me, but I don't know much about Knit so feel free to reopen if I'm missing something

hick209 avatar Jun 05 '24 18:06 hick209