Skip to content

Commit b59771a

Browse files
committed
feat: close the editor with any event that cause active tab to change
1 parent 95edce9 commit b59771a

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

src/commands/ui.show.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import { storageFileName } from "../config";
99
*/
1010
async function initEditor(
1111
state: WorkspaceState,
12-
fsEdirorUri: vscode.Uri
12+
fsEditorUri: vscode.Uri
1313
): Promise<void> {
14-
// cannot use workspace.fs.write because when calling openTextDocument might load old value of BookerEditors because it wasn't close yet from the workspace so need workspace edit to overwrite the document if exist
14+
// cannot use workspace.fs.write because when calling openTextDocument might load old value of BookerEditors because it wasn't closed yet from the workspace so need workspace edit to overwrite the document if exist
1515
const wsEdit = new vscode.WorkspaceEdit();
16-
wsEdit.createFile(fsEdirorUri, { overwrite: true });
16+
wsEdit.createFile(fsEditorUri, { overwrite: true });
1717
await vscode.workspace.applyEdit(wsEdit);
1818
// load file that is unique to the workspace
19-
const doc = await vscode.workspace.openTextDocument(fsEdirorUri);
19+
const doc = await vscode.workspace.openTextDocument(fsEditorUri);
2020
const editor = await vscode.window.showTextDocument(doc);
2121
// fill the editor with saved marks, from first position always
2222
const pos = new vscode.Position(0, 0);
@@ -28,7 +28,7 @@ async function initEditor(
2828
}
2929

3030
// todo i hate it
31-
function assignListeners(state: WorkspaceState, fsEdirorUri: vscode.Uri): void {
31+
function assignListeners(state: WorkspaceState, fsEditorUri: vscode.Uri): void {
3232
// enable autosave for our editor only
3333
const onchangeDisposale = vscode.workspace.onDidChangeTextDocument(
3434
async (tdoc) => {
@@ -44,34 +44,33 @@ function assignListeners(state: WorkspaceState, fsEdirorUri: vscode.Uri): void {
4444
}
4545
);
4646

47-
// visible textEditors are the one that shown in the screen not all tabs [the ones you can see their content]
48-
const changeVisibleDisposable = vscode.window.onDidChangeVisibleTextEditors(
49-
async (tdocs) => {
50-
if (
51-
tdocs.findIndex((val) =>
52-
val.document.fileName.includes(storageFileName)
53-
) === -1
54-
) {
55-
await cleanUp(fsEdirorUri);
47+
const disposal = vscode.window.onDidChangeActiveTextEditor(
48+
async (textEditor) => {
49+
console.log(
50+
"active change",
51+
textEditor,
52+
textEditor?.document.fileName
53+
);
54+
if (!textEditor) {
55+
return;
5656
}
57-
}
58-
);
59-
60-
const closeEditorDisposable = vscode.workspace.onDidCloseTextDocument(
61-
async (tdoc) => {
62-
if (tdoc.fileName.includes(storageFileName)) {
63-
await cleanUp(fsEdirorUri);
57+
// the event fires after the tab change so i want to check the new tab is not ours in this case close our tab if it still open
58+
if (textEditor.document.fileName.includes(storageFileName)) {
59+
return;
6460
}
61+
62+
const wsEdit = new vscode.WorkspaceEdit();
63+
wsEdit.deleteFile(fsEditorUri, { ignoreIfNotExists: true });
64+
await vscode.workspace.applyEdit(wsEdit);
65+
await cleanUp(fsEditorUri);
6566
}
6667
);
67-
6868
/**
69-
* remove event listners and delete fs editor IN ORDER
69+
* remove event listners
7070
*/
71-
async function cleanUp(fsEdirorUri: vscode.Uri): Promise<void> {
71+
async function cleanUp(_fsEdirorUri: vscode.Uri): Promise<void> {
7272
onchangeDisposale.dispose();
73-
changeVisibleDisposable.dispose();
74-
await vscode.workspace.fs.delete(fsEdirorUri);
73+
disposal.dispose();
7574
}
7675
}
7776

0 commit comments

Comments
 (0)