tuil
ReferencePackages@mwillbanks/tuil-codeAPI

TreeSitterCodeParser

class exported by @mwillbanks/tuil-code.

View rawEdit

class

Public class exported by @mwillbanks/tuil-code.

export class TreeSitterCodeParser implements CodeParser {
  readonly id: string;
  readonly languages: readonly string[];
  readonly #engine: IncrementalSyntaxEngine;
  readonly #trees = new WeakMap<CodeParseResult, IncrementalSyntaxTree>();
  #version = 0;

  constructor(
    id: string,
    languages: readonly string[],
    engine: IncrementalSyntaxEngine,
  ) {
    this.id = id;
    this.languages = Object.freeze([...languages]);
    this.#engine = engine;
  }

  async parse(
    source: string,
    options: {
      readonly previous?: CodeParseResult;
      readonly signal: AbortSignal;
    },
  ): Promise<CodeParseResult> {
    if (options.signal.aborted) throw options.signal.reason;
    const tree = await this.#engine.parse(
      source,
      options.previous ? this.#trees.get(options.previous) : undefined,
      options.signal,
    );
    if (options.signal.aborted) throw options.signal.reason;
    const nodes = tree.nodes;
    const result = Object.freeze({
      language: this.languages[0] ?? "text",
      version: ++this.#version,
      spans: Object.freeze(
        nodes.map((node) => ({
          start: node.start,
          end: node.end,
          kind: node.type,
        })),
      ),
      folds: Object.freeze([
        ...new Map(
          nodes.flatMap((node) => {
            if (
              node.startLine === undefined ||
              node.endLine === undefined ||
              node.endLine <= node.startLine
            ) {
              return [];
            }
            return [
              [
                `${node.startLine}:${node.endLine}`,
                {
                  startLine: node.startLine,
                  endLine: node.endLine,
                },
              ] as const,
            ];
          }),
        ).values(),
      ]),
      diagnostics: Object.freeze(
        nodes
          .filter(
            (
              node,
            ): node is IncrementalSyntaxNode & {
              readonly diagnostic: NonNullable<
                IncrementalSyntaxNode["diagnostic"]
              >;
            } => Boolean(node.diagnostic),
          )
          .map((node) => ({
            start: node.start,
            end: node.end,
            kind: node.type,
            ...node.diagnostic,
          })),
      ),
      incremental: tree.incremental,
    });
    this.#trees.set(result, tree);
    return result;
  }

  dispose(): void {
    this.#engine.dispose?.();
  }
}

Members

MemberTypeRequiredDescriptionRelated types
idstringYesThe id member uses the string contract.
languagesreadonly string[]YesThe languages member uses the readonly string[] contract.
#engineIncrementalSyntaxEngineYesThe #engine member uses the IncrementalSyntaxEngine contract.IncrementalSyntaxEngine
#treesWeakMap<CodeParseResult, IncrementalSyntaxTree>YesThe #trees member uses the WeakMap<CodeParseResult, IncrementalSyntaxTree> contract.CodeParseResult, IncrementalSyntaxTree
#versionnumberYesThe #version member uses the number contract.
__constructoranyYesThe __constructor member uses the any contract.
parse(source: string, options: &#123; readonly previous?: CodeParseResult; readonly signal: AbortSignal; &#125;) => Promise<CodeParseResult>YesThe parse member uses the (source: string, options: &#123; readonly previous?: CodeParseResult; readonly signal: AbortSignal; &#125;) => Promise<CodeParseResult> contract.CodeParseResult
dispose() => voidYesThe dispose member uses the () => void contract.

Parameters

This declaration has no public members.

Returns

This declaration does not return a value.

Throws

No thrown errors are documented for this declaration.

Source

View the secondary source reference

Package

@mwillbanks/tuil-code

On this page