diff --git a/src/commands/ui.show.ts b/src/commands/ui.show.ts index 17e365a..a765789 100644 --- a/src/commands/ui.show.ts +++ b/src/commands/ui.show.ts @@ -9,14 +9,14 @@ import { storageFileName } from "../config"; */ async function initEditor( state: WorkspaceState, - fsEdirorUri: vscode.Uri + fsEditorUri: vscode.Uri ): Promise { - // 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 + // 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 const wsEdit = new vscode.WorkspaceEdit(); - wsEdit.createFile(fsEdirorUri, { overwrite: true }); + wsEdit.createFile(fsEditorUri, { overwrite: true }); await vscode.workspace.applyEdit(wsEdit); // load file that is unique to the workspace - const doc = await vscode.workspace.openTextDocument(fsEdirorUri); + const doc = await vscode.workspace.openTextDocument(fsEditorUri); const editor = await vscode.window.showTextDocument(doc); // fill the editor with saved marks, from first position always const pos = new vscode.Position(0, 0); @@ -28,7 +28,7 @@ async function initEditor( } // todo i hate it -function assignListeners(state: WorkspaceState, fsEdirorUri: vscode.Uri): void { +function assignListeners(state: WorkspaceState, fsEditorUri: vscode.Uri): void { // enable autosave for our editor only const onchangeDisposale = vscode.workspace.onDidChangeTextDocument( async (tdoc) => { @@ -44,34 +44,33 @@ function assignListeners(state: WorkspaceState, fsEdirorUri: vscode.Uri): void { } ); - // visible textEditors are the one that shown in the screen not all tabs [the ones you can see their content] - const changeVisibleDisposable = vscode.window.onDidChangeVisibleTextEditors( - async (tdocs) => { - if ( - tdocs.findIndex((val) => - val.document.fileName.includes(storageFileName) - ) === -1 - ) { - await cleanUp(fsEdirorUri); + const disposal = vscode.window.onDidChangeActiveTextEditor( + async (textEditor) => { + console.log( + "active change", + textEditor, + textEditor?.document.fileName + ); + if (!textEditor) { + return; } - } - ); - - const closeEditorDisposable = vscode.workspace.onDidCloseTextDocument( - async (tdoc) => { - if (tdoc.fileName.includes(storageFileName)) { - await cleanUp(fsEdirorUri); + // 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 + if (textEditor.document.fileName.includes(storageFileName)) { + return; } + + const wsEdit = new vscode.WorkspaceEdit(); + wsEdit.deleteFile(fsEditorUri, { ignoreIfNotExists: true }); + await vscode.workspace.applyEdit(wsEdit); + await cleanUp(fsEditorUri); } ); - /** - * remove event listners and delete fs editor IN ORDER + * remove event listners */ - async function cleanUp(fsEdirorUri: vscode.Uri): Promise { + async function cleanUp(_fsEdirorUri: vscode.Uri): Promise { onchangeDisposale.dispose(); - changeVisibleDisposable.dispose(); - await vscode.workspace.fs.delete(fsEdirorUri); + disposal.dispose(); } }