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

Width and height override modifiers #389

Merged
merged 12 commits into from
Oct 17, 2020
30 changes: 30 additions & 0 deletions docs/modifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,36 @@ List("with quotes")
```
````

## `width=`

The `width=` allows you to override max width (deault is 80) of pretty-printed values.


````scala mdoc:mdoc
```scala mdoc:width=20
List.fill(2)(List(1,2,3,4,5))
```
```scala mdoc
List.fill(2)(List(1,2,3,4,5))
```
````


## `height=`

The `height=` allows you to override max height (default is 50) of pretty-printed values.

````scala mdoc:mdoc
```scala mdoc:height=5
List.fill(15)("hello world!")
```
```scala mdoc
List.fill(15)("hello world!")
```
````



## `compile-only`

The `compile-only` modifier ensures the code example compiles without evaluating
Expand Down
10 changes: 10 additions & 0 deletions mdoc/src/main/scala-2/mdoc/internal/markdown/Modifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ sealed abstract class Modifier(val mods: Set[Mod]) {
def isResetObject: Boolean = mods(ResetObject)
def isNest: Boolean = mods(Nest)

def widthOverride: Option[Int] =
mods.collectFirst { case Width(value) =>
value
}

def heightOverride: Option[Int] =
mods.collectFirst { case Height(value) =>
value
}

def isToString: Boolean = mods(ToString)
}
object Modifier {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ object Renderer {
def appendMultiline(sb: PrintStream, string: String): Unit = {
appendMultiline(sb, string, string.length)
}

def appendMultiline(sb: PrintStream, string: String, N: Int): Unit = {
var i = 0
while (i < N) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ class ReplVariablePrinter(
if (binder.isToString) {
Renderer.appendMultiline(sb, binder.runtimeValue.toString)
} else {
val heightOverride = binder.mods.heightOverride
val widthOverride = binder.mods.widthOverride

val lines = pprint.PPrinter.BlackWhite.tokenize(
binder.runtimeValue,
width = width,
height = height,
width = widthOverride.getOrElse(width),
height = heightOverride.getOrElse(height),
indent = 2,
initialOffset = baos.size()
)
Expand Down
28 changes: 26 additions & 2 deletions mdoc/src/main/scala/mdoc/internal/markdown/Mod.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mdoc.internal.markdown

import scala.util.Try

sealed abstract class Mod extends Product with Serializable
object Mod {
case object Fail extends Mod
Expand All @@ -23,7 +25,15 @@ object Mod {
}
case object Nest extends Mod

def all: List[Mod] =
case class Width(value: Int) extends Mod {
override def toString: String = s"width=$value"
}

case class Height(value: Int) extends Mod {
override def toString: String = s"height=$value"
}

def static: List[Mod] =
List(
Passthrough,
Invisible,
Expand All @@ -38,7 +48,21 @@ object Mod {
ToString,
Nest
)

val parametric: PartialFunction[String, Mod] = {
val ToWidth = "width=(\\d+)".r
val ToHeight = "height=(\\d+)".r
object ToInt {
def unapply(s: String): Option[Int] = Try(s.toInt).toOption
}

{
case ToWidth(ToInt(n)) => Width(n)
case ToHeight(ToInt(n)) => Height(n)
}
}

def unapply(string: String): Option[Mod] = {
all.find(_.toString.equalsIgnoreCase(string))
static.find(_.toString.equalsIgnoreCase(string)) orElse parametric.lift(string)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package tests.markdown

class WidthHeightModifierSuite extends BaseMarkdownSuite {

check(
"no-width-override",
"""
|```scala mdoc
|List.fill(2)(List(1,2,3,4,5))
|```
""".stripMargin,
"""
|```scala
|List.fill(2)(List(1,2,3,4,5))
|// res0: List[List[Int]] = List(List(1, 2, 3, 4, 5), List(1, 2, 3, 4, 5))
|```
""".stripMargin
)

check(
"width-override",
"""
|```scala mdoc:width=20
|List.fill(2)(List(1,2,3,4,5))
|```
""".stripMargin,
"""
|```scala
|List.fill(2)(List(1,2,3,4,5))
|// res0: List[List[Int]] = List(
|// List(
|// 1,
|// 2,
|// 3,
|// 4,
|// 5
|// ),
|// List(
|// 1,
|// 2,
|// 3,
|// 4,
|// 5
|// )
|// )
|```
""".stripMargin
)

check(
"height-override",
"""
|```scala mdoc:height=5
|List.fill(15)("hello world!")
|```
""".stripMargin,
"""
|```scala
|List.fill(15)("hello world!")
|// res0: List[String] = List(
|// "hello world!",
|// "hello world!",
|// "hello world!",
|// ...
|```
""".stripMargin
)

check(
"no-height-override",
"""
|```scala mdoc
|List.fill(15)("hello world!")
|```
""".stripMargin,
"""
|```scala
|List.fill(15)("hello world!")
|// res0: List[String] = List(
|// "hello world!",
|// "hello world!",
|// "hello world!",
|// "hello world!",
|// "hello world!",
|// "hello world!",
|// "hello world!",
|// "hello world!",
|// "hello world!",
|// "hello world!",
|// "hello world!",
|// "hello world!",
|// "hello world!",
|// "hello world!",
|// "hello world!"
|// )
|```
""".stripMargin
)

}