<commit_before>package docs

import metaconfig.Configured
import org.scalafmt.Scalafmt
import org.scalafmt.config.Config
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.config.ScalafmtRunner
import scala.meta.inputs.Input
import scala.meta.inputs.Position
import scala.meta.parsers.ParseException
import mdoc.Reporter
import mdoc.StringModifier

class ScalafmtModifier extends StringModifier {
  override val name: String = "scalafmt"
  override def process(
      info: String,
      code: Input,
      reporter: Reporter
  ): String = {
    val runner =
      if (code.text.contains("package")) ScalafmtRunner.default
      else ScalafmtRunner.sbt
    val base = ScalafmtConfig.default.copy(runner = runner, maxColumn = 40)
    val i = code.text.indexOf("---")
    val pos = Position.Range(code, 0, 0)
    if (i == -1) {
      reporter.error(pos, "Missing ---")
      "fail"
    } else {
      val config = Input.Slice(code, 0, i)
      val program = Input.Slice(code, i + 3, code.chars.length)
      Config.fromHoconString(config.text, None, base) match {
        case Configured.Ok(c) =>
          val parsedConfig = c.copy(runner = runner)
          Scalafmt.format(program.text, parsedConfig).toEither match {
            case Right(formatted) =>
              val configText = config.text.trim
              val configBlock =
                if (configText == "") ""
                else
                  mdConfigSection(
                    "Config for this example",
                    mdCodeBlock("yaml", configText))
              val formattedCodeBlock =
                mdCodeBlock("scala formatted", formatted.trim)
              val originalCodeBlock =
                mdCodeBlock("scala original", program.text.trim)
              List(
                formattedCodeBlock,
                originalCodeBlock,
                configBlock
              ).mkString("\n")
            case Left(e: ParseException) =>
              reporter.error(pos, e.toString())
              "parse error"
            case Left(e) =>
              reporter.error(pos, e)
              "fail"
          }
        case Configured.NotOk(e) =>
          reporter.error(pos, e.toString())
          "fail"
      }
    }
  }

  private def mdCodeBlock(language: String, content: String): String =
    s"```$language\n$content\n```"

  private def mdConfigSection(title: String, content: String): String =
    s"<details class='config' open><summary>$title</summary><p>\n$content\n</p></details>\n"

}<commit_msg>Config blocks render better with scala syntax<commit_after>package docs

import metaconfig.Configured
import org.scalafmt.Scalafmt
import org.scalafmt.config.Config
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.config.ScalafmtRunner
import scala.meta.inputs.Input
import scala.meta.inputs.Position
import scala.meta.parsers.ParseException
import mdoc.Reporter
import mdoc.StringModifier

class ScalafmtModifier extends StringModifier {
  override val name: String = "scalafmt"
  override def process(
      info: String,
      code: Input,
      reporter: Reporter
  ): String = {
    val runner =
      if (code.text.contains("package")) ScalafmtRunner.default
      else ScalafmtRunner.sbt
    val base = ScalafmtConfig.default.copy(runner = runner, maxColumn = 40)
    val i = code.text.indexOf("---")
    val pos = Position.Range(code, 0, 0)
    if (i == -1) {
      reporter.error(pos, "Missing ---")
      "fail"
    } else {
      val config = Input.Slice(code, 0, i)
      val program = Input.Slice(code, i + 3, code.chars.length)
      Config.fromHoconString(config.text, None, base) match {
        case Configured.Ok(c) =>
          val parsedConfig = c.copy(runner = runner)
          Scalafmt.format(program.text, parsedConfig).toEither match {
            case Right(formatted) =>
              val configText = config.text.trim
              val configBlock =
                if (configText == "") ""
                else
                  mdConfigSection(
                    "Config for this example",
                    mdCodeBlock("scala", configText))

              val formattedCodeBlock =
                mdCodeBlock("scala formatted", formatted.trim)
              val originalCodeBlock =
                mdCodeBlock("scala original", program.text.trim)
              List(
                formattedCodeBlock,
                originalCodeBlock,
                configBlock
              ).mkString("\n")
            case Left(e: ParseException) =>
              reporter.error(pos, e.toString())
              "parse error"
            case Left(e) =>
              reporter.error(pos, e)
              "fail"
          }
        case Configured.NotOk(e) =>
          reporter.error(pos, e.toString())
          "fail"
      }
    }
  }

  private def mdCodeBlock(language: String, content: String): String =
    s"```$language\n$content\n```"

  private def mdConfigSection(title: String, content: String): String =
    s"<details class='config' open><summary>$title</summary><p>\n$content\n</p></details>\n"

}
{'fraction_non_alphanumeric': 0.11001577287066246, 'fraction_numerical': 0.0027602523659305996, 'mean_word_length': 2.1392326732673266, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2400244', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1414, 'n_words': 326}