UNPKG

@colin_t/lezer-tree-visualizer

Version:
292 lines (284 loc) 8.85 kB
import { SyntaxNode, TreeCursor } from 'lezer-tree'; /** * Default terminal colors (4-bit) and helper functions. */ declare const Color: { readonly Black: ColorType4bit; readonly DarkRed: ColorType4bit; readonly DarkGreen: ColorType4bit; readonly DarkYellow: ColorType4bit; readonly DarkBlue: ColorType4bit; readonly DarkMagenta: ColorType4bit; readonly DarkCyan: ColorType4bit; readonly LightGray: ColorType4bit; readonly DarkGray: ColorType4bit; readonly Red: ColorType4bit; readonly Green: ColorType4bit; readonly Yellow: ColorType4bit; readonly Blue: ColorType4bit; readonly Magenta: ColorType4bit; readonly BrightCyan: ColorType4bit; readonly White: ColorType4bit; readonly vsc: { readonly Black: ColorType; readonly DarkRed: ColorType; readonly DarkGreen: ColorType; readonly DarkYellow: ColorType; readonly DarkBlue: ColorType; readonly DarkMagenta: ColorType; readonly DarkCyan: ColorType; readonly LightGray: ColorType; readonly DarkGray: ColorType; readonly Red: ColorType; readonly Green: ColorType; readonly Yellow: ColorType; readonly Blue: ColorType; readonly Magenta: ColorType; readonly BrightCyan: ColorType; readonly White: ColorType; }; /** * @param color Any color to convert to RGB. * @returns The color in RGB format. */ readonly toRGB: (color: ColorType) => RGB; /** * @see {@link https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit} * @param color4bit 4-bit input color. * @returns 3-bit output color and the lost bright color bit. */ readonly to3bit: (color4bit: ColorType4bit) => [ColorType3bit, boolean]; /** * Compares two colors of the same color format. If the color format is * different, eg. 4-bit and 8-bit, `false` is always returned. * @param a First color. * @param b Second color. * @returns Whether both colors are equal. */ readonly fastEquals: (a: ColorType | null, b: ColorType | null) => boolean; }; /** * Type of a 3-bit color (7 colors). * @see {@link https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit} */ declare type ColorType3bit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7; /** * Type of a 4-bit color (7 colors with a brightness bit). * @see {@link https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit} */ declare type ColorType4bit = ColorType3bit | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15; /** * Type of an 8-bit color. * @see {@link https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit} */ declare type ColorType8bit = [5, number]; /** * Type of a 24-bit RGB color. */ declare type RGB = [number, number, number]; declare type ColorType = ColorType4bit | ColorType8bit | { rgb: RGB; }; /** * Used by the {@link Writer} to store the currently selected colors. */ declare type ColorState = { bg: ColorType | null; fg: ColorType | null; update_fg: boolean; update_bg: boolean; }; interface IWriter { /** * Writes a string to the internal buffer. * @sealed * @param str String to write. */ push(str: string): void; /** * Sets the foreground color. * @param color New color. */ fg(color: ColorType): void; /** * Sets the background color. * @param color New color. */ bg(color: ColorType): void; /** * Resets the current color to the default. * @param fg Foreground color. * @param bg Background color. */ resetColor(fg?: boolean, bg?: boolean): void; /** * Prints the formatted content of this writer in the console. */ print(): void; /** * Clears the internal buffer but keeps the current colors. */ clear(): void; /** * @returns The text content of this writer. */ toString(): string; } /** * A helper class for writing formatted strings. */ declare class Writer implements IWriter { protected buffer: string; protected color: ColorState; /** * Writes a color marker to the internal buffer. * @virtual */ protected pushColor(): void; /** @inheritdoc IWriter.push */ push(str: string): void; /** @inheritdoc IWriter.fg */ fg(color: ColorType): void; /** @inheritdoc IWriter.bg */ bg(color: ColorType): void; /** @inheritdoc IWriter.resetColor */ resetColor(fg?: boolean, bg?: boolean): void; /** @inheritdoc IWriter.print */ print(): void; /** @inheritdoc IWriter.clear */ clear(): void; /** @inheritdoc IWriter.toString */ toString(): string; } interface Theme { name: ColorType; source: ColorType; tree: ColorType; blockQuotes: ColorType; blockReturn: ColorType; colon: ColorType; ellipsis: ColorType; } /** * The default color theme for terminals. */ declare const defaultOsTheme: Theme; /** * The default color theme for web consoles. */ declare const defaultWebTheme: Theme; /** * The default color theme for the current platform. */ declare const defaultTheme: Theme; /** * An object that can be used as a source must have the `substring` method. */ interface Source { substring(from: number, to: number): string; } /** * @see {@link Options.filter} * @returns One of the following values: * * |Value |Description | * |-----------|--------------------------------------------------------------| * |`Hidden` |The node is hidden | * |`Shown` |The node is visible | * |`Collapsed`|Only the node name is visible, the rest remains as an ellipsis| */ declare type FilterFunction = (node: SyntaxNode) => VisibilityModifierValue; /** * @see {@link Options.FilterFunction} */ declare const VisibilityModifier: { readonly Hidden: 0; readonly Shown: 1; readonly Collapsed: 2; }; /** * @see {@link Options.FilterFunction} */ declare type VisibilityModifierValue = typeof VisibilityModifier[keyof typeof VisibilityModifier]; interface Options { /** * Whether to use console colors. * @defaultValue false */ colors?: boolean; /** * Whether to log using only one call to `console.log` or one call for * each line. This option often looks very ugly in the browser, but it might * improve performance and memory usage for very large trees. * @defaultValue false */ lineByLine?: boolean; /** * A function by which nodes are filtered. * @param node The node to be filtered. */ filter?: FilterFunction; /** * Replaces the standard writer. The {@link Options.colors} option is * ignored if set. */ writer?: IWriter; /** * Overrides the {@link defaultTheme | default theme}. */ theme?: Theme; } /** * Visualizes a syntax tree (or node) by printing it in the console. * @param cursor Lezer tree cursor. * @param src Source code of the tree. * @param colors */ declare function visualize(cursor: TreeCursor, src: Source, options?: Options): void; /** * Writes to the console with ANSI escape codes to support terminal colors. * @see {@link https://en.wikipedia.org/wiki/ANSI_escape_code#Colors} */ declare class OsColorWriter extends Writer { /** * @inheritdoc IWriter.push * @override */ push(str: string): void; /** * @inheritdoc IWriter.pushColor * @override */ protected pushColor(): void; /** * @inheritdoc IWriter.print * @override */ print(): void; } /** * Writes to the console with CSS format specifiers to support web console colors. */ declare class WebColorWriter extends Writer { /** * CSS Styles appended to the `console.log` call. */ protected styles: string[]; /** * @inheritdoc IWriter.pushColor * @override */ protected pushColor(): void; /** * @inheritdoc IWriter.print * @override */ print(): void; /** * @inheritdoc IWriter.clear * @override */ clear(): void; } declare const ColorWriter: typeof OsColorWriter | typeof WebColorWriter; export { Color, ColorState, ColorType, ColorType3bit, ColorType4bit, ColorType8bit, ColorWriter, FilterFunction, IWriter, Options, OsColorWriter, RGB, Source, Theme, VisibilityModifier, VisibilityModifierValue, WebColorWriter, Writer, defaultOsTheme, defaultTheme, defaultWebTheme, visualize };