Skip to content

Commit

Permalink
feat: close the editor with any event that cause active tab to change
Browse files Browse the repository at this point in the history
  • Loading branch information
nagy-nabil committed Mar 17, 2024
1 parent 95edce9 commit b59771a
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions src/commands/ui.show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import { storageFileName } from "../config";
*/
async function initEditor(
state: WorkspaceState,
fsEdirorUri: vscode.Uri
fsEditorUri: vscode.Uri
): Promise<void> {
// 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);
Expand All @@ -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) => {
Expand All @@ -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<void> {
async function cleanUp(_fsEdirorUri: vscode.Uri): Promise<void> {
onchangeDisposale.dispose();
changeVisibleDisposable.dispose();
await vscode.workspace.fs.delete(fsEdirorUri);
disposal.dispose();
}
}

Expand Down

0 comments on commit b59771a

Please sign in to comment.