@dou-ui/code 0.1.0-alpha.24 → 0.1.0-alpha.25
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.
- package/dist/assets/default-BWWFJmzn.js.map +1 -0
- package/dist/code-diff.js.map +1 -1
- package/dist/code-diff.vue_vue_type_script_setup_true_lang.js +149 -7
- package/dist/code-diff.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/code-editor.js.map +1 -1
- package/dist/code-editor.vue_vue_type_script_setup_true_lang.js +152 -34
- package/dist/code-editor.vue_vue_type_script_setup_true_lang.js.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/language/highlighter.js +1 -1
- package/dist/language/highlighter.js.map +1 -1
- package/dist/language/language.js +24 -2
- package/dist/language/language.js.map +1 -1
- package/dist/language/remote-tokens.js +131 -0
- package/dist/language/remote-tokens.js.map +1 -0
- package/dist/language/worker-client.js +308 -33
- package/dist/language/worker-client.js.map +1 -1
- package/dist/language/worker-protocol.js.map +1 -1
- package/dist/shiki/index.js +54 -3
- package/dist/shiki/index.js.map +1 -1
- package/dist/types/code-diff.vue.d.ts +4 -1
- package/dist/types/code-editor.vue.d.ts +2 -1
- package/dist/types/index.d.ts +18 -6
- package/dist/types/language/language.d.ts +27 -0
- package/dist/types/language/remote-tokens.d.ts +38 -0
- package/dist/types/language/worker-client.d.ts +85 -15
- package/dist/types/language/worker-protocol.d.ts +53 -2
- package/dist/types/shiki/index.d.ts +36 -1
- package/dist/types/types.d.ts +36 -7
- package/dist/types/worker/index.d.ts +27 -12
- package/dist/worker/default.js +1 -1
- package/dist/worker/index.js +280 -53
- package/dist/worker/index.js.map +1 -1
- package/package.json +3 -3
- package/dist/assets/default-BZBMZKf_.js.map +0 -1
package/dist/shiki/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/shiki/index.ts"],"sourcesContent":["import { type CodeLanguage, type CodeTokenType, defineCodeLanguage } from '../language/language'\n\n/** The part of a TextMate rule stack the editor uses: Shiki's `StateStack`. */\nexport interface ShikiStateStack {\n equals(other: ShikiStateStack): boolean\n}\n\n/** What the editor needs of a Shiki highlighter (`createHighlighter()` from `shiki`). */\nexport interface ShikiHighlighterLike {\n getLanguage(name: string): {\n tokenizeLine(\n line: string,\n state: ShikiStateStack | null,\n timeLimit?: number,\n ): {\n tokens: Array<{ startIndex: number; endIndex: number; scopes: string[] }>\n ruleStack: ShikiStateStack\n /** The time limit ran out before the end of the line. */\n stoppedEarly?: boolean\n }\n }\n}\n\nexport interface ShikiLanguageOptions {\n /** The name the editor and code blocks know it by; defaults to the Shiki language's. */\n name?: string\n aliases?: string[]\n /** How the language writes comments, for toggling them; Shiki doesn't say. */\n comment?: { line?: string; block?: [open: string, close: string] }\n brackets?: string\n autoClose?: string\n indentAfter?: RegExp\n /**\n * The token kind (syntax color) for a token's scopes, outermost first;\n * return `undefined` to use the built-in mapping (`tokenTypeOfScopes`).\n */\n tokenType?: (scopes: readonly string[]) => CodeTokenType | null | undefined\n}\n\nexport interface ShikiState {\n /** The grammar's state at the start of the line being read, or after the last line. */\n stack: ShikiStateStack | null\n /** The tokens of the line being read; positions from `base`. */\n line: { text: string; base: number; ends: number[]; types: Array<CodeTokenType | null> } | null\n}\n\n/** Scope prefixes and the kind each is drawn as; the first that matches a token's scopes, from the innermost scope out, wins. */\nconst SCOPES: Array<[prefix: string, type: CodeTokenType]> = [\n ['comment', 'comment'],\n ['punctuation.definition.comment', 'comment'],\n ['string.regexp', 'string'],\n ['constant.character.escape', 'string'],\n ['punctuation.definition.string', 'string'],\n ['string', 'string'],\n ['constant.numeric', 'number'],\n ['constant.language', 'keyword'],\n ['constant.other.color', 'number'],\n ['keyword.operator', 'operator'],\n ['storage.type.function.arrow', 'operator'],\n ['keyword', 'keyword'],\n ['storage', 'keyword'],\n ['entity.name.function', 'function'],\n ['support.function', 'function'],\n ['meta.function-call', 'function'],\n ['entity.name.tag', 'tag'],\n ['entity.other.attribute-name', 'attribute'],\n ['entity.name.type', 'type'],\n ['entity.name.class', 'type'],\n ['entity.other.inherited-class', 'type'],\n ['support.type.property-name', 'property'],\n ['support.type', 'type'],\n ['support.class', 'type'],\n ['variable.other.property', 'property'],\n ['variable.other.object.property', 'property'],\n ['support.variable.property', 'property'],\n ['meta.object-literal.key', 'property'],\n ['variable.language', 'keyword'],\n ['variable', 'variable'],\n ['entity.name', 'variable'],\n ['markup.heading', 'heading'],\n ['markup.bold', 'strong'],\n ['markup.italic', 'emphasis'],\n ['markup.underline.link', 'link'],\n ['markup.inline.raw', 'string'],\n ['meta.preprocessor', 'meta'],\n ['meta.decorator', 'meta'],\n ['meta.annotation', 'meta'],\n ['invalid', 'invalid'],\n ['punctuation', 'punctuation'],\n]\n\nconst matches = (scope: string, prefix: string) =>\n scope === prefix || (scope.startsWith(prefix) && scope[prefix.length] === '.')\n\n/** The kind a token with these scopes (outermost first, as Shiki gives them) is drawn as. */\nexport function tokenTypeOfScopes(scopes: readonly string[]): CodeTokenType | null {\n for (let i = scopes.length - 1; i > 0; i--) {\n const scope = scopes[i]!\n for (const [prefix, type] of SCOPES) if (matches(scope, prefix)) return type\n }\n return null\n}\n\n/** How long one line may take to read, in milliseconds, so a huge line doesn't hold up typing. */\nconst TIME_LIMIT = 50\n/**\n * Lines up to this long are read to the end even past the time limit. The\n * first lines a grammar reads also build its patterns, which on a busy\n * machine can take longer than the limit; stopping there would cut a block\n * comment or string in two and color the lines after it wrongly.\n */\nconst ALWAYS_WHOLE = 2000\n\ntype ShikiGrammar = ReturnType<ShikiHighlighterLike['getLanguage']>\n/** Reads a line within the time limit, and to its end when it is not too long. */\nfunction readLine(grammar: ShikiGrammar, text: string, stack: ShikiStateStack | null) {\n const result = grammar.tokenizeLine(text, stack, TIME_LIMIT)\n return result.stoppedEarly && text.length <= ALWAYS_WHOLE\n ? grammar.tokenizeLine(text, stack, 0)\n : result\n}\n\n/**\n * Turns a language loaded into a Shiki highlighter into one the editor\n * reads. Shiki's grammar does the reading; its scopes are drawn with the\n * editor's own syntax colors, so the theme and dark mode apply as they do\n * to the built-in languages.\n *\n * ```ts\n * const highlighter = await createHighlighter({ langs: ['rust'], themes: [] })\n * const rust = shikiLanguage(highlighter, 'rust', { comment: { line: '//' } })\n * ```\n */\nexport function shikiLanguage(\n highlighter: ShikiHighlighterLike,\n language: string,\n options: ShikiLanguageOptions = {},\n): CodeLanguage<ShikiState> {\n const grammar = highlighter.getLanguage(language)\n const typeOf = options.tokenType\n ? (scopes: readonly string[]) => {\n const own = options.tokenType!(scopes)\n return own === undefined ? tokenTypeOfScopes(scopes) : own\n }\n : tokenTypeOfScopes\n return defineCodeLanguage<ShikiState>({\n name: options.name ?? language,\n aliases: options.aliases,\n startState: () => ({ stack: null, line: null }),\n // the stack never changes once made: copies share it\n copyState: (state) => ({ stack: state.stack, line: state.line }),\n sameState: (a, b) => a.stack === b.stack || (!!a.stack && !!b.stack && a.stack.equals(b.stack)),\n token(stream, state) {\n let line = state.line\n if (!line || line.text !== stream.string || stream.pos < line.base) {\n // a new line: read it whole, and keep the state after it\n const text = stream.string.slice(stream.pos, stream.end)\n const result = readLine(grammar, text, state.stack)\n const ends: number[] = []\n const types: Array<CodeTokenType | null> = []\n for (const token of result.tokens) {\n ends.push(stream.pos + Math.min(token.endIndex, text.length))\n types.push(typeOf(token.scopes))\n }\n line = { text: stream.string, base: stream.pos, ends, types }\n state.stack = result.ruleStack\n state.line = line\n }\n for (let i = 0; i < line.ends.length; i++)\n if (line.ends[i]! > stream.pos) {\n stream.pos = Math.min(line.ends[i]!, stream.end)\n // the line is read: the next one is read afresh, even with the same text\n if (stream.pos >= stream.end) state.line = null\n return line.types[i]!\n }\n stream.skipToEnd()\n state.line = null\n return null\n },\n blankLine(state) {\n state.stack = readLine(grammar, '', state.stack).ruleStack\n state.line = null\n },\n comment: options.comment,\n brackets: options.brackets,\n autoClose: options.autoClose,\n indentAfter: options.indentAfter,\n })\n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C;IACxB,MAAM,QAAqC,CAAC;IAC5C,KAAK,MAAM,SAAS,OAAO,QAAQ;KACjC,KAAK,KAAK,OAAO,MAAM,KAAK,IAAI,MAAM,UAAU,KAAK,MAAM,CAAC;KAC5D,MAAM,KAAK,OAAO,MAAM,MAAM,CAAC;IACjC;IACA,OAAO;KAAE,MAAM,OAAO;KAAQ,MAAM,OAAO;KAAK;KAAM;IAAM;IAC5D,MAAM,QAAQ,OAAO;IACrB,MAAM,OAAO;GACf;GACA,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,KAAK,QAAQ,KACpC,IAAI,KAAK,KAAK,KAAM,OAAO,KAAK;IAC9B,OAAO,MAAM,KAAK,IAAI,KAAK,KAAK,IAAK,OAAO,GAAG;IAE/C,IAAI,OAAO,OAAO,OAAO,KAAK,MAAM,OAAO;IAC3C,OAAO,KAAK,MAAM;GACpB;GACF,OAAO,UAAU;GACjB,MAAM,OAAO;GACb,OAAO;EACT;EACA,UAAU,OAAO;GACf,MAAM,QAAQ,SAAS,SAAS,IAAI,MAAM,KAAK,CAAC,CAAC;GACjD,MAAM,OAAO;EACf;EACA,SAAS,QAAQ;EACjB,UAAU,QAAQ;EAClB,WAAW,QAAQ;EACnB,aAAa,QAAQ;CACvB,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/shiki/index.ts"],"sourcesContent":["import {\n type CodeLanguage,\n type CodeTokenType,\n type RemoteCodeLanguageOptions,\n defineCodeLanguage,\n remoteCodeLanguage,\n} from '../language/language'\n\nexport { remoteCodeLanguage }\nexport type { RemoteCodeLanguageOptions }\n\n/** The part of a TextMate rule stack the editor uses: Shiki's `StateStack`. */\nexport interface ShikiStateStack {\n equals(other: ShikiStateStack): boolean\n}\n\n/** What the editor needs of a Shiki highlighter (`createHighlighter()` from `shiki`). */\nexport interface ShikiHighlighterLike {\n getLanguage(name: string): {\n tokenizeLine(\n line: string,\n state: ShikiStateStack | null,\n timeLimit?: number,\n ): {\n tokens: Array<{ startIndex: number; endIndex: number; scopes: string[] }>\n ruleStack: ShikiStateStack\n /** The time limit ran out before the end of the line. */\n stoppedEarly?: boolean\n }\n }\n getLoadedLanguages?: () => string[]\n loadLanguage?: (lang: never) => Promise<void>\n}\n\nexport interface ShikiLanguageOptions {\n /** The name the editor and code blocks know it by; defaults to the Shiki language's. */\n name?: string\n aliases?: string[]\n /** How the language writes comments, for toggling them; Shiki doesn't say. */\n comment?: { line?: string; block?: [open: string, close: string] }\n brackets?: string\n autoClose?: string\n indentAfter?: RegExp\n /**\n * The token kind (syntax color) for a token's scopes, outermost first;\n * return `undefined` to use the built-in mapping (`tokenTypeOfScopes`).\n */\n tokenType?: (scopes: readonly string[]) => CodeTokenType | null | undefined\n}\n\nexport interface ShikiState {\n /** The grammar's state at the start of the line being read, or after the last line. */\n stack: ShikiStateStack | null\n /** The tokens of the line being read; positions from `base`. */\n line: { text: string; base: number; ends: number[]; types: Array<CodeTokenType | null> } | null\n}\n\n/** Scope prefixes and the kind each is drawn as; the first that matches a token's scopes, from the innermost scope out, wins. */\nconst SCOPES: Array<[prefix: string, type: CodeTokenType]> = [\n ['comment', 'comment'],\n ['punctuation.definition.comment', 'comment'],\n ['string.regexp', 'string'],\n ['constant.character.escape', 'string'],\n ['punctuation.definition.string', 'string'],\n ['string', 'string'],\n ['constant.numeric', 'number'],\n ['constant.language', 'keyword'],\n ['constant.other.color', 'number'],\n ['keyword.operator', 'operator'],\n ['storage.type.function.arrow', 'operator'],\n ['keyword', 'keyword'],\n ['storage', 'keyword'],\n ['entity.name.function', 'function'],\n ['support.function', 'function'],\n ['meta.function-call', 'function'],\n ['entity.name.tag', 'tag'],\n ['entity.other.attribute-name', 'attribute'],\n ['entity.name.type', 'type'],\n ['entity.name.class', 'type'],\n ['entity.other.inherited-class', 'type'],\n ['support.type.property-name', 'property'],\n ['support.type', 'type'],\n ['support.class', 'type'],\n ['variable.other.property', 'property'],\n ['variable.other.object.property', 'property'],\n ['support.variable.property', 'property'],\n ['meta.object-literal.key', 'property'],\n ['variable.language', 'keyword'],\n ['variable', 'variable'],\n ['entity.name', 'variable'],\n ['markup.heading', 'heading'],\n ['markup.bold', 'strong'],\n ['markup.italic', 'emphasis'],\n ['markup.underline.link', 'link'],\n ['markup.inline.raw', 'string'],\n ['meta.preprocessor', 'meta'],\n ['meta.decorator', 'meta'],\n ['meta.annotation', 'meta'],\n ['invalid', 'invalid'],\n ['punctuation', 'punctuation'],\n]\n\nconst matches = (scope: string, prefix: string) =>\n scope === prefix || (scope.startsWith(prefix) && scope[prefix.length] === '.')\n\n/** The kind a token with these scopes (outermost first, as Shiki gives them) is drawn as. */\nexport function tokenTypeOfScopes(scopes: readonly string[]): CodeTokenType | null {\n for (let i = scopes.length - 1; i > 0; i--) {\n const scope = scopes[i]!\n for (const [prefix, type] of SCOPES) if (matches(scope, prefix)) return type\n }\n return null\n}\n\n/** How long one line may take to read, in milliseconds, so a huge line doesn't hold up typing. */\nconst TIME_LIMIT = 50\n/**\n * Lines up to this long are read to the end even past the time limit. The\n * first lines a grammar reads also build its patterns, which on a busy\n * machine can take longer than the limit; stopping there would cut a block\n * comment or string in two and color the lines after it wrongly.\n */\nconst ALWAYS_WHOLE = 2000\n\ntype ShikiGrammar = ReturnType<ShikiHighlighterLike['getLanguage']>\n/** Reads a line within the time limit, and to its end when it is not too long. */\nfunction readLine(grammar: ShikiGrammar, text: string, stack: ShikiStateStack | null) {\n const result = grammar.tokenizeLine(text, stack, TIME_LIMIT)\n return result.stoppedEarly && text.length <= ALWAYS_WHOLE\n ? grammar.tokenizeLine(text, stack, 0)\n : result\n}\n\n/**\n * Turns a language loaded into a Shiki highlighter into one the editor\n * reads. Shiki's grammar does the reading; its scopes are drawn with the\n * editor's own syntax colors, so the theme and dark mode apply as they do\n * to the built-in languages.\n *\n * ```ts\n * const highlighter = await createHighlighter({ langs: ['rust'], themes: [] })\n * const rust = shikiLanguage(highlighter, 'rust', { comment: { line: '//' } })\n * ```\n */\nexport function shikiLanguage(\n highlighter: ShikiHighlighterLike,\n language: string,\n options: ShikiLanguageOptions = {},\n): CodeLanguage<ShikiState> {\n const grammar = highlighter.getLanguage(language)\n const typeOf = options.tokenType\n ? (scopes: readonly string[]) => {\n const own = options.tokenType!(scopes)\n return own === undefined ? tokenTypeOfScopes(scopes) : own\n }\n : tokenTypeOfScopes\n return defineCodeLanguage<ShikiState>({\n name: options.name ?? language,\n aliases: options.aliases,\n startState: () => ({ stack: null, line: null }),\n // the stack never changes once made: copies share it\n copyState: (state) => ({ stack: state.stack, line: state.line }),\n sameState: (a, b) => a.stack === b.stack || (!!a.stack && !!b.stack && a.stack.equals(b.stack)),\n token(stream, state) {\n let line = state.line\n if (!line || line.text !== stream.string || stream.pos < line.base) {\n // a new line: read it whole, and keep the state after it\n const text = stream.string.slice(stream.pos, stream.end)\n const result = readLine(grammar, text, state.stack)\n const ends: number[] = []\n const types: Array<CodeTokenType | null> = []\n for (const token of result.tokens) {\n ends.push(stream.pos + Math.min(token.endIndex, text.length))\n types.push(typeOf(token.scopes))\n }\n line = { text: stream.string, base: stream.pos, ends, types }\n state.stack = result.ruleStack\n state.line = line\n }\n for (let i = 0; i < line.ends.length; i++)\n if (line.ends[i]! > stream.pos) {\n stream.pos = Math.min(line.ends[i]!, stream.end)\n // the line is read: the next one is read afresh, even with the same text\n if (stream.pos >= stream.end) state.line = null\n return line.types[i]!\n }\n stream.skipToEnd()\n state.line = null\n return null\n },\n blankLine(state) {\n state.stack = readLine(grammar, '', state.stack).ruleStack\n state.line = null\n },\n comment: options.comment,\n brackets: options.brackets,\n autoClose: options.autoClose,\n indentAfter: options.indentAfter,\n // Shiki's StateStack cannot cross postMessage: in a worker, return flat line tokens directly.\n workerMode: 'tokens',\n })\n}\n\nexport interface ShikiResolverOptions {\n /** Lazily returns (or creates) a Shiki highlighter instance inside the worker. */\n highlighter: () => ShikiHighlighterLike | Promise<ShikiHighlighterLike>\n /**\n * Loads a language grammar into the highlighter on demand. When omitted, calls\n * `highlighter.loadLanguage(name)` if available and the language is not yet loaded.\n */\n loadLanguage?: (highlighter: ShikiHighlighterLike, name: string) => Promise<void> | void\n /** Optional `ShikiLanguageOptions` applied to resolved languages. */\n options?: ShikiLanguageOptions | ((name: string) => ShikiLanguageOptions | undefined)\n}\n\n/**\n * Creates a `resolve` callback for `serveCodeHighlighting` that lazy-loads\n * Shiki grammars inside the worker and wraps them with `shikiLanguage`.\n *\n * ```ts\n * // shiki.worker.ts\n * import { createHighlighter } from 'shiki'\n * import { createShikiLanguageResolver } from '@dou-ui/code/shiki'\n * import { serveCodeHighlighting, typescript } from '@dou-ui/code/worker'\n *\n * let highlighter: ReturnType<typeof createHighlighter> | undefined\n * serveCodeHighlighting({\n * languages: [typescript],\n * resolve: createShikiLanguageResolver(\n * () => (highlighter ??= createHighlighter({ langs: [], themes: [] })),\n * ),\n * })\n * ```\n */\nexport function createShikiLanguageResolver(\n highlighterOrOptions: ShikiResolverOptions['highlighter'] | ShikiResolverOptions,\n): (name: string) => Promise<CodeLanguage<ShikiState> | null> {\n const opts: ShikiResolverOptions =\n typeof highlighterOrOptions === 'function'\n ? { highlighter: highlighterOrOptions }\n : highlighterOrOptions\n let shared: Promise<ShikiHighlighterLike> | null = null\n const getHighlighter = () => {\n if (!shared) {\n const pending = Promise.resolve()\n .then(() => opts.highlighter())\n .catch((error) => {\n if (shared === pending) shared = null\n throw error\n })\n shared = pending\n }\n return shared\n }\n\n return async (name: string): Promise<CodeLanguage<ShikiState> | null> => {\n try {\n const hl = await getHighlighter()\n const loaded = hl.getLoadedLanguages?.()\n if (!loaded || !loaded.includes(name)) {\n if (opts.loadLanguage) {\n await opts.loadLanguage(hl, name)\n } else if (hl.loadLanguage) {\n await hl.loadLanguage(name as never)\n }\n }\n const langOptions = typeof opts.options === 'function' ? opts.options(name) : opts.options\n return shikiLanguage(hl, name, { ...langOptions, name: langOptions?.name ?? name })\n } catch {\n return null\n }\n }\n}\n"],"mappings":";;;AA0DA,IAAM,SAAuD;CAC3D,CAA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,CAAC,OAAO,SAAS,IAAI,GAAG;IACrC,IAAI,KAAK,cACP,MAAM,KAAK,aAAa,IAAI,IAAI;SAC3B,IAAI,GAAG,cACZ,MAAM,GAAG,aAAa,IAAa;GAEvC;GACA,MAAM,cAAc,OAAO,KAAK,YAAY,aAAa,KAAK,QAAQ,IAAI,IAAI,KAAK;GACnF,OAAO,cAAc,IAAI,MAAM;IAAE,GAAG;IAAa,MAAM,aAAa,QAAQ;GAAK,CAAC;EACpF,QAAQ;GACN,OAAO;EACT;CACF;AACF"}
|
|
@@ -12,15 +12,18 @@ declare const __VLS_base: import("vue").DefineComponent<CodeDiffProps, CodeDiffE
|
|
|
12
12
|
toolbar: boolean;
|
|
13
13
|
language: import("./index.js").CodeLanguage<any> | null;
|
|
14
14
|
tabSize: number;
|
|
15
|
+
mode: "auto" | "split" | "inline";
|
|
15
16
|
original: string;
|
|
16
17
|
messages: Partial<import("./locale").CodeMessages>;
|
|
17
|
-
mode: "split" | "inline" | "auto";
|
|
18
18
|
modelValue: string;
|
|
19
19
|
bordered: boolean;
|
|
20
|
+
worker: import("./index.js").CodeWorkerOption;
|
|
21
|
+
syncHighlightLines: number;
|
|
20
22
|
wrap: boolean;
|
|
21
23
|
extensions: CodeExtension[];
|
|
22
24
|
editable: boolean;
|
|
23
25
|
widgets: CodeDiffWidget[];
|
|
26
|
+
syncDiffLines: number;
|
|
24
27
|
collapseUnchanged: boolean | {
|
|
25
28
|
context?: number;
|
|
26
29
|
minLines?: number;
|
|
@@ -25,13 +25,14 @@ declare const __VLS_export: import("vue").DefineComponent<CodeEditorProps, CodeE
|
|
|
25
25
|
scrollY: boolean;
|
|
26
26
|
language: import(".").CodeLanguage<any> | null;
|
|
27
27
|
tabSize: number;
|
|
28
|
-
worker: boolean | (() => Worker);
|
|
29
28
|
messages: Partial<import("./locale").CodeMessages>;
|
|
30
29
|
modelValue: string;
|
|
31
30
|
lineNumbers: boolean;
|
|
32
31
|
gutter: boolean;
|
|
33
32
|
bordered: boolean;
|
|
34
33
|
activeLine: boolean;
|
|
34
|
+
worker: import(".").CodeWorkerOption;
|
|
35
|
+
syncHighlightLines: number;
|
|
35
36
|
closeBrackets: boolean;
|
|
36
37
|
matchBrackets: boolean;
|
|
37
38
|
searchable: boolean;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -21,13 +21,14 @@ export declare const DouCodeEditor: import("dou-ui").WithInstall<import("vue").D
|
|
|
21
21
|
scrollY: boolean;
|
|
22
22
|
language: import("./index.js").CodeLanguage<any> | null;
|
|
23
23
|
tabSize: number;
|
|
24
|
-
worker: boolean | (() => Worker);
|
|
25
24
|
messages: Partial<import("./locale").CodeMessages>;
|
|
26
25
|
modelValue: string;
|
|
27
26
|
lineNumbers: boolean;
|
|
28
27
|
gutter: boolean;
|
|
29
28
|
bordered: boolean;
|
|
30
29
|
activeLine: boolean;
|
|
30
|
+
worker: import("./index.js").CodeWorkerOption;
|
|
31
|
+
syncHighlightLines: number;
|
|
31
32
|
closeBrackets: boolean;
|
|
32
33
|
matchBrackets: boolean;
|
|
33
34
|
searchable: boolean;
|
|
@@ -82,15 +83,18 @@ export declare const DouCodeDiff: import("dou-ui").WithInstall<{
|
|
|
82
83
|
toolbar: boolean;
|
|
83
84
|
language: import("./index.js").CodeLanguage<any> | null;
|
|
84
85
|
tabSize: number;
|
|
86
|
+
mode: "auto" | "split" | "inline";
|
|
85
87
|
original: string;
|
|
86
88
|
messages: Partial<import("./locale").CodeMessages>;
|
|
87
|
-
mode: "split" | "inline" | "auto";
|
|
88
89
|
modelValue: string;
|
|
89
90
|
bordered: boolean;
|
|
91
|
+
worker: import("./index.js").CodeWorkerOption;
|
|
92
|
+
syncHighlightLines: number;
|
|
90
93
|
wrap: boolean;
|
|
91
94
|
extensions: import("./extension").CodeExtension[];
|
|
92
95
|
editable: boolean;
|
|
93
96
|
widgets: import("./types").CodeDiffWidget[];
|
|
97
|
+
syncDiffLines: number;
|
|
94
98
|
collapseUnchanged: boolean | {
|
|
95
99
|
context?: number;
|
|
96
100
|
minLines?: number;
|
|
@@ -114,15 +118,18 @@ export declare const DouCodeDiff: import("dou-ui").WithInstall<{
|
|
|
114
118
|
toolbar: boolean;
|
|
115
119
|
language: import("./index.js").CodeLanguage<any> | null;
|
|
116
120
|
tabSize: number;
|
|
121
|
+
mode: "auto" | "split" | "inline";
|
|
117
122
|
original: string;
|
|
118
123
|
messages: Partial<import("./locale").CodeMessages>;
|
|
119
|
-
mode: "split" | "inline" | "auto";
|
|
120
124
|
modelValue: string;
|
|
121
125
|
bordered: boolean;
|
|
126
|
+
worker: import("./index.js").CodeWorkerOption;
|
|
127
|
+
syncHighlightLines: number;
|
|
122
128
|
wrap: boolean;
|
|
123
129
|
extensions: import("./extension").CodeExtension[];
|
|
124
130
|
editable: boolean;
|
|
125
131
|
widgets: import("./types").CodeDiffWidget[];
|
|
132
|
+
syncDiffLines: number;
|
|
126
133
|
collapseUnchanged: boolean | {
|
|
127
134
|
context?: number;
|
|
128
135
|
minLines?: number;
|
|
@@ -146,15 +153,18 @@ export declare const DouCodeDiff: import("dou-ui").WithInstall<{
|
|
|
146
153
|
toolbar: boolean;
|
|
147
154
|
language: import("./index.js").CodeLanguage<any> | null;
|
|
148
155
|
tabSize: number;
|
|
156
|
+
mode: "auto" | "split" | "inline";
|
|
149
157
|
original: string;
|
|
150
158
|
messages: Partial<import("./locale").CodeMessages>;
|
|
151
|
-
mode: "split" | "inline" | "auto";
|
|
152
159
|
modelValue: string;
|
|
153
160
|
bordered: boolean;
|
|
161
|
+
worker: import("./index.js").CodeWorkerOption;
|
|
162
|
+
syncHighlightLines: number;
|
|
154
163
|
wrap: boolean;
|
|
155
164
|
extensions: import("./extension").CodeExtension[];
|
|
156
165
|
editable: boolean;
|
|
157
166
|
widgets: import("./types").CodeDiffWidget[];
|
|
167
|
+
syncDiffLines: number;
|
|
158
168
|
collapseUnchanged: boolean | {
|
|
159
169
|
context?: number;
|
|
160
170
|
minLines?: number;
|
|
@@ -187,8 +197,10 @@ export { defaultKeymap } from './core/keymap';
|
|
|
187
197
|
export { findPartner, matchBracket } from './core/brackets';
|
|
188
198
|
export type { BracketMatch } from './core/brackets';
|
|
189
199
|
export { CodeStream } from './language/stream';
|
|
190
|
-
export { copyPlain, defineCodeLanguage, languageFor, lineParts, samePlain, tokenizeLine, tokenTypes, } from './language/language';
|
|
191
|
-
export type { CodeLanguage, CodeLanguageSpec, CodeLineTokens, CodeTokenType, } from './language/language';
|
|
200
|
+
export { copyPlain, defineCodeLanguage, languageFor, lineParts, remoteCodeLanguage, samePlain, tokenizeLine, tokenTypes, } from './language/language';
|
|
201
|
+
export type { CodeLanguage, CodeLanguageSpec, CodeLineTokens, CodeTokenType, RemoteCodeLanguageOptions, } from './language/language';
|
|
202
|
+
export { createCodeWorkerPool } from './language/worker-client';
|
|
203
|
+
export type { CodeWorkerOption, CodeWorkerPool, CodeWorkerPoolOptions, } from './language/worker-client';
|
|
192
204
|
export { CodeHighlighter } from './language/highlighter';
|
|
193
205
|
export { createCodeHighlighter, highlightToHtml } from './language/to-html';
|
|
194
206
|
export { createJavaScript, javascript, jsx, tsx, typescript } from './languages/javascript';
|
|
@@ -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
|
|
7
|
-
*
|
|
8
|
-
*
|
|
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
|
-
|
|
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
|
-
/**
|
|
44
|
-
|
|
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
|
-
|
|
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 {
|
|
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
|
-
/**
|
|
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>;
|
package/dist/types/types.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ import type { CodeSearchQuery } from './core/search';
|
|
|
7
7
|
import type { CodeSelection } from './core/selection';
|
|
8
8
|
import type { CodeMessages } from './locale';
|
|
9
9
|
import type { CodeLanguage } from './language/language';
|
|
10
|
+
import type { CodeWorkerOption } from './language/worker-client';
|
|
10
11
|
import type { CodeText } from './core/text';
|
|
11
12
|
import type { CodeState, CodeTransaction, CodeTransactionMeta, CodeTransactionResult } from './core/transaction';
|
|
12
13
|
export interface CodeEditorProps {
|
|
@@ -57,14 +58,22 @@ export interface CodeEditorProps {
|
|
|
57
58
|
/** The language: colors its tokens, and tells how to indent, comment and pair brackets. */
|
|
58
59
|
language?: CodeLanguage | null;
|
|
59
60
|
/**
|
|
60
|
-
* Reads the code for its colors in a worker, so
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
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.
|
|
66
67
|
*/
|
|
67
|
-
worker?:
|
|
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).
|
|
75
|
+
*/
|
|
76
|
+
syncHighlightLines?: number;
|
|
68
77
|
/** Typing an opening bracket or quote also types its partner. On by default. */
|
|
69
78
|
closeBrackets?: boolean;
|
|
70
79
|
/** Outlines the bracket next to the cursor and its partner. On by default. */
|
|
@@ -245,6 +254,26 @@ export interface CodeDiffProps {
|
|
|
245
254
|
/** The text after, bound with `v-model`; editable beside the original with `editable`. */
|
|
246
255
|
modelValue?: string;
|
|
247
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;
|
|
248
277
|
/**
|
|
249
278
|
* `split` puts the two texts side by side, `inline` puts the changes in
|
|
250
279
|
* one column. `auto` (the default) is `split` when there is room (560px)
|