# TreeSitterCodeParser

Source: /tuil/docs/reference/packages/code/api/tree-sitter-code-parser
Locale: en

class exported by @mwillbanks/tuil-code.



{/* Generated by tooling/docs/generate-reference.ts. */}

## class [#class]

Public class exported by `@mwillbanks/tuil-code`.

```ts
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 [#members]

| Member          | Type                                                                                                                                      | Required | Description                                                                                                                                                                     | Related types                                                                                                                                                  |
| --------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id`            | `string`                                                                                                                                  | Yes      | The `id` member uses the `string` contract.                                                                                                                                     | —                                                                                                                                                              |
| `languages`     | `readonly string[]`                                                                                                                       | Yes      | The `languages` member uses the `readonly string[]` contract.                                                                                                                   | —                                                                                                                                                              |
| `#engine`       | `IncrementalSyntaxEngine`                                                                                                                 | Yes      | The `#engine` member uses the `IncrementalSyntaxEngine` contract.                                                                                                               | [`IncrementalSyntaxEngine`](/tuil/docs/reference/packages/code/api/incremental-syntax-engine)                                                                       |
| `#trees`        | `WeakMap<CodeParseResult, IncrementalSyntaxTree>`                                                                                         | Yes      | The `#trees` member uses the `WeakMap<CodeParseResult, IncrementalSyntaxTree>` contract.                                                                                        | [`CodeParseResult`](/tuil/docs/reference/packages/code/api/code-parse-result), [`IncrementalSyntaxTree`](/tuil/docs/reference/packages/code/api/incremental-syntax-tree) |
| `#version`      | `number`                                                                                                                                  | Yes      | The `#version` member uses the `number` contract.                                                                                                                               | —                                                                                                                                                              |
| `__constructor` | `any`                                                                                                                                     | Yes      | The `__constructor` member uses the `any` contract.                                                                                                                             | —                                                                                                                                                              |
| `parse`         | `(source: string, options: &#123; readonly previous?: CodeParseResult; readonly signal: AbortSignal; &#125;) => Promise<CodeParseResult>` | Yes      | The `parse` member uses the `(source: string, options: &#123; readonly previous?: CodeParseResult; readonly signal: AbortSignal; &#125;) => Promise<CodeParseResult>` contract. | [`CodeParseResult`](/tuil/docs/reference/packages/code/api/code-parse-result)                                                                                       |
| `dispose`       | `() => void`                                                                                                                              | Yes      | The `dispose` member uses the `() => void` contract.                                                                                                                            | —                                                                                                                                                              |

## Parameters [#parameters]

This declaration has no public members.

## Returns [#returns]

This declaration does not return a value.

## Throws [#throws]

No thrown errors are documented for this declaration.

## Related types [#related-types]

* [`CodeParseResult`](/tuil/docs/reference/packages/code/api/code-parse-result)
* [`CodeParser`](/tuil/docs/reference/packages/code/api/code-parser)
* [`IncrementalSyntaxEngine`](/tuil/docs/reference/packages/code/api/incremental-syntax-engine)
* [`IncrementalSyntaxNode`](/tuil/docs/reference/packages/code/api/incremental-syntax-node)
* [`IncrementalSyntaxTree`](/tuil/docs/reference/packages/code/api/incremental-syntax-tree)

## Source [#source]

[View the secondary source reference](https://github.com/mwillbanks/tuil/blob/main/packages/code/src/index.ts)

## Package [#package]

[@mwillbanks/tuil-code](/tuil/docs/reference/packages/code)
