Skip to content

DocTree

34 exports Added in v1.0.0 Source

Annotations

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

Added in v1.0.0 Source

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

Added in v1.0.0 Source

Remove all annotations from a DocTree.

Signature

declare const unAnnotate: <A>(self: DocTree<A>) => DocTree<never>;

Constructors

annotation

Added in v1.0.0 Source

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>;
};

char

Added in v1.0.0 Source

Signature

declare const char: <A>(char: string) => DocTree<A>;

concat

Added in v1.0.0 Source

Horizontally concatenates multiple DocTrees.

Signature

declare const concat: <A>(trees: ReadonlyArray<DocTree<A>>) => DocTree<A>;

empty

Added in v1.0.0 Source

Signature

declare const empty: DocTree<never>;

line

Added in v1.0.0 Source

Signature

declare const line: <A>(indentation: number) => DocTree<A>;

text

Added in v1.0.0 Source

Signature

declare const text: <A>(text: string) => DocTree<A>;

Conversions

treeForm

Added in v1.0.0 Source

Converts a DocStream<A> into a DocTree<A>.

Signature

declare const treeForm: <A>(stream: DocStream.DocStream<A>) => DocTree<A>;

Folding

foldMap

Added in v1.0.0 Source

Signature

declare const foldMap: {
  <A, M>(M: Monoid<M>, f: (a: A) => M): (self: DocTree<A>) => M;
  <A, M>(self: DocTree<A>, M: Monoid<M>, f: (a: A) => M): M;
};

Instances

Covariant

Added in v1.0.0 Source

Signature

declare const Covariant: covariant.Covariant<DocTree.TypeLambda>;

getMonoid

Added in v1.0.0 Source

Signature

declare const getMonoid: <A>(_: void) => monoid.Monoid<DocTree<A>>;

getSemigroup

Added in v1.0.0 Source

Signature

declare const getSemigroup: <A>(_: void) => semigroup.Semigroup<DocTree<A>>;

Invariant

Added in v1.0.0 Source

Signature

declare const Invariant: invariant.Invariant<DocTree.TypeLambda>;

Model

AnnotationTree interface

Added in v1.0.0 Source

Signature

interface AnnotationTree<A> extends Variance<A> {
  readonly _tag: "AnnotationTree";
  readonly annotation: A;
  readonly tree: DocTree<A>;
}

CharTree interface

Added in v1.0.0 Source

Signature

interface CharTree<A> extends Variance<A> {
  readonly _tag: "CharTree";
  readonly char: string;
}

ConcatTree interface

Added in v1.0.0 Source

Signature

interface ConcatTree<A> extends Variance<A> {
  readonly _tag: "ConcatTree";
  readonly trees: readonly Array<DocTree<A>>;
}

DocTree type

Added in v1.0.0 Source

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

Added in v1.0.0 Source

Signature

interface DocTreeTypeLambda extends TypeLambda {
  readonly type: DocTree<unknown>;
}

EmptyTree interface

Added in v1.0.0 Source

Signature

interface EmptyTree<A> extends Variance<A> {
  readonly _tag: "EmptyTree";
}

LineTree interface

Added in v1.0.0 Source

Signature

interface LineTree<A> extends Variance<A> {
  readonly _tag: "LineTree";
  readonly indentation: number;
}

TextTree interface

Added in v1.0.0 Source

Signature

interface TextTree<A> extends Variance<A> {
  readonly _tag: "TextTree";
  readonly text: string;
}

Other

DocTree

Added in v1.0.0 Source

Refinements

Returns true if the specified DocTree is an AnnotationTree, false otherwise.

Signature

declare const isAnnotationTree: <A>(self: DocTree<A>) => self is AnnotationTree<A>;

isCharTree

Added in v1.0.0 Source

Returns true if the specified DocTree is an CharTree, false otherwise.

Signature

declare const isCharTree: <A>(self: DocTree<A>) => self is CharTree<A>;

isConcatTree

Added in v1.0.0 Source

Returns true if the specified DocTree is an ConcatTree, false otherwise.

Signature

declare const isConcatTree: <A>(self: DocTree<A>) => self is ConcatTree<A>;

isDocTree

Added in v1.0.0 Source

Returns true if the specified value is a DocTree, false otherwise.

Signature

declare const isDocTree: (u: unknown) => u is DocTree<unknown>;

isEmptyTree

Added in v1.0.0 Source

Returns true if the specified DocTree is an EmptyTree, false otherwise.

Signature

declare const isEmptyTree: <A>(self: DocTree<A>) => self is EmptyTree<A>;

isLineTree

Added in v1.0.0 Source

Returns true if the specified DocTree is an LineTree, false otherwise.

Signature

declare const isLineTree: <A>(self: DocTree<A>) => self is LineTree<A>;

isTextTree

Added in v1.0.0 Source

Returns true if the specified DocTree is an TextTree, false otherwise.

Signature

declare const isTextTree: <A>(self: DocTree<A>) => self is TextTree<A>;

Rendering

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

Signature

declare const DocTreeTypeId: unique symbol;

DocTreeTypeId type

Added in v1.0.0 Source

Signature

type DocTreeTypeId = typeof DocTreeTypeId;