@innovatorved/code-lab 0.3.2 → 0.5.0
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/ARCHITECTURE.md +5 -3
- package/CHANGELOG.md +41 -0
- package/README.md +53 -175
- package/dist/apps/web/src/index.d.ts +34 -0
- package/dist/code-lab-sql.css +482 -0
- package/dist/code-lab-sql.d.ts +179 -0
- package/dist/code-lab-sql.esm.js +542 -0
- package/dist/code-lab-sql.umd.js +531 -0
- package/dist/code-lab.css +1799 -98
- package/dist/code-lab.d.ts +110 -15
- package/dist/code-lab.esm.js +49678 -23253
- package/dist/code-lab.js +49648 -23251
- package/dist/code-lab.umd.js +75766 -0
- package/dist/diagnostics.d.ts +3 -0
- package/dist/extensions/cpp.esm.js +19504 -3788
- package/dist/extensions/cpp.js +19501 -3785
- package/dist/extensions/guided-lab.esm.js +19401 -3725
- package/dist/extensions/guided-lab.js +19400 -3724
- package/dist/extensions/python.d.ts +1 -0
- package/dist/extensions/python.esm.js +19597 -3788
- package/dist/extensions/python.js +19597 -3788
- package/dist/extensions/umbrella.d.ts +5 -0
- package/dist/index.d.ts +1 -1
- package/dist/modular-kernel.d.ts +11 -0
- package/dist/packages/commands/src/index.d.ts +96 -0
- package/dist/packages/core/src/di.d.ts +36 -0
- package/dist/packages/core/src/disposable.d.ts +27 -0
- package/dist/packages/core/src/errors.d.ts +69 -0
- package/dist/packages/core/src/events.d.ts +38 -0
- package/dist/packages/core/src/index.d.ts +6 -0
- package/dist/packages/core/src/path.d.ts +14 -0
- package/dist/packages/core/src/registry.d.ts +23 -0
- package/dist/packages/editor/src/index.d.ts +118 -0
- package/dist/packages/fs/src/backends.d.ts +37 -0
- package/dist/packages/fs/src/index.d.ts +3 -0
- package/dist/packages/fs/src/types.d.ts +40 -0
- package/dist/packages/fs/src/vfs-service.d.ts +33 -0
- package/dist/packages/preview/src/index.d.ts +34 -0
- package/dist/packages/runtime/src/index.d.ts +47 -0
- package/dist/packages/settings/src/builtin-schemas.d.ts +15 -0
- package/dist/packages/settings/src/configuration-service.d.ts +107 -0
- package/dist/packages/settings/src/index.d.ts +4 -0
- package/dist/packages/settings/src/jsonc.d.ts +20 -0
- package/dist/packages/settings/src/schema.d.ts +28 -0
- package/dist/packages/sql/src/completions.d.ts +8 -0
- package/dist/packages/sql/src/diagnostics.d.ts +2 -0
- package/dist/packages/sql/src/formatter.d.ts +2 -0
- package/dist/packages/sql/src/index.d.ts +7 -0
- package/dist/packages/sql/src/sql-editor.d.ts +2 -0
- package/dist/packages/sql/src/styles.d.ts +2 -0
- package/dist/packages/sql/src/tokenizer.d.ts +13 -0
- package/dist/packages/sql/src/types.d.ts +130 -0
- package/dist/packages/terminal/src/index.d.ts +54 -0
- package/dist/packages/ui/src/index.d.ts +4 -0
- package/dist/packages/ui/src/observability.d.ts +22 -0
- package/dist/packages/ui/src/plugin-registry.d.ts +90 -0
- package/dist/packages/ui/src/settings-dialog.d.ts +53 -0
- package/dist/packages/ui/src/themes.d.ts +40 -0
- package/dist/preview-sw.js +53 -12
- package/dist/runtime.d.ts +3 -3
- package/dist/runtime.esm.js +467 -44
- package/dist/runtime.js +465 -44
- package/dist/schemas.d.ts +234 -814
- package/dist/schemas.esm.js +19401 -3725
- package/dist/schemas.js +19404 -3728
- package/dist/scm.d.ts +92 -11
- package/dist/shell.d.ts +1 -1
- package/dist/shell.esm.js +1300 -151
- package/dist/shell.js +1300 -151
- package/dist/storage.d.ts +47 -0
- package/dist/terminal.esm.js +62 -3
- package/dist/terminal.js +62 -3
- package/dist/workbench/diff-editor.d.ts +1 -0
- package/dist/workbench/index.d.ts +1 -1
- package/dist/workbench/styles.d.ts +1 -1
- package/package.json +38 -9
- package/packages/commands/README.md +7 -0
- package/packages/commands/package.json +16 -0
- package/packages/commands/src/index.ts +467 -0
- package/packages/commands/tsconfig.json +22 -0
- package/packages/core/README.md +12 -0
- package/packages/core/package.json +13 -0
- package/packages/core/src/di.ts +147 -0
- package/packages/core/src/disposable.ts +112 -0
- package/packages/core/src/errors.ts +205 -0
- package/packages/core/src/events.ts +157 -0
- package/packages/core/src/index.ts +49 -0
- package/packages/core/src/path.ts +118 -0
- package/packages/core/src/registry.ts +61 -0
- package/packages/core/tsconfig.json +19 -0
- package/packages/editor/README.md +9 -0
- package/packages/editor/package.json +18 -0
- package/packages/editor/src/index.ts +344 -0
- package/packages/editor/tsconfig.json +24 -0
- package/packages/fs/README.md +7 -0
- package/packages/fs/package.json +16 -0
- package/packages/fs/src/backends.ts +301 -0
- package/packages/fs/src/index.ts +18 -0
- package/packages/fs/src/types.ts +44 -0
- package/packages/fs/src/vfs-service.ts +257 -0
- package/packages/fs/tsconfig.json +22 -0
- package/packages/preview/README.md +3 -0
- package/packages/preview/package.json +16 -0
- package/packages/preview/src/index.ts +125 -0
- package/packages/preview/tsconfig.json +22 -0
- package/packages/runtime/README.md +3 -0
- package/packages/runtime/package.json +17 -0
- package/packages/runtime/src/index.ts +134 -0
- package/packages/runtime/tsconfig.json +23 -0
- package/packages/settings/README.md +7 -0
- package/packages/settings/package.json +16 -0
- package/packages/settings/src/builtin-schemas.ts +246 -0
- package/packages/settings/src/configuration-service.ts +609 -0
- package/packages/settings/src/index.ts +38 -0
- package/packages/settings/src/jsonc.ts +189 -0
- package/packages/settings/src/schema.ts +164 -0
- package/packages/settings/tsconfig.json +22 -0
- package/packages/sql/README.md +12 -0
- package/packages/sql/package.json +13 -0
- package/packages/sql/src/completions.ts +359 -0
- package/packages/sql/src/diagnostics.ts +200 -0
- package/packages/sql/src/formatter.ts +271 -0
- package/packages/sql/src/index.ts +54 -0
- package/packages/sql/src/sql-editor.ts +802 -0
- package/packages/sql/src/styles.ts +501 -0
- package/packages/sql/src/tokenizer.ts +457 -0
- package/packages/sql/src/types.ts +148 -0
- package/packages/sql/tsconfig.json +19 -0
- package/packages/terminal/README.md +3 -0
- package/packages/terminal/package.json +16 -0
- package/packages/terminal/src/index.ts +183 -0
- package/packages/terminal/tsconfig.json +22 -0
- package/packages/ui/README.md +8 -0
- package/packages/ui/package.json +18 -0
- package/packages/ui/src/index.ts +40 -0
- package/packages/ui/src/observability.ts +175 -0
- package/packages/ui/src/plugin-registry.ts +289 -0
- package/packages/ui/src/settings-dialog.ts +575 -0
- package/packages/ui/src/themes.ts +267 -0
- package/packages/ui/tsconfig.json +24 -0
- package/src/code-lab.css +1799 -98
- package/src/diagnostics.ts +139 -7
- package/src/extensions/cpp.ts +88 -45
- package/src/extensions/python.ts +186 -53
- package/src/extensions/umbrella.ts +5 -0
- package/src/index.ts +1 -1
- package/src/modular-kernel.ts +11 -0
- package/src/preview-sw.js +52 -11
- package/src/runtime.ts +466 -46
- package/src/schemas.ts +62 -62
- package/src/scm.ts +781 -58
- package/src/shell.ts +1299 -151
- package/src/storage.ts +336 -3
- package/src/terminal.ts +60 -2
- package/src/workbench/diff-editor.ts +66 -6
- package/src/workbench/icons.ts +12 -2
- package/src/workbench/styles.ts +25 -19
- package/src/workbench.ts +17059 -6673
- package/dist/runtime/index.d.ts +0 -4
- package/dist/shell/coreutils/compilers-cli.d.ts +0 -24
- package/dist/shell/index.d.ts +0 -4
- package/src/runtime/index.ts +0 -5
- package/src/shell/coreutils/compilers-cli.ts +0 -103
- package/src/shell/index.ts +0 -5
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CodeLabError,
|
|
3
|
+
createServiceId,
|
|
4
|
+
Emitter,
|
|
5
|
+
type Event,
|
|
6
|
+
type IDisposable,
|
|
7
|
+
toDisposable
|
|
8
|
+
} from '@code-lab/core';
|
|
9
|
+
|
|
10
|
+
export type ContextValue = string | number | boolean | null | undefined;
|
|
11
|
+
|
|
12
|
+
export interface IContextKeyService extends IDisposable {
|
|
13
|
+
readonly onDidChangeContext: Event<string>;
|
|
14
|
+
setContext(key: string, value: ContextValue): void;
|
|
15
|
+
getContext(key: string): ContextValue;
|
|
16
|
+
deleteContext(key: string): void;
|
|
17
|
+
evaluateWhen(expression: string | undefined): boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const IContextKeyServiceId = createServiceId<IContextKeyService>('code-lab.contextKeys');
|
|
21
|
+
|
|
22
|
+
interface WhenToken {
|
|
23
|
+
type: 'ident' | 'str' | 'num' | 'op' | 'lparen' | 'rparen';
|
|
24
|
+
value: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function tokenizeWhen(expr: string): WhenToken[] {
|
|
28
|
+
const tokens: WhenToken[] = [];
|
|
29
|
+
let i = 0;
|
|
30
|
+
while (i < expr.length) {
|
|
31
|
+
const ch = expr[i];
|
|
32
|
+
if (/\s/.test(ch)) {
|
|
33
|
+
i++;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (ch === '(') {
|
|
37
|
+
tokens.push({ type: 'lparen', value: '(' });
|
|
38
|
+
i++;
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (ch === ')') {
|
|
42
|
+
tokens.push({ type: 'rparen', value: ')' });
|
|
43
|
+
i++;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const two = expr.slice(i, i + 2);
|
|
47
|
+
if (two === '&&' || two === '||' || two === '==' || two === '!=' || two === '=~') {
|
|
48
|
+
const three = expr.slice(i, i + 3);
|
|
49
|
+
if (three === '===' || three === '!==') {
|
|
50
|
+
tokens.push({ type: 'op', value: three.slice(0, 2) });
|
|
51
|
+
i += 3;
|
|
52
|
+
} else {
|
|
53
|
+
tokens.push({ type: 'op', value: two });
|
|
54
|
+
i += 2;
|
|
55
|
+
}
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (ch === '!') {
|
|
59
|
+
tokens.push({ type: 'op', value: '!' });
|
|
60
|
+
i++;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (ch === '\'' || ch === '"') {
|
|
64
|
+
const quote = ch;
|
|
65
|
+
let val = '';
|
|
66
|
+
i++;
|
|
67
|
+
while (i < expr.length && expr[i] !== quote) {
|
|
68
|
+
if (expr[i] === '\\' && i + 1 < expr.length) {
|
|
69
|
+
val += expr[i + 1];
|
|
70
|
+
i += 2;
|
|
71
|
+
} else {
|
|
72
|
+
val += expr[i];
|
|
73
|
+
i++;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (i < expr.length) i++;
|
|
77
|
+
tokens.push({ type: 'str', value: val });
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (/[0-9]/.test(ch)) {
|
|
81
|
+
let num = '';
|
|
82
|
+
while (i < expr.length && /[0-9.]/.test(expr[i])) {
|
|
83
|
+
num += expr[i++];
|
|
84
|
+
}
|
|
85
|
+
tokens.push({ type: 'num', value: num });
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
let ident = '';
|
|
89
|
+
while (i < expr.length && !/[\s()!&|=]/.test(expr[i])) {
|
|
90
|
+
ident += expr[i++];
|
|
91
|
+
}
|
|
92
|
+
if (ident) {
|
|
93
|
+
tokens.push({ type: 'ident', value: ident });
|
|
94
|
+
} else {
|
|
95
|
+
i++;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return tokens;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export class ContextKeyService implements IContextKeyService {
|
|
102
|
+
private readonly store = new Map<string, ContextValue>();
|
|
103
|
+
private readonly changeEmitter = new Emitter<string>({ name: 'context.change' });
|
|
104
|
+
|
|
105
|
+
public get onDidChangeContext(): Event<string> {
|
|
106
|
+
return this.changeEmitter.event;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public setContext(key: string, value: ContextValue): void {
|
|
110
|
+
if (this.store.get(key) === value) return;
|
|
111
|
+
this.store.set(key, value);
|
|
112
|
+
this.changeEmitter.fire(key);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public getContext(key: string): ContextValue {
|
|
116
|
+
return this.store.get(key);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public deleteContext(key: string): void {
|
|
120
|
+
if (this.store.delete(key)) {
|
|
121
|
+
this.changeEmitter.fire(key);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
public evaluateWhen(expression: string | undefined): boolean {
|
|
126
|
+
if (!expression || !expression.trim()) return true;
|
|
127
|
+
const trimmed = expression.trim();
|
|
128
|
+
if (trimmed === 'true') return true;
|
|
129
|
+
if (trimmed === 'false') return false;
|
|
130
|
+
|
|
131
|
+
const tokens = tokenizeWhen(trimmed);
|
|
132
|
+
let pos = 0;
|
|
133
|
+
|
|
134
|
+
const resolveScalar = (tok: WhenToken | undefined): ContextValue => {
|
|
135
|
+
if (!tok) return undefined;
|
|
136
|
+
if (tok.type === 'str') return tok.value;
|
|
137
|
+
if (tok.type === 'num') return Number(tok.value);
|
|
138
|
+
if (tok.value === 'true') return true;
|
|
139
|
+
if (tok.value === 'false') return false;
|
|
140
|
+
if (tok.value === 'null') return null;
|
|
141
|
+
return this.store.get(tok.value);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const parseOr = (): boolean => {
|
|
145
|
+
let left = parseAnd();
|
|
146
|
+
while (pos < tokens.length && tokens[pos].type === 'op' && tokens[pos].value === '||') {
|
|
147
|
+
pos++;
|
|
148
|
+
const right = parseAnd();
|
|
149
|
+
left = left || right;
|
|
150
|
+
}
|
|
151
|
+
return left;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const parseAnd = (): boolean => {
|
|
155
|
+
let left = parseUnary();
|
|
156
|
+
while (pos < tokens.length && tokens[pos].type === 'op' && tokens[pos].value === '&&') {
|
|
157
|
+
pos++;
|
|
158
|
+
const right = parseUnary();
|
|
159
|
+
left = left && right;
|
|
160
|
+
}
|
|
161
|
+
return left;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const parseUnary = (): boolean => {
|
|
165
|
+
if (pos < tokens.length && tokens[pos].type === 'op' && tokens[pos].value === '!') {
|
|
166
|
+
pos++;
|
|
167
|
+
return !parseUnary();
|
|
168
|
+
}
|
|
169
|
+
return parsePrimary();
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const parsePrimary = (): boolean => {
|
|
173
|
+
const tok = tokens[pos];
|
|
174
|
+
if (!tok) return false;
|
|
175
|
+
if (tok.type === 'lparen') {
|
|
176
|
+
pos++;
|
|
177
|
+
const val = parseOr();
|
|
178
|
+
if (pos < tokens.length && tokens[pos].type === 'rparen') pos++;
|
|
179
|
+
return val;
|
|
180
|
+
}
|
|
181
|
+
pos++;
|
|
182
|
+
const next = tokens[pos];
|
|
183
|
+
if (next && next.type === 'op' && (next.value === '==' || next.value === '!=' || next.value === '=~')) {
|
|
184
|
+
pos++;
|
|
185
|
+
const rhsTok = tokens[pos++];
|
|
186
|
+
const lhsVal = resolveScalar(tok);
|
|
187
|
+
if (next.value === '=~' && rhsTok) {
|
|
188
|
+
const patternRaw = rhsTok.value.replace(/^\/|\/[gimsuy]*$/g, '');
|
|
189
|
+
try {
|
|
190
|
+
return new RegExp(patternRaw).test(String(lhsVal ?? ''));
|
|
191
|
+
} catch {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const rhsVal = resolveScalar(rhsTok);
|
|
196
|
+
const eq = String(lhsVal ?? '') === String(rhsVal ?? '');
|
|
197
|
+
return next.value === '==' ? eq : !eq;
|
|
198
|
+
}
|
|
199
|
+
return Boolean(resolveScalar(tok));
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
return parseOr();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
public dispose(): void {
|
|
206
|
+
this.store.clear();
|
|
207
|
+
this.changeEmitter.dispose();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export type CommandHandler<Args extends unknown[] = unknown[], Result = unknown> = (
|
|
212
|
+
...args: Args
|
|
213
|
+
) => Result | Promise<Result>;
|
|
214
|
+
|
|
215
|
+
export interface CommandDescriptor {
|
|
216
|
+
readonly id: string;
|
|
217
|
+
readonly title: string;
|
|
218
|
+
readonly category?: string;
|
|
219
|
+
readonly when?: string;
|
|
220
|
+
readonly icon?: string;
|
|
221
|
+
readonly handler: CommandHandler;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export interface CommandExecutionEvent {
|
|
225
|
+
readonly id: string;
|
|
226
|
+
readonly args: readonly unknown[];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export interface ICommandRegistry extends IDisposable {
|
|
230
|
+
readonly onDidChangeCommands: Event<string>;
|
|
231
|
+
readonly onDidExecuteCommand: Event<CommandExecutionEvent>;
|
|
232
|
+
registerCommand(descriptor: CommandDescriptor): IDisposable;
|
|
233
|
+
hasCommand(id: string): boolean;
|
|
234
|
+
getCommand(id: string): CommandDescriptor | undefined;
|
|
235
|
+
getCommands(contextKeys?: IContextKeyService): CommandDescriptor[];
|
|
236
|
+
executeCommand<T = unknown>(id: string, ...args: unknown[]): Promise<T>;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export const ICommandRegistryId = createServiceId<ICommandRegistry>('code-lab.commands');
|
|
240
|
+
|
|
241
|
+
export class CommandRegistry implements ICommandRegistry {
|
|
242
|
+
private readonly commands = new Map<string, CommandDescriptor>();
|
|
243
|
+
private readonly changeEmitter = new Emitter<string>({ name: 'commands.change' });
|
|
244
|
+
private readonly execEmitter = new Emitter<CommandExecutionEvent>({ name: 'commands.exec' });
|
|
245
|
+
|
|
246
|
+
public get onDidChangeCommands(): Event<string> {
|
|
247
|
+
return this.changeEmitter.event;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
public get onDidExecuteCommand(): Event<CommandExecutionEvent> {
|
|
251
|
+
return this.execEmitter.event;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
public registerCommand(descriptor: CommandDescriptor): IDisposable {
|
|
255
|
+
if (!descriptor.id) {
|
|
256
|
+
throw new CodeLabError('ERR_INVALID_ARGUMENT', 'Command id must be non-empty');
|
|
257
|
+
}
|
|
258
|
+
this.commands.set(descriptor.id, descriptor);
|
|
259
|
+
this.changeEmitter.fire(descriptor.id);
|
|
260
|
+
return toDisposable(() => {
|
|
261
|
+
if (this.commands.get(descriptor.id) === descriptor) {
|
|
262
|
+
this.commands.delete(descriptor.id);
|
|
263
|
+
this.changeEmitter.fire(descriptor.id);
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
public hasCommand(id: string): boolean {
|
|
269
|
+
return this.commands.has(id);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
public getCommand(id: string): CommandDescriptor | undefined {
|
|
273
|
+
return this.commands.get(id);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
public getCommands(contextKeys?: IContextKeyService): CommandDescriptor[] {
|
|
277
|
+
const all = Array.from(this.commands.values());
|
|
278
|
+
if (!contextKeys) return all;
|
|
279
|
+
return all.filter((cmd) => contextKeys.evaluateWhen(cmd.when));
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
public async executeCommand<T = unknown>(id: string, ...args: unknown[]): Promise<T> {
|
|
283
|
+
const cmd = this.commands.get(id);
|
|
284
|
+
if (!cmd) {
|
|
285
|
+
throw new CodeLabError('ERR_COMMAND_NOT_FOUND', `Command '${id}' is not registered`, {
|
|
286
|
+
context: { id }
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
this.execEmitter.fire({ id, args });
|
|
290
|
+
return (await cmd.handler(...args)) as T;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
public dispose(): void {
|
|
294
|
+
this.commands.clear();
|
|
295
|
+
this.changeEmitter.dispose();
|
|
296
|
+
this.execEmitter.dispose();
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export interface KeybindingRule {
|
|
301
|
+
readonly key: string;
|
|
302
|
+
readonly command: string;
|
|
303
|
+
readonly when?: string;
|
|
304
|
+
readonly args?: unknown;
|
|
305
|
+
readonly source?: 'default' | 'user' | 'plugin';
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export interface KeyboardEventLike {
|
|
309
|
+
readonly key: string;
|
|
310
|
+
readonly ctrlKey?: boolean;
|
|
311
|
+
readonly metaKey?: boolean;
|
|
312
|
+
readonly shiftKey?: boolean;
|
|
313
|
+
readonly altKey?: boolean;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export interface IKeybindingService extends IDisposable {
|
|
317
|
+
readonly onDidChangeKeybindings: Event<void>;
|
|
318
|
+
registerKeybinding(rule: KeybindingRule): IDisposable;
|
|
319
|
+
setUserKeybindings(rules: readonly KeybindingRule[]): void;
|
|
320
|
+
resolve(event: KeyboardEventLike): KeybindingRule | undefined;
|
|
321
|
+
lookupKeybinding(commandId: string): string | undefined;
|
|
322
|
+
getEffectiveRules(): KeybindingRule[];
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export const IKeybindingServiceId = createServiceId<IKeybindingService>('code-lab.keybindings');
|
|
326
|
+
|
|
327
|
+
export function normalizeKeyChord(raw: string): string {
|
|
328
|
+
const parts = raw
|
|
329
|
+
.split('+')
|
|
330
|
+
.map((s) => s.trim().toLowerCase())
|
|
331
|
+
.filter(Boolean);
|
|
332
|
+
let mod = false;
|
|
333
|
+
let alt = false;
|
|
334
|
+
let shift = false;
|
|
335
|
+
let key = '';
|
|
336
|
+
for (const p of parts) {
|
|
337
|
+
if (p === 'mod' || p === 'cmd' || p === 'meta' || p === 'ctrl' || p === 'control' || p === 'cmdorctrl') {
|
|
338
|
+
mod = true;
|
|
339
|
+
} else if (p === 'alt' || p === 'option' || p === 'opt') {
|
|
340
|
+
alt = true;
|
|
341
|
+
} else if (p === 'shift') {
|
|
342
|
+
shift = true;
|
|
343
|
+
} else if (p === 'esc') {
|
|
344
|
+
key = 'escape';
|
|
345
|
+
} else if (p === 'space') {
|
|
346
|
+
key = ' ';
|
|
347
|
+
} else {
|
|
348
|
+
key = p;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
const mods: string[] = [];
|
|
352
|
+
if (mod) mods.push('mod');
|
|
353
|
+
if (alt) mods.push('alt');
|
|
354
|
+
if (shift) mods.push('shift');
|
|
355
|
+
mods.push(key);
|
|
356
|
+
return mods.join('+');
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export function eventToChord(event: KeyboardEventLike): string {
|
|
360
|
+
const mod = Boolean(event.metaKey || event.ctrlKey);
|
|
361
|
+
const alt = Boolean(event.altKey);
|
|
362
|
+
const shift = Boolean(event.shiftKey);
|
|
363
|
+
const rawKey = (event.key || '').toLowerCase();
|
|
364
|
+
const key = rawKey === 'esc' ? 'escape' : rawKey;
|
|
365
|
+
const mods: string[] = [];
|
|
366
|
+
if (mod) mods.push('mod');
|
|
367
|
+
if (alt) mods.push('alt');
|
|
368
|
+
if (shift) mods.push('shift');
|
|
369
|
+
mods.push(key);
|
|
370
|
+
return mods.join('+');
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export function formatShortcutBadge(rawKey: string, isMac = false): string {
|
|
374
|
+
const norm = normalizeKeyChord(rawKey);
|
|
375
|
+
const parts = norm.split('+');
|
|
376
|
+
return parts
|
|
377
|
+
.map((p) => {
|
|
378
|
+
if (p === 'mod') return isMac ? '⌘' : 'Ctrl';
|
|
379
|
+
if (p === 'alt') return isMac ? '⌥' : 'Alt';
|
|
380
|
+
if (p === 'shift') return isMac ? '⇧' : 'Shift';
|
|
381
|
+
if (p === 'enter') return '↵';
|
|
382
|
+
if (p === 'escape') return 'Esc';
|
|
383
|
+
return p.length === 1 ? p.toUpperCase() : p[0].toUpperCase() + p.slice(1);
|
|
384
|
+
})
|
|
385
|
+
.join(isMac ? '' : '+');
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
export class KeybindingService implements IKeybindingService {
|
|
389
|
+
private readonly baseRules: KeybindingRule[] = [];
|
|
390
|
+
private userRules: KeybindingRule[] = [];
|
|
391
|
+
private readonly contextKeys: IContextKeyService;
|
|
392
|
+
private readonly changeEmitter = new Emitter<void>({ name: 'keybindings.change' });
|
|
393
|
+
|
|
394
|
+
public constructor(contextKeys: IContextKeyService) {
|
|
395
|
+
this.contextKeys = contextKeys;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
public get onDidChangeKeybindings(): Event<void> {
|
|
399
|
+
return this.changeEmitter.event;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
public registerKeybinding(rule: KeybindingRule): IDisposable {
|
|
403
|
+
const entry: KeybindingRule = { ...rule, source: rule.source ?? 'default' };
|
|
404
|
+
this.baseRules.push(entry);
|
|
405
|
+
this.changeEmitter.fire();
|
|
406
|
+
return toDisposable(() => {
|
|
407
|
+
const idx = this.baseRules.indexOf(entry);
|
|
408
|
+
if (idx !== -1) {
|
|
409
|
+
this.baseRules.splice(idx, 1);
|
|
410
|
+
this.changeEmitter.fire();
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
public setUserKeybindings(rules: readonly KeybindingRule[]): void {
|
|
416
|
+
this.userRules = rules.map((r) => ({ ...r, source: 'user' as const }));
|
|
417
|
+
this.changeEmitter.fire();
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
public getEffectiveRules(): KeybindingRule[] {
|
|
421
|
+
const disabled = new Set<string>();
|
|
422
|
+
for (const r of this.userRules) {
|
|
423
|
+
if (r.command.startsWith('-')) {
|
|
424
|
+
disabled.add(`${normalizeKeyChord(r.key)}::${r.command.slice(1)}`);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
const merged: KeybindingRule[] = [];
|
|
428
|
+
for (const r of this.baseRules) {
|
|
429
|
+
const sig = `${normalizeKeyChord(r.key)}::${r.command}`;
|
|
430
|
+
if (!disabled.has(sig)) merged.push(r);
|
|
431
|
+
}
|
|
432
|
+
for (const r of this.userRules) {
|
|
433
|
+
if (!r.command.startsWith('-')) merged.push(r);
|
|
434
|
+
}
|
|
435
|
+
return merged;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
public resolve(event: KeyboardEventLike): KeybindingRule | undefined {
|
|
439
|
+
const chord = eventToChord(event);
|
|
440
|
+
const rules = this.getEffectiveRules();
|
|
441
|
+
// VS Code precedence: later rules override earlier rules when `when` matches
|
|
442
|
+
for (let i = rules.length - 1; i >= 0; i--) {
|
|
443
|
+
const rule = rules[i];
|
|
444
|
+
if (normalizeKeyChord(rule.key) !== chord) continue;
|
|
445
|
+
if (this.contextKeys.evaluateWhen(rule.when)) {
|
|
446
|
+
return rule;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return undefined;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
public lookupKeybinding(commandId: string): string | undefined {
|
|
453
|
+
const rules = this.getEffectiveRules();
|
|
454
|
+
for (let i = rules.length - 1; i >= 0; i--) {
|
|
455
|
+
if (rules[i].command === commandId) {
|
|
456
|
+
return rules[i].key;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return undefined;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
public dispose(): void {
|
|
463
|
+
this.baseRules.length = 0;
|
|
464
|
+
this.userRules.length = 0;
|
|
465
|
+
this.changeEmitter.dispose();
|
|
466
|
+
}
|
|
467
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"lib": ["ES2022", "DOM"],
|
|
7
|
+
"strict": true,
|
|
8
|
+
"noImplicitAny": true,
|
|
9
|
+
"strictNullChecks": true,
|
|
10
|
+
"noUnusedLocals": true,
|
|
11
|
+
"noUnusedParameters": true,
|
|
12
|
+
"noImplicitReturns": true,
|
|
13
|
+
"noFallthroughCasesInSwitch": true,
|
|
14
|
+
"noEmit": true,
|
|
15
|
+
"skipLibCheck": true,
|
|
16
|
+
"forceConsistentCasingInFileNames": true,
|
|
17
|
+
"paths": {
|
|
18
|
+
"@code-lab/core": ["../core/src/index.ts"]
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"include": ["src/**/*.ts"]
|
|
22
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# `@code-lab/core`
|
|
2
|
+
|
|
3
|
+
Layer 0 (`core`) foundational primitives for **CodeLab**. Contains **zero DOM or browser-storage dependencies** so it runs identically in Node, Bun, Web Workers, Service Workers, and browsers.
|
|
4
|
+
|
|
5
|
+
## Public API (`src/index.ts`)
|
|
6
|
+
|
|
7
|
+
- **Lifecycle**: `IDisposable`, `toDisposable`, `DisposableStore`, `MutableDisposable`
|
|
8
|
+
- **Events**: `Emitter<T>`, `Event<T>`, `EventUtils`, `setGlobalEventMonitor`
|
|
9
|
+
- **Dependency Injection**: `createServiceId<T>(id)`, `ServiceContainer`, `IServiceContainer`
|
|
10
|
+
- **Errors & Logging**: `CodeLabError`, `isCodeLabError`, `Logger`, `ILogger`
|
|
11
|
+
- **Paths & Globs**: `PosixPath`, `globToRegExp`, `matchesGlob`
|
|
12
|
+
- **Extensibility**: `ContributionRegistry<K, V>`
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@code-lab/core",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Zero-DOM foundational primitives for CodeLab: ServiceContainer DI, IDisposable/DisposableStore, typed Emitter/Event bus, CodeLabError/Logger, ContributionRegistry, and POSIX path/glob utilities.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"types": "./src/index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.ts"
|
|
10
|
+
},
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"license": "MIT"
|
|
13
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @code-lab/core — Decorator-free ServiceId<T> Dependency Injection container.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { DisposableStore, type IDisposable, isDisposable } from './disposable';
|
|
6
|
+
import { CodeLabError } from './errors';
|
|
7
|
+
|
|
8
|
+
export interface ServiceId<T> {
|
|
9
|
+
readonly id: string;
|
|
10
|
+
readonly __brand?: (instance: T) => T;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function createServiceId<T>(id: string): ServiceId<T> {
|
|
14
|
+
if (!id || typeof id !== 'string') {
|
|
15
|
+
throw new CodeLabError('ERR_INVALID_ARGUMENT', 'ServiceId requires a non-empty string id');
|
|
16
|
+
}
|
|
17
|
+
return Object.freeze({ id });
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type ServiceFactory<T> = (container: IServiceContainer) => T;
|
|
21
|
+
|
|
22
|
+
export interface IServiceContainer extends IDisposable {
|
|
23
|
+
registerInstance<T>(serviceId: ServiceId<T>, instance: T): void;
|
|
24
|
+
registerSingleton<T>(serviceId: ServiceId<T>, factory: ServiceFactory<T>): void;
|
|
25
|
+
get<T>(serviceId: ServiceId<T>): T;
|
|
26
|
+
tryGet<T>(serviceId: ServiceId<T>): T | undefined;
|
|
27
|
+
has<T>(serviceId: ServiceId<T>): boolean;
|
|
28
|
+
listRegisteredIds(): string[];
|
|
29
|
+
createChild(): ServiceContainer;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface ProviderEntry {
|
|
33
|
+
kind: 'instance' | 'singleton';
|
|
34
|
+
value?: unknown;
|
|
35
|
+
factory?: ServiceFactory<unknown>;
|
|
36
|
+
resolved: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class ServiceContainer implements IServiceContainer {
|
|
40
|
+
private readonly entries = new Map<string, ProviderEntry>();
|
|
41
|
+
private readonly resolving = new Set<string>();
|
|
42
|
+
private readonly ownedDisposables = new DisposableStore();
|
|
43
|
+
private readonly parent: ServiceContainer | null;
|
|
44
|
+
private disposed = false;
|
|
45
|
+
|
|
46
|
+
public constructor(parent: ServiceContainer | null = null) {
|
|
47
|
+
this.parent = parent;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public registerInstance<T>(serviceId: ServiceId<T>, instance: T): void {
|
|
51
|
+
this.ensureNotDisposed();
|
|
52
|
+
this.entries.set(serviceId.id, {
|
|
53
|
+
kind: 'instance',
|
|
54
|
+
value: instance,
|
|
55
|
+
resolved: true
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public registerSingleton<T>(serviceId: ServiceId<T>, factory: ServiceFactory<T>): void {
|
|
60
|
+
this.ensureNotDisposed();
|
|
61
|
+
this.entries.set(serviceId.id, {
|
|
62
|
+
kind: 'singleton',
|
|
63
|
+
factory: factory as ServiceFactory<unknown>,
|
|
64
|
+
resolved: false
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public has<T>(serviceId: ServiceId<T>): boolean {
|
|
69
|
+
if (this.entries.has(serviceId.id)) return true;
|
|
70
|
+
return this.parent ? this.parent.has(serviceId) : false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public listRegisteredIds(): string[] {
|
|
74
|
+
const ids = new Set<string>(this.entries.keys());
|
|
75
|
+
if (this.parent) {
|
|
76
|
+
for (const id of this.parent.listRegisteredIds()) {
|
|
77
|
+
ids.add(id);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return Array.from(ids).sort();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public tryGet<T>(serviceId: ServiceId<T>): T | undefined {
|
|
84
|
+
const entry = this.entries.get(serviceId.id);
|
|
85
|
+
if (!entry) {
|
|
86
|
+
return this.parent ? this.parent.tryGet(serviceId) : undefined;
|
|
87
|
+
}
|
|
88
|
+
if (entry.resolved) {
|
|
89
|
+
return entry.value as T;
|
|
90
|
+
}
|
|
91
|
+
if (this.resolving.has(serviceId.id)) {
|
|
92
|
+
const chain = [...this.resolving, serviceId.id].join(' -> ');
|
|
93
|
+
throw new CodeLabError(
|
|
94
|
+
'ERR_DI_CYCLE',
|
|
95
|
+
`Circular service dependency detected while resolving "${serviceId.id}": ${chain}`
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
this.resolving.add(serviceId.id);
|
|
99
|
+
try {
|
|
100
|
+
const factory = entry.factory;
|
|
101
|
+
if (!factory) {
|
|
102
|
+
throw new CodeLabError('ERR_DI_MISSING', `Missing factory for service "${serviceId.id}"`);
|
|
103
|
+
}
|
|
104
|
+
const created = factory(this);
|
|
105
|
+
entry.value = created;
|
|
106
|
+
entry.resolved = true;
|
|
107
|
+
if (isDisposable(created)) {
|
|
108
|
+
this.ownedDisposables.add(created);
|
|
109
|
+
}
|
|
110
|
+
return created as T;
|
|
111
|
+
} finally {
|
|
112
|
+
this.resolving.delete(serviceId.id);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public get<T>(serviceId: ServiceId<T>): T {
|
|
117
|
+
const found = this.tryGet(serviceId);
|
|
118
|
+
if (found === undefined && !this.has(serviceId)) {
|
|
119
|
+
throw new CodeLabError(
|
|
120
|
+
'ERR_DI_MISSING',
|
|
121
|
+
`Service "${serviceId.id}" is not registered in ServiceContainer`
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
return found as T;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public createChild(): ServiceContainer {
|
|
128
|
+
this.ensureNotDisposed();
|
|
129
|
+
const child = new ServiceContainer(this);
|
|
130
|
+
this.ownedDisposables.add(child);
|
|
131
|
+
return child;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
public dispose(): void {
|
|
135
|
+
if (this.disposed) return;
|
|
136
|
+
this.disposed = true;
|
|
137
|
+
this.ownedDisposables.dispose();
|
|
138
|
+
this.entries.clear();
|
|
139
|
+
this.resolving.clear();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private ensureNotDisposed(): void {
|
|
143
|
+
if (this.disposed) {
|
|
144
|
+
throw new CodeLabError('ERR_DISPOSED', 'Cannot operate on a disposed ServiceContainer');
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|