|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as testUtil from './util'; |
| 4 | +import * as vscode from 'vscode'; |
| 5 | +import * as edit from '../../../edit'; |
| 6 | +import * as docMirror from '../../../doc-mirror'; |
| 7 | + |
| 8 | +const suiteName = 'Edit Replace Suite'; |
| 9 | + |
| 10 | +const targetFilePath = path.join(testUtil.testDataDir, 'edit-replace-target.clj'); |
| 11 | +const otherFilePath = path.join(testUtil.testDataDir, 'reformattable.clj'); |
| 12 | + |
| 13 | +suite(suiteName, function () { |
| 14 | + teardown(async function () { |
| 15 | + // Revert target file to original content |
| 16 | + const targetDoc = await vscode.workspace.openTextDocument(vscode.Uri.file(targetFilePath)); |
| 17 | + const fullRange = new vscode.Range( |
| 18 | + new vscode.Position(0, 0), |
| 19 | + targetDoc.positionAt(targetDoc.getText().length) |
| 20 | + ); |
| 21 | + const editor = await vscode.window.showTextDocument(targetDoc); |
| 22 | + await editor.edit((builder) => { |
| 23 | + builder.replace(fullRange, '(ns edit-replace-target)\n\n(def a 1)\n\n(def b 2)\n'); |
| 24 | + }); |
| 25 | + await targetDoc.save(); |
| 26 | + }); |
| 27 | + |
| 28 | + test('should apply edit to the passed editor, not activeTextEditor', async function () { |
| 29 | + // Open the target file in Column Two — capture a fresh editor reference |
| 30 | + const targetDoc = await vscode.workspace.openTextDocument(vscode.Uri.file(targetFilePath)); |
| 31 | + const targetEditor = await vscode.window.showTextDocument(targetDoc, { |
| 32 | + viewColumn: vscode.ViewColumn.Two, |
| 33 | + preview: false, |
| 34 | + }); |
| 35 | + |
| 36 | + // Wait for the mirror doc to be available for the target file |
| 37 | + await testUtil.waitForCondition( |
| 38 | + () => { |
| 39 | + try { |
| 40 | + docMirror.getDocument(targetDoc); |
| 41 | + return true; |
| 42 | + } catch { |
| 43 | + return false; |
| 44 | + } |
| 45 | + }, |
| 46 | + 4000, |
| 47 | + 50, |
| 48 | + 'Timed out waiting for mirror document for target file' |
| 49 | + ); |
| 50 | + |
| 51 | + // Show the other file in Column One — explicitly setting viewColumn |
| 52 | + // ensures it opens in a different group and becomes activeTextEditor |
| 53 | + const otherDoc = await vscode.workspace.openTextDocument(vscode.Uri.file(otherFilePath)); |
| 54 | + await vscode.window.showTextDocument(otherDoc, { |
| 55 | + viewColumn: vscode.ViewColumn.One, |
| 56 | + preview: false, |
| 57 | + }); |
| 58 | + await testUtil.waitForCondition( |
| 59 | + () => vscode.window.activeTextEditor?.document.uri.fsPath === otherFilePath, |
| 60 | + 4000, |
| 61 | + 50, |
| 62 | + 'Timed out waiting for other file to become active' |
| 63 | + ); |
| 64 | + |
| 65 | + // Confirm precondition: active editor is NOT the target file |
| 66 | + const activeEditor = vscode.window.activeTextEditor; |
| 67 | + assert.ok(activeEditor, 'There should be an active editor'); |
| 68 | + assert.strictEqual( |
| 69 | + activeEditor.document.uri.fsPath, |
| 70 | + otherFilePath, |
| 71 | + 'Precondition: active editor should be the other file' |
| 72 | + ); |
| 73 | + |
| 74 | + const targetContentBefore = targetDoc.getText(); |
| 75 | + const otherContentBefore = activeEditor.document.getText(); |
| 76 | + |
| 77 | + // Call edit.replace with the target editor (which is NOT activeTextEditor) |
| 78 | + const range = new vscode.Range( |
| 79 | + new vscode.Position(2, 0), |
| 80 | + new vscode.Position(2, '(def a 1)'.length) |
| 81 | + ); |
| 82 | + const result = await edit.replace(targetEditor, range, '(def a 42)', { |
| 83 | + skipFormat: true, |
| 84 | + }); |
| 85 | + |
| 86 | + assert.strictEqual(result, true, 'edit.replace should return true'); |
| 87 | + |
| 88 | + // The edit should have gone to the target file |
| 89 | + const targetContentAfter = targetDoc.getText(); |
| 90 | + assert.ok( |
| 91 | + targetContentAfter.includes('(def a 42)'), |
| 92 | + `Target file should contain the replacement text. Got: ${targetContentAfter}` |
| 93 | + ); |
| 94 | + assert.ok( |
| 95 | + !targetContentAfter.includes('(def a 1)'), |
| 96 | + `Target file should no longer contain the original text. Got: ${targetContentAfter}` |
| 97 | + ); |
| 98 | + |
| 99 | + // The active editor's file should be unchanged |
| 100 | + const otherContentAfter = activeEditor.document.getText(); |
| 101 | + assert.strictEqual( |
| 102 | + otherContentAfter, |
| 103 | + otherContentBefore, |
| 104 | + 'Active editor content should be unchanged' |
| 105 | + ); |
| 106 | + }); |
| 107 | +}); |
0 commit comments