stream-monaco 0.0.42 → 0.0.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- const require_preloadMonacoWorkers_shared = require('./preloadMonacoWorkers.shared-CASMnPWz.cjs');
1
+ const require_preloadMonacoWorkers_shared = require('./preloadMonacoWorkers.shared-DpwiD-Ro.cjs');
2
2
 
3
3
  //#region src/ensureMonacoWorkers.ts
4
4
  function ensureMonacoWorkers() {
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { RevealStrategy, clearHighlighterCache, defaultRevealDebounceMs, detectLanguage, editorWorkerPath, getOrCreateHighlighter, registerMonacoThemes, uniqueWorkerPaths, useMonaco, workerPathByLabel } from "./preloadMonacoWorkers.shared-C6J5pk0l.js";
1
+ import { RevealStrategy, clearHighlighterCache, defaultRevealDebounceMs, detectLanguage, editorWorkerPath, getOrCreateHighlighter, registerMonacoThemes, uniqueWorkerPaths, useMonaco, workerPathByLabel } from "./preloadMonacoWorkers.shared-vZJruB3t.js";
2
2
 
3
3
  //#region src/ensureMonacoWorkers.ts
4
4
  function ensureMonacoWorkers() {
@@ -1,4 +1,4 @@
1
- const require_preloadMonacoWorkers_shared = require('./preloadMonacoWorkers.shared-CASMnPWz.cjs');
1
+ const require_preloadMonacoWorkers_shared = require('./preloadMonacoWorkers.shared-DpwiD-Ro.cjs');
2
2
  const monaco_editor_esm_vs_basic_languages_cpp_cpp = require_preloadMonacoWorkers_shared.__toESM(require("monaco-editor/esm/vs/basic-languages/cpp/cpp"));
3
3
  const monaco_editor_esm_vs_basic_languages_javascript_javascript = require_preloadMonacoWorkers_shared.__toESM(require("monaco-editor/esm/vs/basic-languages/javascript/javascript"));
4
4
  const monaco_editor_esm_vs_basic_languages_powershell_powershell = require_preloadMonacoWorkers_shared.__toESM(require("monaco-editor/esm/vs/basic-languages/powershell/powershell"));
@@ -1,4 +1,4 @@
1
- import { RevealStrategy, clearHighlighterCache, defaultRevealDebounceMs, detectLanguage, getOrCreateHighlighter, monaco_shim_exports, processedLanguage, registerMonacoThemes, useMonaco, workerPathByLabel } from "./preloadMonacoWorkers.shared-C6J5pk0l.js";
1
+ import { RevealStrategy, clearHighlighterCache, defaultRevealDebounceMs, detectLanguage, getOrCreateHighlighter, monaco_shim_exports, processedLanguage, registerMonacoThemes, useMonaco, workerPathByLabel } from "./preloadMonacoWorkers.shared-vZJruB3t.js";
2
2
  import { conf, language } from "monaco-editor/esm/vs/basic-languages/cpp/cpp";
3
3
  import { conf as conf$1, language as language$1 } from "monaco-editor/esm/vs/basic-languages/javascript/javascript";
4
4
  import { conf as conf$2, language as language$2 } from "monaco-editor/esm/vs/basic-languages/powershell/powershell";
@@ -527,6 +527,14 @@ function createScrollWatcherForEditor(ed, opts) {
527
527
  return api;
528
528
  }
529
529
 
530
+ //#endregion
531
+ //#region src/utils/textChunks.ts
532
+ function countLineBreaks(text) {
533
+ let count = 0;
534
+ for (let i = 0; i < text.length; i++) if (text.charCodeAt(i) === 10) count += 1;
535
+ return count;
536
+ }
537
+
530
538
  //#endregion
531
539
  //#region src/core/diffAppearance.ts
532
540
  function parseCssColorRgb(color) {
@@ -4431,9 +4439,10 @@ var DiffEditorManager = class DiffEditorManager {
4431
4439
  shouldAutoScrollDiff: this.shouldAutoScrollDiff
4432
4440
  });
4433
4441
  if (!this.diffEditorView) return;
4442
+ if (!(this.diffAutoScroll && this.autoScrollOnUpdate && this.shouldAutoScrollDiff)) return;
4434
4443
  const hasV = this.hasVerticalScrollbarModified();
4435
4444
  log("diff", "hasVerticalScrollbarModified ->", hasV);
4436
- if (!(this.diffAutoScroll && this.autoScrollOnUpdate && this.shouldAutoScrollDiff && hasV)) return;
4445
+ if (!hasV) return;
4437
4446
  const me = this.diffEditorView.getModifiedEditor();
4438
4447
  const model = me.getModel();
4439
4448
  const currentLine = (model === null || model === void 0 ? void 0 : model.getLineCount()) ?? 1;
@@ -4655,7 +4664,7 @@ var DiffEditorManager = class DiffEditorManager {
4655
4664
  this.diffComputedVersions = null;
4656
4665
  this.diffPresentationDisposables.push(this.diffEditorView.onDidUpdateDiff(() => {
4657
4666
  this.diffComputedVersions = this.captureCurrentDiffVersions();
4658
- this.syncDiffEditorLayoutToContainer();
4667
+ this.scheduleSyncDiffEditorLayoutToContainer();
4659
4668
  this.scheduleSyncDiffPresentationDecorations();
4660
4669
  }));
4661
4670
  this.diffPresentationDisposables.push(oEditor.onDidChangeModelContent(() => {
@@ -4820,6 +4829,7 @@ var DiffEditorManager = class DiffEditorManager {
4820
4829
  }
4821
4830
  updateModified(newCode, codeLanguage) {
4822
4831
  if (!this.diffEditorView || !this.modifiedModel) return;
4832
+ this.syncLastKnownModified();
4823
4833
  if (codeLanguage) {
4824
4834
  const lang = processedLanguage(codeLanguage);
4825
4835
  if (lang && this.modifiedModel.getLanguageId() !== lang) monaco_shim_exports.editor.setModelLanguage(this.modifiedModel, lang);
@@ -4832,7 +4842,7 @@ var DiffEditorManager = class DiffEditorManager {
4832
4842
  if (tailAppend) this.appendModified(newCode.slice(prev.length), codeLanguage);
4833
4843
  else {
4834
4844
  this.flushModifiedAppendBufferSync();
4835
- const prevAfterFlush = this.modifiedModel.getValue();
4845
+ const prevAfterFlush = this.lastKnownModifiedCode ?? this.modifiedModel.getValue();
4836
4846
  const prevLine = this.modifiedModel.getLineCount();
4837
4847
  this.applyMinimalEditToModel(this.modifiedModel, prevAfterFlush, newCode);
4838
4848
  const newLine = this.modifiedModel.getLineCount();
@@ -5082,6 +5092,7 @@ var DiffEditorManager = class DiffEditorManager {
5082
5092
  }
5083
5093
  const { original, modified, lang } = this.pendingDiffUpdate;
5084
5094
  this.pendingDiffUpdate = null;
5095
+ this.syncLastKnownModified();
5085
5096
  this.flushOriginalAppendBufferSync();
5086
5097
  this.flushModifiedAppendBufferSync();
5087
5098
  if (lang) {
@@ -5102,7 +5113,7 @@ var DiffEditorManager = class DiffEditorManager {
5102
5113
  else this.applyMinimalEditToModel(o, prevO, original);
5103
5114
  this.lastKnownOriginalCode = original;
5104
5115
  }
5105
- const prevM = m.getValue();
5116
+ const prevM = this.lastKnownModifiedCode;
5106
5117
  const prevMLineCount = m.getLineCount();
5107
5118
  const modifiedTailAppend = prevM !== modified && modified.startsWith(prevM) && prevM.length < modified.length;
5108
5119
  const hasContentChange = prevO !== original || prevM !== modified;
@@ -5143,6 +5154,33 @@ var DiffEditorManager = class DiffEditorManager {
5143
5154
  this.preserveNativeDiffDecorationsOnStaleAppend = true;
5144
5155
  this.appendToModel(this.modifiedModel, text);
5145
5156
  }
5157
+ splitAppendTextForProgressiveFlush(text, maxChunkChars = 12e3, maxChunkLines = 200) {
5158
+ if (!text || text.length <= maxChunkChars) return [text];
5159
+ const chunks = [];
5160
+ let start = 0;
5161
+ let lineCount = 0;
5162
+ let lastSafeEnd = 0;
5163
+ for (let i = 0; i < text.length; i++) {
5164
+ if (text.charCodeAt(i) === 10) {
5165
+ lineCount += 1;
5166
+ lastSafeEnd = i + 1;
5167
+ }
5168
+ if (i - start + 1 < maxChunkChars && lineCount < maxChunkLines) continue;
5169
+ let end = lastSafeEnd > start ? lastSafeEnd : i + 1;
5170
+ if (end < text.length) {
5171
+ const prev = text.charCodeAt(end - 1);
5172
+ const next = text.charCodeAt(end);
5173
+ if (prev >= 55296 && prev <= 56319 && next >= 56320 && next <= 57343) end += 1;
5174
+ }
5175
+ chunks.push(text.slice(start, end));
5176
+ start = end;
5177
+ i = end - 1;
5178
+ lineCount = 0;
5179
+ lastSafeEnd = start;
5180
+ }
5181
+ if (start < text.length) chunks.push(text.slice(start));
5182
+ return chunks.length ? chunks : [text];
5183
+ }
5146
5184
  async flushAppendBufferDiff() {
5147
5185
  if (!this.diffEditorView) return;
5148
5186
  if (this.appendBufferOriginalDiff.length === 0 && this.appendBufferModifiedDiff.length === 0) return;
@@ -5163,17 +5201,16 @@ var DiffEditorManager = class DiffEditorManager {
5163
5201
  return;
5164
5202
  }
5165
5203
  let parts = this.appendBufferModifiedDiff.splice(0);
5204
+ if (parts.length === 0) {
5205
+ this.eagerlyGrowDiffContainerHeight();
5206
+ return;
5207
+ }
5166
5208
  const prevLineInit = model.getLineCount();
5167
5209
  const totalText = parts.join("");
5168
5210
  const totalChars = totalText.length;
5169
- if (parts.length === 1 && totalChars > 5e3) {
5170
- const lines = totalText.split(/\r?\n/);
5171
- const chunkSize = 200;
5172
- const chunks = [];
5173
- for (let i = 0; i < lines.length; i += chunkSize) chunks.push(`${lines.slice(i, i + chunkSize).join("\n")}\n`);
5174
- if (chunks.length > 1) parts = chunks;
5175
- }
5176
- const applyChunked = parts.length > 1 && (totalChars > 2e3 || model.getLineCount && model.getLineCount() + 0 - prevLineInit > 50);
5211
+ const totalLineBreaks = countLineBreaks(totalText);
5212
+ if (parts.length === 1 && totalChars > 5e3) parts = this.splitAppendTextForProgressiveFlush(totalText);
5213
+ const applyChunked = parts.length > 1 && (totalChars > 2e3 || totalLineBreaks > 50);
5177
5214
  log("diff", "flushAppendBufferDiff start", {
5178
5215
  partsCount: parts.length,
5179
5216
  totalChars,
@@ -5192,6 +5229,8 @@ var DiffEditorManager = class DiffEditorManager {
5192
5229
  } catch {}
5193
5230
  if (applyChunked) {
5194
5231
  log("diff", "flushAppendBufferDiff applying chunked", { partsLen: parts.length });
5232
+ if (this.lastKnownModifiedCode == null) this.lastKnownModifiedCode = model.getValue();
5233
+ let currentLength = this.lastKnownModifiedCode.length;
5195
5234
  let idx = 0;
5196
5235
  for (const part of parts) {
5197
5236
  if (!part) continue;
@@ -5201,17 +5240,9 @@ var DiffEditorManager = class DiffEditorManager {
5201
5240
  partLen: part.length,
5202
5241
  prevLine
5203
5242
  });
5204
- const lastColumn$1 = model.getLineMaxColumn(prevLine);
5205
- const range$1 = new monaco_shim_exports.Range(prevLine, lastColumn$1, prevLine, lastColumn$1);
5206
5243
  this.preserveNativeDiffDecorationsOnStaleAppend = true;
5207
- this.runAsProgrammaticModifiedContentChange(() => {
5208
- model.applyEdits([{
5209
- range: range$1,
5210
- text: part,
5211
- forceMoveMarkers: true
5212
- }]);
5213
- });
5214
- this.lastKnownModifiedCode = model.getValue();
5244
+ this.appendToModel(model, part, currentLength);
5245
+ currentLength += part.length;
5215
5246
  const newLine$1 = model.getLineCount();
5216
5247
  this.lastKnownModifiedLineCount = newLine$1;
5217
5248
  await new Promise((resolve) => typeof requestAnimationFrame !== "undefined" ? requestAnimationFrame(resolve) : setTimeout(resolve, 0));
@@ -5237,23 +5268,15 @@ var DiffEditorManager = class DiffEditorManager {
5237
5268
  newLine: newLine$1
5238
5269
  });
5239
5270
  }
5271
+ this.lastKnownModifiedCode = model.getValue();
5240
5272
  if (suppressedByFlush) watcherApi.setSuppressed(false);
5241
5273
  return;
5242
5274
  }
5243
5275
  const text = totalText;
5244
5276
  this.appendBufferModifiedDiff.length = 0;
5245
5277
  prevLine = model.getLineCount();
5246
- const lastColumn = model.getLineMaxColumn(prevLine);
5247
- const range = new monaco_shim_exports.Range(prevLine, lastColumn, prevLine, lastColumn);
5248
5278
  this.preserveNativeDiffDecorationsOnStaleAppend = true;
5249
- this.runAsProgrammaticModifiedContentChange(() => {
5250
- model.applyEdits([{
5251
- range,
5252
- text,
5253
- forceMoveMarkers: true
5254
- }]);
5255
- });
5256
- this.lastKnownModifiedCode = model.getValue();
5279
+ this.appendToModel(model, text);
5257
5280
  const newLine = model.getLineCount();
5258
5281
  this.lastKnownModifiedLineCount = newLine;
5259
5282
  this.eagerlyGrowDiffContainerHeight();
@@ -5296,8 +5319,30 @@ var DiffEditorManager = class DiffEditorManager {
5296
5319
  });
5297
5320
  if (model === this.modifiedModel) this.lastKnownModifiedLineCount = model.getLineCount();
5298
5321
  }
5299
- appendToModel(model, appendText) {
5322
+ getModelValueLength(model) {
5323
+ const getValueLength = model.getValueLength;
5324
+ if (typeof getValueLength === "function") try {
5325
+ return getValueLength.call(model);
5326
+ } catch {}
5327
+ return model.getValue().length;
5328
+ }
5329
+ mergeKnownAppend(known, appendText, previousLength, model) {
5330
+ if (known == null) return model.getValue();
5331
+ const nextLength = previousLength + appendText.length;
5332
+ if (known.length === previousLength) return known + appendText;
5333
+ if (known.length >= nextLength && known.slice(previousLength, nextLength) === appendText) return known;
5334
+ return model.getValue();
5335
+ }
5336
+ syncKnownCodeAfterAppend(model, appendText, previousLength) {
5337
+ if (model === this.originalModel) {
5338
+ this.lastKnownOriginalCode = this.mergeKnownAppend(this.lastKnownOriginalCode, appendText, previousLength, model);
5339
+ return;
5340
+ }
5341
+ if (model === this.modifiedModel) this.lastKnownModifiedCode = this.mergeKnownAppend(this.lastKnownModifiedCode, appendText, previousLength, model);
5342
+ }
5343
+ appendToModel(model, appendText, knownLength) {
5300
5344
  if (!appendText) return;
5345
+ const previousLength = knownLength != null ? knownLength : this.getModelValueLength(model);
5301
5346
  const lastLine = model.getLineCount();
5302
5347
  const lastColumn = model.getLineMaxColumn(lastLine);
5303
5348
  const range = new monaco_shim_exports.Range(lastLine, lastColumn, lastLine, lastColumn);
@@ -5309,6 +5354,7 @@ var DiffEditorManager = class DiffEditorManager {
5309
5354
  }]);
5310
5355
  });
5311
5356
  if (model === this.modifiedModel) this.lastKnownModifiedLineCount = model.getLineCount();
5357
+ this.syncKnownCodeAfterAppend(model, appendText, previousLength);
5312
5358
  }
5313
5359
  applyModelEdit(model, fn) {
5314
5360
  if (model === this.modifiedModel) {
@@ -5346,6 +5392,8 @@ var EditorManager = class {
5346
5392
  minimalEditMaxChangeRatioValue = minimalEditMaxChangeRatio;
5347
5393
  lastUpdateFlushTime = 0;
5348
5394
  updateThrottleTimer = null;
5395
+ lastAppendFlushTime = 0;
5396
+ appendFlushThrottleTimer = null;
5349
5397
  shouldAutoScroll = true;
5350
5398
  scrollWatcher = null;
5351
5399
  scrollWatcherSuppressionTimer = null;
@@ -5445,6 +5493,10 @@ var EditorManager = class {
5445
5493
  clearTimeout(this.updateThrottleTimer);
5446
5494
  this.updateThrottleTimer = null;
5447
5495
  }
5496
+ if (this.appendFlushThrottleTimer != null) {
5497
+ clearTimeout(this.appendFlushThrottleTimer);
5498
+ this.appendFlushThrottleTimer = null;
5499
+ }
5448
5500
  }
5449
5501
  initDebugFlag() {
5450
5502
  if (typeof window !== "undefined" && window.__STREAM_MONACO_DEBUG__ !== void 0) {
@@ -5541,6 +5593,14 @@ var EditorManager = class {
5541
5593
  }
5542
5594
  maybeScrollToBottom(targetLine) {
5543
5595
  this.rafScheduler.schedule("maybe-scroll", () => {
5596
+ if (!(this.autoScrollOnUpdate && this.shouldAutoScroll)) {
5597
+ this.dlog("maybeScrollToBottom skipped (auto-scroll disabled)", {
5598
+ autoScrollOnUpdate: this.autoScrollOnUpdate,
5599
+ shouldAutoScroll: this.shouldAutoScroll,
5600
+ targetLine
5601
+ });
5602
+ return;
5603
+ }
5544
5604
  const hasVS = this.hasVerticalScrollbar();
5545
5605
  this.dlog("maybeScrollToBottom called", {
5546
5606
  autoScrollOnUpdate: this.autoScrollOnUpdate,
@@ -5548,8 +5608,8 @@ var EditorManager = class {
5548
5608
  hasVerticalScrollbar: hasVS,
5549
5609
  targetLine
5550
5610
  });
5551
- if (!(this.autoScrollOnUpdate && this.shouldAutoScroll && this.hasVerticalScrollbar())) {
5552
- this.dlog("maybeScrollToBottom skipped (auto-scroll conditions not met)");
5611
+ if (!hasVS) {
5612
+ this.dlog("maybeScrollToBottom skipped (no vertical scrollbar)");
5553
5613
  return;
5554
5614
  }
5555
5615
  const model = this.editorView.getModel();
@@ -5809,7 +5869,7 @@ var EditorManager = class {
5809
5869
  this.lastScrollTop = v;
5810
5870
  }
5811
5871
  });
5812
- this.maybeScrollToBottom();
5872
+ if (this.shouldAutoScroll) this.maybeScrollToBottom();
5813
5873
  return this.editorView;
5814
5874
  }
5815
5875
  syncLastKnownCode() {
@@ -5980,10 +6040,31 @@ var EditorManager = class {
5980
6040
  this.appendBuffer.push(appendText);
5981
6041
  if (!this.appendBufferScheduled) {
5982
6042
  this.appendBufferScheduled = true;
5983
- this.rafScheduler.schedule("append", () => this.flushAppendBuffer());
6043
+ this.scheduleFlushAppendBuffer();
5984
6044
  }
5985
6045
  }
5986
6046
  }
6047
+ scheduleFlushAppendBuffer() {
6048
+ const schedule = () => {
6049
+ this.rafScheduler.schedule("append", () => this.flushAppendBuffer());
6050
+ };
6051
+ if (!this.updateThrottleMs) {
6052
+ schedule();
6053
+ return;
6054
+ }
6055
+ const now = Date.now();
6056
+ const since = now - this.lastAppendFlushTime;
6057
+ if (since >= this.updateThrottleMs) {
6058
+ schedule();
6059
+ return;
6060
+ }
6061
+ if (this.appendFlushThrottleTimer != null) return;
6062
+ const wait = this.updateThrottleMs - since;
6063
+ this.appendFlushThrottleTimer = setTimeout(() => {
6064
+ this.appendFlushThrottleTimer = null;
6065
+ schedule();
6066
+ }, wait);
6067
+ }
5987
6068
  applyMinimalEdit(prev, next) {
5988
6069
  if (!this.editorView) return;
5989
6070
  const model = this.editorView.getModel();
@@ -6024,6 +6105,7 @@ var EditorManager = class {
6024
6105
  if (!this.editorView) return;
6025
6106
  if (this.appendBuffer.length === 0) return;
6026
6107
  this.appendBufferScheduled = false;
6108
+ this.lastAppendFlushTime = Date.now();
6027
6109
  const model = this.editorView.getModel();
6028
6110
  if (!model) {
6029
6111
  this.appendBuffer.length = 0;
@@ -6078,6 +6160,11 @@ var EditorManager = class {
6078
6160
  this.updateThrottleTimer = null;
6079
6161
  this.rafScheduler.schedule("update", () => this.flushPendingUpdate());
6080
6162
  }
6163
+ if (!this.updateThrottleMs && this.appendFlushThrottleTimer != null) {
6164
+ clearTimeout(this.appendFlushThrottleTimer);
6165
+ this.appendFlushThrottleTimer = null;
6166
+ this.rafScheduler.schedule("append", () => this.flushAppendBuffer());
6167
+ }
6081
6168
  }
6082
6169
  getUpdateThrottleMs() {
6083
6170
  return this.updateThrottleMs;
@@ -6154,6 +6241,8 @@ function arraysEqual(a, b) {
6154
6241
  const LEGACY_ONIG_INIT_KEY = "__streamMonacoLegacyOnigurumaInit__";
6155
6242
  const LEGACY_ENGINE_KEY = "__streamMonacoLegacyShikiEngine__";
6156
6243
  const LEGACY_MONACO_LANGS_INIT_KEY = "__streamMonacoLegacyMonacoLangsInit__";
6244
+ const PERF_HOOKS_ENABLED_KEY = "__STREAM_MONACO_ENABLE_INTERNAL_PERF_HOOKS__";
6245
+ let instrumentedHighlighterCache = /* @__PURE__ */ new WeakMap();
6157
6246
  async function awaitLegacyOnigurumaInitIfPresent() {
6158
6247
  try {
6159
6248
  var _globalThis;
@@ -6204,9 +6293,109 @@ let lastPatchedHighlighter = null;
6204
6293
  let lastPatchedLanguages = /* @__PURE__ */ new Set();
6205
6294
  const monacoThemeByKey = /* @__PURE__ */ new Map();
6206
6295
  const monacoLanguageSet = /* @__PURE__ */ new Set();
6296
+ function nowMs() {
6297
+ return typeof performance !== "undefined" && typeof performance.now === "function" ? performance.now() : Date.now();
6298
+ }
6299
+ function isPerfHooksEnabled() {
6300
+ try {
6301
+ var _globalThis4;
6302
+ return typeof globalThis !== "undefined" && ((_globalThis4 = globalThis) === null || _globalThis4 === void 0 ? void 0 : _globalThis4[PERF_HOOKS_ENABLED_KEY]) === true;
6303
+ } catch {
6304
+ return false;
6305
+ }
6306
+ }
6307
+ function getPerfHook(name) {
6308
+ if (!isPerfHooksEnabled()) return null;
6309
+ try {
6310
+ var _STREAM_MONACO_PERF__;
6311
+ if (typeof globalThis === "undefined") return null;
6312
+ const hook = (_STREAM_MONACO_PERF__ = globalThis.__STREAM_MONACO_PERF__) === null || _STREAM_MONACO_PERF__ === void 0 ? void 0 : _STREAM_MONACO_PERF__[name];
6313
+ return typeof hook === "function" ? hook : null;
6314
+ } catch {
6315
+ return null;
6316
+ }
6317
+ }
6318
+ function getTokenizationPerfHook() {
6319
+ return getPerfHook("recordTokenize");
6320
+ }
6321
+ function getGrammarTokenizationPerfHook() {
6322
+ return getPerfHook("recordGrammarTokenize");
6323
+ }
6324
+ function getThemeRegistrationPerfHook() {
6325
+ return getPerfHook("recordThemeRegistration");
6326
+ }
6327
+ function recordTokenize(hook, language, durationMs, line, tokenCount, failed) {
6328
+ if (!hook) return;
6329
+ try {
6330
+ hook({
6331
+ language,
6332
+ durationMs,
6333
+ lineLength: line.length,
6334
+ lineSample: line.slice(0, 120),
6335
+ tokenCount,
6336
+ failed
6337
+ });
6338
+ } catch {}
6339
+ }
6340
+ function recordGrammarTokenize(language, durationMs, line, stoppedEarly, tokenCount) {
6341
+ const hook = getGrammarTokenizationPerfHook();
6342
+ if (!hook) return;
6343
+ try {
6344
+ hook({
6345
+ language,
6346
+ durationMs,
6347
+ lineLength: line.length,
6348
+ lineSample: line.slice(0, 120),
6349
+ stoppedEarly,
6350
+ tokenCount
6351
+ });
6352
+ } catch {}
6353
+ }
6354
+ function recordThemeRegistration(event) {
6355
+ const hook = getThemeRegistrationPerfHook();
6356
+ if (!hook) return;
6357
+ try {
6358
+ hook(event);
6359
+ } catch {}
6360
+ }
6361
+ function getProxyMember(target, prop) {
6362
+ const value = Reflect.get(target, prop, target);
6363
+ return typeof value === "function" ? value.bind(target) : value;
6364
+ }
6207
6365
  function themeKey(t) {
6208
6366
  return typeof t === "string" ? t : t.name ?? JSON.stringify(t);
6209
6367
  }
6368
+ function maybeInstrumentHighlighterGrammar(highlighter) {
6369
+ if (!getGrammarTokenizationPerfHook()) return highlighter;
6370
+ if (!highlighter || typeof highlighter !== "object" && typeof highlighter !== "function") return highlighter;
6371
+ const cached = instrumentedHighlighterCache.get(highlighter);
6372
+ if (cached) return cached;
6373
+ const grammarProxyCache = /* @__PURE__ */ new WeakMap();
6374
+ const instrumentedHighlighter = new Proxy(highlighter, { get(target, prop, _receiver) {
6375
+ if (prop !== "getLanguage") return getProxyMember(target, prop);
6376
+ return (language) => {
6377
+ const grammar = target.getLanguage(language);
6378
+ if (!grammar || typeof grammar !== "object" || typeof grammar.tokenizeLine2 !== "function") return grammar;
6379
+ const cachedGrammar = grammarProxyCache.get(grammar);
6380
+ if (cachedGrammar) return cachedGrammar;
6381
+ const instrumentedGrammar = new Proxy(grammar, { get(grammarTarget, grammarProp, _grammarReceiver) {
6382
+ if (grammarProp !== "tokenizeLine2") return getProxyMember(grammarTarget, grammarProp);
6383
+ const originalTokenizeLine2 = grammarTarget.tokenizeLine2.bind(grammarTarget);
6384
+ return (line, ruleStack, timeLimit) => {
6385
+ var _result$tokens;
6386
+ const startedAt = nowMs();
6387
+ const result = originalTokenizeLine2(line, ruleStack, timeLimit);
6388
+ recordGrammarTokenize(language, nowMs() - startedAt, line, !!(result === null || result === void 0 ? void 0 : result.stoppedEarly), typeof (result === null || result === void 0 || (_result$tokens = result.tokens) === null || _result$tokens === void 0 ? void 0 : _result$tokens.length) === "number" ? result.tokens.length / 2 : 0);
6389
+ return result;
6390
+ };
6391
+ } });
6392
+ grammarProxyCache.set(grammar, instrumentedGrammar);
6393
+ return instrumentedGrammar;
6394
+ };
6395
+ } });
6396
+ instrumentedHighlighterCache.set(highlighter, instrumentedHighlighter);
6397
+ return instrumentedHighlighter;
6398
+ }
6210
6399
  async function ensureMonacoHighlighter(themes, languages$1) {
6211
6400
  for (const t of themes) monacoThemeByKey.set(themeKey(t), t);
6212
6401
  for (const l of languages$1) monacoLanguageSet.add(l);
@@ -6216,7 +6405,11 @@ async function ensureMonacoHighlighter(themes, languages$1) {
6216
6405
  monacoHighlighterPromise = createHighlighterWithLegacyEngineIfNeeded({
6217
6406
  themes: initialThemes,
6218
6407
  langs: initialLangs
6219
- }).then((h$1) => h$1);
6408
+ }).then((h$1) => {
6409
+ h$1.__streamMonacoLoadedThemes = new Set(initialThemes.map((t) => themeKey(t)));
6410
+ h$1.__streamMonacoLoadedLangs = new Set(initialLangs);
6411
+ return h$1;
6412
+ });
6220
6413
  }
6221
6414
  const h = await monacoHighlighterPromise;
6222
6415
  const wantsThemes = Array.from(monacoThemeByKey.values());
@@ -6244,7 +6437,11 @@ async function ensureMonacoHighlighter(themes, languages$1) {
6244
6437
  const p = createHighlighterWithLegacyEngineIfNeeded({
6245
6438
  themes: wantsThemes,
6246
6439
  langs: wantsLangs
6247
- }).then((hh) => hh);
6440
+ }).then((hh) => {
6441
+ hh.__streamMonacoLoadedThemes = new Set(wantsThemes.map((t) => themeKey(t)));
6442
+ hh.__streamMonacoLoadedLangs = new Set(wantsLangs);
6443
+ return hh;
6444
+ });
6248
6445
  monacoHighlighterPromise = p;
6249
6446
  return p;
6250
6447
  }
@@ -6263,6 +6460,7 @@ function clearHighlighterCache() {
6263
6460
  monacoThemeByKey.clear();
6264
6461
  monacoLanguageSet.clear();
6265
6462
  themeRegisterPromise = null;
6463
+ instrumentedHighlighterCache = /* @__PURE__ */ new WeakMap();
6266
6464
  languagesRegistered = false;
6267
6465
  currentLanguages = [];
6268
6466
  }
@@ -6329,9 +6527,15 @@ async function getOrCreateHighlighter(themes, languages$1) {
6329
6527
  */
6330
6528
  async function registerMonacoThemes(themes, languages$1) {
6331
6529
  return enqueueRegistration(async () => {
6530
+ const registrationStartedAt = nowMs();
6531
+ let ensureHighlighterMs = 0;
6532
+ let patchMonacoMs = 0;
6533
+ let patchedMonaco = false;
6332
6534
  registerMonacoLanguages(languages$1);
6333
6535
  const p = (async () => {
6536
+ const ensureHighlighterStartedAt = nowMs();
6334
6537
  const highlighter = await ensureMonacoHighlighter(themes, languages$1);
6538
+ ensureHighlighterMs = nowMs() - ensureHighlighterStartedAt;
6335
6539
  const wantsLangs = Array.from(monacoLanguageSet.values());
6336
6540
  const needsLanguagePatch = lastPatchedHighlighter !== highlighter || wantsLangs.some((l) => !lastPatchedLanguages.has(l));
6337
6541
  if (needsLanguagePatch) {
@@ -6352,9 +6556,17 @@ async function registerMonacoThemes(themes, languages$1) {
6352
6556
  provider = {
6353
6557
  ...provider,
6354
6558
  tokenize(line, state) {
6559
+ const hook = getTokenizationPerfHook();
6560
+ const startedAt = hook ? nowMs() : 0;
6561
+ let tokenCount = 0;
6562
+ let failed = false;
6355
6563
  try {
6356
- return originalTokenize(line, state);
6564
+ const result = originalTokenize(line, state);
6565
+ tokenCount = Array.isArray(result === null || result === void 0 ? void 0 : result.tokens) ? result.tokens.length : 0;
6566
+ return result;
6357
6567
  } catch {
6568
+ failed = true;
6569
+ tokenCount = 1;
6358
6570
  return {
6359
6571
  endState: state,
6360
6572
  tokens: [{
@@ -6362,6 +6574,8 @@ async function registerMonacoThemes(themes, languages$1) {
6362
6574
  scopes: ""
6363
6575
  }]
6364
6576
  };
6577
+ } finally {
6578
+ if (hook) recordTokenize(hook, lang, nowMs() - startedAt, line, tokenCount, failed);
6365
6579
  }
6366
6580
  }
6367
6581
  };
@@ -6370,7 +6584,10 @@ async function registerMonacoThemes(themes, languages$1) {
6370
6584
  }
6371
6585
  }
6372
6586
  };
6373
- (0, __shikijs_monaco.shikiToMonaco)(highlighter, monacoProxy);
6587
+ const patchMonacoStartedAt = nowMs();
6588
+ (0, __shikijs_monaco.shikiToMonaco)(maybeInstrumentHighlighterGrammar(highlighter), monacoProxy);
6589
+ patchMonacoMs = nowMs() - patchMonacoStartedAt;
6590
+ patchedMonaco = true;
6374
6591
  lastPatchedHighlighter = highlighter;
6375
6592
  lastPatchedLanguages = new Set(wantsLangs);
6376
6593
  }
@@ -6380,6 +6597,14 @@ async function registerMonacoThemes(themes, languages$1) {
6380
6597
  setThemeRegisterPromise(p);
6381
6598
  try {
6382
6599
  const res = await p;
6600
+ recordThemeRegistration({
6601
+ durationMs: nowMs() - registrationStartedAt,
6602
+ ensureHighlighterMs,
6603
+ patchMonacoMs,
6604
+ themes: themes.length,
6605
+ languages: languages$1.length,
6606
+ patchedMonaco
6607
+ });
6383
6608
  return res;
6384
6609
  } catch (e) {
6385
6610
  setThemeRegisterPromise(null);
@@ -6513,7 +6738,8 @@ function useMonaco(monacoOptions = {}) {
6513
6738
  let modifiedModel = null;
6514
6739
  const themes = monacoOptions.themes && ((_monacoOptions$themes = monacoOptions.themes) === null || _monacoOptions$themes === void 0 ? void 0 : _monacoOptions$themes.length) ? monacoOptions.themes : defaultThemes;
6515
6740
  if (!Array.isArray(themes) || themes.length < 2) throw new Error("Monaco themes must be an array with at least two themes: [darkTheme, lightTheme]");
6516
- const languages$1 = monacoOptions.languages ?? defaultLanguages;
6741
+ const explicitLanguages = monacoOptions.languages;
6742
+ const languages$1 = explicitLanguages ?? defaultLanguages;
6517
6743
  const MAX_HEIGHT = monacoOptions.MAX_HEIGHT ?? 500;
6518
6744
  const autoScrollOnUpdate = monacoOptions.autoScrollOnUpdate ?? true;
6519
6745
  const autoScrollInitial = monacoOptions.autoScrollInitial ?? true;
@@ -6602,10 +6828,27 @@ function useMonaco(monacoOptions = {}) {
6602
6828
  console.warn("onThemeChange callback threw an error:", err);
6603
6829
  }
6604
6830
  }
6605
- async function ensureThemeRegistered(themeName) {
6831
+ function normalizeLanguage(language) {
6832
+ if (!language) return null;
6833
+ return processedLanguage(language) || language;
6834
+ }
6835
+ function resolveCreateLanguages(primaryLanguage) {
6836
+ const primary = normalizeLanguage(primaryLanguage);
6837
+ if (Array.isArray(explicitLanguages) && explicitLanguages.length) {
6838
+ const set = /* @__PURE__ */ new Set();
6839
+ for (const language of explicitLanguages) {
6840
+ const normalized = normalizeLanguage(language);
6841
+ if (normalized) set.add(normalized);
6842
+ }
6843
+ if (primary) set.add(primary);
6844
+ return Array.from(set);
6845
+ }
6846
+ return primary ? [primary] : ["typescript"];
6847
+ }
6848
+ async function ensureThemeRegistered(themeName, requestedLanguages = languages$1) {
6606
6849
  const availableNames = themes.map((t) => typeof t === "string" ? t : t.name);
6607
6850
  const list = availableNames.includes(themeName) ? themes : themes.concat(themeName);
6608
- await registerMonacoThemes(list, languages$1);
6851
+ await registerMonacoThemes(list, requestedLanguages);
6609
6852
  }
6610
6853
  function resolveRequestedThemeName() {
6611
6854
  return requestedThemeName ?? globalRequestedThemeName ?? monacoOptions.theme ?? currentTheme.value;
@@ -6682,11 +6925,12 @@ function useMonaco(monacoOptions = {}) {
6682
6925
  queuedEditorUpdateDuringCreate = null;
6683
6926
  disposeAllPendingCreateDisposables();
6684
6927
  }
6685
- async function resolveCreateThemeName(requestId, kind) {
6928
+ async function resolveCreateThemeName(requestId, kind, primaryLanguage) {
6686
6929
  let themeName = resolveRequestedThemeName();
6930
+ const createLanguages = resolveCreateLanguages(primaryLanguage);
6687
6931
  while (true) {
6688
6932
  assertCreateStillActive(requestId, kind);
6689
- await ensureThemeRegistered(themeName);
6933
+ await ensureThemeRegistered(themeName, createLanguages);
6690
6934
  assertCreateStillActive(requestId, kind);
6691
6935
  const latestThemeName = resolveRequestedThemeName();
6692
6936
  if (latestThemeName === themeName) return themeName;
@@ -6705,7 +6949,7 @@ function useMonaco(monacoOptions = {}) {
6705
6949
  if (requestDisposables.length) pendingCreateDisposables.set(requestId, requestDisposables);
6706
6950
  let nextEditorMgr = null;
6707
6951
  try {
6708
- const initialThemeName = await resolveCreateThemeName(requestId, "editor");
6952
+ const initialThemeName = await resolveCreateThemeName(requestId, "editor", language);
6709
6953
  nextEditorMgr = new EditorManager(monacoOptions, maxHeightValue, maxHeightCSS, autoScrollOnUpdate, autoScrollInitial, autoScrollThresholdPx, autoScrollThresholdLines, monacoOptions.revealDebounceMs, monacoOptions.updateThrottleMs);
6710
6954
  const nextEditorView = await nextEditorMgr.createEditor(container, code, language, initialThemeName);
6711
6955
  assertCreateStillActive(requestId, "editor");
@@ -6752,7 +6996,7 @@ function useMonaco(monacoOptions = {}) {
6752
6996
  if (requestDisposables.length) pendingCreateDisposables.set(requestId, requestDisposables);
6753
6997
  let nextDiffMgr = null;
6754
6998
  try {
6755
- const initialThemeName = await resolveCreateThemeName(requestId, "diff");
6999
+ const initialThemeName = await resolveCreateThemeName(requestId, "diff", language);
6756
7000
  nextDiffMgr = new DiffEditorManager(monacoOptions, maxHeightValue, maxHeightCSS, autoScrollOnUpdate, autoScrollInitial, autoScrollThresholdPx, autoScrollThresholdLines, diffAutoScroll, monacoOptions.revealDebounceMs, monacoOptions.diffUpdateThrottleMs);
6757
7001
  const nextDiffEditorView = await nextDiffMgr.createDiffEditor(container, originalCode, modifiedCode, language, initialThemeName);
6758
7002
  assertCreateStillActive(requestId, "diff");