DocTree
Annotations
alterAnnotations
Change the annotation of a document to a different annotation, or none at all.
Signature
declare const alterAnnotations: { <A, B>(f: (a: A) => Iterable<B>): (self: DocTree<A>) => DocTree<B>; <A, B>(self: DocTree<A>, f: (a: A) => Iterable<B>): DocTree<B>;}reAnnotate
Change the annotation of a DocTree.
Signature
declare const reAnnotate: { <A, B>(f: (a: A) => B): (self: DocTree<A>) => DocTree<B>; <A, B>(self: DocTree<A>, f: (a: A) => B): DocTree<B>;}unAnnotate
Remove all annotations from a DocTree.
Signature
declare const unAnnotate: <A>(self: DocTree<A>) => DocTree<never>Constructors
annotation
Annotate the specified DocTree with an annotation of type A.
Signature
declare const annotation: { <A>(annotation: A): <B>(self: DocTree<B>) => DocTree<A | B>; <A, B>(self: DocTree<A>, annotation: B): DocTree<A | B>;}Signature
declare const char: <A>(char: string) => DocTree<A>Horizontally concatenates multiple DocTrees.
Signature
declare const concat: <A>(trees: ReadonlyArray<DocTree<A>>) => DocTree<A>Signature
declare const empty: DocTree<never>Signature
declare const line: <A>(indentation: number) => DocTree<A>Signature
declare const text: <A>(text: string) => DocTree<A>Conversions
Folding
Instances
Signature
declare const Covariant: covariant.Covariant<DocTree.TypeLambda>Signature
declare const getMonoid: <A>(_: void) => monoid.Monoid<DocTree<A>>getSemigroup
Signature
declare const getSemigroup: <A>(_: void) => semigroup.Semigroup<DocTree<A>>Signature
declare const Invariant: invariant.Invariant<DocTree.TypeLambda>Model
AnnotationTree interface
Signature
interface AnnotationTree<A> extends Variance<A> { readonly _tag: "AnnotationTree"; readonly annotation: A; readonly tree: DocTree<A>;}Signature
interface CharTree<A> extends Variance<A> { readonly _tag: "CharTree"; readonly char: string;}ConcatTree interface
Signature
interface ConcatTree<A> extends Variance<A> { readonly _tag: "ConcatTree"; readonly trees: readonly Array<DocTree<A>>;}Represents a document that has been laid out into a tree-like structure.
A DocStream is a linked list of different annotated cons cells (i.e.
TextStream and then some further DocStream, LineStream and then some
further DocStream, etc.). The DocStream format is quite suitable as a
target for a layout engine, but is not suitable for rendering to a more
structured format, such as HTML, where we do not want to perform a lookahead
until the end of some pre-defined markup. These formats would benefit more
from a tree-like structure that explicitly marks its contents as annotated.
A DocTree is therefore much more suitable for this use case.
Signature
type DocTree<A> = EmptyTree<A> | CharTree<A> | TextTree<A> | LineTree<A> | AnnotationTree<A> | ConcatTree<A>DocTreeTypeLambda interface
Signature
interface DocTreeTypeLambda extends TypeLambda { readonly type: DocTree<unknown>;}Signature
interface EmptyTree<A> extends Variance<A> { readonly _tag: "EmptyTree";}Signature
interface LineTree<A> extends Variance<A> { readonly _tag: "LineTree"; readonly indentation: number;}Signature
interface TextTree<A> extends Variance<A> { readonly _tag: "TextTree"; readonly text: string;}Other
Refinements
isAnnotationTree
Returns true if the specified DocTree is an AnnotationTree, false otherwise.
Signature
declare const isAnnotationTree: <A>(self: DocTree<A>) => self is AnnotationTree<A>isCharTree
Returns true if the specified DocTree is an CharTree, false otherwise.
Signature
declare const isCharTree: <A>(self: DocTree<A>) => self is CharTree<A>isConcatTree
Returns true if the specified DocTree is an ConcatTree, false otherwise.
Signature
declare const isConcatTree: <A>(self: DocTree<A>) => self is ConcatTree<A>Returns true if the specified value is a DocTree, false otherwise.
Signature
declare const isDocTree: (u: unknown) => u is DocTree<unknown>isEmptyTree
Returns true if the specified DocTree is an EmptyTree, false otherwise.
Signature
declare const isEmptyTree: <A>(self: DocTree<A>) => self is EmptyTree<A>isLineTree
Returns true if the specified DocTree is an LineTree, false otherwise.
Signature
declare const isLineTree: <A>(self: DocTree<A>) => self is LineTree<A>isTextTree
Returns true if the specified DocTree is an TextTree, false otherwise.
Signature
declare const isTextTree: <A>(self: DocTree<A>) => self is TextTree<A>Rendering
renderSimplyDecorated
The simplest possible tree-based renderer.
For example, here is a document annotated with void and thee behavior is
to surround annotated regions with »>>>« and »<<<«.
Signature
declare const renderSimplyDecorated: { <A, M>(M: Monoid<M>, renderText: (text: string) => M, renderAnnotation: (annotation: A, out: M) => M): (self: DocTree<A>) => M; <A, M>(self: DocTree<A>, M: Monoid<M>, renderText: (text: string) => M, renderAnnotation: (annotation: A, out: M) => M): M;}Example
import * as assert from "node:assert"import * as Doc from "@effect/printer/Doc"import * as DocTree from "@effect/printer/DocTree"import * as Layout from "@effect/printer/Layout"import { identity, pipe } from "effect/Function"import * as String from "@effect/typeclass/data/String"
const doc: Doc.Doc<void> = Doc.hsep([ Doc.text("hello"), pipe( Doc.text("world"), Doc.annotate(undefined), Doc.cat(Doc.char("!")) )])
const tree = DocTree.treeForm(Layout.pretty(Layout.defaultOptions)(doc))
const rendered = pipe( tree, DocTree.renderSimplyDecorated(String.Monoid, identity, (_, x) => `>>>${x}<<<`))
assert.strictEqual( rendered, "hello >>>world<<<!")Symbol
DocTreeTypeId
Signature
declare const DocTreeTypeId: unique symbolDocTreeTypeId type
Signature
type DocTreeTypeId = typeof DocTreeTypeId