/** * A span of highlighted text with UTF-8 byte offsets. * * This is the native format from tree-sitter. Use this when working with * Rust code that needs to slice strings with `&source[start..end]`. * * For JavaScript string operations, use {@link Utf16Span} instead. * * @internal */ export interface Utf8Span { /** UTF-8 byte offset where the span starts (inclusive) */ start: number; /** UTF-8 byte offset where the span ends (exclusive) */ end: number; /** The capture name (e.g., "keyword", "string", "comment") */ capture: string; } /** * A language injection with UTF-8 byte offsets. * * @internal */ export interface Utf8Injection { /** UTF-8 byte offset where the injection starts (inclusive) */ start: number; /** UTF-8 byte offset where the injection ends (exclusive) */ end: number; language: string; includeChildren: boolean; } /** * Result of parsing source code, with UTF-8 byte offsets. * * @internal */ export interface Utf8ParseResult { spans: Utf8Span[]; injections: Utf8Injection[]; } /** * A span of highlighted text with UTF-16 code unit indices. * * The `start` and `end` fields are compatible with JavaScript string APIs * like `String.prototype.slice()`, `substring()`, and DOM `Range`. * * @example * ```ts * const result = grammar.parse(source); * for (const span of result.spans) { * const text = source.slice(span.start, span.end); * console.log(`${span.capture}: ${text}`); * } * ``` */ export interface Utf16Span { /** UTF-16 code unit index where the span starts (inclusive) */ start: number; /** UTF-16 code unit index where the span ends (exclusive) */ end: number; /** The capture name (e.g., "keyword", "string", "comment") */ capture: string; } /** * A language injection (e.g., JS inside HTML) with UTF-16 code unit indices. */ export interface Utf16Injection { /** UTF-16 code unit index where the injection starts (inclusive) */ start: number; /** UTF-16 code unit index where the injection ends (exclusive) */ end: number; language: string; includeChildren: boolean; } /** * Result of parsing source code, with UTF-16 code unit indices. * * This is the format returned by the public API (`grammar.parse()`, * `session.parse()`). Offsets are compatible with JavaScript string operations. */ export interface Utf16ParseResult { spans: Utf16Span[]; injections: Utf16Injection[]; } /** * @deprecated Use {@link Utf16Span} for JavaScript or {@link Utf8Span} for Rust interop. */ export type Span = Utf16Span; /** * @deprecated Use {@link Utf16Injection} for JavaScript or {@link Utf8Injection} for Rust interop. */ export type Injection = Utf16Injection; /** * @deprecated Use {@link Utf16ParseResult} for JavaScript or {@link Utf8ParseResult} for Rust interop. */ export type ParseResult = Utf16ParseResult; /** * An incremental edit, in the shape tree-sitter needs: UTF-8 byte offsets plus * row/column points, for both the old and new end of the changed range. Field * names are snake_case to match the plugin's wire format. */ export interface Edit { start_byte: number; old_end_byte: number; new_end_byte: number; start_row: number; start_col: number; old_end_row: number; old_end_col: number; new_end_row: number; new_end_col: number; } /** * A parsing session for incremental highlighting. * * Sessions allow you to reuse the parser state between parses, which is more * efficient than creating a new session for each parse. This is useful for * editors where text changes frequently. * * Usage pattern: * - Call `setText(newText)` whenever the text changes. * - Then call `parse()` to parse the current text and get results. * * Example: * ```ts * const session = grammar.createSession(); * session.setText("let x = 1;"); * let result = session.parse(); * // ... user edits text ... * session.setText("let x = 42;"); * result = session.parse(); * session.free(); * ``` */ export interface Session { /** Set the text to parse (full replace; resets the parse tree). */ setText(text: string): void; /** * Apply an incremental edit and re-parse, reusing the unchanged parts of the * previous tree. This is what makes editor highlighting fast — prefer it over * `setText` for keystroke-sized changes. * * `newText` is the full document text *after* the edit; `edit` locates the * change. Returns the updated spans/injections (UTF-16 offsets). */ applyEdit(newText: string, edit: Edit): Utf16ParseResult; /** Parse the current text and return spans/injections with UTF-16 offsets */ parse(): Utf16ParseResult; /** Cancel any in-progress parsing */ cancel(): void; /** * Free the session resources. Must be called when done to prevent memory leaks. * Failure to call free() will result in WASM memory not being released. */ free(): void; } /** A loaded grammar plugin */ export interface Grammar { /** The language identifier */ languageId(): string; /** Languages this grammar may inject */ injectionLanguages(): string[]; /** Highlight source code, returning HTML string */ highlight(source: string): string | Promise; /** Parse source code, returning spans with UTF-16 offsets (creates a one-shot session internally) */ parse(source: string): Utf16ParseResult; /** Create a session for incremental parsing */ createSession(): Session; /** Dispose of resources */ dispose(): void; } /** A highlighting tag */ export interface Highlight { /** Long name, used in `Span` */ name: string; /** Short name, used in HTML tags and CSS variables */ tag: string; /** Parent tag for fallback */ parentTag?: string; } type MaybePromise = T | Promise; export interface ResolveArgs { /** Base URL derived from language, CDN and version */ baseUrl: string; /** Relative path in the module to load */ path: string; } export interface LanguageResolveArgs extends ResolveArgs { /** Language to load the grammar plugin for */ language: string; /** Package version used when building the default CDN URL */ version: string; } /** Configuration for the arborium runtime */ export interface ArboriumConfig { /** Disable auto-highlighting on page load */ manual?: boolean; /** Theme to use: "tokyo-night" | "github-light" | custom */ theme?: string; /** CSS selector for code blocks */ selector?: string; /** CDN to use: "jsdelivr" | "unpkg" | custom base URL */ cdn?: string; /** Package version to load (default: "1" for latest 1.x.x) */ version?: string; /** URL to plugins.json manifest - overrides bundled manifest (for local testing) */ pluginsUrl?: string; /** Base URL for the Rust host module (for local testing) */ hostUrl?: string; /** Object to use for logging (default: global.console) */ logger?: Pick; /** Custom host resolution for JS */ resolveHostJs?(args: ResolveArgs): MaybePromise; /** Custom host resolution for WASM */ resolveHostWasm?(args: ResolveArgs): MaybePromise; /** Custom grammar resolution for JS */ resolveJs?(args: LanguageResolveArgs): MaybePromise; /** Custom grammar resolution for WASM */ resolveWasm?(args: LanguageResolveArgs): MaybePromise; } /** Global config set before script loads */ declare global { interface Window { Arborium?: ArboriumConfig; } } export {};