@pierre/diffs 1.3.0-beta.3 → 1.3.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/README.md +6 -6
  2. package/dist/components/CodeView.js +6 -6
  3. package/dist/components/CodeView.js.map +1 -1
  4. package/dist/components/File.d.ts +3 -2
  5. package/dist/components/File.d.ts.map +1 -1
  6. package/dist/components/File.js +35 -21
  7. package/dist/components/File.js.map +1 -1
  8. package/dist/components/FileDiff.d.ts +8 -4
  9. package/dist/components/FileDiff.d.ts.map +1 -1
  10. package/dist/components/FileDiff.js +66 -56
  11. package/dist/components/FileDiff.js.map +1 -1
  12. package/dist/components/FileStream.js +4 -2
  13. package/dist/components/FileStream.js.map +1 -1
  14. package/dist/components/UnresolvedFile.js +1 -1
  15. package/dist/components/VirtualizedFile.d.ts +6 -2
  16. package/dist/components/VirtualizedFile.d.ts.map +1 -1
  17. package/dist/components/VirtualizedFile.js +89 -24
  18. package/dist/components/VirtualizedFile.js.map +1 -1
  19. package/dist/components/VirtualizedFileDiff.d.ts +8 -2
  20. package/dist/components/VirtualizedFileDiff.d.ts.map +1 -1
  21. package/dist/components/VirtualizedFileDiff.js +91 -15
  22. package/dist/components/VirtualizedFileDiff.js.map +1 -1
  23. package/dist/editor/command.d.ts +1 -1
  24. package/dist/editor/command.d.ts.map +1 -1
  25. package/dist/editor/command.js +3 -3
  26. package/dist/editor/command.js.map +1 -1
  27. package/dist/editor/editStack.d.ts +1 -1
  28. package/dist/editor/editor.d.ts +37 -9
  29. package/dist/editor/editor.d.ts.map +1 -1
  30. package/dist/editor/editor.js +558 -449
  31. package/dist/editor/editor.js.map +1 -1
  32. package/dist/editor/editor2.js +1 -1
  33. package/dist/editor/editor2.js.map +1 -1
  34. package/dist/editor/index.d.ts +2 -2
  35. package/dist/editor/lineAnnotations.d.ts +2 -1
  36. package/dist/editor/lineAnnotations.d.ts.map +1 -1
  37. package/dist/editor/lineAnnotations.js +111 -1
  38. package/dist/editor/lineAnnotations.js.map +1 -1
  39. package/dist/editor/marker.d.ts +33 -0
  40. package/dist/editor/marker.d.ts.map +1 -0
  41. package/dist/editor/marker.js +185 -0
  42. package/dist/editor/marker.js.map +1 -0
  43. package/dist/editor/pieceTable.d.ts +8 -3
  44. package/dist/editor/pieceTable.d.ts.map +1 -1
  45. package/dist/editor/pieceTable.js +74 -12
  46. package/dist/editor/pieceTable.js.map +1 -1
  47. package/dist/editor/searchPanel.d.ts +12 -3
  48. package/dist/editor/searchPanel.d.ts.map +1 -1
  49. package/dist/editor/searchPanel.js +168 -54
  50. package/dist/editor/searchPanel.js.map +1 -1
  51. package/dist/editor/selection.d.ts +19 -3
  52. package/dist/editor/selection.d.ts.map +1 -1
  53. package/dist/editor/selection.js +188 -37
  54. package/dist/editor/selection.js.map +1 -1
  55. package/dist/editor/{quickEdit.d.ts → selectionAction.d.ts} +8 -8
  56. package/dist/editor/selectionAction.d.ts.map +1 -0
  57. package/dist/editor/{quickEdit.js → selectionAction.js} +18 -18
  58. package/dist/editor/selectionAction.js.map +1 -0
  59. package/dist/editor/sprite.d.ts +4 -3
  60. package/dist/editor/sprite.d.ts.map +1 -1
  61. package/dist/editor/sprite.js +19 -5
  62. package/dist/editor/sprite.js.map +1 -1
  63. package/dist/editor/textDocument.d.ts +4 -4
  64. package/dist/editor/textDocument.d.ts.map +1 -1
  65. package/dist/editor/textDocument.js +7 -7
  66. package/dist/editor/textDocument.js.map +1 -1
  67. package/dist/editor/textMeasure.d.ts +1 -0
  68. package/dist/editor/textMeasure.d.ts.map +1 -1
  69. package/dist/editor/textMeasure.js +6 -0
  70. package/dist/editor/textMeasure.js.map +1 -1
  71. package/dist/editor/tokenzier.js +20 -9
  72. package/dist/editor/tokenzier.js.map +1 -1
  73. package/dist/editor/utils.d.ts +3 -1
  74. package/dist/editor/utils.d.ts.map +1 -1
  75. package/dist/editor/utils.js +16 -1
  76. package/dist/editor/utils.js.map +1 -1
  77. package/dist/highlighter/shared_highlighter.js +3 -29
  78. package/dist/highlighter/shared_highlighter.js.map +1 -1
  79. package/dist/highlighter/themes/attachResolvedThemes.js +4 -3
  80. package/dist/highlighter/themes/attachResolvedThemes.js.map +1 -1
  81. package/dist/highlighter/themes/cleanUpResolvedThemes.js +3 -2
  82. package/dist/highlighter/themes/cleanUpResolvedThemes.js.map +1 -1
  83. package/dist/highlighter/themes/constants.d.ts +1 -7
  84. package/dist/highlighter/themes/constants.d.ts.map +1 -1
  85. package/dist/highlighter/themes/constants.js +1 -4
  86. package/dist/highlighter/themes/constants.js.map +1 -1
  87. package/dist/highlighter/themes/getResolvedOrResolveTheme.js +2 -2
  88. package/dist/highlighter/themes/getResolvedOrResolveTheme.js.map +1 -1
  89. package/dist/highlighter/themes/getResolvedThemes.js +2 -8
  90. package/dist/highlighter/themes/getResolvedThemes.js.map +1 -1
  91. package/dist/highlighter/themes/hasResolvedThemes.js +2 -3
  92. package/dist/highlighter/themes/hasResolvedThemes.js.map +1 -1
  93. package/dist/highlighter/themes/registerCustomCSSVariableTheme.js +1 -1
  94. package/dist/highlighter/themes/registerCustomTheme.d.ts +5 -3
  95. package/dist/highlighter/themes/registerCustomTheme.d.ts.map +1 -1
  96. package/dist/highlighter/themes/registerCustomTheme.js +15 -5
  97. package/dist/highlighter/themes/registerCustomTheme.js.map +1 -1
  98. package/dist/highlighter/themes/resolveTheme.js +6 -27
  99. package/dist/highlighter/themes/resolveTheme.js.map +1 -1
  100. package/dist/highlighter/themes/resolveThemes.js +5 -12
  101. package/dist/highlighter/themes/resolveThemes.js.map +1 -1
  102. package/dist/highlighter/themes/themeResolution.d.ts +8 -0
  103. package/dist/highlighter/themes/themeResolution.d.ts.map +1 -0
  104. package/dist/highlighter/themes/themeResolution.js +22 -0
  105. package/dist/highlighter/themes/themeResolution.js.map +1 -0
  106. package/dist/highlighter/themes/themeResolver.d.ts +8 -0
  107. package/dist/highlighter/themes/themeResolver.d.ts.map +1 -0
  108. package/dist/highlighter/themes/themeResolver.js +8 -0
  109. package/dist/highlighter/themes/themeResolver.js.map +1 -0
  110. package/dist/index.d.ts +4 -4
  111. package/dist/index.js +3 -3
  112. package/dist/managers/InteractionManager.js +1 -1
  113. package/dist/managers/InteractionManager.js.map +1 -1
  114. package/dist/managers/ResizeManager.js +1 -1
  115. package/dist/managers/ResizeManager.js.map +1 -1
  116. package/dist/react/CodeView.js +1 -1
  117. package/dist/react/index.d.ts +2 -2
  118. package/dist/react/utils/useFileDiffInstance.js +1 -0
  119. package/dist/react/utils/useFileDiffInstance.js.map +1 -1
  120. package/dist/renderers/DiffHunksRenderer.d.ts +6 -2
  121. package/dist/renderers/DiffHunksRenderer.d.ts.map +1 -1
  122. package/dist/renderers/DiffHunksRenderer.js +183 -12
  123. package/dist/renderers/DiffHunksRenderer.js.map +1 -1
  124. package/dist/renderers/FileRenderer.d.ts +2 -2
  125. package/dist/renderers/FileRenderer.d.ts.map +1 -1
  126. package/dist/renderers/FileRenderer.js +17 -5
  127. package/dist/renderers/FileRenderer.js.map +1 -1
  128. package/dist/ssr/FileDiffReact.js +1 -1
  129. package/dist/ssr/index.d.ts +2 -2
  130. package/dist/types.d.ts +25 -8
  131. package/dist/types.d.ts.map +1 -1
  132. package/dist/utils/getHighlighterThemeStyles.js +16 -12
  133. package/dist/utils/getHighlighterThemeStyles.js.map +1 -1
  134. package/dist/utils/includesFileAnnotations.d.ts +17 -0
  135. package/dist/utils/includesFileAnnotations.d.ts.map +1 -0
  136. package/dist/utils/includesFileAnnotations.js +19 -0
  137. package/dist/utils/includesFileAnnotations.js.map +1 -0
  138. package/dist/utils/parseMergeConflictDiffFromFile.js.map +1 -1
  139. package/dist/utils/parsePatchFiles.js +93 -4
  140. package/dist/utils/parsePatchFiles.js.map +1 -1
  141. package/dist/utils/renderDiffWithHighlighter.js +4 -2
  142. package/dist/utils/renderDiffWithHighlighter.js.map +1 -1
  143. package/dist/utils/renderFileWithHighlighter.js +4 -2
  144. package/dist/utils/renderFileWithHighlighter.js.map +1 -1
  145. package/dist/utils/updateDiffHunks.d.ts +13 -0
  146. package/dist/utils/updateDiffHunks.d.ts.map +1 -0
  147. package/dist/utils/updateDiffHunks.js +171 -0
  148. package/dist/utils/updateDiffHunks.js.map +1 -0
  149. package/dist/utils/virtualDiffLayout.d.ts +2 -1
  150. package/dist/utils/virtualDiffLayout.d.ts.map +1 -1
  151. package/dist/utils/virtualDiffLayout.js +9 -1
  152. package/dist/utils/virtualDiffLayout.js.map +1 -1
  153. package/dist/worker/{wasm-BaDzIkIn.js → wasm-qE0LgnY3.js} +2 -2
  154. package/dist/worker/{wasm-BaDzIkIn.js.map → wasm-qE0LgnY3.js.map} +1 -1
  155. package/dist/worker/worker-portable.js +1016 -275
  156. package/dist/worker/worker-portable.js.map +1 -1
  157. package/dist/worker/worker.js +31 -19
  158. package/dist/worker/worker.js.map +1 -1
  159. package/package.json +5 -10
  160. package/dist/editor/quickEdit.d.ts.map +0 -1
  161. package/dist/editor/quickEdit.js.map +0 -1
@@ -84,24 +84,28 @@ function mapCursorMove(textDocument, selections, shortcut) {
84
84
  if (shortcut === "textStart") {
85
85
  const indent = getLeadingSpaces(textDocument.getLineText(line));
86
86
  character = character === indent ? 0 : indent;
87
- } else character = shortcut === "start" ? 0 : textDocument.getLineText(line).length;
87
+ } else character = shortcut === "start" ? 0 : textDocument.getLineLength(line);
88
88
  if (selection.direction === DirectionBackward) line = selection.start.line;
89
89
  else line = selection.end.line;
90
90
  } else if (shortcut === "up") line = Math.max(0, line - 1);
91
91
  else if (shortcut === "down") line = Math.min(Math.max(lineCount - 1, 0), line + 1);
92
- else if (isCollapsedSelection(selection)) if (shortcut === "left") {
93
- character--;
94
- if (character < 0) if (line === 0) character = 0;
95
- else {
96
- line = Math.max(0, line - 1);
97
- character = textDocument.getLineText(line).length;
98
- }
99
- } else {
100
- character++;
101
- if (character > textDocument.getLineText(line).length) if (line === lineCount - 1) character--;
102
- else {
103
- line = Math.min(Math.max(lineCount - 1, 0), line + 1);
104
- character = 0;
92
+ else if (isCollapsedSelection(selection)) {
93
+ const lineLength = textDocument.getLineLength(line);
94
+ character = Math.min(character, lineLength);
95
+ if (shortcut === "left") {
96
+ character--;
97
+ if (character < 0) if (line === 0) character = 0;
98
+ else {
99
+ line = Math.max(0, line - 1);
100
+ character = textDocument.getLineLength(line);
101
+ }
102
+ } else {
103
+ character++;
104
+ if (character > lineLength) if (line === lineCount - 1) character--;
105
+ else {
106
+ line = Math.min(Math.max(lineCount - 1, 0), line + 1);
107
+ character = 0;
108
+ }
105
109
  }
106
110
  }
107
111
  const pos = {
@@ -137,7 +141,7 @@ function applyTextChangeToSelections(textDocument, selections, edit, lineAnnotat
137
141
  if (selections[selections.length - 1] === void 0) return { nextSelections: [] };
138
142
  const selectionPositions = [];
139
143
  for (const selection of selections) selectionPositions.push(selection.start, selection.end);
140
- const selectionOffsets = textDocument.offsetsAt(selectionPositions);
144
+ const selectionOffsets = selectionPositions.map((position) => textDocument.offsetAt(position));
141
145
  const primaryStartOffset = selectionOffsets[(selections.length - 1) * 2];
142
146
  const primaryEndOffset = selectionOffsets[(selections.length - 1) * 2 + 1];
143
147
  const ordered = [];
@@ -220,7 +224,7 @@ function applyTextReplaceToSelections(textDocument, selections, texts, lineAnnot
220
224
  if (selections.length !== texts.length) throw new Error("Selection text replacements must match the selection count");
221
225
  const selectionPositions = [];
222
226
  for (const selection of selections) selectionPositions.push(selection.start, selection.end);
223
- const selectionOffsets = textDocument.offsetsAt(selectionPositions);
227
+ const selectionOffsets = selectionPositions.map((position) => textDocument.offsetAt(position));
224
228
  const ordered = [];
225
229
  let isAlreadyOrdered = true;
226
230
  for (let index = 0; index < selections.length; index++) {
@@ -241,21 +245,61 @@ function applyTextReplaceToSelections(textDocument, selections, texts, lineAnnot
241
245
  if (endOrder !== 0) return endOrder;
242
246
  return a.index - b.index;
243
247
  });
244
- const edits = [];
248
+ const allDeletes = texts.every((text) => text === "");
249
+ let edits;
245
250
  const nextSelectionOffsets = Array.from({ length: selections.length });
246
- let offsetDelta = 0;
247
- let previousEditEnd = -1;
248
- for (const entry of ordered) {
249
- if (entry.start < previousEditEnd) throw new Error("Overlapping multi-selection edits are not supported");
250
- previousEditEnd = entry.end;
251
- const newText = expandSingleNewlineInsert(textDocument, entry.text, entry.start);
252
- edits.push({
253
- start: entry.start,
254
- end: entry.end,
255
- text: newText
256
- });
257
- nextSelectionOffsets[entry.index] = entry.start + offsetDelta + newText.length;
258
- offsetDelta += newText.length - (entry.end - entry.start);
251
+ if (allDeletes) {
252
+ edits = [];
253
+ let hasEffect = false;
254
+ for (const entry of ordered) {
255
+ nextSelectionOffsets[entry.index] = entry.end;
256
+ if (entry.start >= entry.end) continue;
257
+ hasEffect = true;
258
+ const last = edits[edits.length - 1];
259
+ if (last !== void 0 && entry.start < last.end) edits[edits.length - 1] = {
260
+ start: last.start,
261
+ end: Math.max(last.end, entry.end),
262
+ text: ""
263
+ };
264
+ else edits.push({
265
+ start: entry.start,
266
+ end: entry.end,
267
+ text: ""
268
+ });
269
+ }
270
+ if (!hasEffect) return { nextSelections: selections };
271
+ for (const entry of ordered) {
272
+ const caret = entry.end;
273
+ let delta = 0;
274
+ let next = caret;
275
+ for (const edit of edits) {
276
+ if (caret <= edit.start) break;
277
+ if (caret >= edit.end) {
278
+ delta -= edit.end - edit.start;
279
+ continue;
280
+ }
281
+ next = edit.start + delta;
282
+ break;
283
+ }
284
+ if (next === caret) next += delta;
285
+ nextSelectionOffsets[entry.index] = next;
286
+ }
287
+ } else {
288
+ edits = [];
289
+ let offsetDelta = 0;
290
+ let previousEditEnd = -1;
291
+ for (const entry of ordered) {
292
+ if (entry.start < previousEditEnd) throw new Error("Overlapping multi-selection edits are not supported");
293
+ previousEditEnd = entry.end;
294
+ const newText = expandSingleNewlineInsert(textDocument, entry.text, entry.start);
295
+ edits.push({
296
+ start: entry.start,
297
+ end: entry.end,
298
+ text: newText
299
+ });
300
+ nextSelectionOffsets[entry.index] = entry.start + offsetDelta + newText.length;
301
+ offsetDelta += newText.length - (entry.end - entry.start);
302
+ }
259
303
  }
260
304
  const change = textDocument.applyResolvedEdits(edits, true, selections);
261
305
  const nextSelections = createSelectionsFromOffsetPairs(textDocument, nextSelectionOffsets.map((offset) => [offset, offset]));
@@ -285,7 +329,7 @@ function applyTransposeToSelections(textDocument, selections, lineAnnotations) {
285
329
  }
286
330
  const { line, character } = selection.start;
287
331
  const offset = anchor;
288
- const lineLength = textDocument.getLineText(line).length;
332
+ const lineLength = textDocument.getLineLength(line);
289
333
  let edit;
290
334
  if (character > 0 && character < lineLength) {
291
335
  edit = {
@@ -303,7 +347,7 @@ function applyTransposeToSelections(textDocument, selections, lineAnnotations) {
303
347
  nextOffsetPairs.push([offset, offset]);
304
348
  } else if (character === 0 && line > 0 && lineLength > 0) {
305
349
  const prevLine = line - 1;
306
- const prevLength = textDocument.getLineText(prevLine).length;
350
+ const prevLength = textDocument.getLineLength(prevLine);
307
351
  const prevEnd = textDocument.offsetAt({
308
352
  line: prevLine,
309
353
  character: prevLength
@@ -350,7 +394,66 @@ function applyDeleteHardLineForwardToSelections(textDocument, selections, lineAn
350
394
  direction: DirectionNone
351
395
  };
352
396
  });
353
- if (!deleteSelections.some((selection) => comparePosition(selection.start, selection.end) !== 0)) return { nextSelections: selections };
397
+ return applyTextReplaceToSelections(textDocument, deleteSelections, deleteSelections.map(() => ""), lineAnnotations);
398
+ }
399
+ /**
400
+ * Deletes from each selection back to the start of its soft (visual) line.
401
+ * Non-collapsed selections delete their selected text instead.
402
+ */
403
+ function applyDeleteSoftLineBackwardToSelections(textDocument, selections, getSoftLineStart, lineAnnotations) {
404
+ const deleteSelections = selections.map((selection) => {
405
+ if (!isCollapsedSelection(selection)) return {
406
+ start: selection.start,
407
+ end: selection.end,
408
+ direction: DirectionNone
409
+ };
410
+ const caret = getCaretPosition(selection);
411
+ const { line, character } = caret;
412
+ const softLineStart = getSoftLineStart?.(line, character) ?? 0;
413
+ if (character > softLineStart) return {
414
+ start: {
415
+ line,
416
+ character: softLineStart
417
+ },
418
+ end: {
419
+ line,
420
+ character
421
+ },
422
+ direction: DirectionNone
423
+ };
424
+ if (line === 0) return {
425
+ start: caret,
426
+ end: caret,
427
+ direction: DirectionNone
428
+ };
429
+ const prevLineLength = textDocument.getLineLength(line - 1);
430
+ return {
431
+ start: {
432
+ line: line - 1,
433
+ character: prevLineLength
434
+ },
435
+ end: {
436
+ line,
437
+ character: 0
438
+ },
439
+ direction: DirectionNone
440
+ };
441
+ });
442
+ return applyTextReplaceToSelections(textDocument, deleteSelections, deleteSelections.map(() => ""), lineAnnotations);
443
+ }
444
+ /**
445
+ * Deletes the word or separator group immediately before each selection.
446
+ * Non-collapsed selections delete their selected text instead.
447
+ */
448
+ function applyDeleteWordBackwardToSelections(textDocument, selections, lineAnnotations) {
449
+ const deleteSelections = selections.map((selection) => {
450
+ const [start, end] = resolveDeleteWordBackwardRange(textDocument, selection);
451
+ return {
452
+ start,
453
+ end,
454
+ direction: DirectionNone
455
+ };
456
+ });
354
457
  return applyTextReplaceToSelections(textDocument, deleteSelections, deleteSelections.map(() => ""), lineAnnotations);
355
458
  }
356
459
  /**
@@ -370,7 +473,7 @@ function getCaretPosition(selection) {
370
473
  * Checks if a line is editable.
371
474
  */
372
475
  function isLineEditable(lineType) {
373
- return lineType === "context" || lineType === "change-addition" || lineType === "context-expanded";
476
+ return lineType === "context" || lineType === "context-expanded" || lineType === "change-addition";
374
477
  }
375
478
  /**
376
479
  * Checks whether selections `a` and `b` intersect.
@@ -517,7 +620,7 @@ function getDocumentFullSelection(textDocument) {
517
620
  },
518
621
  end: {
519
622
  line: lastLine,
520
- character: textDocument.getLineText(lastLine)?.length ?? 0
623
+ character: textDocument.getLineLength(lastLine)
521
624
  },
522
625
  direction: DirectionForward
523
626
  };
@@ -529,7 +632,7 @@ function getDocumentBoundarySelection(textDocument, atEnd) {
529
632
  const line = atEnd ? textDocument.lineCount - 1 : 0;
530
633
  const start = {
531
634
  line,
532
- character: atEnd ? textDocument.getLineText(line)?.length ?? 0 : 0
635
+ character: atEnd ? textDocument.getLineLength(line) : 0
533
636
  };
534
637
  return {
535
638
  start,
@@ -615,6 +718,54 @@ function expandCollapsedLineWord(lineText, character) {
615
718
  };
616
719
  }
617
720
  }
721
+ function resolveDeleteWordBackwardRange(textDocument, selection) {
722
+ if (!isCollapsedSelection(selection)) return [selection.start, selection.end];
723
+ const caret = getCaretPosition(selection);
724
+ const { line, character: head } = caret;
725
+ if (head === 0) {
726
+ if (line === 0) return [caret, caret];
727
+ const prevLineLength = textDocument.getLineLength(line - 1);
728
+ return [{
729
+ line: line - 1,
730
+ character: prevLineLength
731
+ }, {
732
+ line,
733
+ character: 0
734
+ }];
735
+ }
736
+ const lineText = textDocument.getLineText(line);
737
+ const graphemeStarts = [0];
738
+ const segmenter = new Intl.Segmenter(void 0, { granularity: "grapheme" });
739
+ for (const segment of segmenter.segment(lineText)) if (segment.index > 0) graphemeStarts.push(segment.index);
740
+ let pos = head;
741
+ let match;
742
+ while (pos > 0) {
743
+ const prev = findClusterBreak(lineText, pos, false, graphemeStarts);
744
+ const nextChar = lineText.slice(prev, pos);
745
+ const nextMatch = !/\S/.test(nextChar) ? 0 : /\p{Alphabetic}|\p{Number}|_/u.test(nextChar) ? 1 : 2;
746
+ if (match !== void 0 && nextMatch !== match) break;
747
+ if (nextChar !== " " || pos !== head) match = nextMatch;
748
+ pos = prev;
749
+ }
750
+ return [{
751
+ line,
752
+ character: pos
753
+ }, {
754
+ line,
755
+ character: head
756
+ }];
757
+ }
758
+ function findClusterBreak(text, pos, forward, graphemeStarts) {
759
+ if (forward) {
760
+ for (const start of graphemeStarts) if (start > pos) return start;
761
+ return text.length;
762
+ }
763
+ for (let i = graphemeStarts.length - 1; i >= 0; i--) {
764
+ const start = graphemeStarts[i];
765
+ if (start < pos) return start;
766
+ }
767
+ return 0;
768
+ }
618
769
  function getSelectionAnchorAndFocusOffsets(textDocument, selection) {
619
770
  const isBackward = selection.direction === DirectionBackward;
620
771
  return [textDocument.offsetAt(isBackward ? selection.end : selection.start), textDocument.offsetAt(getCaretPosition(selection))];
@@ -902,5 +1053,5 @@ function getTextOffset(text, offset) {
902
1053
  }
903
1054
 
904
1055
  //#endregion
905
- export { DirectionBackward, DirectionForward, DirectionNone, applyDeleteHardLineForwardToSelections, applyTextChangeToSelections, applyTextReplaceToSelections, applyTransposeToSelections, comparePosition, convertSelection, createSelectionFrom, createSelectionFromAnchorAndFocusOffsets, expandCollapsedSelectionToWord, extendSelection, extendSelections, findNexMatch, getCaretPosition, getDocumentBoundarySelection, getDocumentFullSelection, getSelectionAnchor, getSelectionText, isCollapsedSelection, isLineEditable, mapCursorMove, mapSelectionShift, mergeOverlappingSelections, resolveIndentEdits, selectionIntersects };
1056
+ export { DirectionBackward, DirectionForward, DirectionNone, applyDeleteHardLineForwardToSelections, applyDeleteSoftLineBackwardToSelections, applyDeleteWordBackwardToSelections, applyTextChangeToSelections, applyTextReplaceToSelections, applyTransposeToSelections, comparePosition, convertSelection, createSelectionFrom, createSelectionFromAnchorAndFocusOffsets, expandCollapsedSelectionToWord, extendSelection, extendSelections, findNexMatch, getCaretPosition, getDocumentBoundarySelection, getDocumentFullSelection, getSelectionAnchor, getSelectionText, isCollapsedSelection, isLineEditable, mapCursorMove, mapSelectionShift, mergeOverlappingSelections, resolveIndentEdits, selectionIntersects };
906
1057
  //# sourceMappingURL=selection.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"selection.js","names":["edits: TextEdit[]","newSelection: EditorSelection","selectionPositions: Position[]","ordered: Array<{\n index: number;\n start: number;\n end: number;\n }>","edits: ResolvedTextEdit[]","nextSelectionOffsets: Array<[number, number] | undefined>","mergedGroup:\n | {\n start: number;\n end: number;\n indices: number[];\n }\n | undefined","nextOffsets: [number, number]","ordered: Array<{\n index: number;\n start: number;\n end: number;\n text: string;\n }>","nextSelectionOffsets: number[]","nextOffsetPairs: Array<[number, number]>","edit: ResolvedTextEdit | undefined","deleteSelections: EditorSelection[]","accepted: {\n index: number;\n selection: EditorSelection;\n }[]","last: HTMLElement | null","lastTextNode: Text | null","normalizedOffsets: number[]","lineEl: HTMLElement | null","tokens: HTMLElement[]","stack: Array<{ container: Node; index: number }>","current: Node | null","offset","target: HTMLElement | null","current: HTMLElement | null","base"],"sources":["../../src/editor/selection.ts"],"sourcesContent":["import type { DiffLineAnnotation } from '../types';\nimport { applyDocumentChangeToLineAnnotations } from './lineAnnotations';\nimport type {\n Position,\n Range,\n ResolvedTextEdit,\n TextDocument,\n TextDocumentChange,\n TextEdit,\n} from './textDocument';\n\nexport const DirectionBackward = -1;\nexport const DirectionNone = 0;\nexport const DirectionForward = 1;\n\nexport type SelectionDirection =\n | typeof DirectionBackward\n | typeof DirectionNone\n | typeof DirectionForward;\n\nexport interface EditorSelection extends Range {\n direction: SelectionDirection;\n}\n\n/**\n * Converts a selection from a web selection to an editor selection.\n */\nexport function convertSelection(\n range: StaticRange,\n direction: SelectionDirection = DirectionNone\n): EditorSelection | undefined {\n const start = boundaryToPosition(range.startContainer, range.startOffset);\n const end = boundaryToPosition(range.endContainer, range.endOffset);\n if (start === null || end === null) {\n return undefined;\n }\n return {\n start,\n end,\n direction,\n };\n}\n\n/**\n * Resolves the indent edits for a selection.\n */\nexport function resolveIndentEdits(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection,\n tabSize: number,\n outdent: boolean\n): [edits: TextEdit[], nextSelection: EditorSelection] {\n if (textDocument === undefined) {\n return [[], selection];\n }\n const { start, end } = selection;\n const edits: TextEdit[] = [];\n let newSelection: EditorSelection = { ...selection };\n let endLine = end.line;\n if (start.line < end.line && end.character === 0) {\n endLine--;\n }\n for (let line = start.line; line <= endLine; line++) {\n const lineText = textDocument.getLineText(line);\n if (lineText === undefined) {\n continue;\n }\n const indentUnit = lineText.startsWith('\\t') ? '\\t' : ' '.repeat(tabSize);\n let deleteLength = 0;\n let newText = indentUnit;\n if (outdent) {\n if (lineText.startsWith('\\t')) {\n deleteLength = 1;\n } else if (lineText.startsWith(' ')) {\n const leadingSpacesLength =\n lineText.length - lineText.trimStart().length;\n deleteLength = Math.min(indentUnit.length, leadingSpacesLength);\n }\n if (deleteLength === 0) {\n continue;\n }\n newText = '';\n }\n edits.push({\n range: {\n start: { line, character: 0 },\n end: { line, character: deleteLength },\n },\n newText,\n });\n const delta = newText.length - deleteLength;\n if (line === start.line) {\n newSelection = {\n ...newSelection,\n start: {\n ...start,\n character: Math.max(0, start.character + delta),\n },\n };\n }\n if (line === end.line) {\n newSelection = {\n ...newSelection,\n end: {\n ...end,\n character: Math.max(0, end.character + delta),\n },\n };\n }\n }\n return [edits, newSelection];\n}\n\n/**\n * Maps the cursor move to all selections.\n */\nexport function mapCursorMove(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[],\n shortcut: 'textStart' | 'start' | 'end' | 'up' | 'down' | 'left' | 'right'\n): EditorSelection[] {\n const lineCount = textDocument.lineCount;\n return selections.map((selection) => {\n let { line, character } =\n shortcut === 'up' || shortcut === 'left'\n ? selection.start\n : selection.end;\n if (\n shortcut === 'textStart' ||\n shortcut === 'start' ||\n shortcut === 'end'\n ) {\n if (shortcut === 'textStart') {\n const indent = getLeadingSpaces(textDocument.getLineText(line));\n character = character === indent ? 0 : indent;\n } else {\n character =\n shortcut === 'start' ? 0 : textDocument.getLineText(line).length;\n }\n if (selection.direction === DirectionBackward) {\n line = selection.start.line;\n } else {\n line = selection.end.line;\n }\n } else if (shortcut === 'up') {\n line = Math.max(0, line - 1);\n } else if (shortcut === 'down') {\n line = Math.min(Math.max(lineCount - 1, 0), line + 1);\n } else if (isCollapsedSelection(selection)) {\n if (shortcut === 'left') {\n character--;\n\n if (character < 0) {\n if (line === 0) {\n character = 0;\n } else {\n line = Math.max(0, line - 1);\n character = textDocument.getLineText(line).length;\n }\n }\n } else {\n character++;\n if (character > textDocument.getLineText(line).length) {\n if (line === lineCount - 1) {\n character--;\n } else {\n line = Math.min(Math.max(lineCount - 1, 0), line + 1);\n character = 0;\n }\n }\n }\n }\n const pos = { line, character };\n return {\n start: pos,\n end: pos,\n direction: DirectionNone,\n };\n });\n}\n\n/**\n * Same as mapCursorMove, but with shift key pressed.\n */\nexport function mapSelectionShift(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[],\n shortcut: 'textStart' | 'start' | 'end' | 'up' | 'down' | 'left' | 'right'\n): EditorSelection[] {\n return selections.map((selection) => {\n const [anchorOffset, focusOffset] = getSelectionAnchorAndFocusOffsets(\n textDocument,\n selection\n );\n const focusPosition = textDocument.positionAt(focusOffset);\n const [movedFocusSelection] = mapCursorMove(\n textDocument,\n [\n {\n start: focusPosition,\n end: focusPosition,\n direction: DirectionNone,\n },\n ],\n shortcut\n );\n const movedFocusOffset = textDocument.offsetAt(movedFocusSelection.start);\n return createSelectionFromAnchorAndFocusOffsets(\n textDocument,\n anchorOffset,\n movedFocusOffset\n );\n });\n}\n\n/**\n * Applies a text change to the given text document\n */\nexport function applyTextChangeToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n edit: ResolvedTextEdit,\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[],\n tabSize = 2\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const primarySelection = selections[selections.length - 1];\n if (primarySelection === undefined) {\n return { nextSelections: [] };\n }\n const selectionPositions: Position[] = [];\n for (const selection of selections) {\n selectionPositions.push(selection.start, selection.end);\n }\n const selectionOffsets = textDocument.offsetsAt(selectionPositions);\n const primaryStartOffset = selectionOffsets[(selections.length - 1) * 2];\n const primaryEndOffset = selectionOffsets[(selections.length - 1) * 2 + 1];\n const ordered: Array<{\n index: number;\n start: number;\n end: number;\n }> = [];\n let isAlreadyOrdered = true;\n for (let index = 0; index < selections.length; index++) {\n const entry = {\n index,\n start: selectionOffsets[index * 2],\n end: selectionOffsets[index * 2 + 1],\n };\n const previous = ordered[ordered.length - 1];\n if (\n previous !== undefined &&\n (entry.start < previous.start ||\n (entry.start === previous.start && entry.end < previous.end))\n ) {\n isAlreadyOrdered = false;\n }\n ordered.push(entry);\n }\n if (!isAlreadyOrdered) {\n ordered.sort((a, b) => {\n const startOrder = a.start - b.start;\n if (startOrder !== 0) {\n return startOrder;\n }\n const endOrder = a.end - b.end;\n if (endOrder !== 0) {\n return endOrder;\n }\n return a.index - b.index;\n });\n }\n const adjustedChange = normalizeLeadingIndentForChange(\n textDocument,\n edit,\n tabSize\n );\n const edits: ResolvedTextEdit[] = [];\n const nextSelectionOffsets: Array<[number, number] | undefined> = Array.from({\n length: selections.length,\n });\n let offsetDelta = 0;\n let mergedGroup:\n | {\n start: number;\n end: number;\n indices: number[];\n }\n | undefined;\n const finalizeMergedGroup = () => {\n if (mergedGroup === undefined) {\n return;\n }\n const perGroupChange = normalizeLeadingIndentForChange(\n textDocument,\n {\n start: mergedGroup.start,\n end: mergedGroup.end,\n text: adjustedChange.text,\n },\n tabSize\n );\n const newText = expandSingleNewlineInsert(\n textDocument,\n perGroupChange.text,\n perGroupChange.start\n );\n edits.push({\n start: perGroupChange.start,\n end: perGroupChange.end,\n text: newText,\n });\n const nextOffsets: [number, number] = [\n mergedGroup.start + offsetDelta + newText.length,\n mergedGroup.start + offsetDelta + newText.length,\n ];\n for (const index of mergedGroup.indices) {\n nextSelectionOffsets[index] = nextOffsets;\n }\n offsetDelta += newText.length - (perGroupChange.end - perGroupChange.start);\n mergedGroup = undefined;\n };\n for (const entry of ordered) {\n const startOffset = Math.max(\n 0,\n entry.start + (adjustedChange.start - primaryStartOffset)\n );\n const endOffset = Math.max(\n startOffset,\n entry.end + (adjustedChange.end - primaryEndOffset)\n );\n if (mergedGroup !== undefined && startOffset < mergedGroup.end) {\n mergedGroup.end = Math.max(mergedGroup.end, endOffset);\n mergedGroup.indices.push(entry.index);\n continue;\n }\n finalizeMergedGroup();\n mergedGroup = {\n start: startOffset,\n end: endOffset,\n indices: [entry.index],\n };\n }\n finalizeMergedGroup();\n\n const change = textDocument.applyResolvedEdits(edits, true, selections);\n const nextSelections = createSelectionsFromOffsetPairs(\n textDocument,\n nextSelectionOffsets.map((offsets) => {\n if (offsets === undefined) {\n throw new Error('Missing next selection offsets');\n }\n return offsets;\n })\n );\n textDocument.setLastUndoSelectionsAfter(nextSelections);\n if (change !== undefined && lineAnnotations !== undefined) {\n const nextLineAnnotations =\n applyDocumentChangeToLineAnnotations<LAnnotation>(\n change,\n lineAnnotations\n );\n if (nextLineAnnotations !== undefined) {\n textDocument.setLastUndoLineAnnotations(\n lineAnnotations,\n nextLineAnnotations\n );\n }\n }\n return { nextSelections, change };\n}\n\n/**\n * Applies a text replace to multiple selections.\n */\nexport function applyTextReplaceToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n texts: string[],\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n if (selections.length !== texts.length) {\n throw new Error(\n 'Selection text replacements must match the selection count'\n );\n }\n const selectionPositions: Position[] = [];\n for (const selection of selections) {\n selectionPositions.push(selection.start, selection.end);\n }\n const selectionOffsets = textDocument.offsetsAt(selectionPositions);\n const ordered: Array<{\n index: number;\n start: number;\n end: number;\n text: string;\n }> = [];\n let isAlreadyOrdered = true;\n for (let index = 0; index < selections.length; index++) {\n const entry = {\n index,\n start: selectionOffsets[index * 2],\n end: selectionOffsets[index * 2 + 1],\n text: texts[index],\n };\n const previous = ordered[ordered.length - 1];\n if (\n previous !== undefined &&\n (entry.start < previous.start ||\n (entry.start === previous.start && entry.end < previous.end))\n ) {\n isAlreadyOrdered = false;\n }\n ordered.push(entry);\n }\n if (!isAlreadyOrdered) {\n ordered.sort((a, b) => {\n const startOrder = a.start - b.start;\n if (startOrder !== 0) {\n return startOrder;\n }\n const endOrder = a.end - b.end;\n if (endOrder !== 0) {\n return endOrder;\n }\n return a.index - b.index;\n });\n }\n const edits: ResolvedTextEdit[] = [];\n const nextSelectionOffsets: number[] = Array.from({\n length: selections.length,\n });\n let offsetDelta = 0;\n let previousEditEnd = -1;\n for (const entry of ordered) {\n if (entry.start < previousEditEnd) {\n throw new Error('Overlapping multi-selection edits are not supported');\n }\n previousEditEnd = entry.end;\n const newText = expandSingleNewlineInsert(\n textDocument,\n entry.text,\n entry.start\n );\n edits.push({\n start: entry.start,\n end: entry.end,\n text: newText,\n });\n nextSelectionOffsets[entry.index] =\n entry.start + offsetDelta + newText.length;\n offsetDelta += newText.length - (entry.end - entry.start);\n }\n\n const change = textDocument.applyResolvedEdits(edits, true, selections);\n const nextSelections = createSelectionsFromOffsetPairs(\n textDocument,\n nextSelectionOffsets.map((offset) => [offset, offset])\n );\n textDocument.setLastUndoSelectionsAfter(nextSelections);\n if (change !== undefined && lineAnnotations !== undefined) {\n const nextLineAnnotations =\n applyDocumentChangeToLineAnnotations<LAnnotation>(\n change,\n lineAnnotations\n );\n if (nextLineAnnotations !== undefined) {\n textDocument.setLastUndoLineAnnotations(\n lineAnnotations,\n nextLineAnnotations\n );\n }\n }\n return { nextSelections, change };\n}\n\n/**\n * Swaps the two characters adjacent to a collapsed selection, matching browser\n * insertTranspose (Ctrl+T) behavior.\n */\nexport function applyTransposeToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const text = textDocument.getText();\n const edits: ResolvedTextEdit[] = [];\n const nextOffsetPairs: Array<[number, number]> = [];\n\n for (const selection of selections) {\n const [anchor, focus] = getSelectionAnchorAndFocusOffsets(\n textDocument,\n selection\n );\n if (!isCollapsedSelection(selection)) {\n nextOffsetPairs.push([anchor, focus]);\n continue;\n }\n\n const { line, character } = selection.start;\n const offset = anchor;\n const lineLength = textDocument.getLineText(line).length;\n let edit: ResolvedTextEdit | undefined;\n\n if (character > 0 && character < lineLength) {\n edit = {\n start: offset - 1,\n end: offset + 1,\n text: text[offset] + text[offset - 1],\n };\n nextOffsetPairs.push([offset + 1, offset + 1]);\n } else if (character === lineLength && lineLength >= 2) {\n edit = {\n start: offset - 2,\n end: offset,\n text: text[offset - 1] + text[offset - 2],\n };\n nextOffsetPairs.push([offset, offset]);\n } else if (character === 0 && line > 0 && lineLength > 0) {\n const prevLine = line - 1;\n const prevLength = textDocument.getLineText(prevLine).length;\n const prevEnd = textDocument.offsetAt({\n line: prevLine,\n character: prevLength,\n });\n const prevStart = prevLength > 0 ? prevEnd - 1 : prevEnd;\n edit = {\n start: prevStart,\n end: offset + 1,\n text:\n text[offset] +\n text.slice(prevEnd, offset) +\n text.slice(prevStart, prevEnd),\n };\n nextOffsetPairs.push([offset + 1, offset + 1]);\n } else {\n nextOffsetPairs.push([anchor, focus]);\n continue;\n }\n\n edits.push(edit);\n }\n\n if (edits.length === 0) {\n return { nextSelections: selections };\n }\n\n edits.sort((a, b) => a.start - b.start);\n for (let index = 1; index < edits.length; index++) {\n if (edits[index].start < edits[index - 1].end) {\n throw new Error('Overlapping multi-selection edits are not supported');\n }\n }\n\n const change = textDocument.applyResolvedEdits(edits, true, selections);\n const nextSelections = createSelectionsFromOffsetPairs(\n textDocument,\n nextOffsetPairs\n );\n textDocument.setLastUndoSelectionsAfter(nextSelections);\n if (change !== undefined && lineAnnotations !== undefined) {\n const nextLineAnnotations =\n applyDocumentChangeToLineAnnotations<LAnnotation>(\n change,\n lineAnnotations\n );\n if (nextLineAnnotations !== undefined) {\n textDocument.setLastUndoLineAnnotations(\n lineAnnotations,\n nextLineAnnotations\n );\n }\n }\n return { nextSelections, change };\n}\n\n/**\n * Deletes from each selection to the end of its line, including the line break\n * when the caret is already at the end of a non-final line. Non-collapsed\n * selections delete their selected text instead.\n */\nexport function applyDeleteHardLineForwardToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const deleteSelections: EditorSelection[] = selections.map((selection) => {\n const range = resolveDeleteHardLineForwardRange(textDocument, selection);\n const deleteSelection: EditorSelection = {\n start: range.start,\n end: range.end,\n direction: DirectionNone,\n };\n return deleteSelection;\n });\n const hasEffect = deleteSelections.some(\n (selection) => comparePosition(selection.start, selection.end) !== 0\n );\n if (!hasEffect) {\n return { nextSelections: selections };\n }\n return applyTextReplaceToSelections(\n textDocument,\n deleteSelections,\n deleteSelections.map(() => ''),\n lineAnnotations\n );\n}\n\n/**\n * Checks if a selection is collapsed.\n */\nexport function isCollapsedSelection(selection: EditorSelection): boolean {\n return (\n selection.start.line === selection.end.line &&\n selection.start.character === selection.end.character\n );\n}\n\n/**\n * Returns the caret (focus) position for a selection.\n */\nexport function getCaretPosition(selection: EditorSelection): Position {\n const { start, end, direction } = selection;\n return direction === DirectionBackward ? start : end;\n}\n\n/**\n * Checks if a line is editable.\n */\nexport function isLineEditable(lineType: string): boolean {\n return (\n lineType === 'context' ||\n lineType === 'change-addition' ||\n lineType === 'context-expanded'\n );\n}\n\n/**\n * Checks whether selections `a` and `b` intersect.\n */\nexport function selectionIntersects(\n a: EditorSelection,\n b: EditorSelection\n): boolean {\n const aCollapsed = isCollapsedSelection(a);\n const bCollapsed = isCollapsedSelection(b);\n if (aCollapsed && bCollapsed) {\n return comparePosition(a.start, b.start) === 0;\n }\n if (aCollapsed) {\n return (\n comparePosition(b.start, a.start) <= 0 &&\n comparePosition(a.start, b.end) <= 0\n );\n }\n if (bCollapsed) {\n return (\n comparePosition(a.start, b.start) <= 0 &&\n comparePosition(b.start, a.end) <= 0\n );\n }\n return (\n comparePosition(a.start, b.end) < 0 && comparePosition(b.start, a.end) < 0\n );\n}\n\n/**\n * Compares two positions.\n */\nexport function comparePosition(a: Position, b: Position): number {\n if (a.line !== b.line) {\n return a.line - b.line;\n }\n return a.character - b.character;\n}\n\n/**\n * Creates a selection from anchor and focus offsets.\n */\nexport function createSelectionFromAnchorAndFocusOffsets(\n textDocument: TextDocument<unknown>,\n anchorOffset: number,\n focusOffset: number\n): EditorSelection {\n const direction =\n anchorOffset === focusOffset\n ? DirectionNone\n : anchorOffset < focusOffset\n ? DirectionForward\n : DirectionBackward;\n const start = Math.min(anchorOffset, focusOffset);\n const end = Math.max(anchorOffset, focusOffset);\n return {\n start: textDocument.positionAt(start),\n end: textDocument.positionAt(end),\n direction,\n };\n}\n\n/**\n * Creates a selection from a anchor and focus selection.\n */\nexport function createSelectionFrom(\n anchorSelection: EditorSelection,\n focusSelection: EditorSelection\n): EditorSelection {\n const anchor =\n anchorSelection.direction === DirectionBackward\n ? anchorSelection.end\n : anchorSelection.start;\n const currentStartOrder = comparePosition(anchor, focusSelection.start);\n const currentEndOrder = comparePosition(anchor, focusSelection.end);\n let focus = focusSelection.end;\n if (currentStartOrder <= 0) {\n focus = focusSelection.end;\n } else if (currentEndOrder >= 0) {\n focus = focusSelection.start;\n } else {\n // When the original anchor sits inside `current`, keep whichever edge\n // stayed at the anchor so drag direction remains stable.\n focus = currentStartOrder === 0 ? focusSelection.end : focusSelection.start;\n }\n const anchorVsFocus = comparePosition(anchor, focus);\n const direction: SelectionDirection =\n anchorVsFocus === 0\n ? DirectionNone\n : anchorVsFocus < 0\n ? DirectionForward\n : DirectionBackward;\n const selectionStart = anchorVsFocus <= 0 ? anchor : focus;\n const selectionEnd = anchorVsFocus <= 0 ? focus : anchor;\n return {\n start: selectionStart,\n end: selectionEnd,\n direction,\n };\n}\n\n/**\n * Extends or shrinks the selection `original` using the endpoints of `target`, \\\n * matching contenteditable shift + click extend behavior.\n */\nexport function extendSelection(\n original: EditorSelection,\n target: EditorSelection\n): EditorSelection {\n const leftExtended = comparePosition(target.start, original.start) < 0;\n const rightExtended = comparePosition(target.end, original.end) > 0;\n\n if (leftExtended && !rightExtended) {\n return {\n start: target.start,\n end: original.end,\n direction: DirectionBackward,\n };\n }\n\n if (rightExtended && !leftExtended) {\n return {\n start: original.start,\n end: target.end,\n direction: DirectionForward,\n };\n }\n\n if (original.direction === DirectionBackward) {\n return {\n start: target.start,\n end: original.end,\n direction:\n comparePosition(target.start, original.end) === 0\n ? DirectionNone\n : DirectionBackward,\n };\n }\n\n return {\n start: original.start,\n end: target.end,\n direction:\n comparePosition(original.start, target.end) === 0\n ? DirectionNone\n : DirectionForward,\n };\n}\n\n/**\n * Extends multiple selections.\n */\nexport function extendSelections(\n selections: EditorSelection[],\n target: EditorSelection\n): EditorSelection[] {\n const newSelections = selections.map((selection) => {\n return extendSelection(selection, target);\n });\n return mergeOverlappingSelections(newSelections);\n}\n\n/**\n * Merges overlapping selections.\n */\nexport function mergeOverlappingSelections(\n selections: EditorSelection[]\n): EditorSelection[] {\n if (selections.length <= 1) {\n return selections;\n }\n const selected = new Set<number>();\n const accepted: {\n index: number;\n selection: EditorSelection;\n }[] = [];\n for (let i = selections.length - 1; i >= 0; i--) {\n const selection = selections[i];\n if (selection === undefined) {\n continue;\n }\n let left = 0;\n let right = accepted.length;\n while (left < right) {\n const mid = Math.floor((left + right) / 2);\n const candidate = accepted[mid]?.selection;\n if (candidate === undefined) {\n break;\n }\n if (comparePosition(candidate.start, selection.start) < 0) {\n left = mid + 1;\n } else {\n right = mid;\n }\n }\n const previous = accepted[left - 1]?.selection;\n const next = accepted[left]?.selection;\n if (\n (previous !== undefined && selectionIntersects(previous, selection)) ||\n (next !== undefined && selectionIntersects(next, selection))\n ) {\n continue;\n }\n accepted.splice(left, 0, { index: i, selection });\n selected.add(i);\n }\n return selections.filter((_, index) => selected.has(index));\n}\n\n/**\n * Finds the next matching word and updates the selections.\n */\nexport function findNexMatch(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[]\n): EditorSelection[] | undefined {\n if (selections.length === 0) {\n return undefined;\n }\n\n const normalizedSelections = selections.map((selection) =>\n isCollapsedSelection(selection)\n ? expandCollapsedSelectionToWord(textDocument, selection)\n : selection\n );\n const texts = normalizedSelections.map((s) => textDocument.getText(s));\n const needle = texts[0];\n if (needle.length === 0 || texts.some((t) => t !== needle)) {\n return undefined;\n }\n\n const occupied = normalizedSelections.map(\n (s) =>\n [textDocument.offsetAt(s.start), textDocument.offsetAt(s.end)] as [\n number,\n number,\n ]\n );\n const nextOffset = textDocument.findNextNonOverlappingSubstring(\n needle,\n occupied\n );\n if (nextOffset === undefined) {\n return normalizedSelections.some((selection, index) => {\n const original = selections[index];\n return (\n comparePosition(selection.start, original.start) !== 0 ||\n comparePosition(selection.end, original.end) !== 0 ||\n selection.direction !== original.direction\n );\n })\n ? normalizedSelections\n : undefined;\n }\n const added = createSelectionFromAnchorAndFocusOffsets(\n textDocument,\n nextOffset,\n nextOffset + needle.length\n );\n return [...normalizedSelections, added];\n}\n\n/**\n * Get the full selection of the document.\n */\nexport function getDocumentFullSelection(\n textDocument: TextDocument<unknown>\n): EditorSelection {\n const lastLine = textDocument.lineCount - 1;\n const lastCharacter = textDocument.getLineText(lastLine)?.length ?? 0;\n return {\n start: { line: 0, character: 0 },\n end: { line: lastLine, character: lastCharacter },\n direction: DirectionForward,\n };\n}\n\n/**\n * Get the boundary selection of the document.\n */\nexport function getDocumentBoundarySelection(\n textDocument: TextDocument<unknown>,\n atEnd: boolean\n): EditorSelection {\n const line = atEnd ? textDocument.lineCount - 1 : 0;\n const character = atEnd ? (textDocument.getLineText(line)?.length ?? 0) : 0;\n const start = { line, character };\n return {\n start: start,\n end: start,\n direction: DirectionForward,\n };\n}\n\n/**\n * Get the text of the selections for the given text document.\n */\nexport function getSelectionText(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[]\n): string {\n return [...selections]\n .sort((a, b) => {\n const startOrder = comparePosition(a.start, b.start);\n if (startOrder !== 0) {\n return startOrder;\n }\n return comparePosition(a.end, b.end);\n })\n .map((selection) => {\n if (isCollapsedSelection(selection)) {\n return textDocument.getLineText(selection.start.line, false);\n }\n return textDocument.getText(selection);\n })\n .join('\\n');\n}\n\n/**\n * Get the anchor node and offset for a selection.\n */\nexport function getSelectionAnchor(\n lineElement: HTMLElement,\n character: number\n): [Node, number] {\n const ch = Math.max(0, character);\n const tokens = collectTokens(lineElement);\n\n let last: HTMLElement | null = null;\n for (const token of tokens) {\n last = token;\n const base = getCharacterIndex(token)!;\n const end = base + (token.textContent?.length ?? 0);\n if (ch <= end) {\n const anchor = textAt(token, ch < base ? 0 : ch - base);\n if (anchor !== null) {\n return anchor;\n }\n }\n }\n\n if (last !== null) {\n const anchor = textAt(last, last.textContent?.length ?? 0);\n if (anchor !== null) {\n return anchor;\n }\n return [last, 0];\n }\n\n let textOffset = 0;\n let lastTextNode: Text | null = null;\n for (const child of lineElement.childNodes) {\n if (child.nodeType === 1 && (child as HTMLElement).tagName === 'BR') {\n return [child, 0];\n }\n if (child.nodeType !== 3) {\n continue;\n }\n lastTextNode = child as Text;\n const len = getTextOffset(\n lastTextNode.textContent,\n lastTextNode.textContent?.length ?? 0\n );\n if (ch <= textOffset + len) {\n return [\n lastTextNode,\n getTextOffset(lastTextNode.textContent, ch - textOffset),\n ];\n }\n textOffset += len;\n }\n\n if (lastTextNode !== null) {\n return [\n lastTextNode,\n getTextOffset(\n lastTextNode.textContent,\n lastTextNode.textContent?.length ?? 0\n ),\n ];\n }\n return [lineElement, 0];\n}\n\n/**\n * Expands a zero-width selection to the word-like segment that contains the caret.\n */\nexport function expandCollapsedSelectionToWord(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): EditorSelection {\n const { line, character } = selection.start;\n const lineText = textDocument.getLineText(line);\n const ch = Math.max(0, Math.min(character, lineText.length));\n const span = expandCollapsedLineWord(lineText, ch);\n if (span === undefined) {\n return selection;\n }\n return {\n start: { line, character: span.start },\n end: { line, character: span.end },\n direction: DirectionForward,\n };\n}\n\nfunction expandCollapsedLineWord(\n lineText: string,\n character: number\n): { start: number; end: number } | undefined {\n const segmenter = new Intl.Segmenter(undefined, {\n granularity: 'word',\n });\n for (const seg of segmenter.segment(lineText)) {\n if (seg.isWordLike !== true) {\n continue;\n }\n const lo = seg.index;\n const hi = lo + seg.segment.length;\n // Match when the cursor is inside the word or immediately touching\n // one of its boundaries — not when separated by non-word characters.\n if (character >= lo && character <= hi) {\n return { start: lo, end: hi };\n }\n }\n return undefined;\n}\n\nfunction getSelectionAnchorAndFocusOffsets(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): [anchorOffset: number, focusOffset: number] {\n const isBackward = selection.direction === DirectionBackward;\n return [\n textDocument.offsetAt(isBackward ? selection.end : selection.start),\n textDocument.offsetAt(getCaretPosition(selection)),\n ];\n}\n\n// Resolves the range removed by deleteHardLineForward for one selection.\nfunction resolveDeleteHardLineForwardRange(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): Range {\n if (!isCollapsedSelection(selection)) {\n return { start: selection.start, end: selection.end };\n }\n const { line, character } = selection.start;\n const lineText = textDocument.getLineText(line);\n const lineLength = lineText.length;\n if (character < lineLength) {\n return {\n start: { line, character },\n end: { line, character: lineLength },\n };\n }\n if (line < textDocument.lineCount - 1) {\n return {\n start: { line, character },\n end: { line: line + 1, character: 0 },\n };\n }\n return {\n start: { line, character },\n end: { line, character },\n };\n}\n\n// When the user inserts a lone line break, copy the current line's indentation onto the new line.\nfunction expandSingleNewlineInsert(\n textDocument: TextDocument<unknown>,\n insertText: string,\n insertStartOffset: number\n): string {\n if (insertText !== '\\n' && insertText !== '\\r\\n') {\n return insertText;\n }\n const line = textDocument.positionAt(insertStartOffset).line;\n const lineText = textDocument.getLineText(line);\n const indentLen = getLeadingSpaces(lineText);\n if (indentLen === 0) {\n return insertText;\n }\n return insertText + lineText.slice(0, indentLen);\n}\n\nfunction getLeadingSpaces(text: string): number {\n let indent = 0;\n for (; indent < text.length; indent++) {\n const c = text.charCodeAt(indent);\n if (c !== /* space */ 32 && c !== /* tab */ 9) {\n break;\n }\n }\n return indent;\n}\n\nfunction createSelectionsFromOffsetPairs(\n textDocument: TextDocument<unknown>,\n offsetPairs: readonly [anchorOffset: number, focusOffset: number][]\n): EditorSelection[] {\n const normalizedOffsets: number[] = [];\n for (const [anchorOffset, focusOffset] of offsetPairs) {\n normalizedOffsets.push(\n Math.min(anchorOffset, focusOffset),\n Math.max(anchorOffset, focusOffset)\n );\n }\n const positions = textDocument.positionsAt(normalizedOffsets);\n return offsetPairs.map(([anchorOffset, focusOffset], index) => {\n const direction =\n anchorOffset === focusOffset\n ? DirectionNone\n : anchorOffset < focusOffset\n ? DirectionForward\n : DirectionBackward;\n return {\n start: positions[index * 2],\n end: positions[index * 2 + 1],\n direction,\n };\n });\n}\n\n// Expands a backspace over leading spaces into one soft-tab width so mixed hard/soft indentation\n// behaves like the explicit outdent command.\nfunction normalizeLeadingIndentForChange(\n textDocument: TextDocument<unknown>,\n change: ResolvedTextEdit,\n tabSize: number\n): ResolvedTextEdit {\n if (change.text !== '' || change.start !== change.end - 1) {\n return change;\n }\n const caretPosition = textDocument.positionAt(change.end);\n if (caretPosition.character === 0) {\n return change;\n }\n const lineText = textDocument.getLineText(caretPosition.line);\n const leadingText = lineText.slice(0, caretPosition.character);\n if (/[^ \\t]/.test(leadingText)) {\n return change;\n }\n if (lineText[caretPosition.character - 1] === '\\t') {\n return change;\n }\n const softTabStart = Math.max(0, caretPosition.character - tabSize);\n const softTabText = lineText.slice(softTabStart, caretPosition.character);\n if (softTabText.length === tabSize && /^ +$/.test(softTabText)) {\n return {\n ...change,\n start: change.end - softTabText.length,\n };\n }\n return change;\n}\n\nfunction boundaryToPosition(node: Node, offset: number): Position | null {\n const host = node.nodeType === 1 ? (node as HTMLElement) : node.parentElement;\n let lineEl: HTMLElement | null = host;\n while (lineEl !== null && getLineIndex(lineEl) === undefined) {\n lineEl = lineEl.parentElement;\n }\n if (lineEl === null) {\n return null;\n }\n const line = getLineIndex(lineEl);\n if (line === undefined) {\n return null;\n }\n\n if (node.nodeType === 3) {\n if (node.parentElement === null) {\n return null;\n }\n if (findTokenSpan(node.parentElement) !== null) {\n return { line, character: getLineChildEnd(node, offset) };\n }\n return {\n line,\n character:\n offsetBefore(lineEl, node) + getTextOffset(node.textContent, offset),\n };\n }\n\n if (node.nodeType === 1) {\n const el = node as HTMLElement;\n if (el.tagName === 'DIV') {\n let character = 0;\n for (let i = 0; i < offset; i++) {\n character = getLineChildEnd(el.childNodes[i]);\n }\n return { line, character };\n }\n if (el.tagName === 'BR') {\n return { line, character: 0 };\n }\n if (el.tagName === 'SPAN') {\n if (offset < el.childNodes.length) {\n const next = el.childNodes[offset];\n if (next?.nodeType === 1) {\n const nextBase = getCharacterIndex(next as HTMLElement);\n if (nextBase !== undefined) {\n return { line, character: nextBase };\n }\n const token = findTokenSpan(next as HTMLElement);\n const tokenBase =\n token === null ? undefined : getCharacterIndex(token);\n if (tokenBase !== undefined) {\n return { line, character: tokenBase };\n }\n }\n }\n return {\n line,\n character:\n offset > 0\n ? getLineChildEnd(el.childNodes[offset - 1])\n : offsetBefore(lineEl, el),\n };\n }\n return { line, character: offsetBefore(lineEl, el) };\n }\n return null;\n}\n\nfunction collectTokens(line: HTMLElement): HTMLElement[] {\n const tokens: HTMLElement[] = [];\n for (const child of line.childNodes) {\n if (child.nodeType !== 1) {\n continue;\n }\n const el = child as HTMLElement;\n if (el.tagName !== 'SPAN') {\n continue;\n }\n const base = getCharacterIndex(el);\n if (base !== undefined) {\n tokens.push(el);\n continue;\n }\n for (const nested of el.childNodes) {\n if (\n nested.nodeType === 1 &&\n getCharacterIndex(nested as HTMLElement) !== undefined\n ) {\n tokens.push(nested as HTMLElement);\n }\n }\n }\n return tokens;\n}\n\nfunction textAt(token: HTMLElement, offset: number): [Node, number] | null {\n let remaining = Math.max(0, offset);\n const stack: Array<{ container: Node; index: number }> = [\n { container: token, index: 0 },\n ];\n while (stack.length > 0) {\n const frame = stack[stack.length - 1];\n if (frame.index >= frame.container.childNodes.length) {\n stack.pop();\n continue;\n }\n const walkNode = frame.container.childNodes[frame.index];\n frame.index++;\n if (walkNode.nodeType === 3) {\n const len = getTextOffset(\n walkNode.textContent,\n walkNode.textContent?.length ?? 0\n );\n if (remaining <= len) {\n return [walkNode, remaining];\n }\n remaining -= len;\n } else if (walkNode.nodeType === 1) {\n stack.push({ container: walkNode, index: 0 });\n }\n }\n return null;\n}\n\nfunction textLengthBefore(root: Node, target: Node): number {\n let before = 0;\n const stack: Array<{ container: Node; index: number }> = [\n { container: root, index: 0 },\n ];\n while (stack.length > 0) {\n const frame = stack[stack.length - 1];\n if (frame.index >= frame.container.childNodes.length) {\n stack.pop();\n continue;\n }\n const walkNode = frame.container.childNodes[frame.index];\n if (walkNode === target) {\n return before;\n }\n frame.index++;\n if (walkNode.nodeType === 3) {\n before += getTextOffset(\n walkNode.textContent,\n walkNode.textContent?.length ?? 0\n );\n } else if (walkNode.nodeType === 1) {\n stack.push({ container: walkNode, index: 0 });\n }\n }\n return before;\n}\n\nfunction isInside(token: HTMLElement, node: Node): boolean {\n let current: Node | null = node;\n while (current !== null) {\n if (current === token) {\n return true;\n }\n current = current.parentElement;\n }\n return false;\n}\n\nfunction offsetBefore(line: HTMLElement, node: Node): number {\n if (node.parentElement === line) {\n let offset = 0;\n const index = Array.prototype.indexOf.call(line.childNodes, node);\n for (let i = 0; i < index; i++) {\n offset = getLineChildEnd(line.childNodes[i]);\n }\n return offset;\n }\n for (const token of collectTokens(line)) {\n if (isInside(token, node)) {\n const base = getCharacterIndex(token)!;\n return base + (node.nodeType === 3 ? textLengthBefore(token, node) : 0);\n }\n }\n let offset = 0;\n let target: HTMLElement | null =\n node.nodeType === 1 ? (node as HTMLElement) : node.parentElement;\n while (target !== null && target.parentElement !== null) {\n if (getLineIndex(target.parentElement) !== undefined) {\n break;\n }\n const parent = target.parentElement;\n const index = Array.prototype.indexOf.call(parent.childNodes, target);\n for (let i = 0; i < index; i++) {\n offset = getLineChildEnd(parent.childNodes[i]);\n }\n target = parent;\n }\n return offset;\n}\n\nfunction findTokenSpan(el: HTMLElement): HTMLElement | null {\n let current: HTMLElement | null = el;\n while (current !== null) {\n if (getLineIndex(current) !== undefined) {\n return null;\n }\n if (getCharacterIndex(current) !== undefined) {\n return current;\n }\n current = current.parentElement;\n }\n return null;\n}\n\nfunction getLineChildEnd(\n child: Node | undefined,\n textOffsetInChild?: number\n): number {\n if (child === undefined) {\n return 0;\n }\n if (child.nodeType === 3) {\n const parent = child.parentElement;\n if (parent === null) {\n return 0;\n }\n const token = findTokenSpan(parent);\n if (token === null) {\n return 0;\n }\n const base = getCharacterIndex(token);\n if (base === undefined) {\n return 0;\n }\n const length =\n textOffsetInChild === undefined\n ? getTextOffset(child.textContent, child.textContent?.length ?? 0)\n : getTextOffset(child.textContent, textOffsetInChild);\n return base + textLengthBefore(token, child) + length;\n }\n if (child.nodeType !== 1) {\n return 0;\n }\n const el = child as HTMLElement;\n if (el.tagName !== 'SPAN' && el.tagName !== 'BR') {\n return 0;\n }\n const base = getCharacterIndex(el);\n if (base !== undefined) {\n return base + (el.textContent?.length ?? 0);\n }\n let end = 0;\n for (const token of el.childNodes) {\n end = Math.max(end, getLineChildEnd(token));\n }\n return end;\n}\n\nfunction getLineIndex(el: HTMLElement): number | undefined {\n const { line } = el.dataset;\n if (line !== undefined) {\n const lineNumber = parseInt(line, 10);\n if (!Number.isNaN(lineNumber)) {\n return lineNumber - 1;\n }\n }\n return undefined;\n}\n\nfunction getCharacterIndex(el: HTMLElement): number | undefined {\n const { char } = el.dataset;\n if (char !== undefined) {\n const charIndex = parseInt(char, 10);\n if (!Number.isNaN(charIndex)) {\n return charIndex;\n }\n }\n return undefined;\n}\n\nfunction getTextOffset(\n text: string | null | undefined,\n offset: number\n): number {\n const value = text ?? '';\n const lineBreakIndex = value.search(/[\\r\\n]/);\n return Math.min(\n offset,\n lineBreakIndex === -1 ? value.length : lineBreakIndex\n );\n}\n"],"mappings":";;;AAWA,MAAa,oBAAoB;AACjC,MAAa,gBAAgB;AAC7B,MAAa,mBAAmB;;;;AAchC,SAAgB,iBACd,OACA,YAAgC,eACH;CAC7B,MAAM,QAAQ,mBAAmB,MAAM,gBAAgB,MAAM,YAAY;CACzE,MAAM,MAAM,mBAAmB,MAAM,cAAc,MAAM,UAAU;AACnE,KAAI,UAAU,QAAQ,QAAQ,KAC5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AI,MAAM,aAAa,GAAG;EACxB,MAAM,SAAS,MAAM;AACrB,MAAI,WAAW,KACb,QAAO;EAET,MAAM,QAAQ,cAAc,OAAO;AACnC,MAAI,UAAU,KACZ,QAAO;EAET,MAAMC,SAAO,kBAAkB,MAAM;AACrC,MAAIA,WAAS,OACX,QAAO;EAET,MAAM,SACJ,sBAAsB,SAClB,cAAc,MAAM,aAAa,MAAM,aAAa,UAAU,EAAE,GAChE,cAAc,MAAM,aAAa,kBAAkB;AACzD,SAAOA,SAAO,iBAAiB,OAAO,MAAM,GAAG;;AAEjD,KAAI,MAAM,aAAa,EACrB,QAAO;CAET,MAAM,KAAK;AACX,KAAI,GAAG,YAAY,UAAU,GAAG,YAAY,KAC1C,QAAO;CAET,MAAM,OAAO,kBAAkB,GAAG;AAClC,KAAI,SAAS,OACX,QAAO,QAAQ,GAAG,aAAa,UAAU;CAE3C,IAAI,MAAM;AACV,MAAK,MAAM,SAAS,GAAG,WACrB,OAAM,KAAK,IAAI,KAAK,gBAAgB,MAAM,CAAC;AAE7C,QAAO;;AAGT,SAAS,aAAa,IAAqC;CACzD,MAAM,EAAE,SAAS,GAAG;AACpB,KAAI,SAAS,QAAW;EACtB,MAAM,aAAa,SAAS,MAAM,GAAG;AACrC,MAAI,CAAC,OAAO,MAAM,WAAW,CAC3B,QAAO,aAAa;;;AAM1B,SAAS,kBAAkB,IAAqC;CAC9D,MAAM,EAAE,SAAS,GAAG;AACpB,KAAI,SAAS,QAAW;EACtB,MAAM,YAAY,SAAS,MAAM,GAAG;AACpC,MAAI,CAAC,OAAO,MAAM,UAAU,CAC1B,QAAO;;;AAMb,SAAS,cACP,MACA,QACQ;CACR,MAAM,QAAQ,QAAQ;CACtB,MAAM,iBAAiB,MAAM,OAAO,SAAS;AAC7C,QAAO,KAAK,IACV,QACA,mBAAmB,KAAK,MAAM,SAAS,eACxC"}
1
+ {"version":3,"file":"selection.js","names":["edits: TextEdit[]","newSelection: EditorSelection","selectionPositions: Position[]","ordered: Array<{\n index: number;\n start: number;\n end: number;\n }>","edits: ResolvedTextEdit[]","nextSelectionOffsets: Array<[number, number] | undefined>","mergedGroup:\n | {\n start: number;\n end: number;\n indices: number[];\n }\n | undefined","nextOffsets: [number, number]","ordered: Array<{\n index: number;\n start: number;\n end: number;\n text: string;\n }>","nextSelectionOffsets: number[]","nextOffsetPairs: Array<[number, number]>","edit: ResolvedTextEdit | undefined","deleteSelections: EditorSelection[]","accepted: {\n index: number;\n selection: EditorSelection;\n }[]","last: HTMLElement | null","lastTextNode: Text | null","match: number | undefined","normalizedOffsets: number[]","lineEl: HTMLElement | null","tokens: HTMLElement[]","stack: Array<{ container: Node; index: number }>","current: Node | null","offset","target: HTMLElement | null","current: HTMLElement | null","base"],"sources":["../../src/editor/selection.ts"],"sourcesContent":["import type { DiffLineAnnotation } from '../types';\nimport { applyDocumentChangeToLineAnnotations } from './lineAnnotations';\nimport type {\n Position,\n Range,\n ResolvedTextEdit,\n TextDocument,\n TextDocumentChange,\n TextEdit,\n} from './textDocument';\n\nexport const DirectionBackward = -1;\nexport const DirectionNone = 0;\nexport const DirectionForward = 1;\n\nexport type SelectionDirection =\n | typeof DirectionBackward\n | typeof DirectionNone\n | typeof DirectionForward;\n\nexport interface EditorSelection extends Range {\n direction: SelectionDirection;\n}\n\n/**\n * Converts a selection from a web selection to an editor selection.\n */\nexport function convertSelection(\n range: StaticRange,\n direction: SelectionDirection = DirectionNone\n): EditorSelection | undefined {\n const start = boundaryToPosition(range.startContainer, range.startOffset);\n const end = boundaryToPosition(range.endContainer, range.endOffset);\n if (start === null || end === null) {\n return undefined;\n }\n return {\n start,\n end,\n direction,\n };\n}\n\n/**\n * Resolves the indent edits for a selection.\n */\nexport function resolveIndentEdits(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection,\n tabSize: number,\n outdent: boolean\n): [edits: TextEdit[], nextSelection: EditorSelection] {\n if (textDocument === undefined) {\n return [[], selection];\n }\n const { start, end } = selection;\n const edits: TextEdit[] = [];\n let newSelection: EditorSelection = { ...selection };\n let endLine = end.line;\n if (start.line < end.line && end.character === 0) {\n endLine--;\n }\n for (let line = start.line; line <= endLine; line++) {\n const lineText = textDocument.getLineText(line);\n if (lineText === undefined) {\n continue;\n }\n const indentUnit = lineText.startsWith('\\t') ? '\\t' : ' '.repeat(tabSize);\n let deleteLength = 0;\n let newText = indentUnit;\n if (outdent) {\n if (lineText.startsWith('\\t')) {\n deleteLength = 1;\n } else if (lineText.startsWith(' ')) {\n const leadingSpacesLength =\n lineText.length - lineText.trimStart().length;\n deleteLength = Math.min(indentUnit.length, leadingSpacesLength);\n }\n if (deleteLength === 0) {\n continue;\n }\n newText = '';\n }\n edits.push({\n range: {\n start: { line, character: 0 },\n end: { line, character: deleteLength },\n },\n newText,\n });\n const delta = newText.length - deleteLength;\n if (line === start.line) {\n newSelection = {\n ...newSelection,\n start: {\n ...start,\n character: Math.max(0, start.character + delta),\n },\n };\n }\n if (line === end.line) {\n newSelection = {\n ...newSelection,\n end: {\n ...end,\n character: Math.max(0, end.character + delta),\n },\n };\n }\n }\n return [edits, newSelection];\n}\n\n/**\n * Maps the cursor move to all selections.\n */\nexport function mapCursorMove(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[],\n shortcut: 'textStart' | 'start' | 'end' | 'up' | 'down' | 'left' | 'right'\n): EditorSelection[] {\n const lineCount = textDocument.lineCount;\n return selections.map((selection) => {\n let { line, character } =\n shortcut === 'up' || shortcut === 'left'\n ? selection.start\n : selection.end;\n if (\n shortcut === 'textStart' ||\n shortcut === 'start' ||\n shortcut === 'end'\n ) {\n if (shortcut === 'textStart') {\n const indent = getLeadingSpaces(textDocument.getLineText(line));\n character = character === indent ? 0 : indent;\n } else {\n character = shortcut === 'start' ? 0 : textDocument.getLineLength(line);\n }\n if (selection.direction === DirectionBackward) {\n line = selection.start.line;\n } else {\n line = selection.end.line;\n }\n } else if (shortcut === 'up') {\n line = Math.max(0, line - 1);\n } else if (shortcut === 'down') {\n line = Math.min(Math.max(lineCount - 1, 0), line + 1);\n } else if (isCollapsedSelection(selection)) {\n const lineLength = textDocument.getLineLength(line);\n character = Math.min(character, lineLength);\n if (shortcut === 'left') {\n character--;\n\n if (character < 0) {\n if (line === 0) {\n character = 0;\n } else {\n line = Math.max(0, line - 1);\n character = textDocument.getLineLength(line);\n }\n }\n } else {\n character++;\n if (character > lineLength) {\n if (line === lineCount - 1) {\n character--;\n } else {\n line = Math.min(Math.max(lineCount - 1, 0), line + 1);\n character = 0;\n }\n }\n }\n }\n const pos = { line, character };\n return {\n start: pos,\n end: pos,\n direction: DirectionNone,\n };\n });\n}\n\n/**\n * Same as mapCursorMove, but with shift key pressed.\n */\nexport function mapSelectionShift(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[],\n shortcut: 'textStart' | 'start' | 'end' | 'up' | 'down' | 'left' | 'right'\n): EditorSelection[] {\n return selections.map((selection) => {\n const [anchorOffset, focusOffset] = getSelectionAnchorAndFocusOffsets(\n textDocument,\n selection\n );\n const focusPosition = textDocument.positionAt(focusOffset);\n const [movedFocusSelection] = mapCursorMove(\n textDocument,\n [\n {\n start: focusPosition,\n end: focusPosition,\n direction: DirectionNone,\n },\n ],\n shortcut\n );\n const movedFocusOffset = textDocument.offsetAt(movedFocusSelection.start);\n return createSelectionFromAnchorAndFocusOffsets(\n textDocument,\n anchorOffset,\n movedFocusOffset\n );\n });\n}\n\n/**\n * Applies a text change to the given text document\n */\nexport function applyTextChangeToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n edit: ResolvedTextEdit,\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[],\n tabSize = 2\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const primarySelection = selections[selections.length - 1];\n if (primarySelection === undefined) {\n return { nextSelections: [] };\n }\n const selectionPositions: Position[] = [];\n for (const selection of selections) {\n selectionPositions.push(selection.start, selection.end);\n }\n const selectionOffsets = selectionPositions.map((position) =>\n textDocument.offsetAt(position)\n );\n const primaryStartOffset = selectionOffsets[(selections.length - 1) * 2];\n const primaryEndOffset = selectionOffsets[(selections.length - 1) * 2 + 1];\n const ordered: Array<{\n index: number;\n start: number;\n end: number;\n }> = [];\n let isAlreadyOrdered = true;\n for (let index = 0; index < selections.length; index++) {\n const entry = {\n index,\n start: selectionOffsets[index * 2],\n end: selectionOffsets[index * 2 + 1],\n };\n const previous = ordered[ordered.length - 1];\n if (\n previous !== undefined &&\n (entry.start < previous.start ||\n (entry.start === previous.start && entry.end < previous.end))\n ) {\n isAlreadyOrdered = false;\n }\n ordered.push(entry);\n }\n if (!isAlreadyOrdered) {\n ordered.sort((a, b) => {\n const startOrder = a.start - b.start;\n if (startOrder !== 0) {\n return startOrder;\n }\n const endOrder = a.end - b.end;\n if (endOrder !== 0) {\n return endOrder;\n }\n return a.index - b.index;\n });\n }\n const adjustedChange = normalizeLeadingIndentForChange(\n textDocument,\n edit,\n tabSize\n );\n const edits: ResolvedTextEdit[] = [];\n const nextSelectionOffsets: Array<[number, number] | undefined> = Array.from({\n length: selections.length,\n });\n let offsetDelta = 0;\n let mergedGroup:\n | {\n start: number;\n end: number;\n indices: number[];\n }\n | undefined;\n const finalizeMergedGroup = () => {\n if (mergedGroup === undefined) {\n return;\n }\n const perGroupChange = normalizeLeadingIndentForChange(\n textDocument,\n {\n start: mergedGroup.start,\n end: mergedGroup.end,\n text: adjustedChange.text,\n },\n tabSize\n );\n const newText = expandSingleNewlineInsert(\n textDocument,\n perGroupChange.text,\n perGroupChange.start\n );\n edits.push({\n start: perGroupChange.start,\n end: perGroupChange.end,\n text: newText,\n });\n const nextOffsets: [number, number] = [\n mergedGroup.start + offsetDelta + newText.length,\n mergedGroup.start + offsetDelta + newText.length,\n ];\n for (const index of mergedGroup.indices) {\n nextSelectionOffsets[index] = nextOffsets;\n }\n offsetDelta += newText.length - (perGroupChange.end - perGroupChange.start);\n mergedGroup = undefined;\n };\n for (const entry of ordered) {\n const startOffset = Math.max(\n 0,\n entry.start + (adjustedChange.start - primaryStartOffset)\n );\n const endOffset = Math.max(\n startOffset,\n entry.end + (adjustedChange.end - primaryEndOffset)\n );\n if (mergedGroup !== undefined && startOffset < mergedGroup.end) {\n mergedGroup.end = Math.max(mergedGroup.end, endOffset);\n mergedGroup.indices.push(entry.index);\n continue;\n }\n finalizeMergedGroup();\n mergedGroup = {\n start: startOffset,\n end: endOffset,\n indices: [entry.index],\n };\n }\n finalizeMergedGroup();\n\n const change = textDocument.applyResolvedEdits(edits, true, selections);\n const nextSelections = createSelectionsFromOffsetPairs(\n textDocument,\n nextSelectionOffsets.map((offsets) => {\n if (offsets === undefined) {\n throw new Error('Missing next selection offsets');\n }\n return offsets;\n })\n );\n textDocument.setLastUndoSelectionsAfter(nextSelections);\n if (change !== undefined && lineAnnotations !== undefined) {\n const nextLineAnnotations =\n applyDocumentChangeToLineAnnotations<LAnnotation>(\n change,\n lineAnnotations\n );\n if (nextLineAnnotations !== undefined) {\n textDocument.setLastUndoLineAnnotations(\n lineAnnotations,\n nextLineAnnotations\n );\n }\n }\n return { nextSelections, change };\n}\n\n/**\n * Applies a text replace to multiple selections.\n */\nexport function applyTextReplaceToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n texts: string[],\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n if (selections.length !== texts.length) {\n throw new Error(\n 'Selection text replacements must match the selection count'\n );\n }\n const selectionPositions: Position[] = [];\n for (const selection of selections) {\n selectionPositions.push(selection.start, selection.end);\n }\n const selectionOffsets = selectionPositions.map((position) =>\n textDocument.offsetAt(position)\n );\n const ordered: Array<{\n index: number;\n start: number;\n end: number;\n text: string;\n }> = [];\n let isAlreadyOrdered = true;\n for (let index = 0; index < selections.length; index++) {\n const entry = {\n index,\n start: selectionOffsets[index * 2],\n end: selectionOffsets[index * 2 + 1],\n text: texts[index],\n };\n const previous = ordered[ordered.length - 1];\n if (\n previous !== undefined &&\n (entry.start < previous.start ||\n (entry.start === previous.start && entry.end < previous.end))\n ) {\n isAlreadyOrdered = false;\n }\n ordered.push(entry);\n }\n if (!isAlreadyOrdered) {\n ordered.sort((a, b) => {\n const startOrder = a.start - b.start;\n if (startOrder !== 0) {\n return startOrder;\n }\n const endOrder = a.end - b.end;\n if (endOrder !== 0) {\n return endOrder;\n }\n return a.index - b.index;\n });\n }\n const allDeletes = texts.every((text) => text === '');\n let edits: ResolvedTextEdit[];\n const nextSelectionOffsets: number[] = Array.from({\n length: selections.length,\n });\n if (allDeletes) {\n edits = [];\n let hasEffect = false;\n for (const entry of ordered) {\n nextSelectionOffsets[entry.index] = entry.end;\n if (entry.start >= entry.end) {\n continue;\n }\n hasEffect = true;\n const last = edits[edits.length - 1];\n if (last !== undefined && entry.start < last.end) {\n edits[edits.length - 1] = {\n start: last.start,\n end: Math.max(last.end, entry.end),\n text: '',\n };\n } else {\n edits.push({ start: entry.start, end: entry.end, text: '' });\n }\n }\n if (!hasEffect) {\n return { nextSelections: selections };\n }\n for (const entry of ordered) {\n const caret = entry.end;\n let delta = 0;\n let next = caret;\n for (const edit of edits) {\n if (caret <= edit.start) {\n break;\n }\n if (caret >= edit.end) {\n delta -= edit.end - edit.start;\n continue;\n }\n next = edit.start + delta;\n break;\n }\n if (next === caret) {\n next += delta;\n }\n nextSelectionOffsets[entry.index] = next;\n }\n } else {\n edits = [];\n let offsetDelta = 0;\n let previousEditEnd = -1;\n for (const entry of ordered) {\n if (entry.start < previousEditEnd) {\n throw new Error('Overlapping multi-selection edits are not supported');\n }\n previousEditEnd = entry.end;\n const newText = expandSingleNewlineInsert(\n textDocument,\n entry.text,\n entry.start\n );\n edits.push({\n start: entry.start,\n end: entry.end,\n text: newText,\n });\n nextSelectionOffsets[entry.index] =\n entry.start + offsetDelta + newText.length;\n offsetDelta += newText.length - (entry.end - entry.start);\n }\n }\n\n const change = textDocument.applyResolvedEdits(edits, true, selections);\n const nextSelections = createSelectionsFromOffsetPairs(\n textDocument,\n nextSelectionOffsets.map((offset) => [offset, offset])\n );\n textDocument.setLastUndoSelectionsAfter(nextSelections);\n if (change !== undefined && lineAnnotations !== undefined) {\n const nextLineAnnotations =\n applyDocumentChangeToLineAnnotations<LAnnotation>(\n change,\n lineAnnotations\n );\n if (nextLineAnnotations !== undefined) {\n textDocument.setLastUndoLineAnnotations(\n lineAnnotations,\n nextLineAnnotations\n );\n }\n }\n return { nextSelections, change };\n}\n\n/**\n * Swaps the two characters adjacent to a collapsed selection, matching browser\n * insertTranspose (Ctrl+T) behavior.\n */\nexport function applyTransposeToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const text = textDocument.getText();\n const edits: ResolvedTextEdit[] = [];\n const nextOffsetPairs: Array<[number, number]> = [];\n\n for (const selection of selections) {\n const [anchor, focus] = getSelectionAnchorAndFocusOffsets(\n textDocument,\n selection\n );\n if (!isCollapsedSelection(selection)) {\n nextOffsetPairs.push([anchor, focus]);\n continue;\n }\n\n const { line, character } = selection.start;\n const offset = anchor;\n const lineLength = textDocument.getLineLength(line);\n let edit: ResolvedTextEdit | undefined;\n\n if (character > 0 && character < lineLength) {\n edit = {\n start: offset - 1,\n end: offset + 1,\n text: text[offset] + text[offset - 1],\n };\n nextOffsetPairs.push([offset + 1, offset + 1]);\n } else if (character === lineLength && lineLength >= 2) {\n edit = {\n start: offset - 2,\n end: offset,\n text: text[offset - 1] + text[offset - 2],\n };\n nextOffsetPairs.push([offset, offset]);\n } else if (character === 0 && line > 0 && lineLength > 0) {\n const prevLine = line - 1;\n const prevLength = textDocument.getLineLength(prevLine);\n const prevEnd = textDocument.offsetAt({\n line: prevLine,\n character: prevLength,\n });\n const prevStart = prevLength > 0 ? prevEnd - 1 : prevEnd;\n edit = {\n start: prevStart,\n end: offset + 1,\n text:\n text[offset] +\n text.slice(prevEnd, offset) +\n text.slice(prevStart, prevEnd),\n };\n nextOffsetPairs.push([offset + 1, offset + 1]);\n } else {\n nextOffsetPairs.push([anchor, focus]);\n continue;\n }\n\n edits.push(edit);\n }\n\n if (edits.length === 0) {\n return { nextSelections: selections };\n }\n\n edits.sort((a, b) => a.start - b.start);\n for (let index = 1; index < edits.length; index++) {\n if (edits[index].start < edits[index - 1].end) {\n throw new Error('Overlapping multi-selection edits are not supported');\n }\n }\n\n const change = textDocument.applyResolvedEdits(edits, true, selections);\n const nextSelections = createSelectionsFromOffsetPairs(\n textDocument,\n nextOffsetPairs\n );\n textDocument.setLastUndoSelectionsAfter(nextSelections);\n if (change !== undefined && lineAnnotations !== undefined) {\n const nextLineAnnotations =\n applyDocumentChangeToLineAnnotations<LAnnotation>(\n change,\n lineAnnotations\n );\n if (nextLineAnnotations !== undefined) {\n textDocument.setLastUndoLineAnnotations(\n lineAnnotations,\n nextLineAnnotations\n );\n }\n }\n return { nextSelections, change };\n}\n\n/**\n * Deletes from each selection to the end of its line, including the line break\n * when the caret is already at the end of a non-final line. Non-collapsed\n * selections delete their selected text instead.\n */\nexport function applyDeleteHardLineForwardToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const deleteSelections: EditorSelection[] = selections.map((selection) => {\n const range = resolveDeleteHardLineForwardRange(textDocument, selection);\n return {\n start: range.start,\n end: range.end,\n direction: DirectionNone,\n };\n });\n return applyTextReplaceToSelections(\n textDocument,\n deleteSelections,\n deleteSelections.map(() => ''),\n lineAnnotations\n );\n}\n\n/**\n * Deletes from each selection back to the start of its soft (visual) line.\n * Non-collapsed selections delete their selected text instead.\n */\nexport function applyDeleteSoftLineBackwardToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n getSoftLineStart?: (line: number, character: number) => number,\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const deleteSelections: EditorSelection[] = selections.map((selection) => {\n if (!isCollapsedSelection(selection)) {\n return {\n start: selection.start,\n end: selection.end,\n direction: DirectionNone,\n };\n }\n const caret = getCaretPosition(selection);\n const { line, character } = caret;\n const softLineStart = getSoftLineStart?.(line, character) ?? 0;\n if (character > softLineStart) {\n return {\n start: { line, character: softLineStart },\n end: { line, character },\n direction: DirectionNone,\n };\n }\n if (line === 0) {\n return {\n start: caret,\n end: caret,\n direction: DirectionNone,\n };\n }\n const prevLineLength = textDocument.getLineLength(line - 1);\n return {\n start: { line: line - 1, character: prevLineLength },\n end: { line, character: 0 },\n direction: DirectionNone,\n };\n });\n return applyTextReplaceToSelections(\n textDocument,\n deleteSelections,\n deleteSelections.map(() => ''),\n lineAnnotations\n );\n}\n\n/**\n * Deletes the word or separator group immediately before each selection.\n * Non-collapsed selections delete their selected text instead.\n */\nexport function applyDeleteWordBackwardToSelections<LAnnotation>(\n textDocument: TextDocument<LAnnotation>,\n selections: EditorSelection[],\n lineAnnotations?: DiffLineAnnotation<LAnnotation>[]\n): {\n nextSelections: EditorSelection[];\n change?: TextDocumentChange;\n} {\n const deleteSelections: EditorSelection[] = selections.map((selection) => {\n const [start, end] = resolveDeleteWordBackwardRange(\n textDocument,\n selection\n );\n return {\n start,\n end,\n direction: DirectionNone,\n };\n });\n return applyTextReplaceToSelections(\n textDocument,\n deleteSelections,\n deleteSelections.map(() => ''),\n lineAnnotations\n );\n}\n\n/**\n * Checks if a selection is collapsed.\n */\nexport function isCollapsedSelection(\n selection: EditorSelection | Range\n): boolean {\n return (\n selection.start.line === selection.end.line &&\n selection.start.character === selection.end.character\n );\n}\n\n/**\n * Returns the caret (focus) position for a selection.\n */\nexport function getCaretPosition(selection: EditorSelection): Position {\n const { start, end, direction } = selection;\n return direction === DirectionBackward ? start : end;\n}\n\n/**\n * Checks if a line is editable.\n */\nexport function isLineEditable(lineType: string): boolean {\n return (\n lineType === 'context' ||\n lineType === 'context-expanded' ||\n lineType === 'change-addition'\n );\n}\n\n/**\n * Checks whether selections `a` and `b` intersect.\n */\nexport function selectionIntersects(\n a: EditorSelection | Range,\n b: EditorSelection | Range\n): boolean {\n const aCollapsed = isCollapsedSelection(a);\n const bCollapsed = isCollapsedSelection(b);\n if (aCollapsed && bCollapsed) {\n return comparePosition(a.start, b.start) === 0;\n }\n if (aCollapsed) {\n return (\n comparePosition(b.start, a.start) <= 0 &&\n comparePosition(a.start, b.end) <= 0\n );\n }\n if (bCollapsed) {\n return (\n comparePosition(a.start, b.start) <= 0 &&\n comparePosition(b.start, a.end) <= 0\n );\n }\n return (\n comparePosition(a.start, b.end) < 0 && comparePosition(b.start, a.end) < 0\n );\n}\n\n/**\n * Compares two positions.\n */\nexport function comparePosition(a: Position, b: Position): number {\n if (a.line !== b.line) {\n return a.line - b.line;\n }\n return a.character - b.character;\n}\n\n/**\n * Creates a selection from anchor and focus offsets.\n */\nexport function createSelectionFromAnchorAndFocusOffsets(\n textDocument: TextDocument<unknown>,\n anchorOffset: number,\n focusOffset: number\n): EditorSelection {\n const direction =\n anchorOffset === focusOffset\n ? DirectionNone\n : anchorOffset < focusOffset\n ? DirectionForward\n : DirectionBackward;\n const start = Math.min(anchorOffset, focusOffset);\n const end = Math.max(anchorOffset, focusOffset);\n return {\n start: textDocument.positionAt(start),\n end: textDocument.positionAt(end),\n direction,\n };\n}\n\n/**\n * Creates a selection from a anchor and focus selection.\n */\nexport function createSelectionFrom(\n anchorSelection: EditorSelection,\n focusSelection: EditorSelection\n): EditorSelection {\n const anchor =\n anchorSelection.direction === DirectionBackward\n ? anchorSelection.end\n : anchorSelection.start;\n const currentStartOrder = comparePosition(anchor, focusSelection.start);\n const currentEndOrder = comparePosition(anchor, focusSelection.end);\n let focus = focusSelection.end;\n if (currentStartOrder <= 0) {\n focus = focusSelection.end;\n } else if (currentEndOrder >= 0) {\n focus = focusSelection.start;\n } else {\n // When the original anchor sits inside `current`, keep whichever edge\n // stayed at the anchor so drag direction remains stable.\n focus = currentStartOrder === 0 ? focusSelection.end : focusSelection.start;\n }\n const anchorVsFocus = comparePosition(anchor, focus);\n const direction: SelectionDirection =\n anchorVsFocus === 0\n ? DirectionNone\n : anchorVsFocus < 0\n ? DirectionForward\n : DirectionBackward;\n const selectionStart = anchorVsFocus <= 0 ? anchor : focus;\n const selectionEnd = anchorVsFocus <= 0 ? focus : anchor;\n return {\n start: selectionStart,\n end: selectionEnd,\n direction,\n };\n}\n\n/**\n * Extends or shrinks the selection `original` using the endpoints of `target`, \\\n * matching contenteditable shift + click extend behavior.\n */\nexport function extendSelection(\n original: EditorSelection,\n target: EditorSelection\n): EditorSelection {\n const leftExtended = comparePosition(target.start, original.start) < 0;\n const rightExtended = comparePosition(target.end, original.end) > 0;\n\n if (leftExtended && !rightExtended) {\n return {\n start: target.start,\n end: original.end,\n direction: DirectionBackward,\n };\n }\n\n if (rightExtended && !leftExtended) {\n return {\n start: original.start,\n end: target.end,\n direction: DirectionForward,\n };\n }\n\n if (original.direction === DirectionBackward) {\n return {\n start: target.start,\n end: original.end,\n direction:\n comparePosition(target.start, original.end) === 0\n ? DirectionNone\n : DirectionBackward,\n };\n }\n\n return {\n start: original.start,\n end: target.end,\n direction:\n comparePosition(original.start, target.end) === 0\n ? DirectionNone\n : DirectionForward,\n };\n}\n\n/**\n * Extends multiple selections.\n */\nexport function extendSelections(\n selections: EditorSelection[],\n target: EditorSelection\n): EditorSelection[] {\n const newSelections = selections.map((selection) => {\n return extendSelection(selection, target);\n });\n return mergeOverlappingSelections(newSelections);\n}\n\n/**\n * Merges overlapping selections.\n */\nexport function mergeOverlappingSelections(\n selections: EditorSelection[]\n): EditorSelection[] {\n if (selections.length <= 1) {\n return selections;\n }\n const selected = new Set<number>();\n const accepted: {\n index: number;\n selection: EditorSelection;\n }[] = [];\n for (let i = selections.length - 1; i >= 0; i--) {\n const selection = selections[i];\n if (selection === undefined) {\n continue;\n }\n let left = 0;\n let right = accepted.length;\n while (left < right) {\n const mid = Math.floor((left + right) / 2);\n const candidate = accepted[mid]?.selection;\n if (candidate === undefined) {\n break;\n }\n if (comparePosition(candidate.start, selection.start) < 0) {\n left = mid + 1;\n } else {\n right = mid;\n }\n }\n const previous = accepted[left - 1]?.selection;\n const next = accepted[left]?.selection;\n if (\n (previous !== undefined && selectionIntersects(previous, selection)) ||\n (next !== undefined && selectionIntersects(next, selection))\n ) {\n continue;\n }\n accepted.splice(left, 0, { index: i, selection });\n selected.add(i);\n }\n return selections.filter((_, index) => selected.has(index));\n}\n\n/**\n * Finds the next matching word and updates the selections.\n */\nexport function findNexMatch(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[]\n): EditorSelection[] | undefined {\n if (selections.length === 0) {\n return undefined;\n }\n\n const normalizedSelections = selections.map((selection) =>\n isCollapsedSelection(selection)\n ? expandCollapsedSelectionToWord(textDocument, selection)\n : selection\n );\n const texts = normalizedSelections.map((s) => textDocument.getText(s));\n const needle = texts[0];\n if (needle.length === 0 || texts.some((t) => t !== needle)) {\n return undefined;\n }\n\n const occupied = normalizedSelections.map(\n (s) =>\n [textDocument.offsetAt(s.start), textDocument.offsetAt(s.end)] as [\n number,\n number,\n ]\n );\n const nextOffset = textDocument.findNextNonOverlappingSubstring(\n needle,\n occupied\n );\n if (nextOffset === undefined) {\n return normalizedSelections.some((selection, index) => {\n const original = selections[index];\n return (\n comparePosition(selection.start, original.start) !== 0 ||\n comparePosition(selection.end, original.end) !== 0 ||\n selection.direction !== original.direction\n );\n })\n ? normalizedSelections\n : undefined;\n }\n const added = createSelectionFromAnchorAndFocusOffsets(\n textDocument,\n nextOffset,\n nextOffset + needle.length\n );\n return [...normalizedSelections, added];\n}\n\n/**\n * Get the full selection of the document.\n */\nexport function getDocumentFullSelection(\n textDocument: TextDocument<unknown>\n): EditorSelection {\n const lastLine = textDocument.lineCount - 1;\n const lastCharacter = textDocument.getLineLength(lastLine);\n return {\n start: { line: 0, character: 0 },\n end: { line: lastLine, character: lastCharacter },\n direction: DirectionForward,\n };\n}\n\n/**\n * Get the boundary selection of the document.\n */\nexport function getDocumentBoundarySelection(\n textDocument: TextDocument<unknown>,\n atEnd: boolean\n): EditorSelection {\n const line = atEnd ? textDocument.lineCount - 1 : 0;\n const character = atEnd ? textDocument.getLineLength(line) : 0;\n const start = { line, character };\n return {\n start: start,\n end: start,\n direction: DirectionForward,\n };\n}\n\n/**\n * Get the text of the selections for the given text document.\n */\nexport function getSelectionText(\n textDocument: TextDocument<unknown>,\n selections: EditorSelection[]\n): string {\n return [...selections]\n .sort((a, b) => {\n const startOrder = comparePosition(a.start, b.start);\n if (startOrder !== 0) {\n return startOrder;\n }\n return comparePosition(a.end, b.end);\n })\n .map((selection) => {\n if (isCollapsedSelection(selection)) {\n return textDocument.getLineText(selection.start.line, false);\n }\n return textDocument.getText(selection);\n })\n .join('\\n');\n}\n\n/**\n * Get the anchor node and offset for a selection.\n */\nexport function getSelectionAnchor(\n lineElement: HTMLElement,\n character: number\n): [Node, number] {\n const ch = Math.max(0, character);\n const tokens = collectTokens(lineElement);\n\n let last: HTMLElement | null = null;\n for (const token of tokens) {\n last = token;\n const base = getCharacterIndex(token)!;\n const end = base + (token.textContent?.length ?? 0);\n if (ch <= end) {\n const anchor = textAt(token, ch < base ? 0 : ch - base);\n if (anchor !== null) {\n return anchor;\n }\n }\n }\n\n if (last !== null) {\n const anchor = textAt(last, last.textContent?.length ?? 0);\n if (anchor !== null) {\n return anchor;\n }\n return [last, 0];\n }\n\n let textOffset = 0;\n let lastTextNode: Text | null = null;\n for (const child of lineElement.childNodes) {\n if (child.nodeType === 1 && (child as HTMLElement).tagName === 'BR') {\n return [child, 0];\n }\n if (child.nodeType !== 3) {\n continue;\n }\n lastTextNode = child as Text;\n const len = getTextOffset(\n lastTextNode.textContent,\n lastTextNode.textContent?.length ?? 0\n );\n if (ch <= textOffset + len) {\n return [\n lastTextNode,\n getTextOffset(lastTextNode.textContent, ch - textOffset),\n ];\n }\n textOffset += len;\n }\n\n if (lastTextNode !== null) {\n return [\n lastTextNode,\n getTextOffset(\n lastTextNode.textContent,\n lastTextNode.textContent?.length ?? 0\n ),\n ];\n }\n return [lineElement, 0];\n}\n\n/**\n * Expands a zero-width selection to the word-like segment that contains the caret.\n */\nexport function expandCollapsedSelectionToWord(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): EditorSelection {\n const { line, character } = selection.start;\n const lineText = textDocument.getLineText(line);\n const ch = Math.max(0, Math.min(character, lineText.length));\n const span = expandCollapsedLineWord(lineText, ch);\n if (span === undefined) {\n return selection;\n }\n return {\n start: { line, character: span.start },\n end: { line, character: span.end },\n direction: DirectionForward,\n };\n}\n\nfunction expandCollapsedLineWord(\n lineText: string,\n character: number\n): { start: number; end: number } | undefined {\n const segmenter = new Intl.Segmenter(undefined, {\n granularity: 'word',\n });\n for (const seg of segmenter.segment(lineText)) {\n if (seg.isWordLike !== true) {\n continue;\n }\n const lo = seg.index;\n const hi = lo + seg.segment.length;\n // Match when the cursor is inside the word or immediately touching\n // one of its boundaries — not when separated by non-word characters.\n if (character >= lo && character <= hi) {\n return { start: lo, end: hi };\n }\n }\n return undefined;\n}\n\n// Resolves the range removed by deleteWordBackward for one selection.\nfunction resolveDeleteWordBackwardRange(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): [start: Position, end: Position] {\n if (!isCollapsedSelection(selection)) {\n return [selection.start, selection.end];\n }\n const caret = getCaretPosition(selection);\n const { line, character: head } = caret;\n if (head === 0) {\n if (line === 0) {\n return [caret, caret];\n }\n const prevLineLength = textDocument.getLineLength(line - 1);\n return [\n { line: line - 1, character: prevLineLength },\n { line, character: 0 },\n ];\n }\n const lineText = textDocument.getLineText(line);\n const graphemeStarts = [0];\n const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });\n for (const segment of segmenter.segment(lineText)) {\n if (segment.index > 0) {\n graphemeStarts.push(segment.index);\n }\n }\n let pos = head;\n let match: number | undefined;\n while (pos > 0) {\n const prev = findClusterBreak(lineText, pos, false, graphemeStarts);\n const nextChar = lineText.slice(prev, pos);\n const nextMatch = !/\\S/.test(nextChar)\n ? 0\n : /\\p{Alphabetic}|\\p{Number}|_/u.test(nextChar)\n ? 1\n : 2;\n if (match !== undefined && nextMatch !== match) {\n break;\n }\n if (nextChar !== ' ' || pos !== head) {\n match = nextMatch;\n }\n pos = prev;\n }\n return [\n { line, character: pos },\n { line, character: head },\n ];\n}\n\nfunction findClusterBreak(\n text: string,\n pos: number,\n forward: boolean,\n graphemeStarts: number[]\n): number {\n if (forward) {\n for (const start of graphemeStarts) {\n if (start > pos) {\n return start;\n }\n }\n return text.length;\n }\n for (let i = graphemeStarts.length - 1; i >= 0; i--) {\n const start = graphemeStarts[i];\n if (start < pos) {\n return start;\n }\n }\n return 0;\n}\n\nfunction getSelectionAnchorAndFocusOffsets(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): [anchorOffset: number, focusOffset: number] {\n const isBackward = selection.direction === DirectionBackward;\n return [\n textDocument.offsetAt(isBackward ? selection.end : selection.start),\n textDocument.offsetAt(getCaretPosition(selection)),\n ];\n}\n\n// Resolves the range removed by deleteHardLineForward for one selection.\nfunction resolveDeleteHardLineForwardRange(\n textDocument: TextDocument<unknown>,\n selection: EditorSelection\n): Range {\n if (!isCollapsedSelection(selection)) {\n return { start: selection.start, end: selection.end };\n }\n const { line, character } = selection.start;\n const lineText = textDocument.getLineText(line);\n const lineLength = lineText.length;\n if (character < lineLength) {\n return {\n start: { line, character },\n end: { line, character: lineLength },\n };\n }\n if (line < textDocument.lineCount - 1) {\n return {\n start: { line, character },\n end: { line: line + 1, character: 0 },\n };\n }\n return {\n start: { line, character },\n end: { line, character },\n };\n}\n\n// When the user inserts a lone line break, copy the current line's indentation onto the new line.\nfunction expandSingleNewlineInsert(\n textDocument: TextDocument<unknown>,\n insertText: string,\n insertStartOffset: number\n): string {\n if (insertText !== '\\n' && insertText !== '\\r\\n') {\n return insertText;\n }\n const line = textDocument.positionAt(insertStartOffset).line;\n const lineText = textDocument.getLineText(line);\n const indentLen = getLeadingSpaces(lineText);\n if (indentLen === 0) {\n return insertText;\n }\n return insertText + lineText.slice(0, indentLen);\n}\n\nfunction getLeadingSpaces(text: string): number {\n let indent = 0;\n for (; indent < text.length; indent++) {\n const c = text.charCodeAt(indent);\n if (c !== /* space */ 32 && c !== /* tab */ 9) {\n break;\n }\n }\n return indent;\n}\n\nfunction createSelectionsFromOffsetPairs(\n textDocument: TextDocument<unknown>,\n offsetPairs: readonly [anchorOffset: number, focusOffset: number][]\n): EditorSelection[] {\n const normalizedOffsets: number[] = [];\n for (const [anchorOffset, focusOffset] of offsetPairs) {\n normalizedOffsets.push(\n Math.min(anchorOffset, focusOffset),\n Math.max(anchorOffset, focusOffset)\n );\n }\n const positions = textDocument.positionsAt(normalizedOffsets);\n return offsetPairs.map(([anchorOffset, focusOffset], index) => {\n const direction =\n anchorOffset === focusOffset\n ? DirectionNone\n : anchorOffset < focusOffset\n ? DirectionForward\n : DirectionBackward;\n return {\n start: positions[index * 2],\n end: positions[index * 2 + 1],\n direction,\n };\n });\n}\n\n// Expands a backspace over leading spaces into one soft-tab width so mixed hard/soft indentation\n// behaves like the explicit outdent command.\nfunction normalizeLeadingIndentForChange(\n textDocument: TextDocument<unknown>,\n change: ResolvedTextEdit,\n tabSize: number\n): ResolvedTextEdit {\n if (change.text !== '' || change.start !== change.end - 1) {\n return change;\n }\n const caretPosition = textDocument.positionAt(change.end);\n if (caretPosition.character === 0) {\n return change;\n }\n const lineText = textDocument.getLineText(caretPosition.line);\n const leadingText = lineText.slice(0, caretPosition.character);\n if (/[^ \\t]/.test(leadingText)) {\n return change;\n }\n if (lineText[caretPosition.character - 1] === '\\t') {\n return change;\n }\n const softTabStart = Math.max(0, caretPosition.character - tabSize);\n const softTabText = lineText.slice(softTabStart, caretPosition.character);\n if (softTabText.length === tabSize && /^ +$/.test(softTabText)) {\n return {\n ...change,\n start: change.end - softTabText.length,\n };\n }\n return change;\n}\n\nfunction boundaryToPosition(node: Node, offset: number): Position | null {\n const host = node.nodeType === 1 ? (node as HTMLElement) : node.parentElement;\n let lineEl: HTMLElement | null = host;\n while (lineEl !== null && getLineIndex(lineEl) === undefined) {\n lineEl = lineEl.parentElement;\n }\n if (lineEl === null) {\n return null;\n }\n const line = getLineIndex(lineEl);\n if (line === undefined) {\n return null;\n }\n\n if (node.nodeType === 3) {\n if (node.parentElement === null) {\n return null;\n }\n if (findTokenSpan(node.parentElement) !== null) {\n return { line, character: getLineChildEnd(node, offset) };\n }\n return {\n line,\n character:\n offsetBefore(lineEl, node) + getTextOffset(node.textContent, offset),\n };\n }\n\n if (node.nodeType === 1) {\n const el = node as HTMLElement;\n if (el.tagName === 'DIV') {\n let character = 0;\n for (let i = 0; i < offset; i++) {\n character = getLineChildEnd(el.childNodes[i]);\n }\n return { line, character };\n }\n if (el.tagName === 'BR') {\n return { line, character: 0 };\n }\n if (el.tagName === 'SPAN') {\n if (offset < el.childNodes.length) {\n const next = el.childNodes[offset];\n if (next?.nodeType === 1) {\n const nextBase = getCharacterIndex(next as HTMLElement);\n if (nextBase !== undefined) {\n return { line, character: nextBase };\n }\n const token = findTokenSpan(next as HTMLElement);\n const tokenBase =\n token === null ? undefined : getCharacterIndex(token);\n if (tokenBase !== undefined) {\n return { line, character: tokenBase };\n }\n }\n }\n return {\n line,\n character:\n offset > 0\n ? getLineChildEnd(el.childNodes[offset - 1])\n : offsetBefore(lineEl, el),\n };\n }\n return { line, character: offsetBefore(lineEl, el) };\n }\n return null;\n}\n\nfunction collectTokens(line: HTMLElement): HTMLElement[] {\n const tokens: HTMLElement[] = [];\n for (const child of line.childNodes) {\n if (child.nodeType !== 1) {\n continue;\n }\n const el = child as HTMLElement;\n if (el.tagName !== 'SPAN') {\n continue;\n }\n const base = getCharacterIndex(el);\n if (base !== undefined) {\n tokens.push(el);\n continue;\n }\n for (const nested of el.childNodes) {\n if (\n nested.nodeType === 1 &&\n getCharacterIndex(nested as HTMLElement) !== undefined\n ) {\n tokens.push(nested as HTMLElement);\n }\n }\n }\n return tokens;\n}\n\nfunction textAt(token: HTMLElement, offset: number): [Node, number] | null {\n let remaining = Math.max(0, offset);\n const stack: Array<{ container: Node; index: number }> = [\n { container: token, index: 0 },\n ];\n while (stack.length > 0) {\n const frame = stack[stack.length - 1];\n if (frame.index >= frame.container.childNodes.length) {\n stack.pop();\n continue;\n }\n const walkNode = frame.container.childNodes[frame.index];\n frame.index++;\n if (walkNode.nodeType === 3) {\n const len = getTextOffset(\n walkNode.textContent,\n walkNode.textContent?.length ?? 0\n );\n if (remaining <= len) {\n return [walkNode, remaining];\n }\n remaining -= len;\n } else if (walkNode.nodeType === 1) {\n stack.push({ container: walkNode, index: 0 });\n }\n }\n return null;\n}\n\nfunction textLengthBefore(root: Node, target: Node): number {\n let before = 0;\n const stack: Array<{ container: Node; index: number }> = [\n { container: root, index: 0 },\n ];\n while (stack.length > 0) {\n const frame = stack[stack.length - 1];\n if (frame.index >= frame.container.childNodes.length) {\n stack.pop();\n continue;\n }\n const walkNode = frame.container.childNodes[frame.index];\n if (walkNode === target) {\n return before;\n }\n frame.index++;\n if (walkNode.nodeType === 3) {\n before += getTextOffset(\n walkNode.textContent,\n walkNode.textContent?.length ?? 0\n );\n } else if (walkNode.nodeType === 1) {\n stack.push({ container: walkNode, index: 0 });\n }\n }\n return before;\n}\n\nfunction isInside(token: HTMLElement, node: Node): boolean {\n let current: Node | null = node;\n while (current !== null) {\n if (current === token) {\n return true;\n }\n current = current.parentElement;\n }\n return false;\n}\n\nfunction offsetBefore(line: HTMLElement, node: Node): number {\n if (node.parentElement === line) {\n let offset = 0;\n const index = Array.prototype.indexOf.call(line.childNodes, node);\n for (let i = 0; i < index; i++) {\n offset = getLineChildEnd(line.childNodes[i]);\n }\n return offset;\n }\n for (const token of collectTokens(line)) {\n if (isInside(token, node)) {\n const base = getCharacterIndex(token)!;\n return base + (node.nodeType === 3 ? textLengthBefore(token, node) : 0);\n }\n }\n let offset = 0;\n let target: HTMLElement | null =\n node.nodeType === 1 ? (node as HTMLElement) : node.parentElement;\n while (target !== null && target.parentElement !== null) {\n if (getLineIndex(target.parentElement) !== undefined) {\n break;\n }\n const parent = target.parentElement;\n const index = Array.prototype.indexOf.call(parent.childNodes, target);\n for (let i = 0; i < index; i++) {\n offset = getLineChildEnd(parent.childNodes[i]);\n }\n target = parent;\n }\n return offset;\n}\n\nfunction findTokenSpan(el: HTMLElement): HTMLElement | null {\n let current: HTMLElement | null = el;\n while (current !== null) {\n if (getLineIndex(current) !== undefined) {\n return null;\n }\n if (getCharacterIndex(current) !== undefined) {\n return current;\n }\n current = current.parentElement;\n }\n return null;\n}\n\nfunction getLineChildEnd(\n child: Node | undefined,\n textOffsetInChild?: number\n): number {\n if (child === undefined) {\n return 0;\n }\n if (child.nodeType === 3) {\n const parent = child.parentElement;\n if (parent === null) {\n return 0;\n }\n const token = findTokenSpan(parent);\n if (token === null) {\n return 0;\n }\n const base = getCharacterIndex(token);\n if (base === undefined) {\n return 0;\n }\n const length =\n textOffsetInChild === undefined\n ? getTextOffset(child.textContent, child.textContent?.length ?? 0)\n : getTextOffset(child.textContent, textOffsetInChild);\n return base + textLengthBefore(token, child) + length;\n }\n if (child.nodeType !== 1) {\n return 0;\n }\n const el = child as HTMLElement;\n if (el.tagName !== 'SPAN' && el.tagName !== 'BR') {\n return 0;\n }\n const base = getCharacterIndex(el);\n if (base !== undefined) {\n return base + (el.textContent?.length ?? 0);\n }\n let end = 0;\n for (const token of el.childNodes) {\n end = Math.max(end, getLineChildEnd(token));\n }\n return end;\n}\n\nfunction getLineIndex(el: HTMLElement): number | undefined {\n const { line } = el.dataset;\n if (line !== undefined) {\n const lineNumber = parseInt(line, 10);\n if (!Number.isNaN(lineNumber)) {\n return lineNumber - 1;\n }\n }\n return undefined;\n}\n\nfunction getCharacterIndex(el: HTMLElement): number | undefined {\n const { char } = el.dataset;\n if (char !== undefined) {\n const charIndex = parseInt(char, 10);\n if (!Number.isNaN(charIndex)) {\n return charIndex;\n }\n }\n return undefined;\n}\n\nfunction getTextOffset(\n text: string | null | undefined,\n offset: number\n): number {\n const value = text ?? '';\n const lineBreakIndex = value.search(/[\\r\\n]/);\n return Math.min(\n offset,\n lineBreakIndex === -1 ? value.length : lineBreakIndex\n );\n}\n"],"mappings":";;;AAWA,MAAa,oBAAoB;AACjC,MAAa,gBAAgB;AAC7B,MAAa,mBAAmB;;;;AAchC,SAAgB,iBACd,OACA,YAAgC,eACH;CAC7B,MAAM,QAAQ,mBAAmB,MAAM,gBAAgB,MAAM,YAAY;CACzE,MAAM,MAAM,mBAAmB,MAAM,cAAc,MAAM,UAAU;AACnE,KAAI,UAAU,QAAQ,QAAQ,KAC5B;AAEF,QAAO;EACL;EACA;EACA;EACD;;;;;AAMH,SAAgB,mBACd,cACA,WACA,SACA,SACqD;AACrD,KAAI,iBAAiB,OACnB,QAAO,CAAC,EAAE,EAAE,UAAU;CAExB,MAAM,EAAE,OAAO,QAAQ;CACvB,MAAMA,QAAoB,EAAE;CAC5B,IAAIC,eAAgC,EAAE,GAAG,WAAW;CACpD,IAAI,UAAU,IAAI;AAClB,KAAI,MAAM,OAAO,IAAI,QAAQ,IAAI,cAAc,EAC7C;AAEF,MAAK,IAAI,OAAO,MAAM,MAAM,QAAQ,SAAS,QAAQ;EACnD,MAAM,WAAW,aAAa,YAAY,KAAK;AAC/C,MAAI,aAAa,OACf;EAEF,MAAM,aAAa,SAAS,WAAW,IAAK,GAAG,MAAO,IAAI,OAAO,QAAQ;EACzE,IAAI,eAAe;EACnB,IAAI,UAAU;AACd,MAAI,SAAS;AACX,OAAI,SAAS,WAAW,IAAK,CAC3B,gBAAe;YACN,SAAS,WAAW,IAAI,EAAE;IACnC,MAAM,sBACJ,SAAS,SAAS,SAAS,WAAW,CAAC;AACzC,mBAAe,KAAK,IAAI,WAAW,QAAQ,oBAAoB;;AAEjE,OAAI,iBAAiB,EACnB;AAEF,aA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