Skip to content

Commit

Permalink
Public all the things (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkase authored Jun 8, 2017
1 parent 749cb2f commit 2d0370a
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 74 deletions.
43 changes: 43 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"object": {
"pins": [
{
"package": "Algebra",
"repositoryURL": "https://github.com/typelift/Algebra.git",
"state": {
"branch": null,
"revision": "e81a20aa543dd0847897b57073a3f939c2124375",
"version": "0.2.0"
}
},
{
"package": "Operadics",
"repositoryURL": "https://github.com/typelift/Operadics.git",
"state": {
"branch": null,
"revision": "bc8cdd83868c395b7075d0db5e930842172012ec",
"version": "0.2.3"
}
},
{
"package": "SwiftCheck",
"repositoryURL": "https://github.com/typelift/SwiftCheck.git",
"state": {
"branch": null,
"revision": "97a244b4c48e5823e82f50562fc4e32b3d6507a8",
"version": "0.8.0"
}
},
{
"package": "Swiftx",
"repositoryURL": "https://github.com/typelift/Swiftx.git",
"state": {
"branch": null,
"revision": "32cb9a833cbc69b1ee2ebad2392eca3090b7211c",
"version": "0.5.3"
}
}
]
},
"version": 1
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ With Swift Package Manager, put this inside your `Package.swift`:

```swift
.Package(url: "https://github.com/bkase/DoctorPretty.git",
majorVersion: 0, minor: 2)
majorVersion: 0, minor: 3)
```

## How does it work?
Expand Down
6 changes: 3 additions & 3 deletions Sources/Alignment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import Operadics
// Alignment and Indentation
extension Doc {
/// Indent all lines of the doc by `i`
func indent(_ i: IndentLevel) -> Doc {
public func indent(_ i: IndentLevel) -> Doc {
return (.text(spaces(i)) <> self).hang(i)
}

/// Hanging indentation
func hang(_ i: IndentLevel) -> Doc {
public func hang(_ i: IndentLevel) -> Doc {
return (Doc.nest(i, self)).align()
}

/// Align this document with the nesting level set to the current column
func align() -> Doc {
public func align() -> Doc {
return .column { k in
.nesting { i in .nest(k - i, self) }
}
Expand Down
48 changes: 24 additions & 24 deletions Sources/Atoms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,49 @@ import Operadics

extension Doc {
/// Enclose a doc between left and right
func enclose(left: Doc, right: Doc) -> Doc {
public func enclose(left: Doc, right: Doc) -> Doc {
return left <> self <> right
}

var squotes: Doc {
public var squotes: Doc {
return enclose(left: .squote, right: .squote)
}

var dquotes: Doc {
public var dquotes: Doc {
return enclose(left: .dquote, right: .dquote)
}

var braces: Doc {
public var braces: Doc {
return enclose(left: .lbrace, right: .rbrace)
}

var parens: Doc {
public var parens: Doc {
return enclose(left: .lparen, right: .rparen)
}

var angles: Doc {
public var angles: Doc {
return enclose(left: .langle, right: .rangle)
}

var brackets: Doc {
public var brackets: Doc {
return enclose(left: .lbracket, right: .rbracket)
}

static let squote: Doc = .char("'")
static let dquote: Doc = .char("\"")
static let lbrace: Doc = .char("{")
static let rbrace: Doc = .char("}")
static let lparen: Doc = .char("(")
static let rparen: Doc = .char(")")
static let langle: Doc = .char("<")
static let rangle: Doc = .char(">")
static let lbracket: Doc = .char("[")
static let rbracket: Doc = .char("]")

static let space: Doc = .char(" ")
static let dot: Doc = .char(".")
static let comma: Doc = .char(",")
static let semi: Doc = .char(";")
static let backslash: Doc = .char("\\")
static let equals: Doc = .char("=")
public static let squote: Doc = .char("'")
public static let dquote: Doc = .char("\"")
public static let lbrace: Doc = .char("{")
public static let rbrace: Doc = .char("}")
public static let lparen: Doc = .char("(")
public static let rparen: Doc = .char(")")
public static let langle: Doc = .char("<")
public static let rangle: Doc = .char(">")
public static let lbracket: Doc = .char("[")
public static let rbracket: Doc = .char("]")

public static let space: Doc = .char(" ")
public static let dot: Doc = .char(".")
public static let comma: Doc = .char(",")
public static let semi: Doc = .char(";")
public static let backslash: Doc = .char("\\")
public static let equals: Doc = .char("=")
}
24 changes: 12 additions & 12 deletions Sources/Doc.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Swiftx
import Operadics

typealias Width = Int
public typealias Width = Int
// TODO: What does this int mean, really
typealias ColumnCount = Int
typealias IndentLevel = Int
public typealias ColumnCount = Int
public typealias IndentLevel = Int
/// The ribbon width is the maximal amount of non-indentation characters on a line
typealias RibbonWidth = Int
public typealias RibbonWidth = Int

indirect enum Doc {
public indirect enum Doc {
case empty
/// Invariant: char != '\n'
case _char(Character)
Expand All @@ -30,34 +30,34 @@ indirect enum Doc {
case columns((ColumnCount?) -> Doc)
case ribbon((RibbonWidth?) -> Doc)

static func char(_ c: Character) -> Doc {
public static func char(_ c: Character) -> Doc {
return c == "\n" ? ._line : ._char(c)
}

static func text(_ str: String) -> Doc {
public static func text(_ str: String) -> Doc {
return str == "" ? .empty : ._text(length: str.characters.count, str)
}

static var line: Doc {
public static var line: Doc {
return .flatAlt(primary: ._line, whenFlattened: .space)
}

static var linebreak: Doc {
public static var linebreak: Doc {
return .flatAlt(primary: ._line, whenFlattened: .zero)
}

static var hardline: Doc {
public static var hardline: Doc {
return ._line
}

/// Used to specify alternative layouts
/// `doc.grouped` removes all line breaks in `doc`. The resulting line
/// is added if it fits on the page. If it doesn't, it's rendered as is
var grouped: Doc {
public var grouped: Doc {
return .union(longerLines: flattened, shorterLines: self)
}

var flattened: Doc {
public var flattened: Doc {
switch self {
case .empty: return self
case ._char(_): return self
Expand Down
4 changes: 2 additions & 2 deletions Sources/DocMonoid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import protocol Algebra.Additive
import Operadics

extension Doc: Additive {
static func <>(l: Doc, r: Doc) -> Doc {
public static func <>(l: Doc, r: Doc) -> Doc {
return .concat(l, r)
}

static var zero: Doc { return .empty }
public static var zero: Doc { return .empty }
}

15 changes: 8 additions & 7 deletions Sources/Enclosed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import Operadics
extension BidirectionalCollection where Iterator.Element == Doc, IndexDistance == Int, SubSequence.Iterator.Element == Doc {

/// Intersperses punctuation inside docs
func punctuate(with punctuation: Doc) -> [Doc] {
public func punctuate(with punctuation: Doc) -> [Doc] {
if let d = first {
return [d] + self.dropFirst().reduce([]) { acc, d2 in
acc <> [punctuation, d2]
return [d] + self.dropFirst().reduce([Doc]()) { t in
let (acc, d2) = t
return acc <> [punctuation, d2]
}
} else {
return []
Expand All @@ -35,7 +36,7 @@ extension BidirectionalCollection where Iterator.Element == Doc, IndexDistance =
/// baz
/// ]
/// Note: The Haskell version sticks the separator at the front
func enclose(left: Doc, right: Doc, separator: Doc, indent: IndentLevel) -> Doc {
public func enclose(left: Doc, right: Doc, separator: Doc, indent: IndentLevel) -> Doc {
if count == 0 {
return left <> right
}
Expand All @@ -51,17 +52,17 @@ extension BidirectionalCollection where Iterator.Element == Doc, IndexDistance =
}

/// See @enclose
func list(indent: IndentLevel) -> Doc {
public func list(indent: IndentLevel) -> Doc {
return enclose(left: Doc.lbracket, right: Doc.rbracket, separator: Doc.comma, indent: indent)
}

/// See @enclose
func tupled(indent: IndentLevel) -> Doc {
public func tupled(indent: IndentLevel) -> Doc {
return enclose(left: Doc.lparen, right: Doc.rparen, separator: Doc.comma, indent: indent)
}

/// See @enclose
func semiBraces(indent: IndentLevel) -> Doc {
public func semiBraces(indent: IndentLevel) -> Doc {
return enclose(left: Doc.lbrace, right: Doc.rbrace, separator: Doc.semi, indent: indent)
}
}
2 changes: 1 addition & 1 deletion Sources/Extras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

import Foundation

func spaces(_ i: Int) -> String {
public func spaces(_ i: Int) -> String {
return (0..<i).map{ _ in " " }.joined()
}
6 changes: 3 additions & 3 deletions Sources/Fills.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import Operadics
extension Doc {
/// Render doc then fill until width == i
/// If too large, put in a line-break and then pad
func fillBreak(_ i: IndentLevel) -> Doc {
public func fillBreak(_ i: IndentLevel) -> Doc {
return width { w in w > i ? .nest(i, .linebreak) : .text(spaces(i - w)) }
}

/// Render doc then fill until width == i
func fill(_ i: IndentLevel) -> Doc {
public func fill(_ i: IndentLevel) -> Doc {
return width { w in w >= i ? .zero : .text(spaces(i - w)) }
}

func width(_ f: @escaping (IndentLevel) -> Doc) -> Doc {
public func width(_ f: @escaping (IndentLevel) -> Doc) -> Doc {
return .column { k1 in self <> .column { k2 in f(k2 - k1) } }
}
}
Loading

0 comments on commit 2d0370a

Please sign in to comment.