From fe18860535d6298eb19288139ecbce000c226884 Mon Sep 17 00:00:00 2001 From: zhanba Date: Wed, 31 Jan 2024 18:44:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=8C=E5=96=84=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/common/vscodeAdaptor/convertor.ts | 4 +- .../common/vscodeAdaptor/libroWorkspace.ts | 48 +++++++++++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/packages/libro-language-client/src/common/vscodeAdaptor/convertor.ts b/packages/libro-language-client/src/common/vscodeAdaptor/convertor.ts index 32333bc08..f4dbd8f56 100644 --- a/packages/libro-language-client/src/common/vscodeAdaptor/convertor.ts +++ b/packages/libro-language-client/src/common/vscodeAdaptor/convertor.ts @@ -9,7 +9,7 @@ import { EndOfLine, NotebookCellKind, Uri } from './vscodeAdaptor.js'; export const l2c = { asNotebookDocument(libroView: LibroView): NotebookDocument { const model = libroView.model as any; - if (!model.filePath) { + if (model.filePath === undefined) { throw new Error('no filePath: invalid libro jupyter model'); } const filePath = model.filePath as string; @@ -32,7 +32,7 @@ export const l2c = { asNotebookCell(cell: CellView): NotebookCell { const model = cell.parent.model as any; - if (!model.filePath) { + if (model.filePath === undefined) { throw new Error('no filePath: invalid libro jupyter model'); } const filePath = model.filePath as string; diff --git a/packages/libro-language-client/src/common/vscodeAdaptor/libroWorkspace.ts b/packages/libro-language-client/src/common/vscodeAdaptor/libroWorkspace.ts index eb189f54e..3dedf10a6 100644 --- a/packages/libro-language-client/src/common/vscodeAdaptor/libroWorkspace.ts +++ b/packages/libro-language-client/src/common/vscodeAdaptor/libroWorkspace.ts @@ -1,3 +1,4 @@ +import type { LibroView } from '@difizen/libro-core'; import { LibroService } from '@difizen/libro-core'; import { inject, noop, singleton } from '@difizen/mana-app'; import type { @@ -37,6 +38,13 @@ import { unsupported } from './util.js'; export class LibroWorkspace implements ILibroWorkspace { @inject(LibroService) private readonly libroService: LibroService; + isValidNotebook(view: LibroView): boolean { + if ((view as any).lspEnabled === true) { + return true; + } + return false; + } + workspaceFolders: WorkspaceFolder[] | undefined = []; getWorkspaceFolder(uri: Uri): WorkspaceFolder | undefined { return; @@ -78,26 +86,57 @@ export class LibroWorkspace implements ILibroWorkspace { applyEdit(edit: WorkspaceEdit, metadata?: WorkspaceEditMetadata): Thenable { return Promise.resolve(true); } - onDidOpenNotebookDocument: Event = (listener) => { + onDidOpenNotebookDocument: Event = ( + listener, + thisArgs, + disposables, + ) => { const disposable = this.libroService.onNotebookViewCreated((libroView) => { + if (!this.isValidNotebook(libroView)) { + return; + } listener(l2c.asNotebookDocument(libroView)); }); + disposables?.push(disposable); return disposable; }; - onDidSaveNotebookDocument: Event = (listener) => { + onDidSaveNotebookDocument: Event = ( + listener, + thisArgs, + disposables, + ) => { const disposable = this.libroService.onNotebookViewSaved((libroView) => { + if (!this.isValidNotebook(libroView)) { + return; + } listener(l2c.asNotebookDocument(libroView)); }); + disposables?.push(disposable); return disposable; }; - onDidCloseNotebookDocument: Event = (listener) => { + onDidCloseNotebookDocument: Event = ( + listener, + thisArgs, + disposables, + ) => { const disposable = this.libroService.onNotebookViewClosed((libroView) => { + if (!this.isValidNotebook(libroView)) { + return; + } listener(l2c.asNotebookDocument(libroView)); }); + disposables?.push(disposable); return disposable; }; - onDidChangeNotebookDocument: Event = (listener) => { + onDidChangeNotebookDocument: Event = ( + listener, + thisArgs, + disposables, + ) => { const disposable = this.libroService.onNotebookViewChanged((e) => { + if (!this.isValidNotebook(e.libroView)) { + return; + } listener({ notebook: l2c.asNotebookDocument(e.libroView), metadata: {}, @@ -116,6 +155,7 @@ export class LibroWorkspace implements ILibroWorkspace { }), }); }); + disposables?.push(disposable); return disposable; };