Skip to content
41 changes: 30 additions & 11 deletions src/commands/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 1 addition & 5 deletions src/decorations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down
21 changes: 13 additions & 8 deletions src/test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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");
Expand All @@ -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"]);
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
5 changes: 3 additions & 2 deletions src/test/utils/saveFile.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Loading