import * as covariant from "@effect/typeclass/Covariant" import type * as invariant from "@effect/typeclass/Invariant" import type * as monoid from "@effect/typeclass/Monoid" import type * as semigroup from "@effect/typeclass/Semigroup" import * as Arr from "effect/Array" import * as Effect from "effect/Effect" import * as Equal from "effect/Equal" import { dual, pipe } from "effect/Function" import * as Hash from "effect/Hash" import { pipeArguments } from "effect/Pipeable" import type * as Doc from "../Doc.js" import type * as Flatten from "../Flatten.js" import type * as PageWidth from "../PageWidth.js" import * as InternalFlatten from "./flatten.js" const DocSymbolKey = "@effect/printer/Doc" /** @internal */ export const DocTypeId: Doc.DocTypeId = Symbol.for(DocSymbolKey) as Doc.DocTypeId const protoHash = { Fail: (_: Doc.Fail) => Hash.combine(Hash.hash(DocSymbolKey))(Hash.hash("@effect/printer/Doc/Fail")), Empty: (_: Doc.Empty) => Hash.combine(Hash.hash(DocSymbolKey))(Hash.hash("@effect/printer/Doc/Empty")), Char: (self: Doc.Char) => Hash.combine(Hash.hash(DocSymbolKey))(Hash.string(self.char)), Text: (self: Doc.Text) => Hash.combine(Hash.hash(DocSymbolKey))(Hash.string(self.text)), Line: (_: Doc.Line) => Hash.combine(Hash.hash(DocSymbolKey))(Hash.hash("@effect/printer/Doc/Line")), FlatAlt: (self: Doc.FlatAlt) => Hash.combine(Hash.hash(DocSymbolKey))( Hash.combine(Hash.hash(self.left))(Hash.hash(self.right)) ), Cat: (self: Doc.Cat) => Hash.combine(Hash.hash(DocSymbolKey))( Hash.combine(Hash.hash(self.left))(Hash.hash(self.right)) ), Nest: (self: Doc.Nest) => Hash.combine(Hash.hash(DocSymbolKey))( Hash.combine(Hash.hash(self.indent))(Hash.hash(self.doc)) ), Union: (self: Doc.Union) => Hash.combine(Hash.hash(DocSymbolKey))( Hash.combine(Hash.hash(self.left))(Hash.hash(self.right)) ), Column: (self: Doc.Column) => Hash.combine(Hash.hash(DocSymbolKey))(Hash.hash(self.react)), WithPageWidth: (self: Doc.WithPageWidth) => Hash.combine(Hash.hash(DocSymbolKey))(Hash.hash(self.react)), Nesting: (self: Doc.Nesting) => Hash.combine(Hash.hash(DocSymbolKey))(Hash.hash(self.react)), Annotated: (self: Doc.Annotated) => Hash.combine(Hash.hash(DocSymbolKey))( Hash.combine(Hash.hash(self.annotation))(Hash.hash(self.doc)) ) } as const const protoEqual = { Fail: (_: Doc.Fail, that: unknown) => isDoc(that) && that._tag === "Fail", Empty: (_: Doc.Empty, that: unknown) => isDoc(that) && that._tag === "Empty", Char: (self: Doc.Char, that: unknown) => isDoc(that) && that._tag === "Char" && self.char === that.char, Text: (self: Doc.Text, that: unknown) => isDoc(that) && that._tag === "Text" && self.text === that.text, Line: (_: Doc.Line, that: unknown) => isDoc(that) && that._tag === "Line", FlatAlt: (self: Doc.FlatAlt, that: unknown) => isDoc(that) && that._tag === "FlatAlt" && Equal.equals(that.left)(self.left) && Equal.equals(that.right)(self.right), Cat: (self: Doc.Cat, that: unknown) => isDoc(that) && that._tag === "Cat" && Equal.equals(that.left)(self.left) && Equal.equals(that.right)(self.right), Nest: (self: Doc.Nest, that: unknown) => isDoc(that) && that._tag === "Nest" && self.indent === that.indent && Equal.equals(that.doc)(self.doc), Union: (self: Doc.Union, that: unknown) => isDoc(that) && that._tag === "Union" && Equal.equals(that.left)(self.left) && Equal.equals(that.right)(self.right), Column: (self: Doc.Column, that: unknown) => isDoc(that) && that._tag === "Column" && Equal.equals(that.react)(self.react), WithPageWidth: (self: Doc.WithPageWidth, that: unknown) => isDoc(that) && that._tag === "WithPageWidth" && Equal.equals(that.react)(self.react), Nesting: (self: Doc.Nesting, that: unknown) => isDoc(that) && that._tag === "Nesting" && Equal.equals(that.react)(self.react), Annotated: (self: Doc.Annotated, that: unknown) => isDoc(that) && that._tag === "Annotated" && Equal.equals(that.annotation)(self.annotation) && Equal.equals(that.doc)(self.doc) } as const const proto = { [DocTypeId]: { _A: (_: never) => _ }, [Hash.symbol](this: Doc.Doc) { return Hash.cached(this, protoHash[this._tag](this as any)) }, [Equal.symbol](this: Doc.Doc, that: unknown): boolean { return protoEqual[this._tag](this as any, that as any) }, pipe() { return pipeArguments(this, arguments) } } // ----------------------------------------------------------------------------- // Refinements // ----------------------------------------------------------------------------- /** @internal */ export const isDoc = (u: unknown): u is Doc.Doc => typeof u === "object" && u != null && DocTypeId in u /** @internal */ export const isFail = (self: Doc.Doc): self is Doc.Fail => self._tag === "Fail" /** @internal */ export const isEmpty = (self: Doc.Doc): self is Doc.Empty => self._tag === "Empty" /** @internal */ export const isChar = (self: Doc.Doc): self is Doc.Char => self._tag === "Char" /** @internal */ export const isText = (self: Doc.Doc): self is Doc.Text => self._tag === "Text" /** @internal */ export const isLine = (self: Doc.Doc): self is Doc.Line => self._tag === "Line" /** @internal */ export const isFlatAlt = (self: Doc.Doc): self is Doc.FlatAlt => self._tag === "FlatAlt" /** @internal */ export const isCat = (self: Doc.Doc): self is Doc.Cat => self._tag === "Cat" /** @internal */ export const isNest = (self: Doc.Doc): self is Doc.Nest => self._tag === "Nest" /** @internal */ export const isUnion = (self: Doc.Doc): self is Doc.Union => self._tag === "Union" /** @internal */ export const isColumn = (self: Doc.Doc): self is Doc.Column => self._tag === "Column" /** @internal */ export const isWithPageWidth = (self: Doc.Doc): self is Doc.WithPageWidth => self._tag === "WithPageWidth" /** @internal */ export const isNesting = (self: Doc.Doc): self is Doc.Nesting => self._tag === "Nesting" /** @internal */ export const isAnnotated = (self: Doc.Doc): self is Doc.Annotated => self._tag === "Annotated" // ----------------------------------------------------------------------------- // Primitives // ----------------------------------------------------------------------------- /** @internal */ export const char = (char: string): Doc.Doc => { const op = Object.create(proto) op._tag = "Char" op.char = char return op } /** @internal */ export const text = (text: string): Doc.Doc => { const op = Object.create(proto) op._tag = "Text" op.text = text return op } /** @internal */ export const flatAlt = dual< (that: Doc.Doc) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, that: Doc.Doc) => Doc.Doc >(2, (self, that) => { const op = Object.create(proto) op._tag = "FlatAlt" op.left = self op.right = that return op }) /** @internal */ export const union = dual< (that: Doc.Doc) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, that: Doc.Doc) => Doc.Doc >(2, (self, that) => { const op = Object.create(proto) op._tag = "Union" op.left = self op.right = that return op }) /** @internal */ export const cat = dual< (that: Doc.Doc) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, that: Doc.Doc) => Doc.Doc >(2, (self, that) => { const op = Object.create(proto) op._tag = "Cat" op.left = self op.right = that return op }) /** @internal */ export const empty: Doc.Doc = (() => { const op = Object.create(proto) op._tag = "Empty" return op })() /** @internal */ export const fail: Doc.Doc = (() => { const op = Object.create(proto) op._tag = "Fail" return op })() /** @internal */ export const hardLine: Doc.Doc = (() => { const op = Object.create(proto) op._tag = "Line" return op })() /** @internal */ export const line: Doc.Doc = flatAlt(hardLine, char(" ")) /** @internal */ export const lineBreak: Doc.Doc = flatAlt(hardLine, empty) /** @internal */ export const softLine: Doc.Doc = union(char(" "), hardLine) /** @internal */ export const softLineBreak: Doc.Doc = union(empty, hardLine) /** @internal */ export const backslash: Doc.Doc = char("\\") /** @internal */ export const colon: Doc.Doc = char(":") /** @internal */ export const comma: Doc.Doc = char(",") /** @internal */ export const dot: Doc.Doc = char(".") /** @internal */ export const dquote: Doc.Doc = char("\"") /** @internal */ export const equalSign: Doc.Doc = char("=") /** @internal */ export const langle: Doc.Doc = char("<") /** @internal */ export const lbrace: Doc.Doc = char("{") /** @internal */ export const lbracket: Doc.Doc = char("[") /** @internal */ export const lparen: Doc.Doc = char("(") /** @internal */ export const rangle: Doc.Doc = char(">") /** @internal */ export const rbrace: Doc.Doc = char("}") /** @internal */ export const rbracket: Doc.Doc = char("]") /** @internal */ export const rparen: Doc.Doc = char(")") /** @internal */ export const semi: Doc.Doc = char(";") /** @internal */ export const slash: Doc.Doc = char("/") /** @internal */ export const squote: Doc.Doc = char("'") /** @internal */ export const space: Doc.Doc = char(" ") /** @internal */ export const vbar: Doc.Doc = char("|") /** @internal */ export const cats = (docs: Iterable>): Doc.Doc => group(vcat(docs)) /** @internal */ export const catWithLine = dual< (that: Doc.Doc) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, that: Doc.Doc) => Doc.Doc >(2, (self, that) => cat(self, cat(line, that))) /** @internal */ export const catWithLineBreak = dual< (that: Doc.Doc) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, that: Doc.Doc) => Doc.Doc >(2, (self, that) => cat(self, cat(lineBreak, that))) /** @internal */ export const catWithSoftLine = dual< (that: Doc.Doc) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, that: Doc.Doc) => Doc.Doc >(2, (self, that) => cat(self, cat(softLine, that))) /** @internal */ export const catWithSoftLineBreak = dual< (that: Doc.Doc) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, that: Doc.Doc) => Doc.Doc >(2, (self, that) => cat(self, cat(softLineBreak, that))) /** @internal */ export const catWithSpace = dual< (that: Doc.Doc) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, that: Doc.Doc) => Doc.Doc >(2, (self, that) => cat(self, cat(space, that))) /** @internal */ export const concatWith = dual< ( f: (left: Doc.Doc, right: Doc.Doc) => Doc.Doc ) => (docs: Iterable>) => Doc.Doc, ( docs: Iterable>, f: (left: Doc.Doc, right: Doc.Doc) => Doc.Doc ) => Doc.Doc >(2, (docs, f) => Arr.matchRight(Arr.fromIterable(docs), { onEmpty: () => empty, onNonEmpty: (init, last) => Arr.reduceRight(init, last, (curr, acc) => f(acc, curr)) })) /** @internal */ export const vcat = (docs: Iterable>): Doc.Doc => concatWith(docs, (left, right) => catWithLineBreak(left, right)) /** @internal */ export const hcat = (docs: Iterable>): Doc.Doc => concatWith(docs, (left, right) => cat(left, right)) /** @internal */ export const fillCat = (docs: Iterable>): Doc.Doc => concatWith(docs, (left, right) => catWithSoftLineBreak(left, right)) // ----------------------------------------------------------------------------- // Separation // ----------------------------------------------------------------------------- /** @internal */ export const hsep = (docs: Iterable>): Doc.Doc => concatWith(docs, (left, right) => catWithSpace(left, right)) /** @internal */ export const vsep = (docs: Iterable>): Doc.Doc => concatWith(docs, (left, right) => catWithLine(left, right)) /** @internal */ export const fillSep = (docs: Iterable>): Doc.Doc => concatWith(docs, (left, right) => catWithSoftLine(left, right)) /** @internal */ export const seps = (docs: Iterable>): Doc.Doc => group(vsep(docs)) /** @internal */ export const group = (self: Doc.Doc): Doc.Doc => { switch (self._tag) { case "FlatAlt": { const flattened = changesUponFlattening(self.right) switch (flattened._tag) { case "Flattened": { return union(flattened.value, self.left) } case "AlreadyFlat": { return union(self.right, self.left) } case "NeverFlat": { return self.left } } } case "Union": { return self } default: { const flattened = changesUponFlattening(self) return InternalFlatten.isFlattened(flattened) ? union(flattened.value, self) : self } } } // ----------------------------------------------------------------------------- // Reactive Layouts // ----------------------------------------------------------------------------- /** @internal */ export const column = (react: (position: number) => Doc.Doc): Doc.Doc => { const op = Object.create(proto) op._tag = "Column" op.react = react return op } /** @internal */ export const nesting = (react: (level: number) => Doc.Doc): Doc.Doc => { const op = Object.create(proto) op._tag = "Nesting" op.react = react return op } /** @internal */ export const width = dual< (react: (width: number) => Doc.Doc) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, react: (width: number) => Doc.Doc) => Doc.Doc >(2, (self, react) => column((colStart) => cat(self, column((colEnd) => react(colEnd - colStart))))) /** @internal */ export const pageWidth = (react: (pageWidth: PageWidth.PageWidth) => Doc.Doc): Doc.Doc => { const op = Object.create(proto) op._tag = "WithPageWidth" op.react = react return op } // ----------------------------------------------------------------------------- // Alignment // ----------------------------------------------------------------------------- /** @internal */ export const nest = dual< (indent: number) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, indent: number) => Doc.Doc >(2, (self, indent) => indent === 0 ? self : (() => { const op = Object.create(proto) op._tag = "Nest" op.indent = indent op.doc = self return op })()) /** @internal */ export const align = (self: Doc.Doc): Doc.Doc => column((position) => nesting((level) => nest(self, position - level))) /** @internal */ export const hang = dual< (indent: number) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, indent: number) => Doc.Doc >(2, (self, indent) => align(nest(self, indent))) /** @internal */ export const indent = dual< (indent: number) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, indent: number) => Doc.Doc >(2, (self, indent) => hang(cat(spaces(indent), self), indent)) /** @internal */ export const encloseSep = dual< ( left: Doc.Doc, right: Doc.Doc, sep: Doc.Doc ) => ( docs: Iterable> ) => Doc.Doc, ( docs: Iterable>, left: Doc.Doc, right: Doc.Doc, sep: Doc.Doc ) => Doc.Doc >(4, ( docs: Iterable>, left: Doc.Doc, right: Doc.Doc, sep: Doc.Doc ) => { const documents = Arr.fromIterable(docs) if (Arr.isEmptyReadonlyArray(documents)) { return cat(left, right) } if (documents.length === 1) { return cat(left, cat(documents[0]!, right)) } const xs = pipe( Arr.makeBy(documents.length - 1, () => sep), Arr.prepend(left), Arr.zipWith(documents, (left: Doc.Doc, right) => cat(left, right)) ) return cat(cats(xs), right) }) /** @internal */ export const list = (docs: Iterable>): Doc.Doc => group( encloseSep( docs, flatAlt(text("[ "), lbracket), flatAlt(text(" ]"), rbracket), text(", ") ) ) /** @internal */ export const tupled = (docs: Iterable>): Doc.Doc => group( encloseSep( docs, flatAlt(text("( "), lparen), flatAlt(text(" )"), rparen), text(", ") ) ) // ----------------------------------------------------------------------------- // Filling // ----------------------------------------------------------------------------- /** @internal */ export const fill = dual< (w: number) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, w: number) => Doc.Doc >(2, (self, w) => width(self, (i) => spaces(w - i))) /** @internal */ export const fillBreak = dual< (w: number) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, w: number) => Doc.Doc >(2, (self, w) => width(self, (i) => (i > w ? nest(lineBreak, w) : spaces(w - i)))) // ----------------------------------------------------------------------------- // Flattening // ----------------------------------------------------------------------------- /** @internal */ export const flatten = (self: Doc.Doc): Doc.Doc => Effect.runSync(flattenSafe(self)) const flattenSafe = (self: Doc.Doc): Effect.Effect> => Effect.gen(function*() { switch (self._tag) { case "Fail": { return self } case "Empty": { return self } case "Char": { return self } case "Text": { return self } case "Line": { return fail } case "FlatAlt": { return yield* flattenSafe(self.right) } case "Cat": { const left = yield* flattenSafe(self.left) const right = yield* flattenSafe(self.right) return cat(left, right) } case "Nest": { const doc = yield* flattenSafe(self.doc) return nest(doc, self.indent) } case "Union": { return yield* flattenSafe(self.left) } case "Column": { return column((position) => flatten(self.react(position))) } case "WithPageWidth": { return pageWidth((pageWidth) => flatten(self.react(pageWidth))) } case "Nesting": { return nesting((level) => flatten(self.react(level))) } case "Annotated": { const doc = yield* flattenSafe(self.doc) return annotate(doc, self.annotation) } } }) /** @internal */ export const changesUponFlattening = (self: Doc.Doc): Flatten.Flatten> => Effect.runSync(changesUponFlatteningSafe(self)) const changesUponFlatteningSafe = (self: Doc.Doc): Effect.Effect>> => Effect.gen(function*() { switch (self._tag) { case "Fail": case "Line": { return InternalFlatten.neverFlat } case "Empty": case "Char": case "Text": { return InternalFlatten.alreadyFlat } case "FlatAlt": { const doc = yield* flattenSafe(self.right) return InternalFlatten.flattened(doc) } case "Cat": { const left = yield* changesUponFlatteningSafe(self.left) const right = yield* changesUponFlatteningSafe(self.right) if (InternalFlatten.isNeverFlat(left) || InternalFlatten.isNeverFlat(right)) { return InternalFlatten.neverFlat } if (InternalFlatten.isFlattened(left) && InternalFlatten.isFlattened(right)) { return InternalFlatten.flattened(cat(left.value, right.value)) } if (InternalFlatten.isFlattened(left) && InternalFlatten.isAlreadyFlat(right)) { return InternalFlatten.flattened(cat(left.value, self.right)) } if (InternalFlatten.isAlreadyFlat(left) && InternalFlatten.isFlattened(right)) { return InternalFlatten.flattened(cat(self.left, right.value)) } if (InternalFlatten.isAlreadyFlat(left) && InternalFlatten.isAlreadyFlat(right)) { return InternalFlatten.alreadyFlat } throw new Error( "[BUG]: Doc.changesUponFlattening - unable to flatten a Cat document " + "- please open an issue at https://github.com/IMax153/contentlayer/issues/new" ) } case "Nest": { return yield* pipe( changesUponFlatteningSafe(self.doc), Effect.map(InternalFlatten.map((doc) => nest(doc, self.indent))) ) } case "Union": { return InternalFlatten.flattened(self.left) } case "Column": { const doc = column((position) => Effect.runSync(flattenSafe(self.react(position)))) return InternalFlatten.flattened(doc) } case "WithPageWidth": { const doc = pageWidth((pageWidth) => Effect.runSync(flattenSafe(self.react(pageWidth)))) return InternalFlatten.flattened(doc) } case "Nesting": { const doc = nesting((level) => Effect.runSync(flattenSafe(self.react(level)))) return InternalFlatten.flattened(doc) } case "Annotated": { return yield* pipe( changesUponFlatteningSafe(self.doc), Effect.map(InternalFlatten.map((doc) => annotate(doc, self.annotation))) ) } } }) // ----------------------------------------------------------------------------- // Annotations // ----------------------------------------------------------------------------- /** @internal */ export const annotate = dual< (annotation: A) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, annotation: A) => Doc.Doc >(2, (self, annotation) => { const op = Object.create(proto) op._tag = "Annotated" op.doc = self op.annotation = annotation return op }) /** @internal */ export const alterAnnotations = dual< (f: (a: A) => Iterable) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, f: (a: A) => Iterable) => Doc.Doc >(2, (self, f) => Effect.runSync(alterAnnotationsSafe(self, f))) const alterAnnotationsSafe = ( self: Doc.Doc, f: (a: A) => Iterable ): Effect.Effect> => { switch (self._tag) { case "Cat": { return Effect.zipWith( Effect.suspend(() => alterAnnotationsSafe(self.left, f)), alterAnnotationsSafe(self.right, f), (left, right) => cat(left, right) ) } case "FlatAlt": { return Effect.zipWith( Effect.suspend(() => alterAnnotationsSafe(self.left, f)), alterAnnotationsSafe(self.right, f), (left, right) => flatAlt(left, right) ) } case "Union": { return Effect.zipWith( Effect.suspend(() => alterAnnotationsSafe(self.left, f)), alterAnnotationsSafe(self.right, f), (left, right) => union(left, right) ) } case "Nest": { return Effect.map( Effect.suspend(() => alterAnnotationsSafe(self.doc, f)), nest(self.indent) ) } case "Column": { return Effect.succeed( column((position) => Effect.runSync(alterAnnotationsSafe(self.react(position), f))) ) } case "WithPageWidth": { return Effect.succeed( pageWidth((pageWidth) => Effect.runSync(alterAnnotationsSafe(self.react(pageWidth), f))) ) } case "Nesting": { return Effect.succeed( nesting((level) => Effect.runSync(alterAnnotationsSafe(self.react(level), f))) ) } case "Annotated": { return Effect.map(alterAnnotationsSafe(self.doc, f), (doc) => Arr.reduceRight( Arr.fromIterable(f(self.annotation)), doc, (doc, b) => annotate(doc, b) )) } default: { return Effect.succeed(self as unknown as Doc.Doc) } } } /** @internal */ export const reAnnotate = dual< (f: (a: A) => B) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, f: (a: A) => B) => Doc.Doc >(2, (self, f) => alterAnnotations(self, (a) => [f(a)])) /** @internal */ export const unAnnotate = (self: Doc.Doc): Doc.Doc => alterAnnotations(() => [])(self) // ----------------------------------------------------------------------------- // Instances // ----------------------------------------------------------------------------- /** @internal */ export const map: { (f: (a: A) => B): (self: Doc.Doc) => Doc.Doc (self: Doc.Doc, f: (a: A) => B): Doc.Doc } = reAnnotate /** @internal */ export const imap = covariant.imap(map) /** @internal */ export const getSemigroup = (): semigroup.Semigroup> => ({ combine: cat, combineMany: (self, others) => cat(self, cats(others)) }) /** @internal */ export const getMonoid = (): monoid.Monoid> => ({ empty, combine: cat, combineMany: (self, others) => cat(self, cats(others)), combineAll: cats }) /** @internal */ export const Covariant: covariant.Covariant = { map, imap } /** @internal */ export const Invariant: invariant.Invariant = { imap } // ----------------------------------------------------------------------------- // Utilities // ----------------------------------------------------------------------------- /** @internal */ export const string = (str: string): Doc.Doc => { return cats( str.split("\n").map((s) => s.length === 0 ? empty : s.length === 1 ? char(s) : text(s) ) ) } const stripControlCharacters = (value: string): string => { let sanitized = "" for (let i = 0; i < value.length; i++) { const code = value.charCodeAt(i) if ( (code > 0x1f || code === 0x09 || code === 0x0a) && (code < 0x7f || code > 0x9f) ) { sanitized += value[i] } } return sanitized } const sanitizeLeaf = (self: Doc.Char | Doc.Text): Doc.Doc => { const value = self._tag === "Char" ? self.char : self.text const sanitized = stripControlCharacters(value) if (sanitized === value) { return self } if (sanitized.length === 0) { return empty } return sanitized.length === 1 ? char(sanitized) : text(sanitized) } /** @internal */ export const sanitize = (self: Doc.Doc): Doc.Doc => Effect.runSync(sanitizeSafe(self)) const sanitizeSafe = (self: Doc.Doc): Effect.Effect> => { switch (self._tag) { case "Char": case "Text": { return Effect.succeed(sanitizeLeaf(self)) } case "Cat": { return Effect.zipWith( Effect.suspend(() => sanitizeSafe(self.left)), sanitizeSafe(self.right), (left, right) => cat(left, right) ) } case "FlatAlt": { return Effect.zipWith( Effect.suspend(() => sanitizeSafe(self.left)), sanitizeSafe(self.right), (left, right) => flatAlt(left, right) ) } case "Union": { return Effect.zipWith( Effect.suspend(() => sanitizeSafe(self.left)), sanitizeSafe(self.right), (left, right) => union(left, right) ) } case "Nest": { return Effect.map( Effect.suspend(() => sanitizeSafe(self.doc)), nest(self.indent) ) } case "Column": { return Effect.succeed(column((position) => Effect.runSync(sanitizeSafe(self.react(position))))) } case "WithPageWidth": { return Effect.succeed(pageWidth((width) => Effect.runSync(sanitizeSafe(self.react(width))))) } case "Nesting": { return Effect.succeed(nesting((level) => Effect.runSync(sanitizeSafe(self.react(level))))) } case "Annotated": { return Effect.map( Effect.suspend(() => sanitizeSafe(self.doc)), (doc) => annotate(doc, self.annotation) ) } default: { return Effect.succeed(self) } } } /** @internal */ export const surround = dual< (left: Doc.Doc, right: Doc.Doc) => (self: Doc.Doc) => Doc.Doc, (self: Doc.Doc, left: Doc.Doc, right: Doc.Doc) => Doc.Doc >(3, (self, left, right) => cat(left, cat(self, right))) /** @internal */ export const singleQuoted = (self: Doc.Doc): Doc.Doc => surround(self, squote, squote) /** @Internal */ export const doubleQuoted = (self: Doc.Doc): Doc.Doc => surround(self, dquote, dquote) /** @internal */ export const parenthesized = (self: Doc.Doc): Doc.Doc => surround(self, lparen, rparen) /** @internal */ export const angleBracketed = (self: Doc.Doc): Doc.Doc => surround(self, langle, rangle) /** @internal */ export const squareBracketed = (self: Doc.Doc): Doc.Doc => surround(self, lbracket, rbracket) /** @internal */ export const curlyBraced = (self: Doc.Doc): Doc.Doc => surround(self, lbrace, rbrace) /** @internal */ export const spaces = (n: number): Doc.Doc => { if (n <= 0) { return empty } if (n === 1) { return char(" ") } return text(textSpaces(n)) } /** @internal */ export const words = (str: string, splitChar = " "): ReadonlyArray> => str.split(splitChar).map((word) => { if (word === "") { return empty } if (word === "\n") { return hardLine } if (word.length === 1) { return char(word) } return text(word) }) /** @internal */ export const reflow = (s: string, char = " "): Doc.Doc => fillSep(words(s, char)) /** @internal */ export const punctuate = dual< (punctuator: Doc.Doc) => (docs: Iterable>) => ReadonlyArray>, (docs: Iterable>, punctuator: Doc.Doc) => ReadonlyArray> >(2, (docs, punctuator) => { const documents = Arr.fromIterable(docs) return Arr.map(documents, (x, i) => documents.length - 1 === i ? x : cat(x, punctuator)) }) /** @internal */ export const match = dual< ( patterns: { readonly Fail: () => R readonly Empty: () => R readonly Char: (char: string) => R readonly Text: (text: string) => R readonly Line: () => R readonly FlatAlt: (x: Doc.Doc, y: Doc.Doc) => R readonly Cat: (x: Doc.Doc, y: Doc.Doc) => R readonly Nest: (indent: number, doc: Doc.Doc) => R readonly Union: (x: Doc.Doc, y: Doc.Doc) => R readonly Column: (react: (position: number) => Doc.Doc) => R readonly WithPageWidth: (react: (pageWidth: PageWidth.PageWidth) => Doc.Doc) => R readonly Nesting: (react: (level: number) => Doc.Doc) => R readonly Annotated: (annotation: A, doc: Doc.Doc) => R } ) => (self: Doc.Doc) => R, ( self: Doc.Doc, patterns: { readonly Fail: () => R readonly Empty: () => R readonly Char: (char: string) => R readonly Text: (text: string) => R readonly Line: () => R readonly FlatAlt: (x: Doc.Doc, y: Doc.Doc) => R readonly Cat: (x: Doc.Doc, y: Doc.Doc) => R readonly Nest: (indent: number, doc: Doc.Doc) => R readonly Union: (x: Doc.Doc, y: Doc.Doc) => R readonly Column: (react: (position: number) => Doc.Doc) => R readonly WithPageWidth: (react: (pageWidth: PageWidth.PageWidth) => Doc.Doc) => R readonly Nesting: (react: (level: number) => Doc.Doc) => R readonly Annotated: (annotation: A, doc: Doc.Doc) => R } ) => R >(2, (self, patterns) => { switch (self._tag) { case "Fail": { return patterns.Fail() } case "Empty": { return patterns.Empty() } case "Char": { return patterns.Char(self.char) } case "Text": { return patterns.Text(self.text) } case "Line": { return patterns.Line() } case "FlatAlt": { return patterns.FlatAlt(self.left, self.right) } case "Cat": { return patterns.Cat(self.left, self.right) } case "Nest": { return patterns.Nest(self.indent, self.doc) } case "Union": { return patterns.Union(self.left, self.right) } case "Column": { return patterns.Column(self.react) } case "WithPageWidth": { return patterns.WithPageWidth(self.react) } case "Nesting": { return patterns.Nesting(self.react) } case "Annotated": { return patterns.Annotated(self.annotation, self.doc) } } }) /** @internal */ export const textSpaces = (n: number): string => { let s = "" for (let i = 0; i < n; i++) { s = s += " " } return s }