@nibling/editor 0.0.0-stage → 0.0.2
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/LICENSE +21 -0
- package/README.md +53 -2
- package/dist/commands/autoclose.d.ts +13 -0
- package/dist/commands/autoclose.js +78 -0
- package/dist/commands/commands.d.ts +104 -0
- package/dist/commands/commands.js +442 -0
- package/dist/commands/comments.d.ts +3 -0
- package/dist/commands/comments.js +9 -0
- package/dist/commands/fold.d.ts +18 -0
- package/dist/commands/fold.js +75 -0
- package/dist/commands/keymap.d.ts +40 -0
- package/dist/commands/keymap.js +118 -0
- package/dist/complete/autocomplete.d.ts +35 -0
- package/dist/complete/autocomplete.js +386 -0
- package/dist/complete/source.d.ts +72 -0
- package/dist/complete/source.js +52 -0
- package/dist/complete/words.d.ts +53 -0
- package/dist/complete/words.js +203 -0
- package/dist/element.d.ts +119 -0
- package/dist/element.js +1011 -0
- package/dist/expand/expansions.d.ts +68 -0
- package/dist/expand/expansions.js +143 -0
- package/dist/expand/projection.d.ts +73 -0
- package/dist/expand/projection.js +189 -0
- package/dist/expand/resolver.d.ts +42 -0
- package/dist/expand/resolver.js +585 -0
- package/dist/expand/slice-tokens.d.ts +25 -0
- package/dist/expand/slice-tokens.js +79 -0
- package/dist/extension.d.ts +58 -0
- package/dist/extension.js +1 -0
- package/dist/find/find.d.ts +12 -0
- package/dist/find/find.js +469 -0
- package/dist/find/search.d.ts +81 -0
- package/dist/find/search.js +308 -0
- package/dist/headless.d.ts +16 -0
- package/dist/headless.js +19 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/inline-expansion.d.ts +89 -0
- package/dist/inline-expansion.js +324 -0
- package/dist/input/edit-context.d.ts +23 -0
- package/dist/input/edit-context.js +102 -0
- package/dist/input/surface.d.ts +101 -0
- package/dist/input/surface.js +84 -0
- package/dist/input/textarea.d.ts +26 -0
- package/dist/input/textarea.js +125 -0
- package/dist/sheet.d.ts +6 -0
- package/dist/sheet.js +12 -0
- package/dist/state/core.d.ts +73 -0
- package/dist/state/core.js +86 -0
- package/dist/state/fold-ranges.d.ts +41 -0
- package/dist/state/fold-ranges.js +167 -0
- package/dist/state/folds.d.ts +45 -0
- package/dist/state/folds.js +240 -0
- package/dist/state/history.d.ts +25 -0
- package/dist/state/history.js +127 -0
- package/dist/state/indent.d.ts +33 -0
- package/dist/state/indent.js +121 -0
- package/dist/state/selection.d.ts +45 -0
- package/dist/state/selection.js +106 -0
- package/dist/state/whitespace.d.ts +16 -0
- package/dist/state/whitespace.js +54 -0
- package/dist/text/changeset.d.ts +56 -0
- package/dist/text/changeset.js +321 -0
- package/dist/text/text.d.ts +30 -0
- package/dist/text/text.js +321 -0
- package/dist/tokens/bridge.d.ts +107 -0
- package/dist/tokens/bridge.js +461 -0
- package/dist/tokens/engine.d.ts +39 -0
- package/dist/tokens/engine.js +360 -0
- package/dist/tokens/protocol.d.ts +113 -0
- package/dist/tokens/protocol.js +38 -0
- package/dist/tokens/textmate/ascii-regex.d.ts +5 -0
- package/dist/tokens/textmate/ascii-regex.js +156 -0
- package/dist/tokens/textmate/index.d.ts +7 -0
- package/dist/tokens/textmate/index.js +19 -0
- package/dist/tokens/textmate/linker.d.ts +67 -0
- package/dist/tokens/textmate/linker.js +238 -0
- package/dist/tokens/textmate/scopes.d.ts +14 -0
- package/dist/tokens/textmate/scopes.js +54 -0
- package/dist/tokens/textmate/theme.d.ts +54 -0
- package/dist/tokens/textmate/theme.js +138 -0
- package/dist/tokens/textmate/tokenizer.d.ts +29 -0
- package/dist/tokens/textmate/tokenizer.js +684 -0
- package/dist/tokens/textmate.d.ts +49 -0
- package/dist/tokens/textmate.js +4 -0
- package/dist/view/decorations.d.ts +183 -0
- package/dist/view/decorations.js +51 -0
- package/dist/view/guides.d.ts +58 -0
- package/dist/view/guides.js +174 -0
- package/dist/view/heightmap.d.ts +97 -0
- package/dist/view/heightmap.js +229 -0
- package/dist/view/line-dom.d.ts +54 -0
- package/dist/view/line-dom.js +168 -0
- package/dist/view/measure.d.ts +37 -0
- package/dist/view/measure.js +136 -0
- package/dist/view/overlays.d.ts +32 -0
- package/dist/view/overlays.js +135 -0
- package/dist/view/rail.d.ts +27 -0
- package/dist/view/rail.js +232 -0
- package/dist/view/remote.d.ts +24 -0
- package/dist/view/remote.js +143 -0
- package/dist/view/scope-focus.d.ts +27 -0
- package/dist/view/scope-focus.js +255 -0
- package/dist/view/view.d.ts +114 -0
- package/dist/view/view.js +1231 -0
- package/dist/vite.d.ts +12 -0
- package/dist/vite.js +66 -0
- package/dist/worker.d.ts +1 -0
- package/dist/worker.js +64 -0
- package/grammars/astro.mjs +2 -0
- package/grammars/chunks/r-90cb351dba.mjs +1 -0
- package/grammars/chunks/r-b92a500aab.mjs +1 -0
- package/grammars/chunks/r-ced5311992.mjs +1 -0
- package/grammars/chunks/r-ebae91d4ec.mjs +1 -0
- package/grammars/chunks/s-1821e66170.mjs +1 -0
- package/grammars/css.mjs +1 -0
- package/grammars/diff.mjs +1 -0
- package/grammars/docker.mjs +1 -0
- package/grammars/dotenv.mjs +1 -0
- package/grammars/graphql.mjs +1 -0
- package/grammars/html.mjs +2 -0
- package/grammars/ini.mjs +1 -0
- package/grammars/javascript.mjs +5 -0
- package/grammars/json.mjs +1 -0
- package/grammars/json5.mjs +1 -0
- package/grammars/jsonc.mjs +1 -0
- package/grammars/less.mjs +1 -0
- package/grammars/manifest.json +58 -0
- package/grammars/markdown.mjs +1 -0
- package/grammars/mdx.mjs +1 -0
- package/grammars/pug.mjs +1 -0
- package/grammars/scss.mjs +1 -0
- package/grammars/shellscript.mjs +1 -0
- package/grammars/sql.mjs +1 -0
- package/grammars/stylus.mjs +1 -0
- package/grammars/svelte.mjs +1 -0
- package/grammars/toml.mjs +1 -0
- package/grammars/tsx.mjs +5 -0
- package/grammars/typescript.mjs +2 -0
- package/grammars/vue.mjs +1 -0
- package/grammars/yaml.mjs +1 -0
- package/package.json +71 -3
- package/themes/dark-plus.json +1 -0
- package/themes/light-plus.json +1 -0
- package/themes/monokai.json +1 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { LinkedGrammar } from "./linker.js";
|
|
2
|
+
import { ScopeTable } from "./scopes.js";
|
|
3
|
+
import { Theme, ThemePair } from "./theme.js";
|
|
4
|
+
import { Tokenizer } from "./tokenizer.js";
|
|
5
|
+
export { LinkedGrammar, ScopeTable, Theme, ThemePair, Tokenizer };
|
|
6
|
+
function own(value, cls, what) {
|
|
7
|
+
if (!(value instanceof cls))
|
|
8
|
+
throw new TypeError(`${what} was not made by this runtime`);
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
export const runtime = {
|
|
12
|
+
linkedGrammar: (root, opts) => new LinkedGrammar(root, opts),
|
|
13
|
+
tokenizer: (grammar, scopes) => new Tokenizer(own(grammar, LinkedGrammar, 'grammar'), own(scopes, ScopeTable, 'scope table')),
|
|
14
|
+
scopeTable: () => new ScopeTable(),
|
|
15
|
+
theme: (compiled, dark) => {
|
|
16
|
+
const t = new Theme(compiled);
|
|
17
|
+
return dark === undefined ? t : new ThemePair(t, new Theme(dark));
|
|
18
|
+
},
|
|
19
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { GrammarModule, LinkedGrammar as LinkedGrammarApi } from '../textmate.ts';
|
|
2
|
+
export declare const MATCH = 0, INCLUDE = 1, BEGIN_END = 2, BEGIN_WHILE = 3, CAPTURE = 4;
|
|
3
|
+
type Name = string | null;
|
|
4
|
+
type Ids = readonly number[];
|
|
5
|
+
/** Rule tuples as compile.mjs emits them. Ids > 0 are rules, ids < 0 are holes (module only). */
|
|
6
|
+
export type Rule = readonly [kind: typeof MATCH, name: Name, pat: number, captures: Ids] | readonly [kind: typeof INCLUDE, name: Name, contentName: Name, patterns: Ids, missing: number] | readonly [kind: typeof BEGIN_END, name: Name, contentName: Name, begin: number, beginCaptures: Ids, end: number, endCaptures: Ids, applyEndPatternLast: number, patterns: Ids, missing: number] | readonly [kind: typeof BEGIN_WHILE, name: Name, contentName: Name, begin: number, beginCaptures: Ids, whilePat: number, whileCaptures: Ids, patterns: Ids, missing: number] | readonly [kind: typeof CAPTURE, name: Name, contentName: Name, retokenize: number];
|
|
7
|
+
export interface RegexExtra {
|
|
8
|
+
readonly clip?: number;
|
|
9
|
+
readonly transfers?: unknown;
|
|
10
|
+
readonly hidden?: readonly number[];
|
|
11
|
+
readonly tpl?: number;
|
|
12
|
+
readonly ascii?: string;
|
|
13
|
+
/** Literals a line must contain one of for either form to match; lowercase under the i flag. */
|
|
14
|
+
readonly gate?: readonly string[];
|
|
15
|
+
}
|
|
16
|
+
/** [source, flags, extra?], or null for a pattern the converter could not take. */
|
|
17
|
+
export type RegexEntry = readonly [source: string, flags: string, extra?: RegexExtra] | null;
|
|
18
|
+
interface ModuleStructure {
|
|
19
|
+
readonly root: number;
|
|
20
|
+
readonly repo: Readonly<Record<string, number>>;
|
|
21
|
+
readonly rules: readonly (Rule | 0)[];
|
|
22
|
+
/** Per pat: its four anchor variants as regex ids, index allowA * 2 + allowG. */
|
|
23
|
+
readonly pats: readonly Ids[];
|
|
24
|
+
readonly injections: readonly (readonly [selector: string, rule: number])[];
|
|
25
|
+
readonly externals: readonly (readonly [scope: string, rule: string | null])[];
|
|
26
|
+
readonly triggers: readonly (readonly [scope: string, source: string | null, flags?: string | null])[];
|
|
27
|
+
readonly probes: Readonly<Record<string, readonly [source: string | null, flags: string | null]>>;
|
|
28
|
+
}
|
|
29
|
+
/** A compiled module (grammars/<lang>.mjs default export). */
|
|
30
|
+
export interface ModuleData extends GrammarModule {
|
|
31
|
+
readonly suffix: string;
|
|
32
|
+
readonly structure: ModuleStructure;
|
|
33
|
+
readonly regexes: readonly (readonly [chunk: string, entries: readonly RegexEntry[]])[];
|
|
34
|
+
}
|
|
35
|
+
/** A pruned rule's probe and the bits of the grammars whose absence pruned it. */
|
|
36
|
+
export type Probe = readonly [re: RegExp, bits: number];
|
|
37
|
+
export interface Effective {
|
|
38
|
+
readonly list: number[];
|
|
39
|
+
readonly missing: boolean;
|
|
40
|
+
readonly lost: number;
|
|
41
|
+
readonly pruned: Probe[];
|
|
42
|
+
}
|
|
43
|
+
export declare class LinkedGrammar implements LinkedGrammarApi {
|
|
44
|
+
#private;
|
|
45
|
+
readonly available: (scope: string) => boolean;
|
|
46
|
+
readonly scopeName: string;
|
|
47
|
+
readonly rules: Rule[];
|
|
48
|
+
readonly pats: Ids[];
|
|
49
|
+
readonly regexes: RegexEntry[];
|
|
50
|
+
readonly injections: [selector: string, rule: number][];
|
|
51
|
+
effective: (Effective | undefined)[];
|
|
52
|
+
readonly root: number;
|
|
53
|
+
/** available(scope) says whether a grammar could ever be linked. A missing grammar that never
|
|
54
|
+
* will be does not need its absence tracked. */
|
|
55
|
+
constructor(rootModule: GrammarModule, { available }?: {
|
|
56
|
+
available?: (scope: string) => boolean;
|
|
57
|
+
});
|
|
58
|
+
/** One bit per embedded scope, so a line can record which missing grammars it consulted. */
|
|
59
|
+
bit(scope: string): number;
|
|
60
|
+
has(scope: string): boolean;
|
|
61
|
+
/** Link an embedded grammar. A grammar that embeds its own scope gets a second instance, as
|
|
62
|
+
* vscode-textmate builds one. */
|
|
63
|
+
link(module: GrammarModule): boolean;
|
|
64
|
+
/** Embedded grammars a document needs, by the pre-scan triggers of every linked grammar. */
|
|
65
|
+
scan(lines: readonly string[], into?: Set<string>): Set<string>;
|
|
66
|
+
}
|
|
67
|
+
export {};
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
export const MATCH = 0, INCLUDE = 1, BEGIN_END = 2, BEGIN_WHILE = 3, CAPTURE = 4;
|
|
2
|
+
const BASE = '$base';
|
|
3
|
+
const SUFFIX = '.§';
|
|
4
|
+
// Rule 0 is never consulted: capture lists use 0 for "no capture" and pattern lists hold ids >= 1.
|
|
5
|
+
const NO_RULE = [CAPTURE, null, null, 0];
|
|
6
|
+
function asModule(m) {
|
|
7
|
+
const d = m;
|
|
8
|
+
if (typeof d.suffix !== 'string' || !d.structure || !Array.isArray(d.regexes))
|
|
9
|
+
throw new TypeError(`not a grammar module: ${m.scopeName}`);
|
|
10
|
+
return d;
|
|
11
|
+
}
|
|
12
|
+
export class LinkedGrammar {
|
|
13
|
+
available;
|
|
14
|
+
scopeName;
|
|
15
|
+
rules = [NO_RULE];
|
|
16
|
+
pats = [];
|
|
17
|
+
regexes = [];
|
|
18
|
+
injections = [];
|
|
19
|
+
effective = [];
|
|
20
|
+
root;
|
|
21
|
+
#probes = new Map();
|
|
22
|
+
#externs = [];
|
|
23
|
+
#externIndex = new Map();
|
|
24
|
+
#instances = new Map();
|
|
25
|
+
#bits = new Map();
|
|
26
|
+
#triggers = [];
|
|
27
|
+
#chunkBase = new Map();
|
|
28
|
+
#rootInstance;
|
|
29
|
+
/** available(scope) says whether a grammar could ever be linked. A missing grammar that never
|
|
30
|
+
* will be does not need its absence tracked. */
|
|
31
|
+
constructor(rootModule, { available = () => true } = {}) {
|
|
32
|
+
const mod = asModule(rootModule);
|
|
33
|
+
this.available = available;
|
|
34
|
+
this.scopeName = mod.scopeName;
|
|
35
|
+
this.#rootInstance = this.#append(mod);
|
|
36
|
+
this.root = this.#rootInstance.root;
|
|
37
|
+
for (const [selector, id] of mod.structure.injections)
|
|
38
|
+
this.injections.push([selector, id + this.#rootInstance.ruleBase]);
|
|
39
|
+
this.#relink();
|
|
40
|
+
}
|
|
41
|
+
/** One bit per embedded scope, so a line can record which missing grammars it consulted. */
|
|
42
|
+
bit(scope) {
|
|
43
|
+
let b = this.#bits.get(scope);
|
|
44
|
+
if (b === undefined) {
|
|
45
|
+
b = this.#bits.size < 31 ? 1 << this.#bits.size : 1 << 30;
|
|
46
|
+
this.#bits.set(scope, b);
|
|
47
|
+
}
|
|
48
|
+
return b;
|
|
49
|
+
}
|
|
50
|
+
has(scope) {
|
|
51
|
+
return this.#instances.has(scope);
|
|
52
|
+
}
|
|
53
|
+
/** Link an embedded grammar. A grammar that embeds its own scope gets a second instance, as
|
|
54
|
+
* vscode-textmate builds one. */
|
|
55
|
+
link(module) {
|
|
56
|
+
const mod = asModule(module);
|
|
57
|
+
if (this.#instances.has(mod.scopeName))
|
|
58
|
+
return false;
|
|
59
|
+
this.#instances.set(mod.scopeName, this.#append(mod));
|
|
60
|
+
this.#relink();
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
// A module's regex ids index the concatenation of its chunks; a chunk another grammar already
|
|
64
|
+
// brought is reused, so each regex compiles once.
|
|
65
|
+
#append(mod) {
|
|
66
|
+
const m = mod.structure;
|
|
67
|
+
const ruleBase = this.rules.length - 1, patBase = this.pats.length;
|
|
68
|
+
const rx = [];
|
|
69
|
+
for (const [chunk, entries] of mod.regexes) {
|
|
70
|
+
let base = this.#chunkBase.get(chunk);
|
|
71
|
+
if (base === undefined) {
|
|
72
|
+
base = this.regexes.length;
|
|
73
|
+
for (const e of entries)
|
|
74
|
+
this.regexes.push(e);
|
|
75
|
+
this.#chunkBase.set(chunk, base);
|
|
76
|
+
}
|
|
77
|
+
for (let i = 0; i < entries.length; i++)
|
|
78
|
+
rx.push(base + i);
|
|
79
|
+
}
|
|
80
|
+
const suffix = `.${mod.suffix}`;
|
|
81
|
+
const name = (v) => (typeof v === 'string' && v.includes(SUFFIX) ? v.split(SUFFIX).join(suffix) : v);
|
|
82
|
+
const externs = m.externals.map(([scope, rule]) => {
|
|
83
|
+
const key = `${scope}#${rule ?? ''}`;
|
|
84
|
+
if (!this.#externIndex.has(key)) {
|
|
85
|
+
this.#externIndex.set(key, this.#externs.length);
|
|
86
|
+
this.#externs.push([scope, rule]);
|
|
87
|
+
}
|
|
88
|
+
return -(this.#externIndex.get(key) + 1);
|
|
89
|
+
});
|
|
90
|
+
const id = (x) => (x > 0 ? x + ruleBase : x < 0 ? externs[-x - 1] : 0);
|
|
91
|
+
const caps = (c) => c.map((x) => (x ? x + ruleBase : 0));
|
|
92
|
+
for (let i = 1; i < m.rules.length; i++) {
|
|
93
|
+
const r = m.rules[i];
|
|
94
|
+
if (r === 0) {
|
|
95
|
+
this.rules.push(NO_RULE);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
switch (r[0]) {
|
|
99
|
+
case MATCH:
|
|
100
|
+
this.rules.push([MATCH, name(r[1]), r[2] + patBase, caps(r[3])]);
|
|
101
|
+
break;
|
|
102
|
+
case INCLUDE:
|
|
103
|
+
this.rules.push([INCLUDE, name(r[1]), name(r[2]), r[3].map(id), r[4]]);
|
|
104
|
+
break;
|
|
105
|
+
case BEGIN_END:
|
|
106
|
+
this.rules.push([BEGIN_END, name(r[1]), name(r[2]), r[3] + patBase, caps(r[4]), r[5] + patBase, caps(r[6]), r[7], r[8].map(id), r[9]]);
|
|
107
|
+
break;
|
|
108
|
+
case BEGIN_WHILE:
|
|
109
|
+
this.rules.push([BEGIN_WHILE, name(r[1]), name(r[2]), r[3] + patBase, caps(r[4]), r[5] + patBase, caps(r[6]), r[7].map(id), r[8]]);
|
|
110
|
+
break;
|
|
111
|
+
case CAPTURE:
|
|
112
|
+
this.rules.push([CAPTURE, name(r[1]), name(r[2]), r[3] ? r[3] + ruleBase : 0]);
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
for (const p of m.pats)
|
|
117
|
+
this.pats.push(p.map((x) => rx[x]));
|
|
118
|
+
const repo = {};
|
|
119
|
+
for (const k in m.repo)
|
|
120
|
+
repo[k] = m.repo[k] + ruleBase;
|
|
121
|
+
for (const k in m.probes) {
|
|
122
|
+
const [src, flags] = m.probes[k];
|
|
123
|
+
this.#probes.set(+k + ruleBase, src === null ? null : new RegExp(src, flags ?? ''));
|
|
124
|
+
}
|
|
125
|
+
for (const t of m.triggers)
|
|
126
|
+
this.#triggers.push(t[1] === null ? { scope: t[0], always: true } : { scope: t[0], re: new RegExp(t[1], t[2] ?? '') });
|
|
127
|
+
return { scopeName: mod.scopeName, root: m.root + ruleBase, repo, ruleBase };
|
|
128
|
+
}
|
|
129
|
+
#resolve(hole) {
|
|
130
|
+
const [scope, rule] = this.#externs[-hole - 1];
|
|
131
|
+
if (scope === BASE)
|
|
132
|
+
return this.root;
|
|
133
|
+
const inst = this.#instances.get(scope);
|
|
134
|
+
if (!inst)
|
|
135
|
+
return -1;
|
|
136
|
+
if (rule === null)
|
|
137
|
+
return inst.root;
|
|
138
|
+
return inst.repo[rule] ?? -1;
|
|
139
|
+
}
|
|
140
|
+
// Effective pattern lists, built the way vscode-textmate's RuleFactory builds a grammar: depth
|
|
141
|
+
// first from the root, captures before patterns, each rule once. A child still under
|
|
142
|
+
// construction when its parent checks it is never pruned, as in vscode-textmate.
|
|
143
|
+
#relink() {
|
|
144
|
+
const n = this.rules.length;
|
|
145
|
+
const state = new Uint8Array(n);
|
|
146
|
+
const eff = new Array(n);
|
|
147
|
+
const visit = (id) => {
|
|
148
|
+
if (state[id])
|
|
149
|
+
return;
|
|
150
|
+
state[id] = 1;
|
|
151
|
+
const r = this.rules[id];
|
|
152
|
+
switch (r[0]) {
|
|
153
|
+
case MATCH:
|
|
154
|
+
visitCaps(r[3]);
|
|
155
|
+
break;
|
|
156
|
+
case INCLUDE:
|
|
157
|
+
eff[id] = patterns(r[3], r[4]);
|
|
158
|
+
break;
|
|
159
|
+
case BEGIN_END:
|
|
160
|
+
visitCaps(r[4]);
|
|
161
|
+
visitCaps(r[6]);
|
|
162
|
+
eff[id] = patterns(r[8], r[9]);
|
|
163
|
+
break;
|
|
164
|
+
case BEGIN_WHILE:
|
|
165
|
+
visitCaps(r[4]);
|
|
166
|
+
visitCaps(r[6]);
|
|
167
|
+
eff[id] = patterns(r[7], r[8]);
|
|
168
|
+
break;
|
|
169
|
+
case CAPTURE:
|
|
170
|
+
if (r[3])
|
|
171
|
+
visit(r[3]);
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
state[id] = 2;
|
|
175
|
+
};
|
|
176
|
+
const visitCaps = (caps) => {
|
|
177
|
+
for (const c of caps)
|
|
178
|
+
if (c)
|
|
179
|
+
visit(c);
|
|
180
|
+
};
|
|
181
|
+
const patterns = (list, staticMissing) => {
|
|
182
|
+
const out = [], pruned = [];
|
|
183
|
+
let lost = 0;
|
|
184
|
+
for (const e of list) {
|
|
185
|
+
let id = e;
|
|
186
|
+
if (e < 0) {
|
|
187
|
+
id = this.#resolve(e);
|
|
188
|
+
if (id === -1) {
|
|
189
|
+
const scope = this.#externs[-e - 1][0];
|
|
190
|
+
if (!this.#instances.has(scope) && this.available(scope))
|
|
191
|
+
lost |= this.bit(scope);
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
visit(id);
|
|
196
|
+
const c = this.rules[id], ce = eff[id];
|
|
197
|
+
if ((c[0] === INCLUDE || c[0] === BEGIN_END || c[0] === BEGIN_WHILE) && state[id] === 2 && ce.missing && ce.list.length === 0) {
|
|
198
|
+
// The rule can only have changed a line where its begin could match, so one probe covers
|
|
199
|
+
// every grammar missing beneath it.
|
|
200
|
+
const probe = this.#probes.get(id);
|
|
201
|
+
const bits = ce.pruned.reduce((a, q) => a | q[1], ce.lost);
|
|
202
|
+
if (probe && bits)
|
|
203
|
+
pruned.push([probe, bits]);
|
|
204
|
+
else {
|
|
205
|
+
lost |= ce.lost;
|
|
206
|
+
for (const q of ce.pruned)
|
|
207
|
+
pruned.push(q);
|
|
208
|
+
}
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
out.push(id);
|
|
212
|
+
}
|
|
213
|
+
return { list: out, missing: list.length + staticMissing !== out.length, lost, pruned };
|
|
214
|
+
};
|
|
215
|
+
visit(this.root);
|
|
216
|
+
for (const [, id] of this.injections)
|
|
217
|
+
visit(id);
|
|
218
|
+
this.effective = eff;
|
|
219
|
+
}
|
|
220
|
+
/** Embedded grammars a document needs, by the pre-scan triggers of every linked grammar. */
|
|
221
|
+
scan(lines, into = new Set()) {
|
|
222
|
+
for (const t of this.#triggers) {
|
|
223
|
+
if (into.has(t.scope) || this.#instances.has(t.scope))
|
|
224
|
+
continue;
|
|
225
|
+
if (t.always) {
|
|
226
|
+
into.add(t.scope);
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
for (let i = 0; i < lines.length; i++) {
|
|
230
|
+
if (t.re.test(`${lines[i]}\n`)) {
|
|
231
|
+
into.add(t.scope);
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return into;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { StyleResolver } from './theme.ts';
|
|
2
|
+
/** Interned scope stacks. A scope id names one path from the root; styles hang off ids per theme. */
|
|
3
|
+
export declare class ScopeTable {
|
|
4
|
+
#private;
|
|
5
|
+
readonly parent: number[];
|
|
6
|
+
readonly name: string[];
|
|
7
|
+
push(parent: number, name: string): number;
|
|
8
|
+
/** TextMate allows "a b" in a name; each segment is its own scope. */
|
|
9
|
+
pushPath(parent: number, path: string): number;
|
|
10
|
+
names(id: number): string[];
|
|
11
|
+
setTheme(theme: StyleResolver): void;
|
|
12
|
+
/** Style id for a scope id under the current theme (vscode-textmate's AttributedScopeStack merge). */
|
|
13
|
+
styleOf(id: number): number;
|
|
14
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/** Interned scope stacks. A scope id names one path from the root; styles hang off ids per theme. */
|
|
2
|
+
export class ScopeTable {
|
|
3
|
+
parent = [];
|
|
4
|
+
name = [];
|
|
5
|
+
#index = new Map();
|
|
6
|
+
#namesCache = [];
|
|
7
|
+
#theme = null;
|
|
8
|
+
#style = [];
|
|
9
|
+
push(parent, name) {
|
|
10
|
+
const key = `${parent}\u0001${name}`;
|
|
11
|
+
let id = this.#index.get(key);
|
|
12
|
+
if (id === undefined) {
|
|
13
|
+
id = this.parent.length;
|
|
14
|
+
this.parent.push(parent);
|
|
15
|
+
this.name.push(name);
|
|
16
|
+
this.#index.set(key, id);
|
|
17
|
+
}
|
|
18
|
+
return id;
|
|
19
|
+
}
|
|
20
|
+
/** TextMate allows "a b" in a name; each segment is its own scope. */
|
|
21
|
+
pushPath(parent, path) {
|
|
22
|
+
if (path.indexOf(' ') === -1)
|
|
23
|
+
return this.push(parent, path);
|
|
24
|
+
let id = parent;
|
|
25
|
+
for (const s of path.split(/ /g))
|
|
26
|
+
id = this.push(id, s);
|
|
27
|
+
return id;
|
|
28
|
+
}
|
|
29
|
+
names(id) {
|
|
30
|
+
let n = this.#namesCache[id];
|
|
31
|
+
if (!n) {
|
|
32
|
+
n = id < 0 ? [] : [...this.names(this.parent[id]), this.name[id]];
|
|
33
|
+
this.#namesCache[id] = n;
|
|
34
|
+
}
|
|
35
|
+
return n;
|
|
36
|
+
}
|
|
37
|
+
setTheme(theme) {
|
|
38
|
+
this.#theme = theme;
|
|
39
|
+
this.#style = [];
|
|
40
|
+
}
|
|
41
|
+
/** Style id for a scope id under the current theme (vscode-textmate's AttributedScopeStack merge). */
|
|
42
|
+
styleOf(id) {
|
|
43
|
+
let s = this.#style[id];
|
|
44
|
+
if (s === undefined) {
|
|
45
|
+
const theme = this.#theme;
|
|
46
|
+
if (!theme)
|
|
47
|
+
throw new Error('styleOf before setTheme');
|
|
48
|
+
const parent = this.parent[id];
|
|
49
|
+
const base = parent < 0 ? theme.defaultStyle : this.styleOf(parent);
|
|
50
|
+
s = this.#style[id] = theme.merge(base, this, id);
|
|
51
|
+
}
|
|
52
|
+
return s;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { ScopeTable } from './scopes.ts';
|
|
2
|
+
/** [fontStyle, fg, bg] or [fontStyle, fg, bg, parentScopes]. */
|
|
3
|
+
type ThemeRule = readonly [number, number, number] | readonly [number, number, number, readonly string[]];
|
|
4
|
+
export interface CompiledThemeData {
|
|
5
|
+
readonly colors: readonly (string | null)[];
|
|
6
|
+
readonly defaults: readonly [number, number, number];
|
|
7
|
+
readonly trie: unknown;
|
|
8
|
+
}
|
|
9
|
+
export interface Style {
|
|
10
|
+
readonly fontStyle: number;
|
|
11
|
+
readonly fg: number;
|
|
12
|
+
readonly bg: number;
|
|
13
|
+
}
|
|
14
|
+
/** A style as CSS values: colors are CSS colors, null where the style sets none. */
|
|
15
|
+
export interface StyleCss {
|
|
16
|
+
readonly fg: string | null;
|
|
17
|
+
readonly bg: string | null;
|
|
18
|
+
readonly fontStyle: number;
|
|
19
|
+
}
|
|
20
|
+
/** What a ScopeTable resolves scopes against: one theme, or a light/dark pair. */
|
|
21
|
+
export interface StyleResolver {
|
|
22
|
+
readonly defaultStyle: number;
|
|
23
|
+
merge(baseStyle: number, scopes: ScopeTable, id: number): number;
|
|
24
|
+
css(style: number): StyleCss;
|
|
25
|
+
}
|
|
26
|
+
export declare class Theme implements StyleResolver {
|
|
27
|
+
#private;
|
|
28
|
+
readonly colors: readonly string[];
|
|
29
|
+
readonly styles: Style[];
|
|
30
|
+
readonly defaultStyle: number;
|
|
31
|
+
constructor(compiled: CompiledThemeData);
|
|
32
|
+
intern(fontStyle: number, fg: number, bg: number): number;
|
|
33
|
+
css(style: number): StyleCss;
|
|
34
|
+
/** Candidate rules for a leaf scope name, most specific first. */
|
|
35
|
+
candidates(name: string): readonly ThemeRule[];
|
|
36
|
+
/** The style for scope id, given its parent's style. */
|
|
37
|
+
merge(baseStyle: number, scopes: ScopeTable, id: number): number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A light and a dark theme resolved together. A style id names a pair of styles, one per theme,
|
|
41
|
+
* so ids are stable across the pair: a scope maps to one class whichever scheme is in use, and
|
|
42
|
+
* `css` gives that class `light-dark(light, dark)` wherever the two disagree. `light-dark()`
|
|
43
|
+
* takes only colors, so where the font styles disagree the dark theme's wins.
|
|
44
|
+
*/
|
|
45
|
+
export declare class ThemePair implements StyleResolver {
|
|
46
|
+
#private;
|
|
47
|
+
readonly light: Theme;
|
|
48
|
+
readonly dark: Theme;
|
|
49
|
+
readonly defaultStyle: number;
|
|
50
|
+
constructor(light: Theme, dark: Theme);
|
|
51
|
+
merge(baseStyle: number, scopes: ScopeTable, id: number): number;
|
|
52
|
+
css(style: number): StyleCss;
|
|
53
|
+
}
|
|
54
|
+
export {};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { lightDark } from "../protocol.js";
|
|
2
|
+
const NOT_SET = -1;
|
|
3
|
+
function matchesScope(name, pattern) {
|
|
4
|
+
return pattern === name || (name.startsWith(pattern) && name[pattern.length] === '.');
|
|
5
|
+
}
|
|
6
|
+
export class Theme {
|
|
7
|
+
colors;
|
|
8
|
+
styles = [];
|
|
9
|
+
defaultStyle;
|
|
10
|
+
#trie;
|
|
11
|
+
#styleIndex = new Map();
|
|
12
|
+
#leafCache = new Map();
|
|
13
|
+
constructor(compiled) {
|
|
14
|
+
// colors[0] is null (no color); every other entry is a hex string.
|
|
15
|
+
this.colors = compiled.colors;
|
|
16
|
+
this.#trie = compiled.trie;
|
|
17
|
+
const [fs, fg, bg] = compiled.defaults;
|
|
18
|
+
this.defaultStyle = this.intern(fs, fg, bg);
|
|
19
|
+
}
|
|
20
|
+
intern(fontStyle, fg, bg) {
|
|
21
|
+
const key = (fontStyle & 0xf) | (fg << 4) | (bg << 14);
|
|
22
|
+
let id = this.#styleIndex.get(key);
|
|
23
|
+
if (id === undefined) {
|
|
24
|
+
id = this.styles.length;
|
|
25
|
+
this.styles.push({ fontStyle, fg, bg });
|
|
26
|
+
this.#styleIndex.set(key, id);
|
|
27
|
+
}
|
|
28
|
+
return id;
|
|
29
|
+
}
|
|
30
|
+
css(style) {
|
|
31
|
+
const st = this.styles[style];
|
|
32
|
+
return { fg: this.colors[st.fg] ?? null, bg: this.colors[st.bg] ?? null, fontStyle: st.fontStyle };
|
|
33
|
+
}
|
|
34
|
+
/** Candidate rules for a leaf scope name, most specific first. */
|
|
35
|
+
candidates(name) {
|
|
36
|
+
const r = this.#leafCache.get(name);
|
|
37
|
+
if (r)
|
|
38
|
+
return r;
|
|
39
|
+
let node = this.#trie;
|
|
40
|
+
let rest = name;
|
|
41
|
+
while (rest !== '') {
|
|
42
|
+
const dot = rest.indexOf('.');
|
|
43
|
+
const head = dot === -1 ? rest : rest.slice(0, dot);
|
|
44
|
+
const kids = node[1];
|
|
45
|
+
if (!kids || !Object.hasOwn(kids, head))
|
|
46
|
+
break;
|
|
47
|
+
node = kids[head];
|
|
48
|
+
rest = dot === -1 ? '' : rest.slice(dot + 1);
|
|
49
|
+
}
|
|
50
|
+
this.#leafCache.set(name, node[0]);
|
|
51
|
+
return node[0];
|
|
52
|
+
}
|
|
53
|
+
/** The style for scope id, given its parent's style. */
|
|
54
|
+
merge(baseStyle, scopes, id) {
|
|
55
|
+
const rules = this.candidates(scopes.name[id]);
|
|
56
|
+
let hit = null;
|
|
57
|
+
for (const rule of rules) {
|
|
58
|
+
if (rule.length < 4 || parentsMatch(scopes, scopes.parent[id], rule[3])) {
|
|
59
|
+
hit = rule;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (!hit)
|
|
64
|
+
return baseStyle;
|
|
65
|
+
const b = this.styles[baseStyle];
|
|
66
|
+
const fs = hit[0] !== NOT_SET ? hit[0] : b.fontStyle;
|
|
67
|
+
const fg = hit[1] !== 0 ? hit[1] : b.fg;
|
|
68
|
+
const bg = hit[2] !== 0 ? hit[2] : b.bg;
|
|
69
|
+
return this.intern(fs, fg, bg);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A light and a dark theme resolved together. A style id names a pair of styles, one per theme,
|
|
74
|
+
* so ids are stable across the pair: a scope maps to one class whichever scheme is in use, and
|
|
75
|
+
* `css` gives that class `light-dark(light, dark)` wherever the two disagree. `light-dark()`
|
|
76
|
+
* takes only colors, so where the font styles disagree the dark theme's wins.
|
|
77
|
+
*/
|
|
78
|
+
export class ThemePair {
|
|
79
|
+
light;
|
|
80
|
+
dark;
|
|
81
|
+
defaultStyle;
|
|
82
|
+
/** [light style, dark style] per pair id. */
|
|
83
|
+
#pairs = [];
|
|
84
|
+
#index = new Map();
|
|
85
|
+
constructor(light, dark) {
|
|
86
|
+
this.light = light;
|
|
87
|
+
this.dark = dark;
|
|
88
|
+
this.defaultStyle = this.#intern(light.defaultStyle, dark.defaultStyle);
|
|
89
|
+
}
|
|
90
|
+
#intern(l, d) {
|
|
91
|
+
// Style ids stay far below 2^16 (one per distinct fontStyle/fg/bg), so the key is exact.
|
|
92
|
+
const key = l * 0x10000 + d;
|
|
93
|
+
let id = this.#index.get(key);
|
|
94
|
+
if (id === undefined) {
|
|
95
|
+
id = this.#pairs.length;
|
|
96
|
+
this.#pairs.push([l, d]);
|
|
97
|
+
this.#index.set(key, id);
|
|
98
|
+
}
|
|
99
|
+
return id;
|
|
100
|
+
}
|
|
101
|
+
merge(baseStyle, scopes, id) {
|
|
102
|
+
const [l, d] = this.#pairs[baseStyle];
|
|
103
|
+
return this.#intern(this.light.merge(l, scopes, id), this.dark.merge(d, scopes, id));
|
|
104
|
+
}
|
|
105
|
+
css(style) {
|
|
106
|
+
const [l, d] = this.#pairs[style];
|
|
107
|
+
const a = this.light.css(l), b = this.dark.css(d);
|
|
108
|
+
return {
|
|
109
|
+
// A side with no foreground inherits its theme's default.
|
|
110
|
+
fg: lightDark(a.fg ?? this.light.css(this.light.defaultStyle).fg, b.fg ?? this.dark.css(this.dark.defaultStyle).fg),
|
|
111
|
+
bg: lightDark(a.bg, b.bg),
|
|
112
|
+
fontStyle: b.fontStyle,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function parentsMatch(scopes, id, parentScopes) {
|
|
117
|
+
for (let i = 0; i < parentScopes.length; i++) {
|
|
118
|
+
let pattern = parentScopes[i];
|
|
119
|
+
let mustMatch = false;
|
|
120
|
+
if (pattern === '>') {
|
|
121
|
+
if (i === parentScopes.length - 1)
|
|
122
|
+
return false;
|
|
123
|
+
pattern = parentScopes[++i];
|
|
124
|
+
mustMatch = true;
|
|
125
|
+
}
|
|
126
|
+
while (id >= 0) {
|
|
127
|
+
if (matchesScope(scopes.name[id], pattern))
|
|
128
|
+
break;
|
|
129
|
+
if (mustMatch)
|
|
130
|
+
return false;
|
|
131
|
+
id = scopes.parent[id];
|
|
132
|
+
}
|
|
133
|
+
if (id < 0)
|
|
134
|
+
return false;
|
|
135
|
+
id = scopes.parent[id];
|
|
136
|
+
}
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Frame as FrameApi, LineResult, Tokenizer as TokenizerApi } from '../textmate.ts';
|
|
2
|
+
import { type LinkedGrammar } from './linker.ts';
|
|
3
|
+
import { ScopeTable } from './scopes.ts';
|
|
4
|
+
export declare class Frame implements FrameApi {
|
|
5
|
+
readonly parent: Frame | null;
|
|
6
|
+
readonly ruleId: number;
|
|
7
|
+
readonly enterPos: number;
|
|
8
|
+
readonly anchorPos: number;
|
|
9
|
+
readonly gen: number;
|
|
10
|
+
readonly capturedEOL: boolean;
|
|
11
|
+
readonly endRule: string | null;
|
|
12
|
+
readonly nameScopes: number;
|
|
13
|
+
readonly contentScopes: number;
|
|
14
|
+
readonly depth: number;
|
|
15
|
+
constructor(parent: Frame | null, ruleId: number, enterPos: number, anchorPos: number, gen: number, capturedEOL: boolean, endRule: string | null, nameScopes: number, contentScopes: number);
|
|
16
|
+
equals(other: Frame | null | undefined): boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare function passesGate(gate: readonly string[], icase: boolean, str: string): boolean;
|
|
19
|
+
export declare class Tokenizer implements TokenizerApi {
|
|
20
|
+
#private;
|
|
21
|
+
readonly initial: Frame;
|
|
22
|
+
constructor(grammar: LinkedGrammar, scopes?: ScopeTable);
|
|
23
|
+
/** Call after the linked grammar relinks: pattern lists may have changed, rule ids have not. */
|
|
24
|
+
invalidate(): void;
|
|
25
|
+
tokenizeLine(line: string, prevState: Frame | null): LineResult & {
|
|
26
|
+
readonly state: Frame;
|
|
27
|
+
readonly tokens: number[];
|
|
28
|
+
};
|
|
29
|
+
}
|