@lvce-editor/editor-worker 2.4.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # editor worker
1
+ # Editor Worker
2
+
3
+ Webworker for the core editing functionality in Lvce Editor.
2
4
 
3
5
  ## Gitpod
4
6
 
@@ -561,6 +561,7 @@ const EmptyString = '';
561
561
  const Space = ' ';
562
562
  const Tab = '\t';
563
563
  const Underline = '_';
564
+ const DoubleQuote$1 = '"';
564
565
  const T = 't';
565
566
 
566
567
  const getTabCount = string => {
@@ -642,9 +643,6 @@ const shouldNormalizeText = text => {
642
643
  return text.includes(Tab);
643
644
  };
644
645
 
645
- // TODO visible selections could also be uint16array
646
- // [top1, left1, width1, height1, top2, left2, width2, height2...]
647
-
648
646
  const getX = (line, column, fontWeight, fontSize, fontFamily, isMonospaceFont, letterSpacing, tabSize, halfCursorWidth, width, averageCharWidth, difference = 0) => {
649
647
  if (!line) {
650
648
  return 0;
@@ -1258,7 +1256,6 @@ const getErrorConstructor = (message, type) => {
1258
1256
  if (type) {
1259
1257
  switch (type) {
1260
1258
  case DomException:
1261
- // @ts-ignore
1262
1259
  return DOMException;
1263
1260
  case TypeError$1:
1264
1261
  return TypeError;
@@ -1283,7 +1280,6 @@ const getErrorConstructor = (message, type) => {
1283
1280
  };
1284
1281
  const constructError = (message, type, name) => {
1285
1282
  const ErrorConstructor = getErrorConstructor(message, type);
1286
- // @ts-ignore
1287
1283
  if (ErrorConstructor === DOMException && name) {
1288
1284
  return new ErrorConstructor(message, name);
1289
1285
  }
@@ -1299,6 +1295,13 @@ const constructError = (message, type, name) => {
1299
1295
  const getNewLineIndex$2 = (string, startIndex = undefined) => {
1300
1296
  return string.indexOf(NewLine$3, startIndex);
1301
1297
  };
1298
+ const getParentStack = error => {
1299
+ let parentStack = error.stack || error.data || error.message || '';
1300
+ if (parentStack.startsWith(' at')) {
1301
+ parentStack = error.message + NewLine$3 + parentStack;
1302
+ }
1303
+ return parentStack;
1304
+ };
1302
1305
  const joinLines$1 = lines => {
1303
1306
  return lines.join(NewLine$3);
1304
1307
  };
@@ -1307,18 +1310,11 @@ const Custom = -32001;
1307
1310
  const splitLines$1 = lines => {
1308
1311
  return lines.split(NewLine$3);
1309
1312
  };
1310
- const getParentStack = error => {
1311
- let parentStack = error.stack || error.data || error.message || '';
1312
- if (parentStack.startsWith(' at')) {
1313
- parentStack = error.message + NewLine$3 + parentStack;
1314
- }
1315
- return parentStack;
1316
- };
1317
1313
  const restoreJsonRpcError = error => {
1318
1314
  if (error && error instanceof Error) {
1319
1315
  return error;
1320
1316
  }
1321
- const currentStack = joinLines$1(splitLines$1(new Error().stack).slice(1));
1317
+ const currentStack = joinLines$1(splitLines$1(new Error().stack || '').slice(1));
1322
1318
  if (error && error.code && error.code === MethodNotFound) {
1323
1319
  const restoredError = new JsonRpcError(error.message);
1324
1320
  const parentStack = getParentStack(error);
@@ -1347,7 +1343,6 @@ const restoreJsonRpcError = error => {
1347
1343
  }
1348
1344
  } else {
1349
1345
  if (error.stack) {
1350
- // TODO accessing stack might be slow
1351
1346
  const lowerStack = restoredError.stack || '';
1352
1347
  // @ts-ignore
1353
1348
  const indexNewLine = getNewLineIndex$2(lowerStack);
@@ -1377,6 +1372,67 @@ const unwrapJsonRpcResult = responseMessage => {
1377
1372
  }
1378
1373
  throw new JsonRpcError('unexpected response message');
1379
1374
  };
1375
+ const isMessagePort = value => {
1376
+ return typeof MessagePort !== 'undefined' && value instanceof MessagePort;
1377
+ };
1378
+ const isInstanceOf = (value, constructorName) => {
1379
+ return value?.constructor?.name === constructorName;
1380
+ };
1381
+ const isMessagePortMain = value => {
1382
+ return isInstanceOf(value, 'MessagePortMain');
1383
+ };
1384
+ const isOffscreenCanvas = value => {
1385
+ return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
1386
+ };
1387
+ const isSocket = value => {
1388
+ return isInstanceOf(value, 'Socket');
1389
+ };
1390
+ const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
1391
+ const isTransferrable = value => {
1392
+ for (const fn of transferrables) {
1393
+ if (fn(value)) {
1394
+ return true;
1395
+ }
1396
+ }
1397
+ return false;
1398
+ };
1399
+ const walkValue = (value, transferrables) => {
1400
+ if (!value) {
1401
+ return;
1402
+ }
1403
+ if (isTransferrable(value)) {
1404
+ transferrables.push(value);
1405
+ }
1406
+ if (Array.isArray(value)) {
1407
+ for (const item of value) {
1408
+ walkValue(item, transferrables);
1409
+ }
1410
+ return;
1411
+ }
1412
+ if (typeof value === 'object') {
1413
+ for (const property of Object.values(value)) {
1414
+ walkValue(property, transferrables);
1415
+ }
1416
+ }
1417
+ };
1418
+ const getTransferrables = value => {
1419
+ const transferrables = [];
1420
+ walkValue(value, transferrables);
1421
+ return transferrables;
1422
+ };
1423
+ const isSingleTransferrable = value => {
1424
+ return isSocket(value);
1425
+ };
1426
+ const getTransferrableParams = value => {
1427
+ const transferrables = getTransferrables(value);
1428
+ if (transferrables.length === 0) {
1429
+ return undefined;
1430
+ }
1431
+ if (isSingleTransferrable(transferrables[0])) {
1432
+ return transferrables[0];
1433
+ }
1434
+ return transferrables;
1435
+ };
1380
1436
  const create$1$1 = (message, error) => {
1381
1437
  return {
1382
1438
  jsonrpc: Two,
@@ -1429,7 +1485,42 @@ const getResponse = async (message, ipc, execute, preparePrettyError, logError,
1429
1485
  return getErrorResponse(message, error, preparePrettyError, logError);
1430
1486
  }
1431
1487
  };
1432
- const handleJsonRpcMessage = async (ipc, message, execute, resolve, preparePrettyError, logError, requiresSocket) => {
1488
+ const defaultPreparePrettyError = error => {
1489
+ return error;
1490
+ };
1491
+ const defaultLogError = () => {
1492
+ // ignore
1493
+ };
1494
+ const defaultRequiresSocket = () => {
1495
+ return false;
1496
+ };
1497
+ const defaultResolve = resolve;
1498
+ const handleJsonRpcMessage = async (...args) => {
1499
+ let message;
1500
+ let ipc;
1501
+ let execute;
1502
+ let preparePrettyError;
1503
+ let logError;
1504
+ let resolve;
1505
+ let requiresSocket;
1506
+ if (args.length === 1) {
1507
+ const arg = args[0];
1508
+ message = arg.message;
1509
+ ipc = arg.ipc;
1510
+ execute = arg.execute;
1511
+ preparePrettyError = arg.preparePrettyError || defaultPreparePrettyError;
1512
+ logError = arg.logError || defaultLogError;
1513
+ requiresSocket = arg.requiresSocket || defaultRequiresSocket;
1514
+ resolve = arg.resolve || defaultResolve;
1515
+ } else {
1516
+ ipc = args[0];
1517
+ message = args[1];
1518
+ execute = args[2];
1519
+ resolve = args[3];
1520
+ preparePrettyError = args[4];
1521
+ logError = args[5];
1522
+ requiresSocket = args[6];
1523
+ }
1433
1524
  if ('id' in message) {
1434
1525
  if ('method' in message) {
1435
1526
  const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
@@ -1460,12 +1551,21 @@ const invoke$5 = async (ipc, method, ...params) => {
1460
1551
  const result = unwrapJsonRpcResult(responseMessage);
1461
1552
  return result;
1462
1553
  };
1554
+
1555
+ // TODO deprecated old typings,
1556
+ // always use automatic transferrable detection
1463
1557
  const invokeAndTransfer$2 = async (ipc, handle, method, ...params) => {
1558
+ let transfer = handle;
1559
+ if (typeof handle === 'string') {
1560
+ params = [method, ...params];
1561
+ method = handle;
1562
+ transfer = getTransferrableParams(params);
1563
+ }
1464
1564
  const {
1465
1565
  message,
1466
1566
  promise
1467
1567
  } = create$2$1(method, params);
1468
- ipc.sendAndTransfer(message, handle);
1568
+ ipc.sendAndTransfer(message, transfer);
1469
1569
  const responseMessage = await promise;
1470
1570
  const result = unwrapJsonRpcResult(responseMessage);
1471
1571
  return result;
@@ -1569,9 +1669,9 @@ const invoke$4 = (method, ...params) => {
1569
1669
  const ipc = get$4();
1570
1670
  return invoke$5(ipc, method, ...params);
1571
1671
  };
1572
- const invokeAndTransfer$1 = async (transfer, method, ...params) => {
1672
+ const invokeAndTransfer$1 = async (method, ...params) => {
1573
1673
  const ipc = get$4();
1574
- return invokeAndTransfer$2(ipc, transfer, method, ...params);
1674
+ return invokeAndTransfer$2(ipc, method, ...params);
1575
1675
  };
1576
1676
  const listen$8 = ipc => {
1577
1677
  set$4(ipc);
@@ -1580,12 +1680,12 @@ const listen$8 = ipc => {
1580
1680
  const invoke$3 = async (method, ...params) => {
1581
1681
  return invoke$4(method, ...params);
1582
1682
  };
1583
- const invokeAndTransfer = async (transfer, method, ...params) => {
1584
- return invokeAndTransfer$1(transfer, method, ...params);
1683
+ const invokeAndTransfer = async (method, ...params) => {
1684
+ return invokeAndTransfer$1(method, ...params);
1585
1685
  };
1586
1686
 
1587
1687
  const sendMessagePortToExtensionHostWorker = async port => {
1588
- await invokeAndTransfer([port], 'SendMessagePortToExtensionHostWorker.sendMessagePortToExtensionHostWorker', port, 'HandleMessagePort.handleMessagePort');
1688
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToExtensionHostWorker', port, 'HandleMessagePort.handleMessagePort');
1589
1689
  };
1590
1690
 
1591
1691
  const withResolvers$1 = () => {
@@ -1666,7 +1766,7 @@ const IpcParentWithExtensionHostWorker = {
1666
1766
  };
1667
1767
 
1668
1768
  const sendMessagePortToSyntaxHighlightingWorker = async port => {
1669
- await invokeAndTransfer([port], 'SendMessagePortToSyntaxHighlightingWorker.sendMessagePortToSyntaxHighlightingWorker', port, 'HandleMessagePort.handleMessagePort');
1769
+ await invokeAndTransfer('SendMessagePortToSyntaxHighlightingWorker.sendMessagePortToSyntaxHighlightingWorker', port, 'HandleMessagePort.handleMessagePort');
1670
1770
  };
1671
1771
 
1672
1772
  const create$3 = async () => {
@@ -1719,7 +1819,7 @@ const IpcParentWithSyntaxHighlightingWorker = {
1719
1819
  };
1720
1820
 
1721
1821
  const sendMessagePortToRendererProcess = async port => {
1722
- await invokeAndTransfer([port], 'SendMessagePortToRendererProcess.sendMessagePortToRendererProcess', port, 'HandleMessagePort.handleMessagePort');
1822
+ await invokeAndTransfer('SendMessagePortToRendererProcess.sendMessagePortToRendererProcess', port, 'HandleMessagePort.handleMessagePort');
1723
1823
  };
1724
1824
 
1725
1825
  const create$2 = async () => {
@@ -2366,19 +2466,14 @@ const closeCompletion = editor => {
2366
2466
  };
2367
2467
  };
2368
2468
 
2369
- // @ts-ignore
2370
2469
  const state$6 = {
2371
2470
  isComposing: false,
2372
2471
  compositionText: ''
2373
2472
  };
2374
-
2375
- // @ts-ignore
2376
2473
  const compositionStart = (editor, event) => {
2377
2474
  state$6.isComposing = true;
2378
2475
  return editor;
2379
2476
  };
2380
-
2381
- // @ts-ignore
2382
2477
  const getCompositionChanges = (selections, data) => {
2383
2478
  const changes = [];
2384
2479
  for (let i = 0; i < selections.length; i += 4) {
@@ -2403,16 +2498,12 @@ const getCompositionChanges = (selections, data) => {
2403
2498
  }
2404
2499
  return changes;
2405
2500
  };
2406
-
2407
- // @ts-ignore
2408
2501
  const compositionUpdate = (editor, data) => {
2409
2502
  const selections = editor.selections;
2410
2503
  const changes = getCompositionChanges(selections, data);
2411
2504
  state$6.compositionText = data;
2412
2505
  return scheduleDocumentAndCursorsSelections(editor, changes);
2413
2506
  };
2414
-
2415
- // @ts-ignore
2416
2507
  const compositionEnd = (editor, data) => {
2417
2508
  const selections = editor.selections;
2418
2509
  const changes = getCompositionChanges(selections, data);
@@ -2544,36 +2635,30 @@ const copy = async editor => {
2544
2635
  // TODO handle multiline selection
2545
2636
  // TODO handle multiple cursors
2546
2637
 
2547
- // @ts-ignore
2548
2638
  const copyLineDown = editor => {
2549
2639
  const {
2550
- selections,
2551
- primarySelectionIndex
2640
+ selections
2552
2641
  } = editor;
2553
- const rowIndex = selections[primarySelectionIndex];
2642
+ const rowIndex = selections[0];
2554
2643
  number$1(rowIndex);
2555
2644
  const position = {
2556
2645
  rowIndex,
2557
2646
  columnIndex: 0
2558
2647
  };
2559
2648
  const changes = [{
2560
- inserted: [getLine(editor, rowIndex), ''],
2561
- deleted: [''],
2562
2649
  start: position,
2563
- end: position
2650
+ end: position,
2651
+ inserted: [getLine(editor, rowIndex), ''],
2652
+ deleted: ['']
2564
2653
  }];
2565
2654
  return scheduleDocumentAndCursorsSelections(editor, changes);
2566
2655
  };
2567
2656
 
2568
- // @ts-ignore
2569
-
2570
- // @ts-ignore
2571
2657
  const copyLineUp = editor => {
2572
2658
  const {
2573
- selections,
2574
- primarySelectionIndex
2659
+ selections
2575
2660
  } = editor;
2576
- const rowIndex = selections[primarySelectionIndex];
2661
+ const rowIndex = selections[0];
2577
2662
  const position = {
2578
2663
  rowIndex: rowIndex,
2579
2664
  columnIndex: 0
@@ -2671,7 +2756,6 @@ const editorCursorHorizontalLeft = (editor, getDelta) => {
2671
2756
  return scheduleSelections(editor, newSelections);
2672
2757
  };
2673
2758
 
2674
- // @ts-ignore
2675
2759
  const characterLeft = (line, columnIndex) => {
2676
2760
  if (!supported()) {
2677
2761
  return 1;
@@ -2683,8 +2767,6 @@ const characterLeft = (line, columnIndex) => {
2683
2767
  const twoCharactersLeft = () => {
2684
2768
  return 2;
2685
2769
  };
2686
-
2687
- // @ts-ignore
2688
2770
  const characterRight = (line, columnIndex) => {
2689
2771
  if (!supported()) {
2690
2772
  return 1;
@@ -2693,13 +2775,9 @@ const characterRight = (line, columnIndex) => {
2693
2775
  const next = segmenter.at(line, columnIndex);
2694
2776
  return next.segment.length;
2695
2777
  };
2696
-
2697
- // @ts-ignore
2698
2778
  const isWhitespace = char => {
2699
2779
  return char === Space || char === Tab;
2700
2780
  };
2701
-
2702
- // @ts-ignore
2703
2781
  const lineCharacterStart = (line, columnIndex) => {
2704
2782
  if (line.length === 0) {
2705
2783
  return 0;
@@ -2711,13 +2789,9 @@ const lineCharacterStart = (line, columnIndex) => {
2711
2789
  }
2712
2790
  return columnIndex;
2713
2791
  };
2714
-
2715
- // @ts-ignore
2716
2792
  const lineEnd = (line, columnIndex) => {
2717
2793
  return line.length - columnIndex;
2718
2794
  };
2719
-
2720
- // @ts-ignore
2721
2795
  const tryRegexArray = (partialLine, regexArray) => {
2722
2796
  for (const regex of regexArray) {
2723
2797
  const match = partialLine.match(regex);
@@ -2742,8 +2816,6 @@ const wordLeft = (line, columnIndex) => {
2742
2816
  const RE_WORD_RIGHT_1 = /^\s*[\u00C0-\u017F\w]+/i;
2743
2817
  const RE_WORD_RIGHT_2 = /^[^a-zA-Z\d]+\w*/;
2744
2818
  const RE_WORD_RIGHT = [RE_WORD_RIGHT_1, RE_WORD_RIGHT_2];
2745
-
2746
- // @ts-ignore
2747
2819
  const wordRight = (line, columnIndex) => {
2748
2820
  const partialLine = line.slice(columnIndex);
2749
2821
  return tryRegexArray(partialLine, RE_WORD_RIGHT);
@@ -2758,8 +2830,6 @@ const RE_PARTIAL_WORD_LEFT_7 = /(?<![A-Z])[A-Z]_+\s*$/;
2758
2830
  const RE_PARTIAL_WORD_LEFT_8 = /[a-z]+\s*$/;
2759
2831
  const RE_PARTIAL_WORD_LEFT_9 = /[^a-zA-Z\d\s]+\s*$/;
2760
2832
  const RE_PARTIAL_WORD_LEFT = [RE_PARTIAL_WORD_LEFT_1, RE_PARTIAL_WORD_LEFT_2, RE_PARTIAL_WORD_LEFT_3, RE_PARTIAL_WORD_LEFT_4, RE_PARTIAL_WORD_LEFT_5, RE_PARTIAL_WORD_LEFT_6, RE_PARTIAL_WORD_LEFT_7, RE_PARTIAL_WORD_LEFT_8, RE_PARTIAL_WORD_LEFT_9];
2761
-
2762
- // @ts-ignore
2763
2833
  const wordPartLeft = (line, columnIndex) => {
2764
2834
  const partialLine = line.slice(0, columnIndex);
2765
2835
  return tryRegexArray(partialLine, RE_PARTIAL_WORD_LEFT);
@@ -2778,8 +2848,6 @@ const ARRAY_PARTIAL_WORD_RIGHT_1 = [RE_PARTIAL_WORD_RIGHT_1,
2778
2848
  RE_PARTIAL_WORD_RIGHT_3, RE_PARTIAL_WORD_RIGHT_4, RE_PARTIAL_WORD_RIGHT_5, RE_PARTIAL_WORD_RIGHT_6];
2779
2849
  const ARRAY_PARTIAL_WORD_RIGHT_2 = [RE_PARTIAL_WORD_RIGHT_1, RE_PARTIAL_WORD_RIGHT_2, RE_PARTIAL_WORD_RIGHT_3, RE_PARTIAL_WORD_RIGHT_4, RE_PARTIAL_WORD_RIGHT_5, RE_PARTIAL_WORD_RIGHT_6];
2780
2850
  const RE_UPPERCASE = /[A-Z]/;
2781
-
2782
- // @ts-ignore
2783
2851
  const wordPartRight = (line, columnIndex) => {
2784
2852
  const partialLine = line.slice(columnIndex);
2785
2853
  // line[columnIndex]//?
@@ -2794,7 +2862,6 @@ const cursorCharacterLeft = editor => {
2794
2862
  return editorCursorHorizontalLeft(editor, characterLeft);
2795
2863
  };
2796
2864
 
2797
- // @ts-ignore
2798
2865
  const editorGetPositionRight = (position, lines, getDelta) => {
2799
2866
  const rowIndex = position.rowIndex;
2800
2867
  const columnIndex = position.columnIndex;
@@ -2813,8 +2880,6 @@ const editorGetPositionRight = (position, lines, getDelta) => {
2813
2880
  columnIndex: columnIndex + delta
2814
2881
  };
2815
2882
  };
2816
-
2817
- // @ts-ignore
2818
2883
  const moveToPositionRight = (selections, i, rowIndex, columnIndex, lines, getDelta) => {
2819
2884
  if (rowIndex >= lines.length) {
2820
2885
  return;
@@ -2830,9 +2895,6 @@ const moveToPositionRight = (selections, i, rowIndex, columnIndex, lines, getDel
2830
2895
  }
2831
2896
  };
2832
2897
 
2833
- // @ts-ignore
2834
-
2835
- // @ts-ignore
2836
2898
  const getNewSelections$a = (selections, lines, getDelta) => {
2837
2899
  const newSelections = new Uint32Array(selections.length);
2838
2900
  for (let i = 0; i < selections.length; i += 4) {
@@ -2846,8 +2908,6 @@ const getNewSelections$a = (selections, lines, getDelta) => {
2846
2908
  }
2847
2909
  return newSelections;
2848
2910
  };
2849
-
2850
- // @ts-ignore
2851
2911
  const editorCursorHorizontalRight = (editor, getDelta) => {
2852
2912
  const {
2853
2913
  lines,
@@ -2904,16 +2964,9 @@ const getNewSelections$8 = selections => {
2904
2964
  return map(selections, moveSelectionWithoutIntlSegmenter);
2905
2965
  };
2906
2966
  const cursorVertical = (editor, getPosition, getEdgePosition, delta) => {
2907
- // if (TextSegmenter.supported()) {
2908
- // return editorCursorsVerticalWithIntlSegmenter(
2909
- // editor,
2910
- // getPosition,
2911
- // getEdgePosition,
2912
- // delta
2913
- // )
2914
- // }
2915
- const selections = editor.selections;
2916
- // @ts-ignore
2967
+ const {
2968
+ selections
2969
+ } = editor;
2917
2970
  const newSelections = getNewSelections$8(selections);
2918
2971
  return scheduleSelections(editor, newSelections);
2919
2972
  };
@@ -2940,7 +2993,6 @@ const cursorWordPartRight = editor => {
2940
2993
  return editorCursorHorizontalRight(editor, wordPartRight);
2941
2994
  };
2942
2995
 
2943
- // @ts-ignore
2944
2996
  const cursorWordRight = editor => {
2945
2997
  return editorCursorHorizontalRight(editor, wordRight);
2946
2998
  };
@@ -5204,22 +5256,39 @@ const editorSelectAllLeft = editor => {
5204
5256
  editorSelectHorizontalLeft(editor, lineCharacterStart);
5205
5257
  };
5206
5258
 
5207
- // TODO handle virtual space
5259
+ const RE_ALPHA_NUMERIC = /[a-zA-Z\d]/;
5260
+ const isAlphaNumeric = char => {
5261
+ return RE_ALPHA_NUMERIC.test(char);
5262
+ };
5208
5263
 
5209
- const getNewSelectionsSingleLineWord = (lines, word) => {
5210
- if (word.length === 0) {
5211
- throw new Error('word length must be greater than zero');
5264
+ const getWordStartIndex = (line, index) => {
5265
+ for (let i = index - 1; i >= 0; i--) {
5266
+ if (!isAlphaNumeric(line[i])) {
5267
+ return i + 1;
5268
+ }
5212
5269
  }
5213
- const newSelections = [];
5214
- for (let i = 0; i < lines.length; i++) {
5215
- const line = lines[i];
5216
- let columnIndex = -word.length;
5217
- while ((columnIndex = line.indexOf(word, columnIndex + word.length)) !== -1) {
5218
- newSelections.push(i, columnIndex, i, columnIndex + word.length);
5270
+ return 0;
5271
+ };
5272
+ const getWordEndIndex = (line, index) => {
5273
+ for (let i = index; i < line.length; i++) {
5274
+ if (!isAlphaNumeric(line[i])) {
5275
+ return i;
5219
5276
  }
5220
5277
  }
5221
- return new Uint32Array(newSelections);
5278
+ return line.length - 1;
5279
+ };
5280
+ const getWordMatchAtPosition = (lines, rowIndex, columnIndex) => {
5281
+ const line = lines[rowIndex];
5282
+ const start = getWordStartIndex(line, columnIndex);
5283
+ const end = getWordEndIndex(line, columnIndex);
5284
+ const word = line.slice(start, end);
5285
+ return {
5286
+ start,
5287
+ end,
5288
+ word
5289
+ };
5222
5290
  };
5291
+
5223
5292
  const isMultiLineMatch = (lines, rowIndex, wordParts) => {
5224
5293
  let j = 0;
5225
5294
  if (!lines[rowIndex + j].endsWith(wordParts[j])) {
@@ -5230,14 +5299,29 @@ const isMultiLineMatch = (lines, rowIndex, wordParts) => {
5230
5299
  return false;
5231
5300
  }
5232
5301
  }
5233
- // j--
5234
5302
  if (!lines[rowIndex + j].startsWith(wordParts[j])) {
5235
-
5236
5303
  return false;
5237
5304
  }
5238
5305
  return true;
5239
5306
  };
5240
5307
 
5308
+ // TODO handle virtual space
5309
+
5310
+ const getNewSelectionsSingleLineWord = (lines, word) => {
5311
+ if (word.length === 0) {
5312
+ throw new Error('word length must be greater than zero');
5313
+ }
5314
+ const newSelections = [];
5315
+ for (let i = 0; i < lines.length; i++) {
5316
+ const line = lines[i];
5317
+ let columnIndex = -word.length;
5318
+ while ((columnIndex = line.indexOf(word, columnIndex + word.length)) !== -1) {
5319
+ newSelections.push(i, columnIndex, i, columnIndex + word.length);
5320
+ }
5321
+ }
5322
+ return new Uint32Array(newSelections);
5323
+ };
5324
+
5241
5325
  // TODO this is very expensive, there might be a better algorithm for this
5242
5326
  // TODO if this matches the given selections, don't schedule selections/rerender
5243
5327
  const getAllOccurrencesMultiLine = (lines, wordParts) => {
@@ -5253,39 +5337,6 @@ const getAllOccurrencesMultiLine = (lines, wordParts) => {
5253
5337
  }
5254
5338
  return new Uint32Array(newSelections);
5255
5339
  };
5256
-
5257
- // TODO duplicate code with EditorSelectNextOccurrence
5258
- const RE_ALPHA_NUMERIC$1 = /[a-zA-Z\d]/;
5259
- const isAlphaNumeric$1 = char => {
5260
- return RE_ALPHA_NUMERIC$1.test(char);
5261
- };
5262
- const getWordStartIndex$1 = (line, index) => {
5263
- for (let i = index - 1; i >= 0; i--) {
5264
- if (!isAlphaNumeric$1(line[i])) {
5265
- return i + 1;
5266
- }
5267
- }
5268
- return 0;
5269
- };
5270
- const getWordEndIndex$1 = (line, index) => {
5271
- for (let i = index; i < line.length; i++) {
5272
- if (!isAlphaNumeric$1(line[i])) {
5273
- return i;
5274
- }
5275
- }
5276
- return line.length - 1;
5277
- };
5278
- const getWordMatchAtPosition$1 = (lines, rowIndex, columnIndex) => {
5279
- const line = lines[rowIndex];
5280
- const start = getWordStartIndex$1(line, columnIndex);
5281
- const end = getWordEndIndex$1(line, columnIndex);
5282
- const word = line.slice(start, end);
5283
- return {
5284
- start,
5285
- end,
5286
- word
5287
- };
5288
- };
5289
5340
  const getNewSelections$3 = (lines, selections) => {
5290
5341
  if (selections.length < 4) {
5291
5342
  throw new Error('selections must have at least one entry');
@@ -5297,7 +5348,7 @@ const getNewSelections$3 = (lines, selections) => {
5297
5348
  const endColumnIndex = selections[firstSelectionIndex + 3];
5298
5349
  if (startRowIndex === endRowIndex) {
5299
5350
  if (startColumnIndex === endColumnIndex) {
5300
- const wordMatch = getWordMatchAtPosition$1(lines, endRowIndex, endColumnIndex);
5351
+ const wordMatch = getWordMatchAtPosition(lines, endRowIndex, endColumnIndex);
5301
5352
  if (wordMatch.start === wordMatch.end) {
5302
5353
  return selections;
5303
5354
  }
@@ -5407,11 +5458,11 @@ const getNewSelections$1 = (lines, selections) => {
5407
5458
  let startColumnIndex = selectionStartColumn;
5408
5459
  let endColumnIndex = selectionEndColumn;
5409
5460
  const line = lines[rowIndex];
5410
- while (startColumnIndex > 0 && line[startColumnIndex] !== '"') {
5461
+ while (startColumnIndex > 0 && line[startColumnIndex] !== DoubleQuote$1) {
5411
5462
  startColumnIndex--;
5412
5463
  }
5413
5464
  startColumnIndex++;
5414
- while (endColumnIndex < line.length && line[endColumnIndex] !== '"') {
5465
+ while (endColumnIndex < line.length && line[endColumnIndex] !== DoubleQuote$1) {
5415
5466
  endColumnIndex++;
5416
5467
  }
5417
5468
  newSelections[i] = rowIndex;
@@ -5434,9 +5485,26 @@ const selectInsideString = editor => {
5434
5485
  return scheduleSelections(editor, newSelections);
5435
5486
  };
5436
5487
 
5437
- // @ts-ignore
5488
+ // TODO handle virtual space
5489
+
5490
+ // TODO editors behave differently when selecting next occurrence, for example:
5491
+
5492
+ // aaa
5493
+ // bbb 1
5494
+ // ccc
5495
+ // bbb 2
5496
+ // bbb 3
5497
+ // aaa
5498
+ // bbb 4
5499
+ // ccc
5500
+
5501
+ // when clicking first at position 4 and then position 2,
5502
+ // - vscode selects next position 3 and refuses to select position 1
5503
+ // - atom also selects next position 3 and refuses to select position 1
5504
+ // - WebStorm also selects next position 3 and refuses to select position 1
5505
+ // - brackets (codemirror) selects position 3 and then selects position 1
5506
+ // - sublime selects next position 1, then next position 3
5438
5507
 
5439
- // @ts-ignore
5440
5508
  const getSelectionEditsSingleLineWord = (lines, selections) => {
5441
5509
  const lastSelectionIndex = selections.length - 4;
5442
5510
  const rowIndex = selections[lastSelectionIndex];
@@ -5515,48 +5583,6 @@ const getSelectionEditsSingleLineWord = (lines, selections) => {
5515
5583
  }
5516
5584
  return undefined;
5517
5585
  };
5518
- const RE_ALPHA_NUMERIC = /[a-zA-Z\d]/;
5519
-
5520
- // @ts-ignore
5521
- const isAlphaNumeric = char => {
5522
- return RE_ALPHA_NUMERIC.test(char);
5523
- };
5524
-
5525
- // @ts-ignore
5526
- const getWordStartIndex = (line, index) => {
5527
- for (let i = index - 1; i >= 0; i--) {
5528
- if (!isAlphaNumeric(line[i])) {
5529
- return i + 1;
5530
- }
5531
- }
5532
- return 0;
5533
- };
5534
-
5535
- // @ts-ignore
5536
- const getWordEndIndex = (line, index) => {
5537
- for (let i = index; i < line.length; i++) {
5538
- if (!isAlphaNumeric(line[i])) {
5539
- return i;
5540
- }
5541
- }
5542
- return line.length - 1;
5543
- };
5544
-
5545
- // @ts-ignore
5546
- const getWordMatchAtPosition = (lines, rowIndex, columnIndex) => {
5547
- const line = lines[rowIndex];
5548
- const index = columnIndex;
5549
- const start = getWordStartIndex(line, index);
5550
- const end = getWordEndIndex(line, index);
5551
- const word = line.slice(start, end);
5552
- return {
5553
- start,
5554
- end,
5555
- word
5556
- };
5557
- };
5558
-
5559
- // @ts-ignore
5560
5586
  const getSelectNextOccurrenceResult = editor => {
5561
5587
  const lines = editor.lines;
5562
5588
  const selections = editor.selections;
@@ -5588,13 +5614,31 @@ const getSelectNextOccurrenceResult = editor => {
5588
5614
  return undefined;
5589
5615
  };
5590
5616
 
5591
- // @ts-ignore
5617
+ // TODO handle virtual space
5618
+
5619
+ // TODO editors behave differently when selecting next occurrence, for example:
5620
+
5621
+ // aaa
5622
+ // bbb 1
5623
+ // ccc
5624
+ // bbb 2
5625
+ // bbb 3
5626
+ // aaa
5627
+ // bbb 4
5628
+ // ccc
5629
+
5630
+ // when clicking first at position 4 and then position 2,
5631
+ // - vscode selects next position 3 and refuses to select position 1
5632
+ // - atom also selects next position 3 and refuses to select position 1
5633
+ // - WebStorm also selects next position 3 and refuses to select position 1
5634
+ // - brackets (codemirror) selects position 3 and then selects position 1
5635
+ // - sublime selects next position 1, then next position 3
5636
+
5592
5637
  const isRangeInViewPort = (minLineY, maxLineY, startRowIndex, endRowIndex) => {
5593
5638
  return startRowIndex >= minLineY && endRowIndex <= maxLineY;
5594
5639
  };
5595
5640
 
5596
5641
  // TODO handle virtual space
5597
- // @ts-ignore
5598
5642
  const selectNextOccurrence = editor => {
5599
5643
  const result = getSelectNextOccurrenceResult(editor);
5600
5644
  if (!result) {
@@ -5609,7 +5653,6 @@ const selectNextOccurrence = editor => {
5609
5653
  }
5610
5654
  // TODO what is this magic number 5?
5611
5655
  // const deltaY = (revealRangeStartRowIndex - 5) * editor.rowHeight
5612
- // @ts-ignore
5613
5656
  return scheduleDocumentAndCursorsSelections(editor, [], selectionEdits);
5614
5657
  };
5615
5658
 
@@ -6528,7 +6571,6 @@ const typeWithAutoClosing = async (editor, text) => {
6528
6571
  // EditorCommandCompletion.openFromType(editor, text)
6529
6572
  };
6530
6573
 
6531
- // @ts-ignore
6532
6574
  const inverseChange = edit => {
6533
6575
  const endColumnIndex = edit.end.columnIndex - edit.deleted[0].length + edit.inserted[0].length;
6534
6576
  return {
@@ -6542,7 +6584,6 @@ const inverseChange = edit => {
6542
6584
  };
6543
6585
  };
6544
6586
 
6545
- // @ts-ignore
6546
6587
  const undo = state => {
6547
6588
  const {
6548
6589
  undoStack
@@ -8834,14 +8875,14 @@ const commandMap = {
8834
8875
  };
8835
8876
  wrapCommands(commandMap);
8836
8877
 
8837
- const MessagePort = 1;
8878
+ const MessagePort$1 = 1;
8838
8879
  const ModuleWorker = 2;
8839
8880
  const ReferencePort = 3;
8840
8881
  const ModuleWorkerAndMessagePort = 8;
8841
8882
  const Auto = () => {
8842
8883
  // @ts-ignore
8843
8884
  if (globalThis.acceptPort) {
8844
- return MessagePort;
8885
+ return MessagePort$1;
8845
8886
  }
8846
8887
  // @ts-ignore
8847
8888
  if (globalThis.acceptReferencePort) {
@@ -9229,7 +9270,7 @@ const getModule = method => {
9229
9270
  return IpcChildWithModuleWorker$1;
9230
9271
  case ModuleWorkerAndMessagePort:
9231
9272
  return IpcChildWithModuleWorkerAndMessagePort$1;
9232
- case MessagePort:
9273
+ case MessagePort$1:
9233
9274
  return IpcChildWithMessagePort$1;
9234
9275
  default:
9235
9276
  throw new Error('unexpected ipc type');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/editor-worker",
3
- "version": "2.4.0",
3
+ "version": "2.6.0",
4
4
  "description": "",
5
5
  "main": "dist/testWorkerMain.js",
6
6
  "type": "module",