* Moves the characters from `start` and `end` to `index`. * * `affinity` controls where the range is anchored at `index`. With the * default `'right'`, it is inserted before the content that starts at `index`; * with `'left'`, it is inserted after the content that ends at `index`. The
(start: number, end: number, index: number, affinity: 'left' | 'right' = 'right')
| 595 | * mirroring the `appendLeft`/`appendRight` distinction. |
| 596 | */ |
| 597 | move(start: number, end: number, index: number, affinity: 'left' | 'right' = 'right'): this { |
| 598 | start = this._resolveIndex(start) |
| 599 | end = this._resolveIndex(end) |
| 600 | index = this._resolveIndex(index) |
| 601 | |
| 602 | if (start === end) |
| 603 | return this |
| 604 | |
| 605 | if (start < 0 || end > this.original.length || index < 0 || index > this.original.length) { |
| 606 | throw new MagicStringError(`range ${start}–${end} is out of bounds`) |
| 607 | } |
| 608 | if (start > end) { |
| 609 | throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`) |
| 610 | } |
| 611 | if (index >= start && index <= end) { |
| 612 | throw new MagicStringError('cannot move a selection inside itself') |
| 613 | } |
| 614 | |
| 615 | this._split(start) |
| 616 | this._split(end) |
| 617 | this._split(index) |
| 618 | |
| 619 | const first = this.byStart.get(start) |
| 620 | const last = this.byEnd.get(end) |
| 621 | if (!first || !last) { |
| 622 | throw new MagicStringError(`range ${start}–${end} is out of bounds`) |
| 623 | } |
| 624 | |
| 625 | // The splicing below assumes the chunks spanning [start, end) are still a |
| 626 | // forward run in the current list. An earlier move can have interleaved a |
| 627 | // chunk from outside the range, or put `last` before `first`, and then the |
| 628 | // pointer rewrites produce a cycle rather than an error, so toString() and |
| 629 | // generateMap() loop forever. |
| 630 | // |
| 631 | // Only move() reorders chunks, so this is skipped until one has run, which |
| 632 | // keeps the common single-move case free of the walk. |
| 633 | if (this.hasMovedChunks) { |
| 634 | let cursor: Chunk | null = first |
| 635 | while (cursor !== last) { |
| 636 | cursor = cursor.next |
| 637 | if (!cursor || cursor.start < start || cursor.end > end) { |
| 638 | throw new MagicStringError( |
| 639 | `cannot move ${start} to ${end} because an earlier move split that range`, |
| 640 | ) |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | const oldLeft = first.previous |
| 646 | const oldRight = last.next |
| 647 | |
| 648 | // Anchor the range at `index` according to `affinity`: `'right'` puts it |
| 649 | // before the chunk starting at `index`, `'left'` after the chunk ending at |
| 650 | // `index`. When no such anchor exists (`index` is at an end of the string) |
| 651 | // the range falls to that end. |
| 652 | // |
| 653 | // The early returns catch the cases where the range is already in place: |
| 654 | // splicing it in next to itself would link the chunk list back on itself |
nothing calls this directly
no test coverage detected