@informalsystems/quint
Version:
Core tool for the Quint specification language
132 lines (131 loc) • 4.39 kB
TypeScript
/**
* Layout a document. This is the end function that produces produces a string
* for the best document layout.
*
* @param maxWidth the maximum width
* @param firstColumn the column to use as the starting position
* @param doc the document to format
* @returns a resulting string
*/
export declare const format: (maxWidth: number, firstColumn: number, doc: Doc) => string;
/**
* A string-like object that returns its width in columns and its string
* representation. Note that `length` and `toString().length` do not have to be
* equal. For instance, this happens when the string representation contains
* ANSI characters that encode text colors.
*
* Note that strings, e.g., `"abc"`, implement the `StringLike` interface.
*/
export interface StringLike {
/**
* The number of characters printed on the screen.
*/
length: number;
/**
* Get the string representation of the text to print, which may be longer
* than `length`.
*
* @returns the string representation
*/
toString: () => string;
}
/**
* The document data type that is generalized by a StringLike implementation.
* This is input of the layout algorithm.
*/
export type Doc = {
kind: 'text';
text: StringLike;
} | {
kind: 'line';
linefeed: StringLike;
space: StringLike;
} | {
kind: 'nest';
indent: StringLike;
child: Doc;
} | {
kind: 'group';
child: Doc;
} | Doc[];
/**
* Create a document that carries an indivisible piece of text
* @param text a string-like text
* @returns a new document that contains the text
*/
export declare const text: (text: StringLike) => Doc;
/**
* Create a document that carries decorated text, e.g., adding color or emphasis.
* It is your responsibility to make sure that the number of columns occupied by
* `decorator(s)` equals to the number of columns occupied by `s`.
* For example, adding ANSI color does not affect the space, but it changes
* string length.
*
* @param decorator a function that decorates plain text
* @param s the string to decorate
* @returns a string-like object
*/
export declare const richtext: (decorator: (s: string) => string, s: string) => Doc;
/**
* A convenience operator that wraps all strings with `text(...)` while keeping
* all documents untouched.
*
* @param ds an array of documents and strings
* @returns an array of documents, in which all strings are wrapped with `text(...)`
*/
export declare const textify: (ds: (Doc | string)[]) => Doc[];
/**
* Create an optional line feed.
* @param lf the text to use as a line feed
* @param space the text to use a space
* @returns a new document that represents a line break
*/
export declare const line: (lf?: StringLike, space?: StringLike) => Doc;
/**
* A potential line break that is either a line feed, or an empty string.
*/
export declare const linebreak: Doc;
/**
* Simply a non-breaking space, that is, `' '`.
*/
export declare const space: Doc;
/**
* Create a document that introduces `indent` spaces after every line break.
* @param indent the text to use for indentation, e.g., a few spaces
* @param doc the document to decorate with indentation
* @returns a new document that decorates doc
*/
export declare const nest: (indent: StringLike, doc: Doc) => Doc;
/**
* Create a group document. If all of its children fit on a single line,
* it should turn all line breaks into spaces. Otherwise, the group stacks
* its non-group children vertically.
* @param doc the document to group
* @returns a new document that groups doc
*/
export declare const group: (child: Doc) => Doc;
/**
* Join documents with a separator.
* @param separator the separator to use
* @param docs documents to join
* @returns a new document that contains the elements joined with the separator
*/
export declare const docJoin: (separator: Doc, docs: Doc[]) => Doc;
/**
* Enclose a document in braces `{ ... }`
* @param doc the document to enclose
* @returns the document `{doc}`
*/
export declare const braces: (doc: Doc) => Doc;
/**
* Enclose a document in parentheses `( ... )`
* @param doc the document to enclose
* @returns the document `(doc)`
*/
export declare const parens: (doc: Doc) => Doc;
/**
* Enclose a document in brackets `[ ... ]`
* @param doc the document to enclose
* @returns the document `[doc]`
*/
export declare const brackets: (doc: Doc) => Doc;