Skip to content

Commit

Permalink
Make the line continuation lazy. (#10)
Browse files Browse the repository at this point in the history
This pull-request makes the continuation of the line lazy, and performance improves drastically.
  • Loading branch information
chriseidhof authored and bkase committed Jun 28, 2018
1 parent 7e1b0fe commit d481eee
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Sources/DoctorPretty/RenderPretty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension Doc {
// This parameter is only used during annotation, but I'm
// keeping it here to simplify adding the annotation case
// in doc if that ever happens
z: (IndentLevel, ColumnCount) -> SimpleDoc,
z: @escaping (IndentLevel, ColumnCount) -> SimpleDoc,
indentationDocs: Docs
) -> SimpleDoc {
switch indentationDocs {
Expand All @@ -62,7 +62,7 @@ extension Doc {
let newColumn = currColumn + length
return SimpleDoc.text(length: length, str, best(currNesting: currNesting, currColumn: newColumn, z: z, indentationDocs: rest))
case ._line:
return SimpleDoc.line(indent: indent, best(currNesting: indent, currColumn: indent, z: z, indentationDocs: rest))
return SimpleDoc.line(indent: indent, { best(currNesting: indent, currColumn: indent, z: z, indentationDocs: rest) })
case let .flatAlt(primary, whenFlattened: _):
return best(currNesting: currNesting, currColumn: currColumn, z: z, indentationDocs: .Cons((indent, primary), rest))
case let .concat(d1, d2):
Expand Down
4 changes: 2 additions & 2 deletions Sources/DoctorPretty/SimpleDoc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public indirect enum SimpleDoc {
case empty
case char(Character, SimpleDoc)
case text(length: Int, String, SimpleDoc)
case line(indent: Int, SimpleDoc)
case line(indent: Int, () -> SimpleDoc)

public func display<M: Additive>(readString: (String) -> M) -> M {
switch self {
case .empty: return M.zero
case let .char(c, rest): return readString(String(c)) <> rest.display(readString: readString)
case let .text(_, s, rest): return readString(s) <> rest.display(readString: readString)
case let .line(indent, rest):
return readString("\n" + spaces(indent)) <> rest.display(readString: readString)
return readString("\n" + spaces(indent)) <> rest().display(readString: readString)
}
}

Expand Down
10 changes: 10 additions & 0 deletions Tests/DoctorPrettyTests/DoctorPrettyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import Operadics

// Most tests ported from https://github.com/minad/wl-pprint-annotated/blob/master/test/WLPPrintTests.hs
class DoctorPrettyTests: XCTestCase {

func testSlow() {
measure {
let doc = (1...30).map { _ in
(1...30).map { _ in "foo" }.map(Doc.text).fillSep()
}.vcat()
_ = doc.renderPrettyDefault()
}
}

func assertPretty(pageWidth: Width, str: String, doc: Doc, file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(doc.renderPretty(ribbonFrac: 1.0, pageWidth: pageWidth).displayString(), str, file: file, line: line)
}
Expand Down

0 comments on commit d481eee

Please sign in to comment.