|
| 1 | +import * as assert from "node:assert/strict"; |
| 2 | +import type { |
| 3 | + Disposable, |
| 4 | + TextDocumentChangeEvent, |
| 5 | +} from "@cursorless/lib-common"; |
| 6 | +import { |
| 7 | + FakeIDE, |
| 8 | + InMemoryTextEditor, |
| 9 | + Range, |
| 10 | + Selection, |
| 11 | +} from "@cursorless/lib-common"; |
| 12 | +import { RangeUpdater } from "./RangeUpdater"; |
| 13 | +import { performEditsAndUpdateSelections } from "./updateSelections"; |
| 14 | + |
| 15 | +suite("performEditsAndUpdateSelections", () => { |
| 16 | + test("treats CRLF-normalized replace inserts as replace edits", async () => { |
| 17 | + const ide = new TestIDE(); |
| 18 | + const editor = new InMemoryTextEditor({ |
| 19 | + ide, |
| 20 | + content: "abc\r\ndef", |
| 21 | + }); |
| 22 | + const rangeUpdater = new RangeUpdater(ide); |
| 23 | + const selection = new Selection(0, 3, 0, 3); |
| 24 | + |
| 25 | + const { updated } = await performEditsAndUpdateSelections({ |
| 26 | + rangeUpdater, |
| 27 | + editor, |
| 28 | + edits: [ |
| 29 | + { |
| 30 | + range: new Range(0, 3, 0, 3), |
| 31 | + text: "\n", |
| 32 | + isReplace: true, |
| 33 | + }, |
| 34 | + ], |
| 35 | + preserveCursorSelections: true, |
| 36 | + selections: { updated: [selection] }, |
| 37 | + }); |
| 38 | + |
| 39 | + assert.equal(editor.document.getText(), "abc\r\n\r\ndef"); |
| 40 | + assert.equal(updated.length, 1); |
| 41 | + assert.equal(updated[0].anchor.line, 0); |
| 42 | + assert.equal(updated[0].anchor.character, 3); |
| 43 | + assert.equal(updated[0].active.line, 0); |
| 44 | + assert.equal(updated[0].active.character, 3); |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +class TestIDE extends FakeIDE { |
| 49 | + private textDocumentChangeListeners: (( |
| 50 | + event: TextDocumentChangeEvent, |
| 51 | + ) => void)[] = []; |
| 52 | + |
| 53 | + override onDidChangeTextDocument = ( |
| 54 | + listener: (event: TextDocumentChangeEvent) => void, |
| 55 | + ): Disposable => { |
| 56 | + this.textDocumentChangeListeners.push(listener); |
| 57 | + |
| 58 | + return { |
| 59 | + dispose: () => { |
| 60 | + this.textDocumentChangeListeners = |
| 61 | + this.textDocumentChangeListeners.filter( |
| 62 | + (candidate) => candidate !== listener, |
| 63 | + ); |
| 64 | + }, |
| 65 | + }; |
| 66 | + }; |
| 67 | + |
| 68 | + override emitDidChangeTextDocument = (event: TextDocumentChangeEvent) => { |
| 69 | + this.textDocumentChangeListeners.forEach((listener) => listener(event)); |
| 70 | + }; |
| 71 | +} |
0 commit comments