# code/editor/patch-parse (zidane-ui/code/editor)

Import: `import { … } from 'zidane-ui/code/editor'`. Source: [ui/src/code/editor/patch-parse.ts](https://github.com/Tahul/zidane/blob/main/ui/src/code/editor/patch-parse.ts).

## Functions

### `createPatchParser`

Function.

A parser that runs `parseWorkingTree` in a worker (one, made on first use), so a burst of re-reads on a
repository with a large diff never parses megabytes on the main thread. Without `createWorker`, or once the
worker failed (a load error, a crash), it parses on this thread: what a dead worker held is settled there.

The worker is an app's module that answers each `ParseRequest` with `parseWorkingTree(request)`.

```ts
function createPatchParser(createWorker: (() => Worker) | null): PatchParser
```

**Parameters**

- `createWorker` (`(() => Worker) | null`): Makes the worker; `null`: parse on this thread.

**Returns** `PatchParser`

Source: [ui/src/code/editor/patch-parse.ts:141](https://github.com/Tahul/zidane/blob/main/ui/src/code/editor/patch-parse.ts#L141)

### `hashText`

Function.

FNV-1a, 32 bits, as hex: fast, and collisions only cost a reused parse of the same path.

```ts
function hashText(text: string): string
```

**Parameters**

- `text` (`string`): The text to hash.

**Returns** `string`

Source: [ui/src/code/editor/patch-parse.ts:23](https://github.com/Tahul/zidane/blob/main/ui/src/code/editor/patch-parse.ts#L23)

### `parseFilePatch`

Function.

Parses a one-file patch (a file's working-tree diff); `undefined` when it holds no file.

```ts
function parseFilePatch(patch: string, cacheKey: string): FileDiffMetadata | undefined
```

**Parameters**

- `patch` (`string`): A unified patch of one file.
- `cacheKey` (`string`): Prefix of pierre's cache keys for it.

**Returns** `FileDiffMetadata | undefined`

Source: [ui/src/code/editor/patch-parse.ts:125](https://github.com/Tahul/zidane/blob/main/ui/src/code/editor/patch-parse.ts#L125)

### `parseSection`

Function.

Parse one file section; null when pierre can't read it (that file is left out, the others still show).

```ts
function parseSection(key: string, text: string): FileDiffMetadata | null
```

**Parameters**

- `key` (`string`): The section's content key (pierre's cache key).
- `text` (`string`): The section.

**Returns** `FileDiffMetadata | null`

Source: [ui/src/code/editor/patch-parse.ts:78](https://github.com/Tahul/zidane/blob/main/ui/src/code/editor/patch-parse.ts#L78)

### `parseWorkingTree`

Function.

Parses the sections of a patch the caller doesn't hold yet. What a parser worker runs on each message.

```ts
function parseWorkingTree(req: ParseRequest): ParseResponse
```

**Parameters**

- `req` (`ParseRequest`): The patch and the keys already held.

**Returns** `ParseResponse`

Source: [ui/src/code/editor/patch-parse.ts:109](https://github.com/Tahul/zidane/blob/main/ui/src/code/editor/patch-parse.ts#L109)

### `sectionKeys`

Function.

The file sections of a patch with their content keys.

```ts
function sectionKeys(patch: string): SectionKey[]
```

**Parameters**

- `patch` (`string`): A unified patch of any number of files.

**Returns** `SectionKey[]`

Source: [ui/src/code/editor/patch-parse.ts:62](https://github.com/Tahul/zidane/blob/main/ui/src/code/editor/patch-parse.ts#L62)

### `splitPatch`

Function.

Split a multi-file patch at its `diff --git` headers (text before the first one is dropped).

```ts
function splitPatch(patch: string): string[]
```

**Parameters**

- `patch` (`string`): A unified patch of any number of files.

**Returns** `string[]`

Source: [ui/src/code/editor/patch-parse.ts:37](https://github.com/Tahul/zidane/blob/main/ui/src/code/editor/patch-parse.ts#L37)

## Interfaces

### `ParseRequest`

Interface.

What a parser worker receives.

```ts
interface ParseRequest
```

**Properties**

- `id` (`number`): Matches the response to the request.
- `known` (`string[]`): Sections the caller already holds parsed: sent back as keys only.
- `patch` (`string`): The working-tree patch.

Source: [ui/src/code/editor/patch-parse.ts:87](https://github.com/Tahul/zidane/blob/main/ui/src/code/editor/patch-parse.ts#L87)

### `ParseResponse`

Interface.

What a parser worker answers.

```ts
interface ParseResponse
```

**Properties**

- `files` (`{ file?: FileDiffMetadata | null; key: string }[]`): In patch order: a key the caller knows, or a freshly parsed file (null when unreadable).
- `id` (`number`): The request's id.

Source: [ui/src/code/editor/patch-parse.ts:97](https://github.com/Tahul/zidane/blob/main/ui/src/code/editor/patch-parse.ts#L97)

### `SectionKey`

Interface.

One file section of a patch, keyed by its content.

```ts
interface SectionKey
```

**Properties**

- `key` (`string`): Content hash plus the section's index among equal hashes (two identical sections stay distinct).
- `text` (`string`): The section's text.

Source: [ui/src/code/editor/patch-parse.ts:50](https://github.com/Tahul/zidane/blob/main/ui/src/code/editor/patch-parse.ts#L50)

## Types

### `PatchParser`

Type.

Parses a working-tree patch, the sections it doesn't hold yet only.

```ts
type PatchParser = (patch: string, known: string[]) => Promise<ParseResponse>
```

Source: [ui/src/code/editor/patch-parse.ts:130](https://github.com/Tahul/zidane/blob/main/ui/src/code/editor/patch-parse.ts#L130)
