@innovatorved/code-lab 0.7.1 → 0.7.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.
Files changed (71) hide show
  1. package/ARCHITECTURE.md +2 -2
  2. package/CHANGELOG.md +10 -0
  3. package/README.md +5 -5
  4. package/dist/code-lab-sql.esm.js +2 -2
  5. package/dist/code-lab-sql.umd.js +3 -3
  6. package/dist/code-lab.css +1 -1
  7. package/dist/code-lab.d.ts +47 -38
  8. package/dist/code-lab.esm.js +918 -884
  9. package/dist/code-lab.js +917 -881
  10. package/dist/extensions/cpp.esm.js +33 -33
  11. package/dist/extensions/cpp.js +33 -33
  12. package/dist/extensions/python.esm.js +79 -49
  13. package/dist/extensions/python.js +79 -49
  14. package/dist/index.d.ts +1 -1
  15. package/dist/logger.d.ts +4 -20
  16. package/dist/modular-kernel.d.ts +0 -1
  17. package/dist/packages/core/src/errors.d.ts +5 -1
  18. package/dist/packages/fs/src/backends.d.ts +0 -27
  19. package/dist/packages/fs/src/index.d.ts +1 -1
  20. package/dist/packages/ui/src/html.d.ts +1 -0
  21. package/dist/packages/ui/src/index.d.ts +1 -0
  22. package/dist/{apps/web/src/index.d.ts → packages/ui/src/kernel.d.ts} +3 -2
  23. package/dist/packages/ui/src/observability.d.ts +1 -1
  24. package/dist/packages/ui/src/settings-dialog.d.ts +0 -1
  25. package/dist/preview-sw.js +1 -1
  26. package/dist/runtime.d.ts +3 -0
  27. package/dist/runtime.esm.js +2 -2
  28. package/dist/runtime.js +2 -2
  29. package/dist/schemas.d.ts +1 -1
  30. package/dist/shell.esm.js +277 -285
  31. package/dist/shell.js +277 -285
  32. package/dist/terminal.esm.js +2 -2
  33. package/dist/terminal.js +2 -2
  34. package/dist/workbench/editor.d.ts +0 -4
  35. package/dist/workbench/index.d.ts +0 -2
  36. package/dist/workbench/intellisense.d.ts +0 -1
  37. package/dist/workbench/xterm-adapter.d.ts +0 -1
  38. package/package.json +2 -4
  39. package/src/code-lab.css +0 -8086
  40. package/src/csp.ts +0 -590
  41. package/src/diagnostics.ts +0 -287
  42. package/src/extensions/cpp.ts +0 -2371
  43. package/src/extensions/guided-lab.ts +0 -68
  44. package/src/extensions/javascript.ts +0 -54
  45. package/src/extensions/python.ts +0 -1743
  46. package/src/extensions/typescript.ts +0 -85
  47. package/src/extensions/umbrella.ts +0 -5
  48. package/src/html.ts +0 -88
  49. package/src/index.ts +0 -129
  50. package/src/logger.ts +0 -139
  51. package/src/modular-kernel.ts +0 -7
  52. package/src/preview-sw.js +0 -296
  53. package/src/runtime/test-harness.ts +0 -57
  54. package/src/runtime.ts +0 -10565
  55. package/src/sample.html +0 -304
  56. package/src/schemas.ts +0 -366
  57. package/src/scm.ts +0 -2453
  58. package/src/search.ts +0 -433
  59. package/src/shell.ts +0 -10990
  60. package/src/storage.ts +0 -773
  61. package/src/terminal.ts +0 -1402
  62. package/src/workbench/command-palette.ts +0 -101
  63. package/src/workbench/diff-editor.ts +0 -472
  64. package/src/workbench/editor.ts +0 -49
  65. package/src/workbench/icons.ts +0 -255
  66. package/src/workbench/index.ts +0 -29
  67. package/src/workbench/intellisense.ts +0 -1884
  68. package/src/workbench/share.ts +0 -128
  69. package/src/workbench/xterm-adapter.ts +0 -1571
  70. package/src/workbench.ts +0 -21825
  71. package/src/zip.ts +0 -348
@@ -1,101 +0,0 @@
1
- /**
2
- * @innovatorved/code-lab — VS Code Quick Open & Fuzzy Command Palette (Cmd+P / Cmd+Shift+P / F1)
3
- */
4
-
5
- export interface CommandPaletteItem {
6
- id: string;
7
- label: string;
8
- category?: string;
9
- detail?: string;
10
- shortcut?: string;
11
- action: () => void;
12
- }
13
-
14
- export interface FuzzyMatchResult {
15
- score: number;
16
- indices: number[];
17
- }
18
-
19
- function escapeHtml(str: unknown): string {
20
- return String(str ?? '')
21
- .replace(/&/g, '&')
22
- .replace(/</g, '&lt;')
23
- .replace(/>/g, '&gt;')
24
- .replace(/"/g, '&quot;')
25
- .replace(/'/g, '&#39;');
26
- }
27
-
28
- /**
29
- * VS Code-style subsequence fuzzy matcher with bonuses for consecutive matches,
30
- * word-boundary / camelCase transitions, and prefix alignment.
31
- */
32
- export function fuzzyScoreText(query: string, candidate: string): FuzzyMatchResult | null {
33
- const q = String(query || '').trim();
34
- const text = String(candidate || '');
35
- if (!q) return { score: 1, indices: [] };
36
-
37
- const qLower = q.toLowerCase();
38
- const tLower = text.toLowerCase();
39
-
40
- let qi = 0;
41
- let score = 0;
42
- let consecutive = 0;
43
- const indices: number[] = [];
44
-
45
- for (let ti = 0; ti < tLower.length && qi < qLower.length; ti++) {
46
- if (tLower[ti] === qLower[qi]) {
47
- indices.push(ti);
48
- score += 10;
49
-
50
- if (indices.length > 1 && indices[indices.length - 2] === ti - 1) {
51
- consecutive++;
52
- score += 12 + consecutive * 4;
53
- } else {
54
- consecutive = 0;
55
- }
56
-
57
- if (ti === 0) {
58
- score += 25;
59
- } else {
60
- const prev = text[ti - 1];
61
- const curr = text[ti];
62
- if (prev === '/' || prev === '\\' || prev === '-' || prev === '_' || prev === '.' || prev === ' ' || prev === ':') {
63
- score += 18;
64
- } else if (prev >= 'a' && prev <= 'z' && curr >= 'A' && curr <= 'Z') {
65
- score += 14;
66
- }
67
- }
68
-
69
- if (text[ti] === q[qi]) score += 2;
70
- qi++;
71
- }
72
- }
73
-
74
- if (qi < qLower.length) return null;
75
-
76
- score -= Math.min(20, Math.floor((text.length - q.length) / 4));
77
- if (tLower === qLower) score += 100;
78
- else if (tLower.startsWith(qLower)) score += 45;
79
-
80
- return { score, indices };
81
- }
82
-
83
- export function renderHighlightedText(text: string, indices: number[]): string {
84
- if (!indices || indices.length === 0) return escapeHtml(text);
85
- const set = new Set(indices);
86
- let html = '';
87
- let inMark = false;
88
- for (let i = 0; i < text.length; i++) {
89
- const matched = set.has(i);
90
- if (matched && !inMark) {
91
- html += '<mark class="codelab-palette-hl">';
92
- inMark = true;
93
- } else if (!matched && inMark) {
94
- html += '</mark>';
95
- inMark = false;
96
- }
97
- html += escapeHtml(text[i]);
98
- }
99
- if (inMark) html += '</mark>';
100
- return html;
101
- }
@@ -1,472 +0,0 @@
1
- /**
2
- * @innovatorved/code-lab — Inline Monaco Side-by-Side Diff Editor & Fallback Unified Diff
3
- */
4
-
5
- import { CL_ICONS } from './icons';
6
- import { getMonacoLanguageForFile, getMonacoThemeName } from './intellisense';
7
-
8
- function escapeHtml(str: unknown): string {
9
- return String(str ?? '')
10
- .replace(/&/g, '&amp;')
11
- .replace(/</g, '&lt;')
12
- .replace(/>/g, '&gt;')
13
- .replace(/"/g, '&quot;')
14
- .replace(/'/g, '&#39;');
15
- }
16
-
17
- export interface DiffComputeResult {
18
- hunks: Array<{ type: 'context' | 'add' | 'del'; text: string; oldLine: number | null; newLine: number | null }>;
19
- added: number;
20
- removed: number;
21
- }
22
-
23
- export interface GitDiffOpenOptions {
24
- staged?: boolean;
25
- originalText: string;
26
- modifiedText: string;
27
- label?: string;
28
- onStage?: () => void | Promise<void>;
29
- onUnstage?: () => void | Promise<void>;
30
- onDiscard?: () => void | Promise<void>;
31
- }
32
-
33
- export function computeSimpleDiff(oldText: string, newText: string): DiffComputeResult {
34
- const oldLines = String(oldText || '').split('\n');
35
- const newLines = String(newText || '').split('\n');
36
- const hunks: Array<{ type: 'context' | 'add' | 'del'; text: string; oldLine: number | null; newLine: number | null }> = [];
37
- let added = 0;
38
- let removed = 0;
39
-
40
- const m = oldLines.length;
41
- const n = newLines.length;
42
- let i = 0;
43
- let j = 0;
44
- let oldLineNum = 1;
45
- let newLineNum = 1;
46
-
47
- while (i < m && j < n) {
48
- if (oldLines[i] === newLines[j]) {
49
- hunks.push({ type: 'context', text: oldLines[i], oldLine: oldLineNum++, newLine: newLineNum++ });
50
- i++;
51
- j++;
52
- } else {
53
- let matchIdx = -1;
54
- for (let k = j + 1; k < Math.min(n, j + 5); k++) {
55
- if (newLines[k] === oldLines[i]) {
56
- matchIdx = k;
57
- break;
58
- }
59
- }
60
- if (matchIdx !== -1) {
61
- while (j < matchIdx) {
62
- hunks.push({ type: 'add', text: newLines[j++], oldLine: null, newLine: newLineNum++ });
63
- added++;
64
- }
65
- } else {
66
- hunks.push({ type: 'del', text: oldLines[i++], oldLine: oldLineNum++, newLine: null });
67
- removed++;
68
- }
69
- }
70
- }
71
-
72
- while (i < m) {
73
- hunks.push({ type: 'del', text: oldLines[i++], oldLine: oldLineNum++, newLine: null });
74
- removed++;
75
- }
76
- while (j < n) {
77
- hunks.push({ type: 'add', text: newLines[j++], oldLine: null, newLine: newLineNum++ });
78
- added++;
79
- }
80
-
81
- return { hunks, added, removed };
82
- }
83
-
84
- function renderFallbackUnifiedDiffHtml(oldText: string, newText: string): string {
85
- const diff = computeSimpleDiff(oldText, newText);
86
- if (diff.added === 0 && diff.removed === 0) {
87
- return `<div class="codelab-diff-empty">No textual changes between versions.</div>`;
88
- }
89
- const rows = diff.hunks.map((h) => {
90
- const cls = h.type === 'add' ? 'is-add' : h.type === 'del' ? 'is-del' : 'is-ctx';
91
- const prefix = h.type === 'add' ? '+' : h.type === 'del' ? '-' : ' ';
92
- const oldNum = h.oldLine !== null ? String(h.oldLine) : '';
93
- const newNum = h.newLine !== null ? String(h.newLine) : '';
94
- return `<div class="codelab-diff-fallback-row ${cls}">` +
95
- `<span class="codelab-diff-ln">${oldNum}</span>` +
96
- `<span class="codelab-diff-ln">${newNum}</span>` +
97
- `<span class="codelab-diff-sign">${prefix}</span>` +
98
- `<span class="codelab-diff-code">${escapeHtml(h.text)}</span>` +
99
- `</div>`;
100
- }).join('');
101
- return `<div class="codelab-diff-fallback" role="region" aria-label="Unified diff view">${rows}</div>`;
102
- }
103
-
104
- export class CodeLabDiffEditorManager {
105
- private workbench: any;
106
- private diffEditor: any = null;
107
- private originalModel: any = null;
108
- private modifiedModel: any = null;
109
- public isActive = false;
110
- public activeFile = '';
111
- public currentTarget: 'starter' | 'solution' | 'git' = 'solution';
112
-
113
- constructor(workbench: any) {
114
- this.workbench = workbench;
115
- }
116
-
117
- private _ensureDiffContainer(): { diffContainer: any; editorHost: any; fallbackTextarea: any } | null {
118
- const editorHost = this.workbench.container?.querySelector('#cl-editor-host')
119
- || this.workbench.container?.querySelector('#cl-monaco-host');
120
- const fallbackTextarea = this.workbench.container?.querySelector('#cl-fallback-editor');
121
- if (!editorHost) return null;
122
-
123
- let diffContainer = this.workbench.container.querySelector('#cl-inline-diff-container');
124
- if (!diffContainer) {
125
- diffContainer = document.createElement('div');
126
- diffContainer.id = 'cl-inline-diff-container';
127
- diffContainer.className = 'codelab-diff-container';
128
- editorHost.parentElement.appendChild(diffContainer);
129
- }
130
-
131
- const welcomeHost = this.workbench.container?.querySelector('#cl-welcome-host');
132
- if (welcomeHost) welcomeHost.style.display = 'none';
133
-
134
- diffContainer.hidden = false;
135
- diffContainer.style.display = 'flex';
136
- editorHost.style.display = 'none';
137
- if (fallbackTextarea) fallbackTextarea.style.display = 'none';
138
-
139
- this._disposeEditorModels();
140
- return { diffContainer, editorHost, fallbackTextarea };
141
- }
142
-
143
- private _disposeEditorModels(): void {
144
- if (this.diffEditor) {
145
- try { this.diffEditor.dispose(); } catch (_err) { void _err; }
146
- this.diffEditor = null;
147
- }
148
- if (this.originalModel) {
149
- try { this.originalModel.dispose(); } catch (_err) { void _err; }
150
- this.originalModel = null;
151
- }
152
- if (this.modifiedModel) {
153
- try { this.modifiedModel.dispose(); } catch (_err) { void _err; }
154
- this.modifiedModel = null;
155
- }
156
- }
157
-
158
- /**
159
- * Opens a VS Code Source Control side-by-side diff (`Working Tree` or `Index` vs `HEAD`).
160
- */
161
- public openGitDiff(file: string, opts: GitDiffOpenOptions): boolean {
162
- const monaco = typeof window !== 'undefined' ? (window as any).monaco : null;
163
- const hosts = this._ensureDiffContainer();
164
- if (!hosts) return false;
165
-
166
- const { diffContainer } = hosts;
167
- this.isActive = true;
168
- this.activeFile = file;
169
- this.currentTarget = 'git';
170
-
171
- const staged = Boolean(opts.staged);
172
- const titleLabel = opts.label || (staged ? `${file} (Index ↔ HEAD)` : `${file} (Working Tree)`);
173
- const safeTitle = escapeHtml(titleLabel);
174
-
175
- diffContainer.innerHTML = `
176
- <div class="codelab-diff-toolbar">
177
- <span class="codelab-diff-toolbar__title">
178
- ${CL_ICONS.diff}
179
- <span>${safeTitle}</span>
180
- </span>
181
- <div class="codelab-diff-toolbar__actions">
182
- ${staged
183
- ? (opts.onUnstage ? `<button type="button" class="codelab-btn codelab-btn--sm" id="cl-git-diff-unstage-btn" title="Unstage Changes">${CL_ICONS.minus || '−'} Unstage</button>` : '')
184
- : `${opts.onDiscard ? `<button type="button" class="codelab-btn codelab-btn--sm" id="cl-git-diff-discard-btn" title="Discard Changes">${CL_ICONS.discard || CL_ICONS.refresh} Discard</button>` : ''}
185
- ${opts.onStage ? `<button type="button" class="codelab-btn codelab-btn--sm codelab-btn--primary" id="cl-git-diff-stage-btn" title="Stage Changes">${CL_ICONS.plus || '+'} Stage</button>` : ''}`
186
- }
187
- <button type="button" class="codelab-btn codelab-btn--sm" id="cl-git-diff-open-file-btn" title="Open File">Open File</button>
188
- <button type="button" class="codelab-btn codelab-btn--sm" id="cl-diff-close-btn" title="Close Diff (Esc)">${CL_ICONS.x}</button>
189
- </div>
190
- </div>
191
- <div class="codelab-diff-host" id="cl-diff-editor-slot"></div>
192
- `;
193
-
194
- const slot = diffContainer.querySelector('#cl-diff-editor-slot');
195
- if (monaco?.editor && typeof monaco.editor.createDiffEditor === 'function' && slot) {
196
- const resolvedTheme = typeof this.workbench._resolvedTheme === 'function'
197
- ? this.workbench._resolvedTheme()
198
- : this.workbench.theme;
199
- const monacoTheme = getMonacoThemeName(resolvedTheme, monaco);
200
- const lang = getMonacoLanguageForFile(file, this.workbench.lang);
201
-
202
- this.diffEditor = monaco.editor.createDiffEditor(slot, {
203
- readOnly: staged,
204
- originalEditable: false,
205
- theme: monacoTheme,
206
- renderSideBySide: true,
207
- automaticLayout: true,
208
- minimap: { enabled: false },
209
- scrollBeyondLastLine: false,
210
- fontFamily: '"Geist Mono", ui-monospace, SFMono-Regular, "SF Mono", Menlo, Monaco, Consolas, monospace',
211
- fontSize: Number(this.workbench._editorFontSize) || 13
212
- });
213
-
214
- this.originalModel = monaco.editor.createModel(String(opts.originalText ?? ''), lang);
215
- this.modifiedModel = monaco.editor.createModel(String(opts.modifiedText ?? ''), lang);
216
- this.diffEditor.setModel({
217
- original: this.originalModel,
218
- modified: this.modifiedModel
219
- });
220
-
221
- if (!staged) {
222
- this.modifiedModel.onDidChangeContent(() => {
223
- this.syncFileToWorkbench(file, this.modifiedModel.getValue());
224
- });
225
- }
226
- } else if (slot) {
227
- slot.innerHTML = renderFallbackUnifiedDiffHtml(opts.originalText, opts.modifiedText);
228
- }
229
-
230
- diffContainer.querySelector('#cl-git-diff-stage-btn')?.addEventListener('click', async () => {
231
- await opts.onStage?.();
232
- this.closeInlineDiff();
233
- });
234
- diffContainer.querySelector('#cl-git-diff-unstage-btn')?.addEventListener('click', async () => {
235
- await opts.onUnstage?.();
236
- this.closeInlineDiff();
237
- });
238
- diffContainer.querySelector('#cl-git-diff-discard-btn')?.addEventListener('click', async () => {
239
- await opts.onDiscard?.();
240
- this.closeInlineDiff();
241
- });
242
- diffContainer.querySelector('#cl-git-diff-open-file-btn')?.addEventListener('click', () => {
243
- this.closeInlineDiff();
244
- if (typeof this.workbench.openFile === 'function') {
245
- this.workbench.openFile(file);
246
- }
247
- });
248
- diffContainer.querySelector('#cl-diff-close-btn')?.addEventListener('click', () => {
249
- this.closeInlineDiff();
250
- });
251
-
252
- return true;
253
- }
254
-
255
- /**
256
- * Mounts a side-by-side (or inline) Monaco DiffEditor directly into an existing slot element
257
- * without overwriting the surrounding `.codelab-scm-diff-header` toolbar.
258
- */
259
- public mountMonacoDiffIntoSlot(
260
- slotEl: HTMLElement,
261
- opts: {
262
- relPath: string;
263
- originalContent: string;
264
- modifiedContent: string;
265
- sideBySide?: boolean;
266
- readOnly?: boolean;
267
- }
268
- ): boolean {
269
- const monaco = this.workbench?.monaco || (typeof window !== 'undefined' ? (window as any).monaco : null);
270
- if (!slotEl || !monaco?.editor || typeof monaco.editor.createDiffEditor !== 'function') {
271
- return false;
272
- }
273
- this._disposeEditorModels();
274
- this.isActive = true;
275
- this.activeFile = opts.relPath;
276
- this.currentTarget = 'git';
277
-
278
- const resolvedTheme = typeof this.workbench?._resolvedTheme === 'function'
279
- ? this.workbench._resolvedTheme()
280
- : (this.workbench?.theme || 'dark');
281
- const monacoTheme = getMonacoThemeName(resolvedTheme, monaco);
282
- const lang = getMonacoLanguageForFile(opts.relPath, this.workbench?.lang);
283
-
284
- slotEl.innerHTML = '';
285
- this.diffEditor = monaco.editor.createDiffEditor(slotEl, {
286
- readOnly: opts.readOnly !== false,
287
- originalEditable: false,
288
- theme: monacoTheme,
289
- renderSideBySide: opts.sideBySide !== false,
290
- automaticLayout: true,
291
- minimap: { enabled: false },
292
- scrollBeyondLastLine: false,
293
- fontFamily: '"Geist Mono", ui-monospace, SFMono-Regular, "SF Mono", Menlo, Monaco, Consolas, monospace',
294
- fontSize: Number(this.workbench?._editorFontSize) || 13
295
- });
296
-
297
- this.originalModel = monaco.editor.createModel(String(opts.originalContent ?? ''), lang);
298
- this.modifiedModel = monaco.editor.createModel(String(opts.modifiedContent ?? ''), lang);
299
- this.diffEditor.setModel({
300
- original: this.originalModel,
301
- modified: this.modifiedModel
302
- });
303
- return true;
304
- }
305
-
306
- public openInlineDiff(file: string, target: 'starter' | 'solution' = 'solution'): boolean {
307
- const monaco = typeof window !== 'undefined' ? (window as any).monaco : null;
308
- this.activeFile = file;
309
- this.currentTarget = target;
310
-
311
- const editorHost = this.workbench.container?.querySelector('#cl-editor-host') || this.workbench.container?.querySelector('#cl-monaco-host');
312
- const fallbackTextarea = this.workbench.container?.querySelector('#cl-fallback-editor');
313
- if (!editorHost) return false;
314
-
315
- const isStarter = target === 'starter';
316
- const baseFiles = this.workbench._getBaseFilesForModeAndLang
317
- ? this.workbench._getBaseFilesForModeAndLang(isStarter ? 'guided' : 'sandbox', this.workbench.lang)
318
- : (isStarter ? (this.workbench.starterFiles || {}) : (this.workbench.solutionFiles || {}));
319
-
320
- const refCode = baseFiles[file] !== undefined ? baseFiles[file] : '';
321
- const currentCode = this.workbench.files[file] !== undefined ? this.workbench.files[file] : '';
322
-
323
- if (!monaco || !monaco.editor || typeof monaco.editor.createDiffEditor !== 'function') {
324
- if (typeof this.workbench.showDiffModalFallback === 'function') {
325
- this.workbench.showDiffModalFallback(file, target);
326
- }
327
- return false;
328
- }
329
-
330
- const hosts = this._ensureDiffContainer();
331
- if (!hosts) return false;
332
- const { diffContainer } = hosts;
333
- this.isActive = true;
334
-
335
- const safeFile = escapeHtml(file);
336
- diffContainer.innerHTML = `
337
- <div class="codelab-diff-toolbar">
338
- <span class="codelab-diff-toolbar__title">
339
- ${CL_ICONS.diff}
340
- <span>Diff: <code>${safeFile}</code> (${isStarter ? 'Current vs Starter' : 'Current vs Solution'})</span>
341
- </span>
342
- <div class="codelab-segmented" style="margin-left:12px;">
343
- <button type="button" class="codelab-segmented__btn ${target === 'starter' ? 'active' : ''}" id="cl-diff-toggle-starter">Starter</button>
344
- <button type="button" class="codelab-segmented__btn ${target === 'solution' ? 'active' : ''}" id="cl-diff-toggle-solution">Solution</button>
345
- </div>
346
- <div class="codelab-diff-toolbar__actions">
347
- <button type="button" class="codelab-btn codelab-btn--sm" id="cl-diff-revert-btn">${CL_ICONS.refresh} Revert File</button>
348
- <button type="button" class="codelab-btn codelab-btn--sm codelab-btn--primary" id="cl-diff-apply-btn">${CL_ICONS.check} Apply Solution</button>
349
- <button type="button" class="codelab-btn codelab-btn--sm" id="cl-diff-close-btn">${CL_ICONS.x} Close Diff</button>
350
- </div>
351
- </div>
352
- <div class="codelab-diff-host" id="cl-diff-editor-slot"></div>
353
- `;
354
-
355
- const slot = diffContainer.querySelector('#cl-diff-editor-slot');
356
- const resolvedTheme = typeof this.workbench._resolvedTheme === 'function'
357
- ? this.workbench._resolvedTheme()
358
- : this.workbench.theme;
359
- const monacoTheme = getMonacoThemeName(resolvedTheme, monaco);
360
- this.diffEditor = monaco.editor.createDiffEditor(slot, {
361
- readOnly: false,
362
- originalEditable: false,
363
- theme: monacoTheme,
364
- renderSideBySide: true,
365
- automaticLayout: true,
366
- minimap: { enabled: false },
367
- scrollBeyondLastLine: false,
368
- fontFamily: '"Geist Mono", ui-monospace, SFMono-Regular, "SF Mono", Menlo, Monaco, Consolas, monospace',
369
- fontSize: Number(this.workbench._editorFontSize) || 13
370
- });
371
-
372
- const lang = getMonacoLanguageForFile(file, this.workbench.lang);
373
- this.originalModel = monaco.editor.createModel(refCode, lang);
374
- this.modifiedModel = monaco.editor.createModel(currentCode, lang);
375
-
376
- this.diffEditor.setModel({
377
- original: this.originalModel,
378
- modified: this.modifiedModel
379
- });
380
-
381
- this.modifiedModel.onDidChangeContent(() => {
382
- this.syncFileToWorkbench(file, this.modifiedModel.getValue());
383
- });
384
-
385
- diffContainer.querySelector('#cl-diff-toggle-starter')?.addEventListener('click', () => {
386
- this.openInlineDiff(file, 'starter');
387
- });
388
- diffContainer.querySelector('#cl-diff-toggle-solution')?.addEventListener('click', () => {
389
- this.openInlineDiff(file, 'solution');
390
- });
391
- diffContainer.querySelector('#cl-diff-revert-btn')?.addEventListener('click', () => {
392
- const starterMap = this.workbench.starterFiles || (typeof this.workbench._getBaseFilesForModeAndLang === 'function' ? this.workbench._getBaseFilesForModeAndLang('guided', this.workbench.lang) : {});
393
- const starterCode = (starterMap && starterMap[file]) || '';
394
- if (typeof this.workbench.setFile === 'function') {
395
- this.workbench.setFile(file, starterCode);
396
- } else {
397
- this.syncFileToWorkbench(file, starterCode);
398
- }
399
- this.closeInlineDiff();
400
- this.workbench.openFile(file);
401
- });
402
- diffContainer.querySelector('#cl-diff-apply-btn')?.addEventListener('click', () => {
403
- if (typeof this.workbench.applyReferenceSolution === 'function') {
404
- this.closeInlineDiff();
405
- this.workbench.applyReferenceSolution();
406
- return;
407
- }
408
- const solutionMap = this.workbench.solutionFiles || (typeof this.workbench._getBaseFilesForModeAndLang === 'function' ? this.workbench._getBaseFilesForModeAndLang('sandbox', this.workbench.lang) : {});
409
- const solutionCode = (solutionMap && solutionMap[file]) || '';
410
- if (typeof this.workbench.setFile === 'function') {
411
- this.workbench.setFile(file, solutionCode);
412
- } else {
413
- this.syncFileToWorkbench(file, solutionCode);
414
- }
415
- this.closeInlineDiff();
416
- this.workbench.openFile(file);
417
- });
418
- diffContainer.querySelector('#cl-diff-close-btn')?.addEventListener('click', () => {
419
- this.closeInlineDiff();
420
- });
421
-
422
- return true;
423
- }
424
-
425
- public syncFileToWorkbench(file: string, nextVal: string): void {
426
- this.workbench.files[file] = nextVal;
427
- const mainModel = this.workbench.models instanceof Map ? this.workbench.models.get(file) : null;
428
- if (mainModel && typeof mainModel.getValue === 'function' && mainModel.getValue() !== nextVal) {
429
- mainModel.setValue(nextVal);
430
- }
431
- const fallbackEl = this.workbench.container?.querySelector('#cl-fallback-editor');
432
- if (fallbackEl && this.workbench.activeFile === file) {
433
- fallbackEl.value = nextVal;
434
- }
435
- if (this.workbench.shell?.vfs && typeof this.workbench.shell.vfs.writeFileSync === 'function') {
436
- try {
437
- this.workbench.shell.vfs.writeFileSync('/workspace/' + String(file).replace(/^\/+/, ''), nextVal);
438
- } catch (err) {
439
- if (this.workbench.logger && typeof this.workbench.logger.debug === 'function') {
440
- this.workbench.logger.debug('Diff VFS sync skipped.', err);
441
- }
442
- }
443
- }
444
- if (typeof this.workbench._saveState === 'function') {
445
- this.workbench._saveState();
446
- }
447
- if (typeof this.workbench._markDirty === 'function') {
448
- this.workbench._markDirty(file);
449
- }
450
- if (typeof this.workbench._refreshGitDecorations === 'function') {
451
- this.workbench._refreshGitDecorations();
452
- }
453
- }
454
-
455
- public closeInlineDiff(): void {
456
- this.isActive = false;
457
- const diffContainer = this.workbench.container?.querySelector('#cl-inline-diff-container');
458
- if (diffContainer) {
459
- diffContainer.hidden = true;
460
- diffContainer.style.display = 'none';
461
- }
462
- const isWelcomeActive = this.workbench.activeFile === '__welcome__';
463
- const welcomeHost = this.workbench.container?.querySelector('#cl-welcome-host');
464
- const editorHost = this.workbench.container?.querySelector('#cl-editor-host') || this.workbench.container?.querySelector('#cl-monaco-host');
465
- if (isWelcomeActive && welcomeHost) {
466
- welcomeHost.style.display = 'flex';
467
- } else if (editorHost) {
468
- editorHost.style.display = '';
469
- }
470
- this._disposeEditorModels();
471
- }
472
- }
@@ -1,49 +0,0 @@
1
- /**
2
- * @innovatorved/code-lab — Monaco Editor Host, Model Lifecycle & Formatting
3
- */
4
-
5
- export interface EditorPosition {
6
- lineNumber: number;
7
- column: number;
8
- }
9
-
10
- /**
11
- * Normalizes C++ source indentation and braces.
12
- */
13
- export function formatCppSource(code: string): string {
14
- if (!code || typeof code !== 'string') return code;
15
- const lines = code.split('\n');
16
- const formatted: string[] = [];
17
- let indentLevel = 0;
18
-
19
- for (let i = 0; i < lines.length; i++) {
20
- const rawLine = lines[i];
21
- const trimmed = rawLine.trim();
22
-
23
- if (!trimmed) {
24
- formatted.push('');
25
- continue;
26
- }
27
-
28
- if (trimmed.startsWith('#')) {
29
- formatted.push(trimmed);
30
- continue;
31
- }
32
-
33
- // Dedent for closing brace
34
- let currentIndent = indentLevel;
35
- if (trimmed.startsWith('}') || trimmed.startsWith(');') || trimmed.startsWith('public:') || trimmed.startsWith('private:') || trimmed.startsWith('protected:')) {
36
- currentIndent = Math.max(0, indentLevel - 1);
37
- }
38
-
39
- formatted.push(' '.repeat(currentIndent) + trimmed);
40
-
41
- // Update indentLevel
42
- for (let c = 0; c < trimmed.length; c++) {
43
- if (trimmed[c] === '{') indentLevel++;
44
- else if (trimmed[c] === '}') indentLevel = Math.max(0, indentLevel - 1);
45
- }
46
- }
47
-
48
- return formatted.join('\n');
49
- }