Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preseve original formatting including trailing statement comments #70

Merged
merged 1 commit into from
Sep 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion mdoc/src/main/scala/mdoc/internal/markdown/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

Expand Down Expand Up @@ -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"))
Expand Down
6 changes: 5 additions & 1 deletion runtime/src/main/scala/mdoc/document/Document.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 = {
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/src/test/scala/tests/cli/CliSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class MarkdownCompilerSuite extends FunSuite with DiffAssertions {

check(
"non-val",
"""println("hello world!") """,
"""println("hello world!")""",
"""
|```scala
|println("hello world!")
Expand All @@ -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
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down