diff --git a/cli/src/main/scala/mdoc/internal/cli/CliEnrichments.scala b/cli/src/main/scala/mdoc/internal/cli/CliEnrichments.scala index ce0e91f2..7229dfab 100644 --- a/cli/src/main/scala/mdoc/internal/cli/CliEnrichments.scala +++ b/cli/src/main/scala/mdoc/internal/cli/CliEnrichments.scala @@ -8,7 +8,6 @@ import scala.meta.Input import scala.meta.Position import scala.meta.io.AbsolutePath import scala.meta.io.RelativePath -import mdoc.internal.cli.Settings import scala.meta.internal.io.PathIO import scala.util.control.NonFatal import coursierapi.Dependency diff --git a/mdoc-interfaces/src/main/scala/mdoc/interfaces/Mdoc.java b/mdoc-interfaces/src/main/scala/mdoc/interfaces/Mdoc.java index f8b72abb..4a941963 100644 --- a/mdoc-interfaces/src/main/scala/mdoc/interfaces/Mdoc.java +++ b/mdoc-interfaces/src/main/scala/mdoc/interfaces/Mdoc.java @@ -7,6 +7,7 @@ public abstract class Mdoc { public abstract EvaluatedWorksheet evaluateWorksheet(String filename, String text); + public abstract EvaluatedWorksheet evaluateWorksheet(String filename, String text, String modifier); public abstract Mdoc withWorkingDirectory(Path workingDirectory); public abstract Mdoc withClasspath(List classpath); public abstract Mdoc withScalacOptions(List options); diff --git a/mdoc/src/main/scala/mdoc/internal/worksheets/Mdoc.scala b/mdoc/src/main/scala/mdoc/internal/worksheets/Mdoc.scala index 46c67a22..f87cf228 100644 --- a/mdoc/src/main/scala/mdoc/internal/worksheets/Mdoc.scala +++ b/mdoc/src/main/scala/mdoc/internal/worksheets/Mdoc.scala @@ -1,20 +1,22 @@ package mdoc.internal.worksheets -import java.{util => ju} -import java.io.File -import java.io.PrintStream -import java.nio.file.Path -import mdoc.internal.pos.PositionSyntax._ -import mdoc.{interfaces => i} +import coursierapi.Logger +import mdoc.MainSettings import mdoc.internal.cli.Context import mdoc.internal.cli.Settings -import scala.meta.internal.io.PathIO import mdoc.internal.io.ConsoleReporter import mdoc.internal.markdown.MarkdownCompiler -import scala.meta.inputs.Input +import mdoc.internal.markdown.Modifier +import mdoc.internal.pos.PositionSyntax._ import mdoc.internal.worksheets.Compat._ -import mdoc.MainSettings -import coursierapi.Logger +import mdoc.{interfaces => i} + +import java.io.File +import java.io.PrintStream +import java.nio.file.Path +import java.{util => ju} +import scala.meta.inputs.Input +import scala.meta.internal.io.PathIO class Mdoc(settings: MainSettings) extends i.Mdoc { @@ -46,10 +48,19 @@ class Mdoc(settings: MainSettings) extends i.Mdoc { } } - def evaluateWorksheet(filename: String, text: String): EvaluatedWorksheet = + def evaluateWorksheet(filename: String, text: String): i.EvaluatedWorksheet = { + new WorksheetProvider(settings.settings).evaluateWorksheet( + Input.VirtualFile(filename, text), + context(), + modifier = None + ) + } + + def evaluateWorksheet(filename: String, text: String, modifier: String): i.EvaluatedWorksheet = new WorksheetProvider(settings.settings).evaluateWorksheet( Input.VirtualFile(filename, text), - context() + context(), + Modifier(modifier) ) private def context(): Context = { diff --git a/mdoc/src/main/scala/mdoc/internal/worksheets/WorksheetProvider.scala b/mdoc/src/main/scala/mdoc/internal/worksheets/WorksheetProvider.scala index 5f366314..5de007bb 100644 --- a/mdoc/src/main/scala/mdoc/internal/worksheets/WorksheetProvider.scala +++ b/mdoc/src/main/scala/mdoc/internal/worksheets/WorksheetProvider.scala @@ -26,9 +26,10 @@ class WorksheetProvider(settings: Settings) { def evaluateWorksheet( input: Input.VirtualFile, - ctx: Context + ctx: Context, + modifier: Option[Modifier] ): EvaluatedWorksheet = { - val sectionInput = SectionInput(input, ctx) + val sectionInput = SectionInput(input, modifier.getOrElse(Modifier.Default()), ctx) val sectionInputs = List(sectionInput) val file = InputFile.fromRelativeFilename(input.path, settings) val instrumented = Instrumenter.instrument(file, sectionInputs, settings, reporter) diff --git a/tests/worksheets/src/test/scala/tests/worksheets/WorksheetSuite.scala b/tests/worksheets/src/test/scala/tests/worksheets/WorksheetSuite.scala index 43d9a24e..8289dbb5 100644 --- a/tests/worksheets/src/test/scala/tests/worksheets/WorksheetSuite.scala +++ b/tests/worksheets/src/test/scala/tests/worksheets/WorksheetSuite.scala @@ -51,6 +51,27 @@ class WorksheetSuite extends BaseSuite { |""".stripMargin ) + checkDiagnostics( + "value-class", + """|object Foo { + | case class Bar(b: Int) extends AnyVal + |}""".stripMargin, + "", + modifier = Some("reset-object") + ) + + checkDecorations( + "multi-mods", + """|object Foo { + | case class Bar(b: Int) extends AnyVal + |} + | + |Foo.Bar(1) + |""".stripMargin, + "", + modifier = Some("reset-object:compile-only") + ) + checkDecorations( "lazy", """ @@ -404,11 +425,12 @@ class WorksheetSuite extends BaseSuite { options: TestOptions, original: String, expected: String, - compat: Map[String, String] = Map.empty + compat: Map[String, String] = Map.empty, + modifier: Option[String] = None ): Unit = { test(options) { val filename = options.name + ".scala" - val worksheet = mdoc.evaluateWorksheet(filename, original) + val worksheet = evaluateWorksheet(filename, original, modifier) val input = Input.VirtualFile(options.name, original) val out = new StringBuilder() var i = 0 @@ -435,11 +457,12 @@ class WorksheetSuite extends BaseSuite { options: TestOptions, original: String, expected: String, - compat: Map[String, String] = Map.empty + compat: Map[String, String] = Map.empty, + modifier: Option[String] = None ): Unit = { test(options) { val filename = options.name + ".scala" - val worksheet = mdoc.evaluateWorksheet(filename, original) + val worksheet = evaluateWorksheet(filename, original, modifier) val statements = worksheet.statements().asScala.sortBy(_.position().startLine()) val input = Input.VirtualFile(options.name, original) val out = new StringBuilder() @@ -468,4 +491,13 @@ class WorksheetSuite extends BaseSuite { assertNoDiff(obtained, Compat(expected, compat)) } } + + private def evaluateWorksheet(filename: String, original: String, modifier: Option[String]) = { + modifier match { + case Some(mod) => + mdoc.evaluateWorksheet(filename, original, mod) + case None => + mdoc.evaluateWorksheet(filename, original) + } + } }