@lvce-editor/editor-worker 19.75.1 → 19.75.3

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);
@@ -4627,7 +4741,7 @@ const getSelectionFromChange = change => {
4627
4741
  start: newPosition
4628
4742
  };
4629
4743
  };
4630
- const setSelections$2 = (editor, selections) => {
4744
+ const setSelections$2 = (editor, selections, centerOffscreenSelection = false) => {
4631
4745
  object(editor);
4632
4746
  const {
4633
4747
  foldingRanges = []
@@ -4653,10 +4767,12 @@ const setSelections$2 = (editor, selections) => {
4653
4767
  selections: normalizedSelections
4654
4768
  };
4655
4769
  }
4656
- const desiredStartVisualRow = visualRow < startVisualRow ? visualRow : visualRow - editor.numberOfVisibleLines + 1;
4770
+ const desiredStartVisualRow = centerOffscreenSelection ? visualRow - Math.floor(editor.numberOfVisibleLines / 2) : visualRow < startVisualRow ? visualRow : visualRow - editor.numberOfVisibleLines + 1;
4771
+ const desiredDeltaY = desiredStartVisualRow * editor.itemHeight;
4772
+ const deltaY = centerOffscreenSelection ? clamp(desiredDeltaY, 0, editor.finalDeltaY ?? 0) : desiredDeltaY;
4657
4773
  return updateLayout({
4658
4774
  ...editor,
4659
- deltaY: desiredStartVisualRow * editor.itemHeight,
4775
+ deltaY,
4660
4776
  selections: normalizedSelections
4661
4777
  }, foldingRanges);
4662
4778
  }
@@ -4686,7 +4802,7 @@ const setSelections$2 = (editor, selections) => {
4686
4802
  lines,
4687
4803
  scrollBarHeight
4688
4804
  } = editor;
4689
- const desiredMinLineY = rowIndex < minLineY ? rowIndex : rowIndex - numberOfVisibleLines + 1;
4805
+ const desiredMinLineY = centerOffscreenSelection ? rowIndex - Math.floor(numberOfVisibleLines / 2) : rowIndex < minLineY ? rowIndex : rowIndex - numberOfVisibleLines + 1;
4690
4806
  const deltaY = clamp(desiredMinLineY * itemHeight, 0, finalDeltaY);
4691
4807
  const newMinLineY = Math.floor(deltaY / itemHeight);
4692
4808
  const newMaxLineY = Math.min(newMinLineY + numberOfVisibleLines, lines.length);
@@ -5658,88 +5774,6 @@ const getVisibleBracketMatches = async editor => {
5658
5774
  return infos.filter(info => info !== undefined);
5659
5775
  };
5660
5776
 
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
5777
  const shouldUpdateDiagnosticData = (oldState, newState) => {
5744
5778
  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
5779
  };
@@ -12600,7 +12634,7 @@ const getFindWidgetFontSize = async () => {
12600
12634
  };
12601
12635
  const setSelections$1 = async (editorUid, selections) => {
12602
12636
  const editor = getEditor$1(editorUid);
12603
- const newEditor = setSelections$2(editor, selections);
12637
+ const newEditor = setSelections$2(editor, selections, true);
12604
12638
  await updateEditor(editorUid, editor, newEditor);
12605
12639
  };
12606
12640
 
@@ -15918,6 +15952,17 @@ const ensure = async (fontName, fontUrl) => {
15918
15952
  await TextMeasurementWorker.invoke('TextMeasurement.ensureFont', fontName, fontUrl);
15919
15953
  };
15920
15954
 
15955
+ const getComponentState = uid => {
15956
+ const {
15957
+ newState
15958
+ } = get$9(uid);
15959
+ return {
15960
+ ...newState,
15961
+ cursorUndoStack: newState.cursorUndoStack?.map(selection => [...selection]),
15962
+ selections: [...newState.selections]
15963
+ };
15964
+ };
15965
+
15921
15966
  const findWidgetFocusContexts = [FindWidget, FocusFindWidgetReplace$1, FocusFindWidgetOptions, FocusFindWidgetToggleReplace, FocusFindWidgetPreviousMatchButton, FocusFindWidgetNextMatchButton, FocusFindWidgetCloseButton, FocusFindWidgetReplaceButton, FocusFindWidgetReplaceAllButton];
15922
15967
  const getFindWidgetFocusKeyBindings = () => {
15923
15968
  return findWidgetFocusContexts.flatMap(focusContext => [{
@@ -19281,6 +19326,62 @@ const sendDeprecatedExtensionHostPortToExtensionManagementWorker = async (port,
19281
19326
  await RendererWorker.sendMessagePortToExtensionManagementWorker(port, rpcId);
19282
19327
  };
19283
19328
 
19329
+ const restoreSelections = value => {
19330
+ if (!Array.isArray(value) || value.length % 4 !== 0 || value.some(item => !Number.isSafeInteger(item) || item < 0 || item > 0xff_ff_ff_ff)) {
19331
+ throw new TypeError('Editor selections must be an array of unsigned integer ranges');
19332
+ }
19333
+ return new Uint32Array(value);
19334
+ };
19335
+ const applyComponentState = async (currentState, value) => {
19336
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
19337
+ throw new TypeError('Editor state must be an object');
19338
+ }
19339
+ const state = value;
19340
+ const {
19341
+ cursorUndoStack: jsonCursorUndoStack,
19342
+ lines,
19343
+ selections: jsonSelections
19344
+ } = state;
19345
+ for (const key of ['uid', 'id', 'uri']) {
19346
+ if (state[key] !== currentState[key]) {
19347
+ throw new Error(`Editor state ${key} must remain ${currentState[key]}`);
19348
+ }
19349
+ }
19350
+ if (!Array.isArray(lines) || lines.some(line => typeof line !== 'string')) {
19351
+ throw new TypeError('Editor lines must be an array of strings');
19352
+ }
19353
+ if (jsonCursorUndoStack !== undefined && !Array.isArray(jsonCursorUndoStack)) {
19354
+ throw new TypeError('Editor cursorUndoStack must be an array');
19355
+ }
19356
+ const selections = restoreSelections(jsonSelections);
19357
+ const cursorUndoStack = jsonCursorUndoStack?.map(restoreSelections);
19358
+ const normalized = {
19359
+ ...state,
19360
+ cursorUndoStack,
19361
+ selections
19362
+ };
19363
+ const currentJsonState = getComponentState(currentState.uid);
19364
+ // Keep unchanged collections so a JSON round trip does not invalidate every render cache.
19365
+ const entries = Object.entries(normalized).map(([key, entry]) => [key, JSON.stringify(state[key]) === JSON.stringify(currentJsonState[key]) ? currentState[key] : entry]);
19366
+ // A save can repeat an earlier live edit with its old text caches still in the JSON.
19367
+ const newState = {
19368
+ ...Object.fromEntries(entries),
19369
+ differences: currentState.differences,
19370
+ textInfos: currentState.textInfos
19371
+ };
19372
+ const updatedState = newState.lines === currentState.lines ? newState : {
19373
+ ...newState,
19374
+ incrementalEdits: emptyIncrementalEdits,
19375
+ invalidStartIndex: 0,
19376
+ numberOfLines: newState.lines.length
19377
+ };
19378
+ return updateDerivedState(currentState, updatedState);
19379
+ };
19380
+ const applyState = wrapCommand$2(applyComponentState);
19381
+ const setComponentState = (uid, value) => {
19382
+ return enqueue(uid, () => applyState(uid, value));
19383
+ };
19384
+
19284
19385
  const setDebugEnabled = (state, enabled) => {
19285
19386
  return state;
19286
19387
  };
@@ -19474,6 +19575,7 @@ const commandMap = {
19474
19575
  'Editor.format': wrapCommand(format),
19475
19576
  'Editor.getCommandIds': getCommandIds,
19476
19577
  'Editor.getCompletionContext': getCompletionContext,
19578
+ 'Editor.getComponentState': getComponentState,
19477
19579
  'Editor.getDiagnostics': getDiagnostics,
19478
19580
  'Editor.getKeyBindings': getKeyBindings,
19479
19581
  'Editor.getKeys': getKeys,
@@ -19596,6 +19698,7 @@ const commandMap = {
19596
19698
  'Editor.selectWord': wrapCommand(selectWord),
19597
19699
  'Editor.selectWordLeft': wrapCommand(selectWordLeft),
19598
19700
  'Editor.selectWordRight': wrapCommand(selectWordRight),
19701
+ 'Editor.setComponentState': setComponentState,
19599
19702
  'Editor.setDebugEnabled': wrapCommand(setDebugEnabled),
19600
19703
  'Editor.setDecorations': wrapCommand(setDecorations),
19601
19704
  '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);
@@ -5040,7 +5154,7 @@ const getSelectionFromChange = change => {
5040
5154
  start: newPosition
5041
5155
  };
5042
5156
  };
5043
- const setSelections$2 = (editor, selections) => {
5157
+ const setSelections$2 = (editor, selections, centerOffscreenSelection = false) => {
5044
5158
  object(editor);
5045
5159
  const {
5046
5160
  foldingRanges = []
@@ -5066,10 +5180,12 @@ const setSelections$2 = (editor, selections) => {
5066
5180
  selections: normalizedSelections
5067
5181
  };
5068
5182
  }
5069
- const desiredStartVisualRow = visualRow < startVisualRow ? visualRow : visualRow - editor.numberOfVisibleLines + 1;
5183
+ const desiredStartVisualRow = centerOffscreenSelection ? visualRow - Math.floor(editor.numberOfVisibleLines / 2) : visualRow < startVisualRow ? visualRow : visualRow - editor.numberOfVisibleLines + 1;
5184
+ const desiredDeltaY = desiredStartVisualRow * editor.itemHeight;
5185
+ const deltaY = centerOffscreenSelection ? clamp(desiredDeltaY, 0, editor.finalDeltaY ?? 0) : desiredDeltaY;
5070
5186
  return updateLayout({
5071
5187
  ...editor,
5072
- deltaY: desiredStartVisualRow * editor.itemHeight,
5188
+ deltaY,
5073
5189
  selections: normalizedSelections
5074
5190
  }, foldingRanges);
5075
5191
  }
@@ -5099,7 +5215,7 @@ const setSelections$2 = (editor, selections) => {
5099
5215
  lines,
5100
5216
  scrollBarHeight
5101
5217
  } = editor;
5102
- const desiredMinLineY = rowIndex < minLineY ? rowIndex : rowIndex - numberOfVisibleLines + 1;
5218
+ const desiredMinLineY = centerOffscreenSelection ? rowIndex - Math.floor(numberOfVisibleLines / 2) : rowIndex < minLineY ? rowIndex : rowIndex - numberOfVisibleLines + 1;
5103
5219
  const deltaY = clamp(desiredMinLineY * itemHeight, 0, finalDeltaY);
5104
5220
  const newMinLineY = Math.floor(deltaY / itemHeight);
5105
5221
  const newMaxLineY = Math.min(newMinLineY + numberOfVisibleLines, lines.length);
@@ -6071,88 +6187,6 @@ const getVisibleBracketMatches = async editor => {
6071
6187
  return infos.filter(info => info !== undefined);
6072
6188
  };
6073
6189
 
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
6190
  const shouldUpdateDiagnosticData = (oldState, newState) => {
6157
6191
  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
6192
  };
@@ -13004,7 +13038,7 @@ const getFindWidgetFontSize = async () => {
13004
13038
  };
13005
13039
  const setSelections$1 = async (editorUid, selections) => {
13006
13040
  const editor = getEditor$1(editorUid);
13007
- const newEditor = setSelections$2(editor, selections);
13041
+ const newEditor = setSelections$2(editor, selections, true);
13008
13042
  await updateEditor(editorUid, editor, newEditor);
13009
13043
  };
13010
13044
 
@@ -16322,6 +16356,17 @@ const ensure = async (fontName, fontUrl) => {
16322
16356
  await invoke$f('TextMeasurement.ensureFont', fontName, fontUrl);
16323
16357
  };
16324
16358
 
16359
+ const getComponentState = uid => {
16360
+ const {
16361
+ newState
16362
+ } = get$8(uid);
16363
+ return {
16364
+ ...newState,
16365
+ cursorUndoStack: newState.cursorUndoStack?.map(selection => [...selection]),
16366
+ selections: [...newState.selections]
16367
+ };
16368
+ };
16369
+
16325
16370
  const findWidgetFocusContexts = [FindWidget, FocusFindWidgetReplace$1, FocusFindWidgetOptions, FocusFindWidgetToggleReplace, FocusFindWidgetPreviousMatchButton, FocusFindWidgetNextMatchButton, FocusFindWidgetCloseButton, FocusFindWidgetReplaceButton, FocusFindWidgetReplaceAllButton];
16326
16371
  const getFindWidgetFocusKeyBindings = () => {
16327
16372
  return findWidgetFocusContexts.flatMap(focusContext => [{
@@ -19685,6 +19730,62 @@ const sendDeprecatedExtensionHostPortToExtensionManagementWorker = async (port,
19685
19730
  await sendMessagePortToExtensionManagementWorker$1(port, rpcId);
19686
19731
  };
19687
19732
 
19733
+ const restoreSelections = value => {
19734
+ if (!Array.isArray(value) || value.length % 4 !== 0 || value.some(item => !Number.isSafeInteger(item) || item < 0 || item > 0xff_ff_ff_ff)) {
19735
+ throw new TypeError('Editor selections must be an array of unsigned integer ranges');
19736
+ }
19737
+ return new Uint32Array(value);
19738
+ };
19739
+ const applyComponentState = async (currentState, value) => {
19740
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
19741
+ throw new TypeError('Editor state must be an object');
19742
+ }
19743
+ const state = value;
19744
+ const {
19745
+ cursorUndoStack: jsonCursorUndoStack,
19746
+ lines,
19747
+ selections: jsonSelections
19748
+ } = state;
19749
+ for (const key of ['uid', 'id', 'uri']) {
19750
+ if (state[key] !== currentState[key]) {
19751
+ throw new Error(`Editor state ${key} must remain ${currentState[key]}`);
19752
+ }
19753
+ }
19754
+ if (!Array.isArray(lines) || lines.some(line => typeof line !== 'string')) {
19755
+ throw new TypeError('Editor lines must be an array of strings');
19756
+ }
19757
+ if (jsonCursorUndoStack !== undefined && !Array.isArray(jsonCursorUndoStack)) {
19758
+ throw new TypeError('Editor cursorUndoStack must be an array');
19759
+ }
19760
+ const selections = restoreSelections(jsonSelections);
19761
+ const cursorUndoStack = jsonCursorUndoStack?.map(restoreSelections);
19762
+ const normalized = {
19763
+ ...state,
19764
+ cursorUndoStack,
19765
+ selections
19766
+ };
19767
+ const currentJsonState = getComponentState(currentState.uid);
19768
+ // Keep unchanged collections so a JSON round trip does not invalidate every render cache.
19769
+ const entries = Object.entries(normalized).map(([key, entry]) => [key, JSON.stringify(state[key]) === JSON.stringify(currentJsonState[key]) ? currentState[key] : entry]);
19770
+ // A save can repeat an earlier live edit with its old text caches still in the JSON.
19771
+ const newState = {
19772
+ ...Object.fromEntries(entries),
19773
+ differences: currentState.differences,
19774
+ textInfos: currentState.textInfos
19775
+ };
19776
+ const updatedState = newState.lines === currentState.lines ? newState : {
19777
+ ...newState,
19778
+ incrementalEdits: emptyIncrementalEdits,
19779
+ invalidStartIndex: 0,
19780
+ numberOfLines: newState.lines.length
19781
+ };
19782
+ return updateDerivedState(currentState, updatedState);
19783
+ };
19784
+ const applyState = wrapCommand$2(applyComponentState);
19785
+ const setComponentState = (uid, value) => {
19786
+ return enqueue(uid, () => applyState(uid, value));
19787
+ };
19788
+
19688
19789
  const setDebugEnabled = (state, enabled) => {
19689
19790
  return state;
19690
19791
  };
@@ -19875,6 +19976,7 @@ const commandMap = {
19875
19976
  'Editor.format': wrapCommand(format),
19876
19977
  'Editor.getCommandIds': getCommandIds,
19877
19978
  'Editor.getCompletionContext': getCompletionContext,
19979
+ 'Editor.getComponentState': getComponentState,
19878
19980
  'Editor.getDiagnostics': getDiagnostics,
19879
19981
  'Editor.getKeyBindings': getKeyBindings,
19880
19982
  'Editor.getKeys': getKeys,
@@ -19997,6 +20099,7 @@ const commandMap = {
19997
20099
  'Editor.selectWord': wrapCommand(selectWord),
19998
20100
  'Editor.selectWordLeft': wrapCommand(selectWordLeft),
19999
20101
  'Editor.selectWordRight': wrapCommand(selectWordRight),
20102
+ 'Editor.setComponentState': setComponentState,
20000
20103
  'Editor.setDebugEnabled': wrapCommand(setDebugEnabled),
20001
20104
  'Editor.setDecorations': wrapCommand(setDecorations),
20002
20105
  '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.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git@github.com:lvce-editor/editor-worker.git"