Skip to content

Commit

Permalink
Respect nest modifier in fail instrumenter. (#306)
Browse files Browse the repository at this point in the history
Alternative approach to #305.

Previously, the fail and nest modifiers didn't play nicely together.
Now, the fail instrumenter respects nest modifiers so that the two
modifiers can be used together in the same document.
  • Loading branch information
olafurpg authored Mar 16, 2020
1 parent dd729b8 commit 8156763
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ final class FailInstrumenter(sections: List[SectionInput], i: Int) {
private val out = new ByteArrayOutputStream()
private val sb = new PrintStream(out)
private val gensym = new Gensym()
private val nest = new Nesting(sb)
def instrument(): String = {
printAsScript()
out.toString
Expand All @@ -20,13 +21,17 @@ final class FailInstrumenter(sections: List[SectionInput], i: Int) {
if (j > i) ()
else {
if (section.mod.isReset) {
nest.unnest()
sb.print(Instrumenter.reset(section.mod, gensym.fresh("App")))
} else if (section.mod.isNest) {
nest.nest()
}
if (j == i || !section.mod.isFail) {
sb.println(section.input.text)
}
}
}
sb.println("\n }\n}")
nest.unnest()
}
}
18 changes: 4 additions & 14 deletions mdoc/src/main/scala/mdoc/internal/markdown/Instrumenter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,15 @@ class Instrumenter(sections: List[SectionInput]) {
private val out = new ByteArrayOutputStream()
private val sb = new PrintStream(out)
val gensym = new Gensym()
private var nestCount = 0
private def nest(): Unit = {
nestCount += 1
sb.append(s"_root_.scala.Predef.locally {\n")
}
private def unnest(): Unit = {
1.to(nestCount).foreach { _ =>
sb.print("};")
}
nestCount = 0
}
val nest = new Nesting(sb)
private def printAsScript(): Unit = {
sections.zipWithIndex.foreach {
case (section, i) =>
if (section.mod.isReset) {
unnest()
nest.unnest()
sb.print(Instrumenter.reset(section.mod, gensym.fresh("App")))
} else if (section.mod.isNest) {
nest()
nest.nest()
}
sb.println("\n$doc.startSection();")
if (section.mod.isFail) {
Expand Down Expand Up @@ -68,7 +58,7 @@ class Instrumenter(sections: List[SectionInput]) {
}
sb.println("$doc.endSection();")
}
unnest()
nest.unnest()
}

private def printBinder(name: String, pos: Position): Unit = {
Expand Down
17 changes: 17 additions & 0 deletions mdoc/src/main/scala/mdoc/internal/markdown/Nesting.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package mdoc.internal.markdown

import java.io.PrintStream

class Nesting(sb: PrintStream) {
private var nestCount = 0
def nest(): Unit = {
nestCount += 1
sb.append(s"_root_.scala.Predef.locally {\n")
}
def unnest(): Unit = {
1.to(nestCount).foreach { _ =>
sb.print("};")
}
nestCount = 0
}
}
25 changes: 25 additions & 0 deletions tests/unit/src/test/scala/tests/markdown/NestSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,29 @@ class NestSuite extends BaseMarkdownSuite {
|""".stripMargin
)

check(
"fail",
"""
|```scala mdoc
|case class Foo(i: Int)
|```
|
|```scala mdoc:nest:fail
|case class Foo(i: Int) { val x = y }
|```
""".stripMargin,
"""|
|```scala
|case class Foo(i: Int)
|```
|
|```scala
|case class Foo(i: Int) { val x = y }
|// error: not found: value y
|// case class Foo(i: Int) { val x = y }
|// ^
|```
""".stripMargin
)

}

0 comments on commit 8156763

Please sign in to comment.