Skip to content

Commit

Permalink
fix: skip window changed when target window id is 1000 (#1667)
Browse files Browse the repository at this point in the history
* fix: skip window changed when target window id is 1000

This event is triggered by our layout sync, skip it

* chore: more concise code
  • Loading branch information
xiyaowong committed Nov 30, 2023
1 parent 8f6447c commit 56ca01f
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/buffer_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { config } from "./config";
import { EventBusData, eventBus } from "./eventBus";
import { createLogger } from "./logger";
import { MainController } from "./main_controller";
import { ManualPromise, callAtomic, convertByteNumToCharNum, disposeAll } from "./utils";
import { ManualPromise, callAtomic, convertByteNumToCharNum, disposeAll, wait } from "./utils";

// !Note: document and editors in vscode events and namespace are reference stable
// ! Integration notes:
Expand Down Expand Up @@ -340,25 +340,28 @@ export class BufferManager implements Disposable {
}

private handleWindowChanged = async (winId: number): Promise<void> => {
logger.debug(`onWindowChanged, target window id: ${winId}`);
logger.debug(`window changed, target window id: ${winId}`);
if (winId === 1000) {
// This event is triggered by our layout sync, skip it
logger.debug("window id is 1000, skipping");
return;
}

const returnToActiveEditor = async () => {
if (window.activeTextEditor) {
await window.showTextDocument(window.activeTextEditor.document, window.activeTextEditor.viewColumn);
}
const e = window.activeTextEditor;
if (e) await window.showTextDocument(e.document, e.viewColumn);
};

let targetEditor = this.getEditorFromWinId(winId);
if (!targetEditor) {
logger.debug(`target editor not found <check 1>, return to active editor`);
await returnToActiveEditor();
return;
return returnToActiveEditor();
}
if (window.activeTextEditor === targetEditor) return;
// since the event could be triggered by vscode side operations
// we need to wait a bit to let vscode finish its internal operations
// then check if the target editor is still the same
await new Promise((res) => setTimeout(res, 50));
await wait(50);
this.syncLayoutPromise && (await this.syncLayoutPromise.promise);
this.syncActiveEditorPromise && (await this.syncActiveEditorPromise.promise);
// triggered by vscode side operations
Expand All @@ -372,8 +375,7 @@ export class BufferManager implements Disposable {
targetEditor = this.getEditorFromWinId(curwin);
if (!targetEditor) {
logger.debug(`target editor not found <check 2>, return to active editor`);
returnToActiveEditor();
return;
return returnToActiveEditor();
}
if (window.activeTextEditor === targetEditor) return;
await this.main.cursorManager.waitForCursorUpdate(targetEditor);
Expand All @@ -391,7 +393,7 @@ export class BufferManager implements Disposable {
// 1. jump to target notebook
await window.showTextDocument(targetEditor.document, targetNotebook.viewColumn);
// wait a bit to let vscode finish its internal operations
await new Promise((res) => setTimeout(res, 50));
await wait(50);
// 2. jump to target cell
await window.showTextDocument(targetEditor.document, targetEditor.viewColumn);
return;
Expand Down

0 comments on commit 56ca01f

Please sign in to comment.