Skip to content

Commit

Permalink
Merge pull request #5 from tonicebrian/indent_combinator
Browse files Browse the repository at this point in the history
Provide Indent combinator
  • Loading branch information
satabin authored Feb 25, 2017
2 parents 2e9f063 + 3ef6736 commit 373f9c1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pomExtra := (
</issueManagement>
)

lazy val benchmarks = project in file("benchmarks") dependsOn(root)
lazy val benchmarks = project in file("benchmarks") aggregate (root)

scalaVersion in benchmarks := "2.11.8"

Expand Down
21 changes: 13 additions & 8 deletions src/main/scala/gnieh/pp/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ package object pp {
def nest(indent: Int)(inner: Doc): Doc =
NestDoc(indent, inner)

/* Deindent the document */
/** Deindent the document */
@inline
def unnest(indent: Int)(inner: Doc): Doc =
nest(-indent)(inner)
Expand Down Expand Up @@ -66,6 +66,11 @@ package object pp {
val empty: Doc =
EmptyDoc

/** Renders the document with prepending n spaces to the current column */
@inline
def indent(i: Int)(doc: Doc): Doc =
hang(i)(ConsDoc(TextDoc(" " * i), doc))

/** Renders the document with nesting level set to the current column */
@inline
def align(doc: Doc): Doc =
Expand Down Expand Up @@ -184,30 +189,30 @@ package object pp {
group(vcat(docs))

@inline
implicit def s2doc(s: String) =
implicit def s2doc(s: String): Doc =
string(s)

@inline
implicit def i2doc(i: Int) =
implicit def i2doc(i: Int): Doc =
int(i)

@inline
implicit def l2doc(l: Long) =
implicit def l2doc(l: Long): Doc =
long(l)

@inline
implicit def f2doc(f: Float) =
implicit def f2doc(f: Float): Doc =
float(f)

@inline
implicit def d2doc(d: Double) =
implicit def d2doc(d: Double): Doc =
double(d)

@inline
implicit def c2doc(c: Char) =
implicit def c2doc(c: Char): Doc =
char(c)

implicit def opt2doc[T <% Doc](o: Option[T]): Doc = o match {
implicit def opt2doc[T](o: Option[T])(implicit ev: T => Doc): Doc = o match {
case Some(d) => d
case None => empty
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/scala/gnieh/pp/tests/IndentTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gnieh.pp.tests

import gnieh.pp._

class IndentTest extends PpTest {
"the indent operator" should "indent text as block" in {
val docs: List[Doc] = List("line1", "line2", "line3")
val finalDoc = indent(2)(vcat(docs))

render80(finalDoc) should be(" line1\n line2\n line3")
}
it should "behave as original Haskell implementation" in {
val doc = indent(4)(fillSep(words("the indent combinator indents these words !")))

val expectedRender =
""" the indent
| combinator
| indents these
| words !""".stripMargin

render20(doc) should be(expectedRender)
}
}
1 change: 0 additions & 1 deletion src/test/scala/gnieh/pp/tests/ListCombinatorsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ class ListCombinatorsTest extends PpTest {
val test2 = "some" :+: align(vsep(someText))
render80(test2) should be("some text\n to\n lay\n out")
}

}

0 comments on commit 373f9c1

Please sign in to comment.