@dou-ui/code 0.1.0-alpha.3 → 0.1.0-alpha.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/assets/default-BWWFJmzn.js.map +1 -0
  2. package/dist/code-diff.js.map +1 -1
  3. package/dist/code-diff.vue_vue_type_script_setup_true_lang.js +756 -63
  4. package/dist/code-diff.vue_vue_type_script_setup_true_lang.js.map +1 -1
  5. package/dist/code-editor.js.map +1 -1
  6. package/dist/code-editor.vue_vue_type_script_setup_true_lang.js +784 -474
  7. package/dist/code-editor.vue_vue_type_script_setup_true_lang.js.map +1 -1
  8. package/dist/extension.js +6 -1
  9. package/dist/extension.js.map +1 -1
  10. package/dist/index.js +3 -2
  11. package/dist/index.js.map +1 -1
  12. package/dist/language/highlighter.js +1 -1
  13. package/dist/language/highlighter.js.map +1 -1
  14. package/dist/language/language.js +24 -2
  15. package/dist/language/language.js.map +1 -1
  16. package/dist/language/remote-tokens.js +131 -0
  17. package/dist/language/remote-tokens.js.map +1 -0
  18. package/dist/language/worker-client.js +308 -33
  19. package/dist/language/worker-client.js.map +1 -1
  20. package/dist/language/worker-protocol.js.map +1 -1
  21. package/dist/shiki/index.js +54 -3
  22. package/dist/shiki/index.js.map +1 -1
  23. package/dist/style.css +1 -1
  24. package/dist/types/code-diff.vue.d.ts +18 -21
  25. package/dist/types/code-editor.vue.d.ts +3 -1
  26. package/dist/types/extension.d.ts +57 -5
  27. package/dist/types/index.d.ts +97 -21
  28. package/dist/types/language/language.d.ts +27 -0
  29. package/dist/types/language/remote-tokens.d.ts +38 -0
  30. package/dist/types/language/worker-client.d.ts +85 -15
  31. package/dist/types/language/worker-protocol.d.ts +53 -2
  32. package/dist/types/shiki/index.d.ts +36 -1
  33. package/dist/types/types.d.ts +166 -10
  34. package/dist/types/view/measure.d.ts +6 -0
  35. package/dist/types/worker/index.d.ts +27 -12
  36. package/dist/view/measure.js +23 -5
  37. package/dist/view/measure.js.map +1 -1
  38. package/dist/worker/default.js +1 -1
  39. package/dist/worker/index.js +280 -53
  40. package/dist/worker/index.js.map +1 -1
  41. package/package.json +3 -3
  42. package/dist/assets/default-BZBMZKf_.js.map +0 -1
@@ -45,6 +45,12 @@ export interface CodeLanguageSpec<S> {
45
45
  * ones the names are looked up in.
46
46
  */
47
47
  nested?: readonly CodeLanguage[];
48
+ /**
49
+ * How a highlighting worker transfers results for this language:
50
+ * - `'states'` (default): sends serialized state checkpoints so the page tokenizes visible lines.
51
+ * - `'tokens'`: keeps non-serializable states (such as Shiki's `StateStack`) in the worker and sends flat line token arrays directly.
52
+ */
53
+ workerMode?: 'states' | 'tokens';
48
54
  }
49
55
  /** A language: how to read its tokens, and a few facts for editing it. */
50
56
  export interface CodeLanguage<S = any> {
@@ -64,6 +70,7 @@ export interface CodeLanguage<S = any> {
64
70
  readonly indentAfter?: RegExp;
65
71
  languageAt(state: S): CodeLanguage | null;
66
72
  readonly nested: readonly CodeLanguage[];
73
+ readonly workerMode?: 'states' | 'tokens';
67
74
  }
68
75
  /** Copies arrays and plain objects; everything else (languages among them) is shared. */
69
76
  export declare function copyPlain<T>(value: T): T;
@@ -86,6 +93,26 @@ export declare function samePlain(a: unknown, b: unknown): boolean;
86
93
  * ```
87
94
  */
88
95
  export declare function defineCodeLanguage<S = Record<string, never>>(spec: CodeLanguageSpec<S>): CodeLanguage<S>;
96
+ export interface RemoteCodeLanguageOptions {
97
+ /** The language name resolved inside the highlighting worker (e.g. `'rust'`, `'go'`, `'proto'`). */
98
+ name: string;
99
+ aliases?: string[];
100
+ /** How the language writes comments, for toggling them. */
101
+ comment?: {
102
+ line?: string;
103
+ block?: [open: string, close: string];
104
+ };
105
+ brackets?: string;
106
+ autoClose?: string;
107
+ indentAfter?: RegExp;
108
+ }
109
+ /**
110
+ * Describes a language highlighted entirely inside a Web Worker (such as a
111
+ * Shiki grammar loaded in the worker), without loading its tokenizer or WASM
112
+ * on the main thread. Editing features (comments, bracket pairing, auto-close,
113
+ * indentation) still work on the page; without a worker it renders plain text.
114
+ */
115
+ export declare function remoteCodeLanguage(options: RemoteCodeLanguageOptions): CodeLanguage;
89
116
  /** The language among `languages` named `name` or one of its aliases, ignoring case. */
90
117
  export declare function languageFor(languages: readonly CodeLanguage[], name: string | undefined): CodeLanguage | null;
91
118
  /**
@@ -0,0 +1,38 @@
1
+ import type { ChangeSet } from '../core/changes';
2
+ import type { CodeText } from '../core/text';
3
+ import type { CodeLineTokens } from './language';
4
+ /** Compresses sorted 1-based line numbers into contiguous inclusive `[from, to]` intervals. */
5
+ export declare function compressLineRanges(numbers: readonly number[]): Array<[from: number, to: number]>;
6
+ /**
7
+ * Caches flat per-line tokens returned by a highlighting worker in `tokens`
8
+ * mode (e.g. Shiki grammars whose state lives only inside the worker).
9
+ *
10
+ * Lines before an edit stay fresh across document versions; lines after an edit
11
+ * shift by the line delta so the viewport keeps its colors without flickering
12
+ * while the worker recomputes the new version.
13
+ */
14
+ export declare class RemoteTokenStore {
15
+ private readonly maxSize;
16
+ /** 1-based line number → flat tokens, oldest first for LRU eviction. */
17
+ private tokens;
18
+ /** 1-based line number → version at which the cached tokens were produced. */
19
+ private versions;
20
+ private currentVersion;
21
+ private workerFrontier;
22
+ constructor(maxSize?: number);
23
+ get version(): number;
24
+ get frontier(): number;
25
+ /** Cached tokens for 1-based line `n`, or `null` if never received yet. */
26
+ line(n: number): CodeLineTokens | null;
27
+ /** Whether every line in 1-based inclusive `[from, to]` has tokens for the current version. */
28
+ readyRange(from: number, to: number): boolean;
29
+ /** Whether every 1-based line number in `numbers` has tokens for the current version. */
30
+ readyNumbers(numbers: readonly number[]): boolean;
31
+ /** Stores flat line tokens starting at 1-based line `from` from the worker. */
32
+ adopt(from: number, lines: readonly CodeLineTokens[], frontier: number): void;
33
+ /** Stores flat line tokens for multiple disjoint visible ranges returned by the worker. */
34
+ adoptRanges(ranges: ReadonlyArray<readonly [from: number, lines: readonly CodeLineTokens[]]>, frontier: number): void;
35
+ /** Shifts cached lines across an edit so unchanged regions avoid flicker while stale lines re-fetch. */
36
+ update(oldDoc: CodeText, nextDoc: CodeText, changes?: ChangeSet): void;
37
+ clear(): void;
38
+ }
@@ -1,11 +1,24 @@
1
1
  import type { ChangeSet } from '../core/changes';
2
+ import type { CodeDiffHunk, CodeDiffOptions } from '../core/diff';
2
3
  import type { CodeText } from '../core/text';
3
- import type { CodeLanguage } from './language';
4
+ import type { CodeLanguage, CodeLineTokens } from './language';
5
+ export declare function defaultPoolSize(): number;
6
+ export interface SyntaxWorkerHandlers {
7
+ states: (points: Array<[line: number, state: unknown]>) => void;
8
+ /** Flat per-line tokens returned in `tokens` mode (e.g. Shiki or non-serializable states). */
9
+ tokens?: (from: number, lines: readonly CodeLineTokens[], frontier: number, ranges?: ReadonlyArray<readonly [from: number, lines: readonly CodeLineTokens[]]>) => void;
10
+ /** The worker hasn't the language named `missing`, or can't highlight it (no `missing`). */
11
+ unsupported: (missing?: string) => void;
12
+ /** The worker could not run (it failed to load, or threw). */
13
+ failed: (error: unknown) => void;
14
+ /** Notifies a worker pool whether this client currently has an in-flight request. */
15
+ onBusyChange?: (busy: boolean) => void;
16
+ }
4
17
  /**
5
18
  * The editor's side of a highlighting worker (`serveCodeHighlighting`).
6
- * The text goes over the first time states far ahead are wanted (most
7
- * texts never need it), in pieces between other work; every change
8
- * follows it.
19
+ * The text goes over the first time syntax data is wanted, in pieces between
20
+ * other work; every change follows it. Newer viewport requests preempt older
21
+ * ones, and `cancel()` aborts pending work when scrolled out of view.
9
22
  */
10
23
  export declare class SyntaxWorkerClient {
11
24
  private readonly worker;
@@ -17,7 +30,12 @@ export declare class SyntaxWorkerClient {
17
30
  private readonly id;
18
31
  /** Changes sent so far; the worker's answers name how many it had taken in. */
19
32
  private version;
33
+ /** Monotonic request counter so newer viewport requests preempt stale ones. */
34
+ private seq;
20
35
  private asking;
36
+ private paused;
37
+ private busy;
38
+ private lastAsked;
21
39
  private closed;
22
40
  /** Sending the text: messages wait until it is all there. */
23
41
  private sending;
@@ -25,28 +43,80 @@ export declare class SyntaxWorkerClient {
25
43
  private queue;
26
44
  private timer;
27
45
  private readonly languages;
46
+ private readonly mode;
28
47
  constructor(worker: Worker, language: CodeLanguage,
29
48
  /** The text as it is now. */
30
- text: () => CodeText, tabSize: number, handlers: {
31
- states: (points: Array<[line: number, state: unknown]>) => void;
32
- /** The worker hasn't the language named `missing`, or can't send its states (no `missing`). */
33
- unsupported: (missing?: string) => void;
34
- /** The worker could not run (it failed to load, or threw). */
35
- failed: (error: unknown) => void;
36
- });
49
+ text: () => CodeText, tabSize: number, handlers: SyntaxWorkerHandlers);
50
+ private setBusy;
37
51
  private fail;
38
52
  private post;
39
53
  private open;
40
54
  private listen;
41
55
  /** Passes on a change made to the text, once the worker has it. */
42
56
  change(changes: ChangeSet): void;
43
- /** Asks for the states from line index `from` on, once the worker has read up to `until`. */
44
- want(from: number, until: number): void;
57
+ /**
58
+ * Requests syntax data up to `until` (and flat tokens for 1-based inclusive
59
+ * `[tokensFrom, tokensTo]` or disjoint `tokenRanges` when in `tokens` mode).
60
+ * A new range preempts any older in-flight request.
61
+ */
62
+ want(from: number, until: number, tokensFrom?: number, tokensTo?: number, tokenRanges?: Array<[from: number, to: number]>): void;
63
+ /** Cancels any pending highlighting task for this document (e.g. when scrolled out of view). */
64
+ cancel(): void;
65
+ dispose(): void;
66
+ }
67
+ export interface WorkerDiffRequest {
68
+ promise: Promise<CodeDiffHunk[]>;
69
+ cancel(): void;
70
+ }
71
+ /** Runs `diffText` asynchronously inside a `serveCodeHighlighting` worker, with cancellation support. */
72
+ export declare function requestWorkerDiff(worker: Worker, original: string, modified: string, options?: CodeDiffOptions): WorkerDiffRequest;
73
+ export interface CodeWorkerPoolOptions {
74
+ /**
75
+ * Maximum number of workers spawned lazily on demand.
76
+ * Defaults to `Math.min(4, Math.max(2, Math.floor((hardwareConcurrency || 4) / 2)))` (1 on single-core).
77
+ */
78
+ maxWorkers?: number;
79
+ /**
80
+ * Milliseconds to keep an idle worker alive after its last user releases it,
81
+ * avoiding repeated worker/WASM startup during fast list scrolling.
82
+ * Defaults to `10_000`. Set to `0` to terminate immediately when idle.
83
+ */
84
+ idleTimeout?: number;
85
+ }
86
+ /**
87
+ * A lazily-spawned pool of highlighting workers shared across `DouCodeEditor`
88
+ * and `DouCodeDiff` instances via the `worker` prop.
89
+ */
90
+ export interface CodeWorkerPool {
91
+ readonly kind: 'pool';
92
+ /** Number of currently live worker instances in the pool. */
93
+ readonly size: number;
94
+ /** Maximum number of workers the pool may spawn. */
95
+ readonly maxWorkers: number;
96
+ /** Terminates all live workers and clears idle timers. */
45
97
  dispose(): void;
46
98
  }
47
- export declare function ownWorker(): Promise<{
99
+ /** Accepted values for the `worker` prop on `DouCodeEditor` and `DouCodeDiff`. */
100
+ export type CodeWorkerOption = boolean | (() => Worker) | CodeWorkerPool;
101
+ export interface AcquiredWorker {
48
102
  worker: Worker;
49
103
  release: () => void;
50
- }>;
104
+ setBusy?: (busy: boolean) => void;
105
+ }
106
+ /**
107
+ * Creates a lazily-spawned worker pool that can be passed to the `worker` prop
108
+ * of multiple `DouCodeEditor` and `DouCodeDiff` instances.
109
+ *
110
+ * Workers are created only when concurrent tasks need them (up to `maxWorkers`)
111
+ * and kept warm briefly when idle so scrolling through a multi-file list avoids
112
+ * re-initializing workers or Shiki WASM grammars.
113
+ */
114
+ export declare function createCodeWorkerPool(factory: () => Worker, options?: CodeWorkerPoolOptions): CodeWorkerPool;
115
+ export declare function isCodeWorkerPool(value: unknown): value is CodeWorkerPool;
116
+ export declare function acquirePoolWorker(pool: CodeWorkerPool): Promise<AcquiredWorker>;
117
+ /** Acquires a worker from the built-in lazily-spawned worker pool (`worker: true`). */
118
+ export declare function ownWorker(): Promise<AcquiredWorker>;
119
+ /** Disposes the built-in worker pool (useful in tests or teardown). */
120
+ export declare function disposeOwnWorkerPool(): void;
51
121
  /** Counts an editor using `worker`; the returned function lets go, ending the worker after the last. */
52
122
  export declare function holdWorker(worker: Worker): () => void;
@@ -1,6 +1,9 @@
1
- import type { CodeLanguage } from './language';
1
+ import type { CodeDiffHunk, CodeDiffOptions } from '../core/diff';
2
+ import type { CodeLanguage, CodeLineTokens } from './language';
2
3
  /** A change as sent: `[from, to, insert]`. */
3
4
  export type WireChange = [from: number, to: number, insert: string];
5
+ /** How a worker transfers syntax results back to the page for a document. */
6
+ export type WorkerSyntaxMode = 'auto' | 'states' | 'tokens';
4
7
  export type ToWorker =
5
8
  /** `nested`: the names of the languages it may hold, which the worker's language must hold too. */
6
9
  {
@@ -9,6 +12,7 @@ export type ToWorker =
9
12
  language: string;
10
13
  nested: string[];
11
14
  tabSize: number;
15
+ mode?: WorkerSyntaxMode;
12
16
  }
13
17
  /** The text, in pieces of whole lines; `done` on the last. */
14
18
  | {
@@ -26,21 +30,68 @@ export type ToWorker =
26
30
  id: number;
27
31
  from: number;
28
32
  until: number;
33
+ /** 1-based inclusive line range whose flat tokens should be returned in `tokens` mode. */
34
+ tokensFrom?: number;
35
+ tokensTo?: number;
36
+ /**
37
+ * Specific 1-based inclusive line ranges whose flat tokens should be
38
+ * returned in `tokens` mode, skipping folded or collapsed hidden lines.
39
+ */
40
+ tokenRanges?: Array<[from: number, to: number]>;
41
+ /** Monotonic request sequence so newer viewport requests preempt older ones. */
42
+ seq?: number;
43
+ }
44
+ /** Cancels any pending highlighting work for this document without closing it. */
45
+ | {
46
+ t: 'cancel';
47
+ id: number;
29
48
  } | {
30
49
  t: 'close';
31
50
  id: number;
51
+ }
52
+ /** Requests an asynchronous text diff inside the worker. */
53
+ | {
54
+ t: 'diff';
55
+ id: number;
56
+ original: string;
57
+ modified: string;
58
+ options?: CodeDiffOptions;
59
+ }
60
+ /** Cancels a queued diff request before it runs. */
61
+ | {
62
+ t: 'cancelDiff';
63
+ id: number;
32
64
  };
33
65
  export type FromWorker = {
34
66
  t: 'states';
35
67
  id: number;
36
68
  version: number;
69
+ seq?: number;
37
70
  points: Array<[line: number, state: unknown]>;
38
71
  }
39
- /** The worker hasn't the language named `missing`, or (without it) can't send its states. */
72
+ /** Flat per-line token arrays starting at 1-based line `from` (inclusive), or disjoint `ranges`. */
73
+ | {
74
+ t: 'tokens';
75
+ id: number;
76
+ version: number;
77
+ seq?: number;
78
+ from: number;
79
+ lines: CodeLineTokens[];
80
+ /** Present when the request asked for multiple disjoint visible line ranges. */
81
+ ranges?: Array<[from: number, lines: CodeLineTokens[]]>;
82
+ frontier: number;
83
+ }
84
+ /** The worker hasn't the language named `missing`, or (without it) can't highlight it. */
40
85
  | {
41
86
  t: 'unsupported';
42
87
  id: number;
43
88
  missing?: string;
89
+ }
90
+ /** Result of an asynchronous `diff` request. */
91
+ | {
92
+ t: 'diffResult';
93
+ id: number;
94
+ hunks: CodeDiffHunk[];
44
95
  };
45
96
  /** A state as plain data, its languages replaced by their names. */
46
97
  export declare function encodeState(value: unknown): unknown;
@@ -1,4 +1,6 @@
1
- import { type CodeLanguage, type CodeTokenType } from '../language/language';
1
+ import { type CodeLanguage, type CodeTokenType, type RemoteCodeLanguageOptions, remoteCodeLanguage } from '../language/language';
2
+ export { remoteCodeLanguage };
3
+ export type { RemoteCodeLanguageOptions };
2
4
  /** The part of a TextMate rule stack the editor uses: Shiki's `StateStack`. */
3
5
  export interface ShikiStateStack {
4
6
  equals(other: ShikiStateStack): boolean;
@@ -17,6 +19,8 @@ export interface ShikiHighlighterLike {
17
19
  stoppedEarly?: boolean;
18
20
  };
19
21
  };
22
+ getLoadedLanguages?: () => string[];
23
+ loadLanguage?: (lang: never) => Promise<void>;
20
24
  }
21
25
  export interface ShikiLanguageOptions {
22
26
  /** The name the editor and code blocks know it by; defaults to the Shiki language's. */
@@ -61,3 +65,34 @@ export declare function tokenTypeOfScopes(scopes: readonly string[]): CodeTokenT
61
65
  * ```
62
66
  */
63
67
  export declare function shikiLanguage(highlighter: ShikiHighlighterLike, language: string, options?: ShikiLanguageOptions): CodeLanguage<ShikiState>;
68
+ export interface ShikiResolverOptions {
69
+ /** Lazily returns (or creates) a Shiki highlighter instance inside the worker. */
70
+ highlighter: () => ShikiHighlighterLike | Promise<ShikiHighlighterLike>;
71
+ /**
72
+ * Loads a language grammar into the highlighter on demand. When omitted, calls
73
+ * `highlighter.loadLanguage(name)` if available and the language is not yet loaded.
74
+ */
75
+ loadLanguage?: (highlighter: ShikiHighlighterLike, name: string) => Promise<void> | void;
76
+ /** Optional `ShikiLanguageOptions` applied to resolved languages. */
77
+ options?: ShikiLanguageOptions | ((name: string) => ShikiLanguageOptions | undefined);
78
+ }
79
+ /**
80
+ * Creates a `resolve` callback for `serveCodeHighlighting` that lazy-loads
81
+ * Shiki grammars inside the worker and wraps them with `shikiLanguage`.
82
+ *
83
+ * ```ts
84
+ * // shiki.worker.ts
85
+ * import { createHighlighter } from 'shiki'
86
+ * import { createShikiLanguageResolver } from '@dou-ui/code/shiki'
87
+ * import { serveCodeHighlighting, typescript } from '@dou-ui/code/worker'
88
+ *
89
+ * let highlighter: ReturnType<typeof createHighlighter> | undefined
90
+ * serveCodeHighlighting({
91
+ * languages: [typescript],
92
+ * resolve: createShikiLanguageResolver(
93
+ * () => (highlighter ??= createHighlighter({ langs: [], themes: [] })),
94
+ * ),
95
+ * })
96
+ * ```
97
+ */
98
+ export declare function createShikiLanguageResolver(highlighterOrOptions: ShikiResolverOptions['highlighter'] | ShikiResolverOptions): (name: string) => Promise<CodeLanguage<ShikiState> | null>;
@@ -1,11 +1,13 @@
1
- import type { Component } from 'vue';
2
- import type { CodeDiagnostic, CodeExtension } from './extension';
1
+ import type { Component, VNodeChild } from 'vue';
2
+ import type { CodeDiagnostic, CodeExtension, CodeGutterHoverMarker, CodeGutterMarker } from './extension';
3
3
  import type { CodeCommand } from './core/commands';
4
+ import type { CodeDiffHunk } from './core/diff';
4
5
  import type { CodeKeymap } from './core/keymap';
5
6
  import type { CodeSearchQuery } from './core/search';
6
7
  import type { CodeSelection } from './core/selection';
7
8
  import type { CodeMessages } from './locale';
8
9
  import type { CodeLanguage } from './language/language';
10
+ import type { CodeWorkerOption } from './language/worker-client';
9
11
  import type { CodeText } from './core/text';
10
12
  import type { CodeState, CodeTransaction, CodeTransactionMeta, CodeTransactionResult } from './core/transaction';
11
13
  export interface CodeEditorProps {
@@ -38,6 +40,14 @@ export interface CodeEditorProps {
38
40
  * default.
39
41
  */
40
42
  bordered?: boolean;
43
+ /**
44
+ * Lets the editor scroll vertically inside its own box. Off, vertical
45
+ * scrolling is disabled (`overflow-y: hidden`) while horizontal scrolling
46
+ * stays on (`overflow-x: auto`), so the content expands to its full line
47
+ * count and an outer scroll container (such as `DouVirtualList`) owns
48
+ * vertical scrolling. On by default.
49
+ */
50
+ scrollY?: boolean;
41
51
  /** Tints the line with the cursor. */
42
52
  activeLine?: boolean;
43
53
  /** Columns per tab stop, also the width of one indent. */
@@ -48,14 +58,22 @@ export interface CodeEditorProps {
48
58
  /** The language: colors its tokens, and tells how to indent, comment and pair brackets. */
49
59
  language?: CodeLanguage | null;
50
60
  /**
51
- * Reads the code for its colors in a worker, so a long file opened far
52
- * down is colored without the page doing the reading. `true` uses the
53
- * editor's own worker, which knows the built-in languages; a function
54
- * makes your own (`serveCodeHighlighting` from `@dou-ui/code/worker`),
55
- * called in the browser once the editor is mounted. The editor ends the
56
- * worker when it unmounts or no longer uses it.
61
+ * Reads the code for its colors in a worker, so highlighting stays off the
62
+ * main thread. `true` uses the editor's own lazily-spawned worker pool for
63
+ * the built-in languages; a `CodeWorkerPool` (`createCodeWorkerPool`) shares
64
+ * a custom worker pool across instances; a function makes a worker
65
+ * (`serveCodeHighlighting` from `@dou-ui/code/worker`) once the editor is
66
+ * mounted and ends it when unmounted.
67
+ */
68
+ worker?: CodeWorkerOption;
69
+ /**
70
+ * Maximum number of unread lines tokenized synchronously on the main thread
71
+ * during rendering. Beyond this threshold, unread lines paint as plain text
72
+ * first and highlight asynchronously in the worker or browser idle slices.
73
+ * Defaults to `2000`. Set to `0` to always paint plain text first and keep
74
+ * tokenization off the render frame (useful in fast-scrolling multi-file lists).
57
75
  */
58
- worker?: boolean | (() => Worker);
76
+ syncHighlightLines?: number;
59
77
  /** Typing an opening bracket or quote also types its partner. On by default. */
60
78
  closeBrackets?: boolean;
61
79
  /** Outlines the bracket next to the cursor and its partner. On by default. */
@@ -236,6 +254,26 @@ export interface CodeDiffProps {
236
254
  /** The text after, bound with `v-model`; editable beside the original with `editable`. */
237
255
  modelValue?: string;
238
256
  language?: CodeLanguage | null;
257
+ /**
258
+ * Forwards `worker` to the inner `DouCodeEditor` instances and computes
259
+ * large text diffs asynchronously inside the worker. Accepts `true` (built-in
260
+ * lazy worker pool), a `CodeWorkerPool` (`createCodeWorkerPool`), or a worker
261
+ * factory function (`() => Worker`).
262
+ */
263
+ worker?: CodeWorkerOption;
264
+ /**
265
+ * Forwards `syncHighlightLines` to the inner `DouCodeEditor` instances.
266
+ * Defaults to `2000`; set to `0` to paint plain text first and offload
267
+ * syntax highlighting to the worker or idle slices.
268
+ */
269
+ syncHighlightLines?: number;
270
+ /**
271
+ * When `worker` is enabled, runs `diffText` asynchronously in the worker
272
+ * whenever the combined line count of `original` and `modelValue` exceeds
273
+ * this threshold. Defaults to `500`. Set to `0` to always compute diffs in
274
+ * the worker, or `Infinity` to keep diff computation synchronous.
275
+ */
276
+ syncDiffLines?: number;
239
277
  /**
240
278
  * `split` puts the two texts side by side, `inline` puts the changes in
241
279
  * one column. `auto` (the default) is `split` when there is room (560px)
@@ -260,11 +298,30 @@ export interface CodeDiffProps {
260
298
  words?: boolean;
261
299
  /** Wraps long lines in the inline view; side by side, lines never wrap, to stay aligned. */
262
300
  wrap?: boolean;
263
- /** Extensions of the modified side, side by side only. */
301
+ /**
302
+ * Extensions of the modified side, side by side only: for things that
303
+ * read the text, such as a language service or a linter. Markers and
304
+ * widgets for both sides go in `gutters` and `widgets`.
305
+ */
264
306
  extensions?: CodeExtension[];
307
+ /**
308
+ * Columns left of the line numbers, with markers on either text's lines;
309
+ * drawn on both sides side by side, and on the matching rows inline.
310
+ */
311
+ gutters?: CodeDiffGutter[];
312
+ /**
313
+ * Components between lines of either text, e.g. review comments. The
314
+ * other side leaves a blank of the same height, so the two stay lined up.
315
+ */
316
+ widgets?: CodeDiffWidget[];
265
317
  tabSize?: number;
266
318
  /** The toolbar above: change count, previous and next, expand, and the view toggle. On by default. */
267
319
  toolbar?: boolean;
320
+ /**
321
+ * Keeps the toolbar stuck to the top of the enclosing scroll container while
322
+ * scrolling through a long diff. Off by default.
323
+ */
324
+ stickyToolbar?: boolean;
268
325
  /**
269
326
  * The hairline frame and rounded corners around the whole diff. Off,
270
327
  * the diff takes no border at all and no focus edge, for a panel or
@@ -272,9 +329,108 @@ export interface CodeDiffProps {
272
329
  * sides stay. On by default.
273
330
  */
274
331
  bordered?: boolean;
332
+ /**
333
+ * Lets the inner editors scroll vertically. Off, vertical scrolling is
334
+ * disabled (`overflow-y: hidden`) while horizontal scrolling stays on
335
+ * (`overflow-x: auto`), so the diff expands to its full line count and an
336
+ * outer scroll container (such as `DouVirtualList`) owns vertical scrolling.
337
+ * On by default.
338
+ */
339
+ scrollY?: boolean;
275
340
  messages?: Partial<CodeMessages>;
276
341
  }
342
+ /** Which of the two texts a line is counted in. */
343
+ export type CodeDiffSide = 'original' | 'modified';
344
+ /** A marker in a column of `DouCodeDiff`, on line `line` (from 1) of side `side`. */
345
+ export interface CodeDiffGutterMarker extends CodeGutterMarker {
346
+ side: CodeDiffSide;
347
+ }
348
+ /** `DouCodeDiff`'s `hoverMarker`: on the lines of one side only, with `side`. */
349
+ export interface CodeDiffHoverMarker extends Omit<CodeGutterHoverMarker, 'title'> {
350
+ /**
351
+ * Its name; a function names the marker of each line, with the line and
352
+ * side `onPress` gets there, e.g. "Comment on line 3 of the new text".
353
+ * It is called for every line in view, often: keep it quick.
354
+ */
355
+ title?: string | ((line: number, side: CodeDiffSide) => string);
356
+ /** Only on the lines of this side; on both when left out. */
357
+ side?: CodeDiffSide;
358
+ }
359
+ /** A column of `DouCodeDiff`'s gutter, left of the line numbers. */
360
+ export interface CodeDiffGutter {
361
+ name: string;
362
+ /** In px. Defaults to 16 (or 18 when rendered as an overlay). */
363
+ width?: number;
364
+ /**
365
+ * Floats markers over the gutter's right edge without reserving a dedicated
366
+ * column width: `true` always overlays, `'compact'` overlays only in narrow
367
+ * containers (`< 480px`). Defaults to `false`.
368
+ */
369
+ overlay?: boolean | 'compact';
370
+ markers?: CodeDiffGutterMarker[];
371
+ /**
372
+ * A marker on every other line, shown on hover. With `side`, the other
373
+ * side's column only answers presses on its own markers.
374
+ */
375
+ hoverMarker?: CodeDiffHoverMarker;
376
+ /**
377
+ * Where `onPress` answers: `any` cell of the column (the default), or only
378
+ * its markers. With a `hoverMarker` on one side, only markers anyway.
379
+ */
380
+ pressOn?: 'any' | 'markers';
381
+ /**
382
+ * A press on the column, on line `line` of `side`. Inline, a row both
383
+ * texts share counts as the marker's side there, else as `modified`.
384
+ */
385
+ onPress?: (line: number, event: PointerEvent | KeyboardEvent, side: CodeDiffSide) => void;
386
+ }
387
+ /** A component drawn between lines of `DouCodeDiff`. */
388
+ export interface CodeDiffWidget {
389
+ /** Tells widgets apart across both sides; its measured height is kept by it. */
390
+ id: string;
391
+ side: CodeDiffSide;
392
+ /** Below line `line` (from 1) of `side`; above with `position: 'above'`. */
393
+ line: number;
394
+ /** Defaults to `below`. */
395
+ position?: 'above' | 'below';
396
+ render: () => VNodeChild;
397
+ /** Its height in px before it has been drawn and measured; defaults to one line. */
398
+ height?: number;
399
+ /** What it is, read out when the cursor moves onto its line. */
400
+ label?: string;
401
+ /**
402
+ * Side by side: `side` (the default) draws it in its own column, `both`
403
+ * across the two. Inline, it always takes the one column.
404
+ */
405
+ span?: 'side' | 'both';
406
+ }
277
407
  export interface CodeDiffEmits {
278
408
  'update:modelValue': [value: string];
279
409
  'update:mode': [mode: 'split' | 'inline'];
280
410
  }
411
+ export interface CodeDiffSlots {
412
+ /**
413
+ * Leading content in the toolbar, such as a file path. Overflows with an
414
+ * ellipsis, moving the change counts beside the toolbar buttons.
415
+ */
416
+ title?: () => unknown;
417
+ }
418
+ export interface CodeDiffExpose {
419
+ /** The changes found now. */
420
+ readonly hunks: readonly CodeDiffHunk[];
421
+ /** Whether any run of unchanged lines can be collapsed under the current settings. */
422
+ readonly anyHideable: boolean;
423
+ /** How many unchanged lines are currently hidden behind collapse bars. */
424
+ readonly hiddenCount: number;
425
+ /** Moves to the next change; `false` when there is none. */
426
+ next: () => boolean;
427
+ /** Moves to the previous change; `false` when there is none. */
428
+ previous: () => boolean;
429
+ /** Shows every unchanged line. */
430
+ expandAll: () => void;
431
+ /** Hides the runs of unchanged lines again. */
432
+ collapseAll: () => void;
433
+ /** The editors drawn now: both sides, or the inline one as `modified`. */
434
+ readonly original: CodeEditorExpose | undefined;
435
+ readonly modified: CodeEditorExpose | undefined;
436
+ }
@@ -7,9 +7,15 @@ export declare class Measure {
7
7
  padY: number;
8
8
  /** Space before and after each line (`--dou-code-editor-padding-x`). */
9
9
  padX: number;
10
+ /** Space left of line numbers in the gutter (`--dou-code-editor-gutter-padding-left`). */
11
+ gutterPadLeft: number;
12
+ /** Space right of line numbers in the gutter (`--dou-code-editor-gutter-padding-right`). */
13
+ gutterPadRight: number;
10
14
  tabSize: number;
11
15
  private widths;
12
16
  constructor(probe: () => HTMLElement | null | undefined);
17
+ /** Reads the CSS padding variables; returns `true` when any changed. */
18
+ refreshPadding(): boolean;
13
19
  /** Reads the cell width and line height again, e.g. after the font changed. */
14
20
  refresh(tabSize: number): void;
15
21
  /** Width of one character as drawn. */
@@ -1,8 +1,10 @@
1
1
  import type { CodeLanguage } from '../language/language';
2
2
  export type { CodeLanguage };
3
- export { defineCodeLanguage, copyPlain, samePlain } from '../language/language';
4
- export type { CodeLanguageSpec, CodeTokenType } from '../language/language';
3
+ export { defineCodeLanguage, remoteCodeLanguage, copyPlain, samePlain } from '../language/language';
4
+ export type { CodeLanguageSpec, CodeLineTokens, CodeTokenType, RemoteCodeLanguageOptions, } from '../language/language';
5
5
  export type { CodeStream } from '../language/stream';
6
+ export { diffText } from '../core/diff';
7
+ export type { CodeDiffHunk, CodeDiffOptions, CodeDiffPart } from '../core/diff';
6
8
  export { createJavaScript, javascript, jsx, tsx, typescript } from '../languages/javascript';
7
9
  export { createCss, css, scss } from '../languages/css';
8
10
  export { json } from '../languages/json';
@@ -12,10 +14,26 @@ export { python } from '../languages/python';
12
14
  export { shell } from '../languages/shell';
13
15
  export { sql } from '../languages/sql';
14
16
  export { yaml } from '../languages/yaml';
17
+ export interface WorkerMessageScope {
18
+ postMessage(message: unknown): void;
19
+ addEventListener(type: 'message', listener: (event: MessageEvent) => void): void;
20
+ }
21
+ export interface ServeCodeHighlightingOptions {
22
+ /** Languages registered up front. */
23
+ languages?: readonly CodeLanguage[];
24
+ /**
25
+ * Resolves a language by name on demand (for example lazy-loading a Shiki
26
+ * grammar inside the worker). Concurrent requests for the same name share
27
+ * one resolution; returning `null` or `undefined` marks it unsupported.
28
+ */
29
+ resolve?: (name: string) => CodeLanguage | null | undefined | Promise<CodeLanguage | null | undefined>;
30
+ }
15
31
  /**
16
- * Reads code in a worker for `DouCodeEditor`'s `worker`, so a long file is
17
- * colored without the page doing the reading. Call it in the worker's own
18
- * module with the languages the editors use:
32
+ * Reads code and computes diffs in a worker for `DouCodeEditor` and
33
+ * `DouCodeDiff`'s `worker`, so syntax highlighting and large diffs stay off
34
+ * the main thread. Call it in the worker's own module with the languages the
35
+ * editors use, or with a `resolve` callback to lazy-load grammars (such as
36
+ * Shiki) on demand:
19
37
  *
20
38
  * ```ts
21
39
  * // code-worker.ts
@@ -24,11 +42,8 @@ export { yaml } from '../languages/yaml';
24
42
  * serveCodeHighlighting([typescript, css])
25
43
  * ```
26
44
  *
27
- * One worker serves every editor that is given it. A language works here
28
- * when its states are plain data (the built-in ones are); those read by
29
- * the Shiki adapter stay on the page.
45
+ * Languages with plain-data states send state checkpoints; languages with
46
+ * non-serializable states (such as Shiki's `StateStack`) automatically keep
47
+ * their state in the worker and return flat per-line token arrays.
30
48
  */
31
- export declare function serveCodeHighlighting(languages: readonly CodeLanguage[], scope?: {
32
- postMessage(message: unknown): void;
33
- addEventListener(type: 'message', listener: (event: MessageEvent) => void): void;
34
- }): void;
49
+ export declare function serveCodeHighlighting(languagesOrOptions: readonly CodeLanguage[] | ServeCodeHighlightingOptions, scope?: WorkerMessageScope): void;