Skip to content

Commit

Permalink
Crash blocks are incorrectly indented when nesting (scalameta#553)
Browse files Browse the repository at this point in the history
* Fix scalameta#552: crash blocks are incorrectly indented
  • Loading branch information
keynmol authored and fdietze committed Sep 17, 2021
1 parent c5f14a4 commit 562436e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class CodePrinter(ps: PrintStream, baseIndent: Int = 0, baseNest: Int = 0) {

def append(s: String) = {ps.append(s); this }

def println(s: String) = {ps.print(indent + s + "\n"); this}
def println(s: String) = {
ps.print(indent + s + "\n"); this
}

def definition(header: String)(cb: CodePrinter => Unit): CodePrinter = {
val newCB = new CodePrinter(ps, baseIndent + 1, baseNest)

val newCB = new CodePrinter(ps, baseIndent + 1, nestCount)
this.println(header + " {")
cb(newCB)
this.println("}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class Instrumenter(
}
}

sb.println("\n$doc.endStatement();")
sb.println("\n") // newline for posterity
sb.println("$doc.endStatement();")

case Nil =>
}
Expand Down
8 changes: 6 additions & 2 deletions tests/tests/src/main/scala/tests/BaseSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ class BaseSuite extends FunSuite {
loc: Location
): Unit = {
super.assertNoDiff(
Compat(obtained, Map.empty, postProcessObtained),
Compat(expected, Map.empty, postProcessExpected),
Compat(processOutput(obtained), Map.empty, postProcessObtained),
Compat(processOutput(expected), Map.empty, postProcessExpected),
clue
)
}

private def processOutput(raw: String) =
raw.replaceAll("\t", " ")

object OnlyScala213 extends munit.Tag("OnlyScala213")
object OnlyScala3 extends munit.Tag("OnlyScala3")
object SkipScala3 extends munit.Tag("SkipScala3")
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/src/test/scala/tests/markdown/CrashSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,40 @@ class CrashSuite extends BaseMarkdownSuite {
)
)

check(
"significant-indentation".tag(OnlyScala3),
"""
|```scala mdoc:nest
|println("what!")
|```
|
|```scala mdoc:crash
|def hello(x: Int) =
| if x != 0 then
| println(x)
| x / 0
|hello(5)
|```
|""".stripMargin,
"""
|```scala
|println("what!")
|// what!
|```
|
|```scala
|def hello(x: Int) =
| if x != 0 then
| println(x)
| x / 0
|hello(5)
|// java.lang.ArithmeticException: / by zero
|// at repl.MdocSession$App.hello$1(significant-indentation.md:19)
|// at repl.MdocSession$App.$init$$$anonfun$1(significant-indentation.md:20)
|// at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
|```""".stripMargin
)

check(
"multiple-statements",
"""
Expand Down

0 comments on commit 562436e

Please sign in to comment.