@lvce-editor/editor-worker 19.75.1 → 19.75.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/diff-sdk.js CHANGED
@@ -445,6 +445,7 @@ const editorStates = create$i();
445
445
  const {
446
446
  dispose: dispose$6,
447
447
  getCommandIds,
448
+ wrapCommand: wrapCommand$2,
448
449
  wrapGetter
449
450
  } = editorStates;
450
451
  const get$9 = id => {
@@ -1114,6 +1115,13 @@ const getHorizontalVisibleRange = (line, deltaX, width, averageCharWidth, tabSiz
1114
1115
  };
1115
1116
  const getHorizontalVisibleRangesMeasured = async (requests, measureWidths) => {
1116
1117
  const fallback = request => {
1118
+ if (request.line.length <= request.threshold) {
1119
+ return {
1120
+ difference: getHorizontalVisibleDifference(request.line, 0, request.deltaX, request.averageCharWidth, request.tabSize, []),
1121
+ end: request.line.length,
1122
+ start: 0
1123
+ };
1124
+ }
1117
1125
  const segments = request.segments || getSegments(request.line);
1118
1126
  const range = getHorizontalVisibleRange(request.line, request.deltaX, request.width, request.averageCharWidth, request.tabSize, request.threshold, segments);
1119
1127
  return {
@@ -1895,7 +1903,10 @@ const positionAt = (textDocument, offset) => {
1895
1903
  };
1896
1904
 
1897
1905
  const maxTokenizerLoadPasses$1 = 10;
1898
- const getGraphemeSegments = async lines => {
1906
+ const getGraphemeSegments = async (lines, threshold) => {
1907
+ if (lines.every(line => line.length <= threshold)) {
1908
+ return undefined;
1909
+ }
1899
1910
  try {
1900
1911
  return await TextMeasurementWorker.invoke('TextMeasurement.getGraphemeSegments', lines);
1901
1912
  } catch {
@@ -2228,7 +2239,7 @@ const getLineInfosViewport = async (editor, tokens, embeddedResults, minLineY, m
2228
2239
  let offset = minLineOffset;
2229
2240
  const tabSize = editor.tabSize ?? 2;
2230
2241
  const visibleLines = lines.slice(minLineY, maxLineY);
2231
- const graphemeSegments = await getGraphemeSegments(visibleLines);
2242
+ const graphemeSegments = await getGraphemeSegments(visibleLines, horizontalVirtualizationThreshold);
2232
2243
  const measuredRanges = await getHorizontalVisibleRangesMeasured(visibleLines.map((line, index) => ({
2233
2244
  averageCharWidth,
2234
2245
  deltaX,
@@ -3999,7 +4010,7 @@ const getTabCount = string => {
3999
4010
  return count;
4000
4011
  };
4001
4012
 
4002
- const getX = async (line, column, fontWeight, fontSize, fontFamily, isMonospaceFont, letterSpacing, tabSize, halfCursorWidth, width, averageCharWidth, difference = 0) => {
4013
+ const getX = async (line, column, fontWeight, fontSize, fontFamily, isMonospaceFont, letterSpacing, tabSize, halfCursorWidth, width, averageCharWidth, difference = 0, measuredLineWidth) => {
4003
4014
  if (!line) {
4004
4015
  return 0;
4005
4016
  }
@@ -4018,7 +4029,7 @@ const getX = async (line, column, fontWeight, fontSize, fontFamily, isMonospaceF
4018
4029
  const normalizedLine = normalizeText(line, normalize, tabSize);
4019
4030
  const tabCount = getTabCount(line.slice(0, column));
4020
4031
  const partialText = normalizedLine.slice(0, column + tabCount * (tabSize - 1));
4021
- const textWidth = await measureTextWidth(partialText, fontWeight, fontSize, fontFamily, letterSpacing, isMonospaceFont, averageCharWidth);
4032
+ const textWidth = column === line.length && measuredLineWidth !== undefined ? measuredLineWidth : await measureTextWidth(partialText, fontWeight, fontSize, fontFamily, letterSpacing, isMonospaceFont, averageCharWidth);
4022
4033
  return Math.min(width, textWidth - halfCursorWidth + difference);
4023
4034
  };
4024
4035
 
@@ -4401,6 +4412,109 @@ const px = value => {
4401
4412
  return `${value}px`;
4402
4413
  };
4403
4414
 
4415
+ // Owned by the editor lifecycle, with only the current document's distinct lines retained.
4416
+ const caches = new WeakMap();
4417
+ const getFontKey = ({
4418
+ charWidth,
4419
+ fontFamily,
4420
+ fontSize,
4421
+ fontWeight,
4422
+ isMonospaceFont,
4423
+ letterSpacing,
4424
+ tabSize
4425
+ }) => JSON.stringify([charWidth, fontFamily, fontSize, fontWeight, isMonospaceFont, letterSpacing, tabSize]);
4426
+ const getCachedLineWidth = (state, line) => {
4427
+ const {
4428
+ largeFile,
4429
+ lifecycle,
4430
+ lines
4431
+ } = state;
4432
+ if (largeFile || lifecycle?.disposed) {
4433
+ return undefined;
4434
+ }
4435
+ const cache = caches.get(lifecycle || lines);
4436
+ return cache?.font === getFontKey(state) ? cache.widths.get(line) : undefined;
4437
+ };
4438
+ const updateHorizontalLayout = async state => {
4439
+ const {
4440
+ breakPoints = [],
4441
+ deltaX,
4442
+ gutterDecorations = [],
4443
+ largeFile,
4444
+ lifecycle,
4445
+ lightBulbRowIndex = -1,
4446
+ lineNumbers
4447
+ } = state;
4448
+ if (largeFile || lifecycle?.disposed) {
4449
+ return state;
4450
+ }
4451
+ const {
4452
+ charWidth,
4453
+ fontFamily,
4454
+ fontSize,
4455
+ fontWeight,
4456
+ isMonospaceFont,
4457
+ letterSpacing,
4458
+ lines,
4459
+ tabSize,
4460
+ width
4461
+ } = state;
4462
+ const font = getFontKey(state);
4463
+ const owner = lifecycle || lines;
4464
+ const previous = caches.get(owner);
4465
+ const previousWidths = previous?.font === font ? previous.widths : undefined;
4466
+ const widths = new Map();
4467
+ const textsToMeasure = new Set();
4468
+ const textByLine = new Map();
4469
+ for (const line of lines) {
4470
+ const measured = previousWidths?.get(line);
4471
+ if (measured !== undefined) {
4472
+ widths.set(line, measured);
4473
+ continue;
4474
+ }
4475
+ const text = line.replaceAll('\t', () => ' '.repeat(tabSize));
4476
+ textByLine.set(line, text);
4477
+ textsToMeasure.add(text);
4478
+ }
4479
+ const showGutter = lineNumbers || breakPoints.length > 0 || lightBulbRowIndex >= 0 || gutterDecorations.length > 0;
4480
+ const gutterText = String(lines.length);
4481
+ if (showGutter && !widths.has(gutterText)) {
4482
+ const measured = previousWidths?.get(gutterText);
4483
+ if (measured === undefined) {
4484
+ textsToMeasure.add(gutterText);
4485
+ } else {
4486
+ widths.set(gutterText, measured);
4487
+ }
4488
+ }
4489
+ const texts = [...textsToMeasure];
4490
+ const measuredWidths = texts.length > 0 ? await measureTextWidths(texts, fontWeight, fontSize, fontFamily, letterSpacing, isMonospaceFont, charWidth) : [];
4491
+ const measuredByText = new Map(texts.map((text, index) => [text, measuredWidths[index]]));
4492
+ for (const [line, text] of textByLine) {
4493
+ widths.set(line, measuredByText.get(text));
4494
+ }
4495
+ if (showGutter && !widths.has(gutterText)) {
4496
+ widths.set(gutterText, measuredByText.get(gutterText));
4497
+ }
4498
+ let longestLineWidth = 0;
4499
+ for (const line of lines) {
4500
+ longestLineWidth = Math.max(longestLineWidth, widths.get(line));
4501
+ }
4502
+ if (lifecycle?.disposed) {
4503
+ return state;
4504
+ }
4505
+ const gutterWidth = showGutter ? Math.max(30, widths.get(gutterText) + 1) : 0;
4506
+ caches.set(owner, {
4507
+ font,
4508
+ widths
4509
+ });
4510
+ return {
4511
+ ...state,
4512
+ deltaX: clamp(deltaX, 0, Math.max(0, longestLineWidth - Math.max(0, width - gutterWidth))),
4513
+ gutterWidth,
4514
+ longestLineWidth
4515
+ };
4516
+ };
4517
+
4404
4518
  const fromRange = (startRowIndex, startColumnIndex, endRowIndex, endColumnIndex) => {
4405
4519
  return new Uint32Array([startRowIndex, startColumnIndex, endRowIndex, endColumnIndex]);
4406
4520
  };
@@ -4545,7 +4659,7 @@ const getVisible = async editor => {
4545
4659
  const relativeEndLineRow = getRelativeRow(selectionEndRow);
4546
4660
  const endLineDifference = horizontalOffset;
4547
4661
  const endLine = lines[selectionEndRow];
4548
- const endLineEndX = await getX(endLine, selectionEndColumn, fontWeight, fontSize, fontFamily, isMonospaceFont, letterSpacing, tabSize, halfCursorWidth, width, averageCharWidth, endLineDifference);
4662
+ const endLineEndX = await getX(endLine, selectionEndColumn, fontWeight, fontSize, fontFamily, isMonospaceFont, letterSpacing, tabSize, halfCursorWidth, width, averageCharWidth, endLineDifference, getCachedLineWidth(editor, endLine));
4549
4663
  const endLineY = relativeEndLineRow * rowHeight;
4550
4664
  if (isEmpty$1(selectionStartRow, selectionStartColumn, selectionEndRow, selectionEndColumn) && endLineEndX > 0) {
4551
4665
  visibleCursors.push(endLineEndX, endLineY);
@@ -5658,88 +5772,6 @@ const getVisibleBracketMatches = async editor => {
5658
5772
  return infos.filter(info => info !== undefined);
5659
5773
  };
5660
5774
 
5661
- // Owned by the editor lifecycle, with only the current document's distinct lines retained.
5662
- const caches = new WeakMap();
5663
- const updateHorizontalLayout = async state => {
5664
- const {
5665
- breakPoints = [],
5666
- deltaX,
5667
- gutterDecorations = [],
5668
- largeFile,
5669
- lifecycle,
5670
- lightBulbRowIndex = -1,
5671
- lineNumbers
5672
- } = state;
5673
- if (largeFile || lifecycle?.disposed) {
5674
- return state;
5675
- }
5676
- const {
5677
- charWidth,
5678
- fontFamily,
5679
- fontSize,
5680
- fontWeight,
5681
- isMonospaceFont,
5682
- letterSpacing,
5683
- lines,
5684
- tabSize,
5685
- width
5686
- } = state;
5687
- const font = JSON.stringify([charWidth, fontFamily, fontSize, fontWeight, isMonospaceFont, letterSpacing, tabSize]);
5688
- const owner = lifecycle || lines;
5689
- const previous = caches.get(owner);
5690
- const previousWidths = previous?.font === font ? previous.widths : undefined;
5691
- const widths = new Map();
5692
- const textsToMeasure = new Set();
5693
- const textByLine = new Map();
5694
- for (const line of lines) {
5695
- const measured = previousWidths?.get(line);
5696
- if (measured !== undefined) {
5697
- widths.set(line, measured);
5698
- continue;
5699
- }
5700
- const text = line.replaceAll('\t', () => ' '.repeat(tabSize));
5701
- textByLine.set(line, text);
5702
- textsToMeasure.add(text);
5703
- }
5704
- const showGutter = lineNumbers || breakPoints.length > 0 || lightBulbRowIndex >= 0 || gutterDecorations.length > 0;
5705
- const gutterText = String(lines.length);
5706
- if (showGutter && !widths.has(gutterText)) {
5707
- const measured = previousWidths?.get(gutterText);
5708
- if (measured === undefined) {
5709
- textsToMeasure.add(gutterText);
5710
- } else {
5711
- widths.set(gutterText, measured);
5712
- }
5713
- }
5714
- const texts = [...textsToMeasure];
5715
- const measuredWidths = texts.length > 0 ? await measureTextWidths(texts, fontWeight, fontSize, fontFamily, letterSpacing, isMonospaceFont, charWidth) : [];
5716
- const measuredByText = new Map(texts.map((text, index) => [text, measuredWidths[index]]));
5717
- for (const [line, text] of textByLine) {
5718
- widths.set(line, measuredByText.get(text));
5719
- }
5720
- if (showGutter && !widths.has(gutterText)) {
5721
- widths.set(gutterText, measuredByText.get(gutterText));
5722
- }
5723
- let longestLineWidth = 0;
5724
- for (const line of lines) {
5725
- longestLineWidth = Math.max(longestLineWidth, widths.get(line));
5726
- }
5727
- if (lifecycle?.disposed) {
5728
- return state;
5729
- }
5730
- const gutterWidth = showGutter ? Math.max(30, widths.get(gutterText) + 1) : 0;
5731
- caches.set(owner, {
5732
- font,
5733
- widths
5734
- });
5735
- return {
5736
- ...state,
5737
- deltaX: clamp(deltaX, 0, Math.max(0, longestLineWidth - Math.max(0, width - gutterWidth))),
5738
- gutterWidth,
5739
- longestLineWidth
5740
- };
5741
- };
5742
-
5743
5775
  const shouldUpdateDiagnosticData = (oldState, newState) => {
5744
5776
  return oldState.diagnostics !== newState.diagnostics || (newState.diagnostics?.length ?? 0) > 0 && (oldState.deltaX !== newState.deltaX || oldState.minLineY !== newState.minLineY || oldState.charWidth !== newState.charWidth || oldState.fontFamily !== newState.fontFamily || oldState.fontSize !== newState.fontSize || oldState.fontWeight !== newState.fontWeight || oldState.isMonospaceFont !== newState.isMonospaceFont || oldState.letterSpacing !== newState.letterSpacing || oldState.lines !== newState.lines || oldState.rowHeight !== newState.rowHeight || oldState.tabSize !== newState.tabSize || oldState.width !== newState.width);
5745
5777
  };
@@ -15918,6 +15950,17 @@ const ensure = async (fontName, fontUrl) => {
15918
15950
  await TextMeasurementWorker.invoke('TextMeasurement.ensureFont', fontName, fontUrl);
15919
15951
  };
15920
15952
 
15953
+ const getComponentState = uid => {
15954
+ const {
15955
+ newState
15956
+ } = get$9(uid);
15957
+ return {
15958
+ ...newState,
15959
+ cursorUndoStack: newState.cursorUndoStack?.map(selection => [...selection]),
15960
+ selections: [...newState.selections]
15961
+ };
15962
+ };
15963
+
15921
15964
  const findWidgetFocusContexts = [FindWidget, FocusFindWidgetReplace$1, FocusFindWidgetOptions, FocusFindWidgetToggleReplace, FocusFindWidgetPreviousMatchButton, FocusFindWidgetNextMatchButton, FocusFindWidgetCloseButton, FocusFindWidgetReplaceButton, FocusFindWidgetReplaceAllButton];
15922
15965
  const getFindWidgetFocusKeyBindings = () => {
15923
15966
  return findWidgetFocusContexts.flatMap(focusContext => [{
@@ -19281,6 +19324,62 @@ const sendDeprecatedExtensionHostPortToExtensionManagementWorker = async (port,
19281
19324
  await RendererWorker.sendMessagePortToExtensionManagementWorker(port, rpcId);
19282
19325
  };
19283
19326
 
19327
+ const restoreSelections = value => {
19328
+ if (!Array.isArray(value) || value.length % 4 !== 0 || value.some(item => !Number.isSafeInteger(item) || item < 0 || item > 0xff_ff_ff_ff)) {
19329
+ throw new TypeError('Editor selections must be an array of unsigned integer ranges');
19330
+ }
19331
+ return new Uint32Array(value);
19332
+ };
19333
+ const applyComponentState = async (currentState, value) => {
19334
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
19335
+ throw new TypeError('Editor state must be an object');
19336
+ }
19337
+ const state = value;
19338
+ const {
19339
+ cursorUndoStack: jsonCursorUndoStack,
19340
+ lines,
19341
+ selections: jsonSelections
19342
+ } = state;
19343
+ for (const key of ['uid', 'id', 'uri']) {
19344
+ if (state[key] !== currentState[key]) {
19345
+ throw new Error(`Editor state ${key} must remain ${currentState[key]}`);
19346
+ }
19347
+ }
19348
+ if (!Array.isArray(lines) || lines.some(line => typeof line !== 'string')) {
19349
+ throw new TypeError('Editor lines must be an array of strings');
19350
+ }
19351
+ if (jsonCursorUndoStack !== undefined && !Array.isArray(jsonCursorUndoStack)) {
19352
+ throw new TypeError('Editor cursorUndoStack must be an array');
19353
+ }
19354
+ const selections = restoreSelections(jsonSelections);
19355
+ const cursorUndoStack = jsonCursorUndoStack?.map(restoreSelections);
19356
+ const normalized = {
19357
+ ...state,
19358
+ cursorUndoStack,
19359
+ selections
19360
+ };
19361
+ const currentJsonState = getComponentState(currentState.uid);
19362
+ // Keep unchanged collections so a JSON round trip does not invalidate every render cache.
19363
+ const entries = Object.entries(normalized).map(([key, entry]) => [key, JSON.stringify(state[key]) === JSON.stringify(currentJsonState[key]) ? currentState[key] : entry]);
19364
+ // A save can repeat an earlier live edit with its old text caches still in the JSON.
19365
+ const newState = {
19366
+ ...Object.fromEntries(entries),
19367
+ differences: currentState.differences,
19368
+ textInfos: currentState.textInfos
19369
+ };
19370
+ const updatedState = newState.lines === currentState.lines ? newState : {
19371
+ ...newState,
19372
+ incrementalEdits: emptyIncrementalEdits,
19373
+ invalidStartIndex: 0,
19374
+ numberOfLines: newState.lines.length
19375
+ };
19376
+ return updateDerivedState(currentState, updatedState);
19377
+ };
19378
+ const applyState = wrapCommand$2(applyComponentState);
19379
+ const setComponentState = (uid, value) => {
19380
+ return enqueue(uid, () => applyState(uid, value));
19381
+ };
19382
+
19284
19383
  const setDebugEnabled = (state, enabled) => {
19285
19384
  return state;
19286
19385
  };
@@ -19474,6 +19573,7 @@ const commandMap = {
19474
19573
  'Editor.format': wrapCommand(format),
19475
19574
  'Editor.getCommandIds': getCommandIds,
19476
19575
  'Editor.getCompletionContext': getCompletionContext,
19576
+ 'Editor.getComponentState': getComponentState,
19477
19577
  'Editor.getDiagnostics': getDiagnostics,
19478
19578
  'Editor.getKeyBindings': getKeyBindings,
19479
19579
  'Editor.getKeys': getKeys,
@@ -19596,6 +19696,7 @@ const commandMap = {
19596
19696
  'Editor.selectWord': wrapCommand(selectWord),
19597
19697
  'Editor.selectWordLeft': wrapCommand(selectWordLeft),
19598
19698
  'Editor.selectWordRight': wrapCommand(selectWordRight),
19699
+ 'Editor.setComponentState': setComponentState,
19599
19700
  'Editor.setDebugEnabled': wrapCommand(setDebugEnabled),
19600
19701
  'Editor.setDecorations': wrapCommand(setDecorations),
19601
19702
  'Editor.setDelta': wrapCommand(setDelta),
@@ -1796,6 +1796,7 @@ const {
1796
1796
  dispose: dispose$6,
1797
1797
  getCommandIds,
1798
1798
  registerCommands,
1799
+ wrapCommand: wrapCommand$2,
1799
1800
  wrapGetter
1800
1801
  } = editorStates;
1801
1802
  const get$8 = id => {
@@ -2465,6 +2466,13 @@ const getHorizontalVisibleRange = (line, deltaX, width, averageCharWidth, tabSiz
2465
2466
  };
2466
2467
  const getHorizontalVisibleRangesMeasured = async (requests, measureWidths) => {
2467
2468
  const fallback = request => {
2469
+ if (request.line.length <= request.threshold) {
2470
+ return {
2471
+ difference: getHorizontalVisibleDifference(request.line, 0, request.deltaX, request.averageCharWidth, request.tabSize, []),
2472
+ end: request.line.length,
2473
+ start: 0
2474
+ };
2475
+ }
2468
2476
  const segments = request.segments || getSegments(request.line);
2469
2477
  const range = getHorizontalVisibleRange(request.line, request.deltaX, request.width, request.averageCharWidth, request.tabSize, request.threshold, segments);
2470
2478
  return {
@@ -3246,7 +3254,10 @@ const positionAt = (textDocument, offset) => {
3246
3254
  };
3247
3255
 
3248
3256
  const maxTokenizerLoadPasses$1 = 10;
3249
- const getGraphemeSegments = async lines => {
3257
+ const getGraphemeSegments = async (lines, threshold) => {
3258
+ if (lines.every(line => line.length <= threshold)) {
3259
+ return undefined;
3260
+ }
3250
3261
  try {
3251
3262
  return await invoke$f('TextMeasurement.getGraphemeSegments', lines);
3252
3263
  } catch {
@@ -3579,7 +3590,7 @@ const getLineInfosViewport = async (editor, tokens, embeddedResults, minLineY, m
3579
3590
  let offset = minLineOffset;
3580
3591
  const tabSize = editor.tabSize ?? 2;
3581
3592
  const visibleLines = lines.slice(minLineY, maxLineY);
3582
- const graphemeSegments = await getGraphemeSegments(visibleLines);
3593
+ const graphemeSegments = await getGraphemeSegments(visibleLines, horizontalVirtualizationThreshold);
3583
3594
  const measuredRanges = await getHorizontalVisibleRangesMeasured(visibleLines.map((line, index) => ({
3584
3595
  averageCharWidth,
3585
3596
  deltaX,
@@ -4412,7 +4423,7 @@ const getTabCount = string => {
4412
4423
  return count;
4413
4424
  };
4414
4425
 
4415
- const getX = async (line, column, fontWeight, fontSize, fontFamily, isMonospaceFont, letterSpacing, tabSize, halfCursorWidth, width, averageCharWidth, difference = 0) => {
4426
+ const getX = async (line, column, fontWeight, fontSize, fontFamily, isMonospaceFont, letterSpacing, tabSize, halfCursorWidth, width, averageCharWidth, difference = 0, measuredLineWidth) => {
4416
4427
  if (!line) {
4417
4428
  return 0;
4418
4429
  }
@@ -4431,7 +4442,7 @@ const getX = async (line, column, fontWeight, fontSize, fontFamily, isMonospaceF
4431
4442
  const normalizedLine = normalizeText(line, normalize, tabSize);
4432
4443
  const tabCount = getTabCount(line.slice(0, column));
4433
4444
  const partialText = normalizedLine.slice(0, column + tabCount * (tabSize - 1));
4434
- const textWidth = await measureTextWidth(partialText, fontWeight, fontSize, fontFamily, letterSpacing, isMonospaceFont, averageCharWidth);
4445
+ const textWidth = column === line.length && measuredLineWidth !== undefined ? measuredLineWidth : await measureTextWidth(partialText, fontWeight, fontSize, fontFamily, letterSpacing, isMonospaceFont, averageCharWidth);
4435
4446
  return Math.min(width, textWidth - halfCursorWidth + difference);
4436
4447
  };
4437
4448
 
@@ -4814,6 +4825,109 @@ const px = value => {
4814
4825
  return `${value}px`;
4815
4826
  };
4816
4827
 
4828
+ // Owned by the editor lifecycle, with only the current document's distinct lines retained.
4829
+ const caches = new WeakMap();
4830
+ const getFontKey = ({
4831
+ charWidth,
4832
+ fontFamily,
4833
+ fontSize,
4834
+ fontWeight,
4835
+ isMonospaceFont,
4836
+ letterSpacing,
4837
+ tabSize
4838
+ }) => JSON.stringify([charWidth, fontFamily, fontSize, fontWeight, isMonospaceFont, letterSpacing, tabSize]);
4839
+ const getCachedLineWidth = (state, line) => {
4840
+ const {
4841
+ largeFile,
4842
+ lifecycle,
4843
+ lines
4844
+ } = state;
4845
+ if (largeFile || lifecycle?.disposed) {
4846
+ return undefined;
4847
+ }
4848
+ const cache = caches.get(lifecycle || lines);
4849
+ return cache?.font === getFontKey(state) ? cache.widths.get(line) : undefined;
4850
+ };
4851
+ const updateHorizontalLayout = async state => {
4852
+ const {
4853
+ breakPoints = [],
4854
+ deltaX,
4855
+ gutterDecorations = [],
4856
+ largeFile,
4857
+ lifecycle,
4858
+ lightBulbRowIndex = -1,
4859
+ lineNumbers
4860
+ } = state;
4861
+ if (largeFile || lifecycle?.disposed) {
4862
+ return state;
4863
+ }
4864
+ const {
4865
+ charWidth,
4866
+ fontFamily,
4867
+ fontSize,
4868
+ fontWeight,
4869
+ isMonospaceFont,
4870
+ letterSpacing,
4871
+ lines,
4872
+ tabSize,
4873
+ width
4874
+ } = state;
4875
+ const font = getFontKey(state);
4876
+ const owner = lifecycle || lines;
4877
+ const previous = caches.get(owner);
4878
+ const previousWidths = previous?.font === font ? previous.widths : undefined;
4879
+ const widths = new Map();
4880
+ const textsToMeasure = new Set();
4881
+ const textByLine = new Map();
4882
+ for (const line of lines) {
4883
+ const measured = previousWidths?.get(line);
4884
+ if (measured !== undefined) {
4885
+ widths.set(line, measured);
4886
+ continue;
4887
+ }
4888
+ const text = line.replaceAll('\t', () => ' '.repeat(tabSize));
4889
+ textByLine.set(line, text);
4890
+ textsToMeasure.add(text);
4891
+ }
4892
+ const showGutter = lineNumbers || breakPoints.length > 0 || lightBulbRowIndex >= 0 || gutterDecorations.length > 0;
4893
+ const gutterText = String(lines.length);
4894
+ if (showGutter && !widths.has(gutterText)) {
4895
+ const measured = previousWidths?.get(gutterText);
4896
+ if (measured === undefined) {
4897
+ textsToMeasure.add(gutterText);
4898
+ } else {
4899
+ widths.set(gutterText, measured);
4900
+ }
4901
+ }
4902
+ const texts = [...textsToMeasure];
4903
+ const measuredWidths = texts.length > 0 ? await measureTextWidths(texts, fontWeight, fontSize, fontFamily, letterSpacing, isMonospaceFont, charWidth) : [];
4904
+ const measuredByText = new Map(texts.map((text, index) => [text, measuredWidths[index]]));
4905
+ for (const [line, text] of textByLine) {
4906
+ widths.set(line, measuredByText.get(text));
4907
+ }
4908
+ if (showGutter && !widths.has(gutterText)) {
4909
+ widths.set(gutterText, measuredByText.get(gutterText));
4910
+ }
4911
+ let longestLineWidth = 0;
4912
+ for (const line of lines) {
4913
+ longestLineWidth = Math.max(longestLineWidth, widths.get(line));
4914
+ }
4915
+ if (lifecycle?.disposed) {
4916
+ return state;
4917
+ }
4918
+ const gutterWidth = showGutter ? Math.max(30, widths.get(gutterText) + 1) : 0;
4919
+ caches.set(owner, {
4920
+ font,
4921
+ widths
4922
+ });
4923
+ return {
4924
+ ...state,
4925
+ deltaX: clamp(deltaX, 0, Math.max(0, longestLineWidth - Math.max(0, width - gutterWidth))),
4926
+ gutterWidth,
4927
+ longestLineWidth
4928
+ };
4929
+ };
4930
+
4817
4931
  const fromRange = (startRowIndex, startColumnIndex, endRowIndex, endColumnIndex) => {
4818
4932
  return new Uint32Array([startRowIndex, startColumnIndex, endRowIndex, endColumnIndex]);
4819
4933
  };
@@ -4958,7 +5072,7 @@ const getVisible = async editor => {
4958
5072
  const relativeEndLineRow = getRelativeRow(selectionEndRow);
4959
5073
  const endLineDifference = horizontalOffset;
4960
5074
  const endLine = lines[selectionEndRow];
4961
- const endLineEndX = await getX(endLine, selectionEndColumn, fontWeight, fontSize, fontFamily, isMonospaceFont, letterSpacing, tabSize, halfCursorWidth, width, averageCharWidth, endLineDifference);
5075
+ const endLineEndX = await getX(endLine, selectionEndColumn, fontWeight, fontSize, fontFamily, isMonospaceFont, letterSpacing, tabSize, halfCursorWidth, width, averageCharWidth, endLineDifference, getCachedLineWidth(editor, endLine));
4962
5076
  const endLineY = relativeEndLineRow * rowHeight;
4963
5077
  if (isEmpty$1(selectionStartRow, selectionStartColumn, selectionEndRow, selectionEndColumn) && endLineEndX > 0) {
4964
5078
  visibleCursors.push(endLineEndX, endLineY);
@@ -6071,88 +6185,6 @@ const getVisibleBracketMatches = async editor => {
6071
6185
  return infos.filter(info => info !== undefined);
6072
6186
  };
6073
6187
 
6074
- // Owned by the editor lifecycle, with only the current document's distinct lines retained.
6075
- const caches = new WeakMap();
6076
- const updateHorizontalLayout = async state => {
6077
- const {
6078
- breakPoints = [],
6079
- deltaX,
6080
- gutterDecorations = [],
6081
- largeFile,
6082
- lifecycle,
6083
- lightBulbRowIndex = -1,
6084
- lineNumbers
6085
- } = state;
6086
- if (largeFile || lifecycle?.disposed) {
6087
- return state;
6088
- }
6089
- const {
6090
- charWidth,
6091
- fontFamily,
6092
- fontSize,
6093
- fontWeight,
6094
- isMonospaceFont,
6095
- letterSpacing,
6096
- lines,
6097
- tabSize,
6098
- width
6099
- } = state;
6100
- const font = JSON.stringify([charWidth, fontFamily, fontSize, fontWeight, isMonospaceFont, letterSpacing, tabSize]);
6101
- const owner = lifecycle || lines;
6102
- const previous = caches.get(owner);
6103
- const previousWidths = previous?.font === font ? previous.widths : undefined;
6104
- const widths = new Map();
6105
- const textsToMeasure = new Set();
6106
- const textByLine = new Map();
6107
- for (const line of lines) {
6108
- const measured = previousWidths?.get(line);
6109
- if (measured !== undefined) {
6110
- widths.set(line, measured);
6111
- continue;
6112
- }
6113
- const text = line.replaceAll('\t', () => ' '.repeat(tabSize));
6114
- textByLine.set(line, text);
6115
- textsToMeasure.add(text);
6116
- }
6117
- const showGutter = lineNumbers || breakPoints.length > 0 || lightBulbRowIndex >= 0 || gutterDecorations.length > 0;
6118
- const gutterText = String(lines.length);
6119
- if (showGutter && !widths.has(gutterText)) {
6120
- const measured = previousWidths?.get(gutterText);
6121
- if (measured === undefined) {
6122
- textsToMeasure.add(gutterText);
6123
- } else {
6124
- widths.set(gutterText, measured);
6125
- }
6126
- }
6127
- const texts = [...textsToMeasure];
6128
- const measuredWidths = texts.length > 0 ? await measureTextWidths(texts, fontWeight, fontSize, fontFamily, letterSpacing, isMonospaceFont, charWidth) : [];
6129
- const measuredByText = new Map(texts.map((text, index) => [text, measuredWidths[index]]));
6130
- for (const [line, text] of textByLine) {
6131
- widths.set(line, measuredByText.get(text));
6132
- }
6133
- if (showGutter && !widths.has(gutterText)) {
6134
- widths.set(gutterText, measuredByText.get(gutterText));
6135
- }
6136
- let longestLineWidth = 0;
6137
- for (const line of lines) {
6138
- longestLineWidth = Math.max(longestLineWidth, widths.get(line));
6139
- }
6140
- if (lifecycle?.disposed) {
6141
- return state;
6142
- }
6143
- const gutterWidth = showGutter ? Math.max(30, widths.get(gutterText) + 1) : 0;
6144
- caches.set(owner, {
6145
- font,
6146
- widths
6147
- });
6148
- return {
6149
- ...state,
6150
- deltaX: clamp(deltaX, 0, Math.max(0, longestLineWidth - Math.max(0, width - gutterWidth))),
6151
- gutterWidth,
6152
- longestLineWidth
6153
- };
6154
- };
6155
-
6156
6188
  const shouldUpdateDiagnosticData = (oldState, newState) => {
6157
6189
  return oldState.diagnostics !== newState.diagnostics || (newState.diagnostics?.length ?? 0) > 0 && (oldState.deltaX !== newState.deltaX || oldState.minLineY !== newState.minLineY || oldState.charWidth !== newState.charWidth || oldState.fontFamily !== newState.fontFamily || oldState.fontSize !== newState.fontSize || oldState.fontWeight !== newState.fontWeight || oldState.isMonospaceFont !== newState.isMonospaceFont || oldState.letterSpacing !== newState.letterSpacing || oldState.lines !== newState.lines || oldState.rowHeight !== newState.rowHeight || oldState.tabSize !== newState.tabSize || oldState.width !== newState.width);
6158
6190
  };
@@ -16322,6 +16354,17 @@ const ensure = async (fontName, fontUrl) => {
16322
16354
  await invoke$f('TextMeasurement.ensureFont', fontName, fontUrl);
16323
16355
  };
16324
16356
 
16357
+ const getComponentState = uid => {
16358
+ const {
16359
+ newState
16360
+ } = get$8(uid);
16361
+ return {
16362
+ ...newState,
16363
+ cursorUndoStack: newState.cursorUndoStack?.map(selection => [...selection]),
16364
+ selections: [...newState.selections]
16365
+ };
16366
+ };
16367
+
16325
16368
  const findWidgetFocusContexts = [FindWidget, FocusFindWidgetReplace$1, FocusFindWidgetOptions, FocusFindWidgetToggleReplace, FocusFindWidgetPreviousMatchButton, FocusFindWidgetNextMatchButton, FocusFindWidgetCloseButton, FocusFindWidgetReplaceButton, FocusFindWidgetReplaceAllButton];
16326
16369
  const getFindWidgetFocusKeyBindings = () => {
16327
16370
  return findWidgetFocusContexts.flatMap(focusContext => [{
@@ -19685,6 +19728,62 @@ const sendDeprecatedExtensionHostPortToExtensionManagementWorker = async (port,
19685
19728
  await sendMessagePortToExtensionManagementWorker$1(port, rpcId);
19686
19729
  };
19687
19730
 
19731
+ const restoreSelections = value => {
19732
+ if (!Array.isArray(value) || value.length % 4 !== 0 || value.some(item => !Number.isSafeInteger(item) || item < 0 || item > 0xff_ff_ff_ff)) {
19733
+ throw new TypeError('Editor selections must be an array of unsigned integer ranges');
19734
+ }
19735
+ return new Uint32Array(value);
19736
+ };
19737
+ const applyComponentState = async (currentState, value) => {
19738
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
19739
+ throw new TypeError('Editor state must be an object');
19740
+ }
19741
+ const state = value;
19742
+ const {
19743
+ cursorUndoStack: jsonCursorUndoStack,
19744
+ lines,
19745
+ selections: jsonSelections
19746
+ } = state;
19747
+ for (const key of ['uid', 'id', 'uri']) {
19748
+ if (state[key] !== currentState[key]) {
19749
+ throw new Error(`Editor state ${key} must remain ${currentState[key]}`);
19750
+ }
19751
+ }
19752
+ if (!Array.isArray(lines) || lines.some(line => typeof line !== 'string')) {
19753
+ throw new TypeError('Editor lines must be an array of strings');
19754
+ }
19755
+ if (jsonCursorUndoStack !== undefined && !Array.isArray(jsonCursorUndoStack)) {
19756
+ throw new TypeError('Editor cursorUndoStack must be an array');
19757
+ }
19758
+ const selections = restoreSelections(jsonSelections);
19759
+ const cursorUndoStack = jsonCursorUndoStack?.map(restoreSelections);
19760
+ const normalized = {
19761
+ ...state,
19762
+ cursorUndoStack,
19763
+ selections
19764
+ };
19765
+ const currentJsonState = getComponentState(currentState.uid);
19766
+ // Keep unchanged collections so a JSON round trip does not invalidate every render cache.
19767
+ const entries = Object.entries(normalized).map(([key, entry]) => [key, JSON.stringify(state[key]) === JSON.stringify(currentJsonState[key]) ? currentState[key] : entry]);
19768
+ // A save can repeat an earlier live edit with its old text caches still in the JSON.
19769
+ const newState = {
19770
+ ...Object.fromEntries(entries),
19771
+ differences: currentState.differences,
19772
+ textInfos: currentState.textInfos
19773
+ };
19774
+ const updatedState = newState.lines === currentState.lines ? newState : {
19775
+ ...newState,
19776
+ incrementalEdits: emptyIncrementalEdits,
19777
+ invalidStartIndex: 0,
19778
+ numberOfLines: newState.lines.length
19779
+ };
19780
+ return updateDerivedState(currentState, updatedState);
19781
+ };
19782
+ const applyState = wrapCommand$2(applyComponentState);
19783
+ const setComponentState = (uid, value) => {
19784
+ return enqueue(uid, () => applyState(uid, value));
19785
+ };
19786
+
19688
19787
  const setDebugEnabled = (state, enabled) => {
19689
19788
  return state;
19690
19789
  };
@@ -19875,6 +19974,7 @@ const commandMap = {
19875
19974
  'Editor.format': wrapCommand(format),
19876
19975
  'Editor.getCommandIds': getCommandIds,
19877
19976
  'Editor.getCompletionContext': getCompletionContext,
19977
+ 'Editor.getComponentState': getComponentState,
19878
19978
  'Editor.getDiagnostics': getDiagnostics,
19879
19979
  'Editor.getKeyBindings': getKeyBindings,
19880
19980
  'Editor.getKeys': getKeys,
@@ -19997,6 +20097,7 @@ const commandMap = {
19997
20097
  'Editor.selectWord': wrapCommand(selectWord),
19998
20098
  'Editor.selectWordLeft': wrapCommand(selectWordLeft),
19999
20099
  'Editor.selectWordRight': wrapCommand(selectWordRight),
20100
+ 'Editor.setComponentState': setComponentState,
20000
20101
  'Editor.setDebugEnabled': wrapCommand(setDebugEnabled),
20001
20102
  'Editor.setDecorations': wrapCommand(setDecorations),
20002
20103
  'Editor.setDelta': wrapCommand(setDelta),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/editor-worker",
3
- "version": "19.75.1",
3
+ "version": "19.75.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git@github.com:lvce-editor/editor-worker.git"