diff --git a/src/commands/select.ts b/src/commands/select.ts index ea43211..e7a3c33 100644 --- a/src/commands/select.ts +++ b/src/commands/select.ts @@ -18,6 +18,30 @@ import { preview } from "./preview"; const cursorInitChar = 5; // 5 characters for "/000 " +function closeOldDocument(oldUri: vscode.Uri) { + // Close the old oil document after opening the new file + setTimeout(async () => { + try { + // Find and close the old document tab + const tabs = vscode.window.tabGroups.all.flatMap((group) => group.tabs); + const oldTab = tabs.find( + (tab) => + tab.input instanceof vscode.TabInputText && + tab.input.uri.toString() === oldUri.toString() + ); + if (oldTab) { + await vscode.window.tabGroups.close(oldTab); + } + } catch (error) { + // Fallback method if tab API fails + await vscode.window.showTextDocument(oldUri); + await vscode.commands.executeCommand( + "workbench.action.revertAndCloseActiveEditor" + ); + } + }, 50); +} + export async function select({ overRideLineText, overRideTargetPath, @@ -104,18 +128,13 @@ export async function select({ const newDoc = await vscode.workspace.openTextDocument(newUri); await vscode.languages.setTextDocumentLanguage(newDoc, "oil"); - // Show the new document in the same editor const editor = await vscode.window.showTextDocument(newDoc, { viewColumn: viewColumn || activeEditor.viewColumn, preview: false, }); - if (!viewColumn) { - // Close the old document - await vscode.window.showTextDocument(oldUri); - await vscode.commands.executeCommand( - "workbench.action.revertAndCloseActiveEditor" - ); + // Close the old document after the new one is shown + closeOldDocument(oldUri); } // Position cursor appropriately @@ -220,12 +239,12 @@ export async function select({ const fileUri = vscode.Uri.file(targetPath); const fileDoc = await vscode.workspace.openTextDocument(fileUri); const viewColumnToUse = viewColumn || activeEditor.viewColumn; + if (!viewColumn) { - await vscode.window.showTextDocument(activeEditor.document.uri); - await vscode.commands.executeCommand( - "workbench.action.revertAndCloseActiveEditor" - ); + // Close the old oil document after opening the new file + closeOldDocument(activeEditor.document.uri); } + // For different column, show in new column (old one stays open) await vscode.window.showTextDocument(fileDoc, { viewColumn: viewColumnToUse, preview: false, diff --git a/src/decorations.ts b/src/decorations.ts index 29feb96..d2b229e 100644 --- a/src/decorations.ts +++ b/src/decorations.ts @@ -4,11 +4,7 @@ import { getNerdFontFileIcon } from "./nerd-fonts"; // Create decoration type for hidden prefix const hiddenPrefixDecoration = vscode.window.createTextEditorDecorationType({ - textDecoration: "none; display: none", - opacity: "0", - letterSpacing: "-100em", - fontWeight: "normal", - fontStyle: "normal", + textDecoration: "none; font-size: 0pt", rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed, }); diff --git a/src/test/extension.test.ts b/src/test/extension.test.ts index 9408d37..2104d0b 100644 --- a/src/test/extension.test.ts +++ b/src/test/extension.test.ts @@ -340,6 +340,7 @@ suite("oil.code", () => { const filePosition = new vscode.Position(2, 0); editor.selection = new vscode.Selection(filePosition, filePosition); await vscode.commands.executeCommand("oil-code.select"); + await sleep(200); const mockFileContent = `mock file content`; await vscode.window.activeTextEditor?.edit((editBuilder) => { @@ -352,7 +353,9 @@ suite("oil.code", () => { // Move cursor to the file name const position = new vscode.Position(2, 0); - editor.selection = new vscode.Selection(position, position); + const editor2 = vscode.window.activeTextEditor; + assert.ok(editor2, "No active editor2"); + editor2.selection = new vscode.Selection(position, position); // Cut selection await vscode.commands.executeCommand("editor.action.deleteLines"); @@ -362,17 +365,19 @@ suite("oil.code", () => { editor.selection = new vscode.Selection(position3, position3); await vscode.commands.executeCommand("oil-code.select"); - await sleep(100); + await sleep(200); - const editor2 = vscode.window.activeTextEditor; - assert.ok(editor2, "No active editor"); - editor2.edit((editBuilder) => { + const editor3 = vscode.window.activeTextEditor; + assert.ok(editor3, "No active editor3"); + editor3.edit((editBuilder) => { editBuilder.insert(new vscode.Position(0, 8), newline); editBuilder.insert(new vscode.Position(1, 0), `/002 oil-file.md`); }); await saveFile(); + await sleep(100); + await waitForDocumentText(["/000 ../", "/003 oil-file.md"]); await assertProjectFileStructure(["sub-dir/", " oil-file.md"]); @@ -426,7 +431,7 @@ suite("oil.code", () => { editor.selection = new vscode.Selection(position3, position3); await vscode.commands.executeCommand("oil-code.select"); - await sleep(100); + await sleep(200); const editor2 = vscode.window.activeTextEditor; assert.ok(editor2, "No active editor"); @@ -475,7 +480,7 @@ suite("oil.code", () => { new vscode.Position(1, 0) ); await vscode.commands.executeCommand("oil-code.select"); - await sleep(100); + await sleep(200); const editor2 = vscode.window.activeTextEditor; assert.ok(editor2, "No active editor"); @@ -529,7 +534,7 @@ suite("oil.code", () => { new vscode.Position(1, 0) ); await vscode.commands.executeCommand("oil-code.select"); - await sleep(100); + await sleep(200); const editor2 = vscode.window.activeTextEditor; assert.ok(editor2, "No active editor"); diff --git a/src/test/utils/saveFile.ts b/src/test/utils/saveFile.ts index 383bc72..e8359d0 100644 --- a/src/test/utils/saveFile.ts +++ b/src/test/utils/saveFile.ts @@ -1,10 +1,11 @@ import * as vscode from "vscode"; +import { sleep } from "./sleep"; export async function saveFile() { - await new Promise((resolve) => setTimeout(resolve, 100)); + await sleep(200); await vscode.commands.executeCommand("workbench.action.files.save"); // Give the file save operation time to complete - await new Promise((resolve) => setTimeout(resolve, 300)); + await sleep(400); }