From 923249602190f3fc19f5a4b81b735ade8b1536ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93lafur=20P=C3=A1ll=20Geirsson?= Date: Sun, 9 Sep 2018 14:58:07 +0200 Subject: [PATCH] Preseve original formatting including trailing statement comments --- .../mdoc/internal/markdown/Renderer.scala | 5 ++++- readme.md | 6 ++++-- .../main/scala/mdoc/document/Document.scala | 6 +++++- .../internal/document/DocumentBuilder.scala | 5 +++-- .../src/test/scala/tests/cli/CliSuite.scala | 19 +++++++++++++++++++ .../markdown/MarkdownCompilerSuite.scala | 12 +++++++++++- .../tests/markdown/VariableRegexSuite.scala | 2 +- 7 files changed, 47 insertions(+), 8 deletions(-) diff --git a/mdoc/src/main/scala/mdoc/internal/markdown/Renderer.scala b/mdoc/src/main/scala/mdoc/internal/markdown/Renderer.scala index 2bd4ead9d..aad26046a 100644 --- a/mdoc/src/main/scala/mdoc/internal/markdown/Renderer.scala +++ b/mdoc/src/main/scala/mdoc/internal/markdown/Renderer.scala @@ -99,7 +99,10 @@ object Renderer { } else { sb.append("\n") } - sb.append(tree.syntax) + val pos = tree.pos + val endOfLinePosition = + Position.Range(pos.input, pos.startLine, pos.startColumn, pos.endLine, Int.MaxValue) + sb.append(endOfLinePosition.text) if (statement.out.nonEmpty) { sb.append("\n") appendFreshMultiline(sb, statement.out) diff --git a/readme.md b/readme.md index fc144b742..2fd203142 100644 --- a/readme.md +++ b/readme.md @@ -376,7 +376,7 @@ You can choose the Scastie theme when initializing the Scastie modifier: ```scala import mdoc.modifiers.ScastieModifier -new ScastieModifier(theme = "dark") +new ScastieModifier(theme = "dark") // default is "light" // res0: ScastieModifier = StringModifier(mdoc:scastie) ``` @@ -521,7 +521,9 @@ After: ```scala case class User(name: String) -object User { implicit val ordering: Ordering[User] = Ordering.by(_.name) } +object User { + implicit val ordering: Ordering[User] = Ordering.by(_.name) +} List(User("John"), User("Susan")).sorted // res0: List[User] = List(User("John"), User("Susan")) diff --git a/runtime/src/main/scala/mdoc/document/Document.scala b/runtime/src/main/scala/mdoc/document/Document.scala index a41e64b4d..79964731b 100644 --- a/runtime/src/main/scala/mdoc/document/Document.scala +++ b/runtime/src/main/scala/mdoc/document/Document.scala @@ -19,7 +19,11 @@ object Document { final case class Section(statements: List[Statement]) -final case class Statement(binders: List[Binder[_]], out: String) +final case class Statement( + binders: List[Binder[_]], + out: String, + position: RangePosition +) final class Binder[T](val value: T, val name: String, val tpe: TPrint[T], pos: RangePosition) { override def toString: String = { diff --git a/runtime/src/main/scala/mdoc/internal/document/DocumentBuilder.scala b/runtime/src/main/scala/mdoc/internal/document/DocumentBuilder.scala index 47ec85a6d..2df1b9a1d 100644 --- a/runtime/src/main/scala/mdoc/internal/document/DocumentBuilder.scala +++ b/runtime/src/main/scala/mdoc/internal/document/DocumentBuilder.scala @@ -18,6 +18,7 @@ trait DocumentBuilder { private val mySections = ArrayBuffer.empty[Section] private val myOut = new ByteArrayOutputStream() private val myPs = new PrintStream(myOut) + private var statementPosition = RangePosition.empty private var lastPosition = RangePosition.empty object $doc { @@ -37,13 +38,13 @@ trait DocumentBuilder { } def startStatement(startLine: Int, startColumn: Int, endLine: Int, endColumn: Int): Unit = { - position(startLine, startColumn, endLine, endColumn) + statementPosition = position(startLine, startColumn, endLine, endColumn) myBinders.clear() myOut.reset() } def endStatement(): Unit = { val out = myOut.toString() - myStatements.append(Statement(myBinders.toList, out)) + myStatements.append(Statement(myBinders.toList, out, statementPosition)) } def startSection(): Unit = { diff --git a/tests/unit/src/test/scala/tests/cli/CliSuite.scala b/tests/unit/src/test/scala/tests/cli/CliSuite.scala index af0ad80fc..38f25eb51 100644 --- a/tests/unit/src/test/scala/tests/cli/CliSuite.scala +++ b/tests/unit/src/test/scala/tests/cli/CliSuite.scala @@ -5,6 +5,25 @@ import mdoc.internal.BuildInfo class CliSuite extends BaseCliSuite { + checkCli( + "formatting", + """ + |/index.md + |```scala mdoc + |List(1, + | 2, 3, 4) // comment + |``` + """.stripMargin, + """ + |/index.md + |```scala + |List(1, + | 2, 3, 4) // comment + |// res0: List[Int] = List(1, 2, 3, 4) + |``` + """.stripMargin + ) + checkCli( "classpath", """ diff --git a/tests/unit/src/test/scala/tests/markdown/MarkdownCompilerSuite.scala b/tests/unit/src/test/scala/tests/markdown/MarkdownCompilerSuite.scala index b9194d58b..a9c15a570 100644 --- a/tests/unit/src/test/scala/tests/markdown/MarkdownCompilerSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/MarkdownCompilerSuite.scala @@ -75,7 +75,7 @@ class MarkdownCompilerSuite extends FunSuite with DiffAssertions { check( "non-val", - """println("hello world!") """, + """println("hello world!")""", """ |```scala |println("hello world!") @@ -84,4 +84,14 @@ class MarkdownCompilerSuite extends FunSuite with DiffAssertions { |""".stripMargin ) + check( + "comment1", + """val x = 2 // comment""", + """ + |```scala + |val x = 2 // comment + |// x: Int = 2 + |``` + |""".stripMargin + ) } diff --git a/tests/unit/src/test/scala/tests/markdown/VariableRegexSuite.scala b/tests/unit/src/test/scala/tests/markdown/VariableRegexSuite.scala index 0c816bc93..0301d9776 100644 --- a/tests/unit/src/test/scala/tests/markdown/VariableRegexSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/VariableRegexSuite.scala @@ -43,7 +43,7 @@ class VariableRegexSuite extends BaseMarkdownSuite { |val x = "1.0" |// x: String = "1.0" | - |x.length + |x.length // should match "1.0".length |// res0: Int = 3 |``` """.stripMargin,