From cf726ea29be99364e0197bd360a34cb1a8a244f7 Mon Sep 17 00:00:00 2001 From: rebornix Date: Mon, 4 May 2020 10:04:40 -0700 Subject: [PATCH] remove dep on EditorModel from NotebookEditor --- .../notebook/browser/notebookEditor.ts | 4 +- .../browser/viewModel/notebookViewModel.ts | 50 +++++++++---------- .../contrib/notebook/common/notebookCommon.ts | 5 -- .../notebook/common/notebookEditorModel.ts | 35 +------------ .../notebook/test/notebookViewModel.test.ts | 2 +- .../notebook/test/testNotebookEditor.ts | 33 +----------- 6 files changed, 29 insertions(+), 100 deletions(-) diff --git a/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts b/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts index a786c472aba..56aca7586fd 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts @@ -417,7 +417,7 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor { await super.setInput(input, options, token); const model = await input.resolve(); - if (this.notebookViewModel === undefined || !this.notebookViewModel.equal(model) || this.webview === null) { + if (this.notebookViewModel === undefined || !this.notebookViewModel.equal(model.notebook) || this.webview === null) { this.detachModel(); await this.attachModel(input, model); } @@ -477,7 +477,7 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor { await this.webview.waitForInitialization(); this.eventDispatcher = new NotebookEventDispatcher(); - this.viewModel = this.instantiationService.createInstance(NotebookViewModel, input.viewType!, model, this.eventDispatcher, this.getLayoutInfo()); + this.viewModel = this.instantiationService.createInstance(NotebookViewModel, input.viewType!, model.notebook, this.eventDispatcher, this.getLayoutInfo()); this.eventDispatcher.emit([new NotebookLayoutChangedEvent({ width: true, fontInfo: true }, this.getLayoutInfo())]); this.updateForMetadata(); diff --git a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts index 0dc084c621e..142ac8e24b7 100644 --- a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts +++ b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts @@ -18,7 +18,6 @@ import { ModelDecorationOptions } from 'vs/editor/common/model/textModel'; import { WorkspaceTextEdit } from 'vs/editor/common/modes'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo'; -import { NotebookEditorModel } from 'vs/workbench/contrib/notebook/common/notebookEditorModel'; import { CellEditState, CellFindMatch, ICellRange, ICellViewModel, NotebookLayoutInfo } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; import { DeleteCellEdit, InsertCellEdit, MoveCellEdit, SpliceCellsEdit } from 'vs/workbench/contrib/notebook/browser/viewModel/cellEdit'; import { CodeCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/codeCellViewModel'; @@ -26,8 +25,9 @@ import { NotebookEventDispatcher, NotebookMetadataChangedEvent } from 'vs/workbe import { CellFoldingState, EditorFoldingStateDelegate } from 'vs/workbench/contrib/notebook/browser/contrib/fold/foldingModel'; import { MarkdownCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/markdownCellViewModel'; import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel'; -import { CellKind, INotebookEditorModel } from 'vs/workbench/contrib/notebook/common/notebookCommon'; +import { CellKind } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { FoldingRegions } from 'vs/editor/contrib/folding/foldingRanges'; +import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel'; export interface INotebookEditorViewState { editingCells: { [key: number]: boolean }; @@ -172,27 +172,27 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD } get notebookDocument() { - return this._model.notebook; + return this._notebook; } get renderers() { - return this._model.notebook!.renderers; + return this._notebook!.renderers; } get handle() { - return this._model.notebook.handle; + return this._notebook.handle; } get languages() { - return this._model.notebook.languages; + return this._notebook.languages; } get uri() { - return this._model.notebook.uri; + return this._notebook.uri; } get metadata() { - return this._model.notebook.metadata; + return this._notebook.metadata; } private readonly _onDidChangeViewCells = new Emitter(); @@ -227,7 +227,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD } this._selections = selections; - this._model.notebook.selections = selections; + this._notebook.selections = selections; this._onDidChangeSelection.fire(); } @@ -241,7 +241,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD constructor( public viewType: string, - private _model: INotebookEditorModel, + private _notebook: NotebookTextModel, readonly eventDispatcher: NotebookEventDispatcher, private _layoutInfo: NotebookLayoutInfo | null, @IInstantiationService private readonly instantiationService: IInstantiationService, @@ -254,7 +254,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD this.id = '$notebookViewModel' + MODEL_ID; this._instanceId = strings.singleLetterHash(MODEL_ID); - this._register(this._model.onDidChangeCells(e => { + this._register(this._notebook.onDidChangeCells(e => { const diffs = e.map(splice => { return [splice[0], splice[1], splice[2].map(cell => { return createCellViewModel(this.instantiationService, this, cell as NotebookCellTextModel); @@ -315,7 +315,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD this.selectionHandles = endSelectionHandles; })); - this._register(this._model.notebook.onDidChangeMetadata(e => { + this._register(this._notebook.onDidChangeMetadata(e => { this.eventDispatcher.emit([new NotebookMetadataChangedEvent(e)]); })); @@ -335,7 +335,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD }); })); - this._viewCells = this._model!.notebook!.cells.map(cell => { + this._viewCells = this._notebook!.cells.map(cell => { return createCellViewModel(this.instantiationService, this, cell); }); @@ -419,10 +419,6 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD return this._hiddenRanges; } - isDirty() { - return this._model.isDirty(); - } - hide() { this._viewCells.forEach(cell => { if (cell.getText() !== '') { @@ -465,7 +461,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD } getVersionId() { - return this._model.notebook.versionId; + return this._notebook.versionId; } getTrackedRange(id: string): ICellRange | null { @@ -580,7 +576,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD private _insertCellDelegate(insertIndex: number, insertCell: CellViewModel) { this._viewCells!.splice(insertIndex, 0, insertCell); this._handleToViewCellMapping.set(insertCell.handle, insertCell); - this._model.insertCell(insertCell.model, insertIndex); + this._notebook.insertNewCell(insertIndex, [insertCell.model as NotebookCellTextModel]); this._localStore.add(insertCell); this._onDidChangeViewCells.fire({ synchronous: true, splices: [[insertIndex, 0, [insertCell]]] }); } @@ -590,7 +586,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD this._viewCells.splice(deleteIndex, 1); this._handleToViewCellMapping.delete(deleteCell.handle); - this._model.deleteCell(deleteIndex); + this._notebook.removeCell(deleteIndex); this._onDidChangeViewCells.fire({ synchronous: true, splices: [[deleteIndex, 1, []]] }); } @@ -599,11 +595,11 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD } createCell(index: number, source: string[], language: string, type: CellKind, synchronous: boolean) { - const cell = this._model.notebook.createCellTextModel(source, language, type, [], undefined); + const cell = this._notebook.createCellTextModel(source, language, type, [], undefined); let newCell: CellViewModel = createCellViewModel(this.instantiationService, this, cell); this._viewCells!.splice(index, 0, newCell); this._handleToViewCellMapping.set(newCell.handle, newCell); - this._model.insertCell(cell, index); + this._notebook.insertNewCell(index, [cell]); this._localStore.add(newCell); this.undoService.pushElement(new InsertCellEdit(this.uri, index, newCell, { @@ -622,7 +618,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD this._viewCells!.splice(index, 0, newCell); this._handleToViewCellMapping.set(newCell.handle, newCell); - this._model.insertCell(newCell.model, index); + this._notebook.insertNewCell(index, [newCell.model]); this._localStore.add(newCell); this.undoService.pushElement(new InsertCellEdit(this.uri, index, newCell, { insertCell: this._insertCellDelegate.bind(this), @@ -642,7 +638,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD this._viewCells.splice(index, 1); this._handleToViewCellMapping.delete(viewCell.handle); - this._model.deleteCell(index); + this._notebook.removeCell(index); let endSelections: number[] = []; if (this.selectionHandles.length) { @@ -686,7 +682,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD this.viewCells.splice(index, 1); this.viewCells!.splice(newIdx, 0, viewCell); - this._model.moveCellToIdx(index, newIdx); + this._notebook.moveCellToIdx(index, newIdx); if (pushedToUndoStack) { this.undoService.pushElement(new MoveCellEdit(this.uri, index, newIdx, { @@ -869,8 +865,8 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD this.undoService.redo(this.uri); } - equal(model: NotebookEditorModel) { - return this._model === model; + equal(notebook: NotebookTextModel) { + return this._notebook === notebook; } dispose() { diff --git a/src/vs/workbench/contrib/notebook/common/notebookCommon.ts b/src/vs/workbench/contrib/notebook/common/notebookCommon.ts index 3b31a6fef7c..620f9df6f97 100644 --- a/src/vs/workbench/contrib/notebook/common/notebookCommon.ts +++ b/src/vs/workbench/contrib/notebook/common/notebookCommon.ts @@ -466,11 +466,6 @@ export const NOTEBOOK_EDITOR_CURSOR_BOUNDARY = new RawContextKey<'none' | 'top' export interface INotebookEditorModel extends IEditorModel { notebook: NotebookTextModel; - onDidChangeCells: Event; isDirty(): boolean; - getNotebook(): NotebookTextModel; - insertCell(cell: ICell, index: number): void; - deleteCell(index: number): void; - moveCellToIdx(index: number, newIdx: number): void; save(): Promise; } diff --git a/src/vs/workbench/contrib/notebook/common/notebookEditorModel.ts b/src/vs/workbench/contrib/notebook/common/notebookEditorModel.ts index 8f5e0aa2d86..aba2c01bb23 100644 --- a/src/vs/workbench/contrib/notebook/common/notebookEditorModel.ts +++ b/src/vs/workbench/contrib/notebook/common/notebookEditorModel.ts @@ -5,7 +5,7 @@ import { EditorModel, IRevertOptions } from 'vs/workbench/common/editor'; import { Emitter, Event } from 'vs/base/common/event'; -import { ICell, NotebookCellTextModelSplice, INotebookEditorModel } from 'vs/workbench/contrib/notebook/common/notebookCommon'; +import { ICell, INotebookEditorModel } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel'; import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel'; import { Disposable, IDisposable, dispose } from 'vs/base/common/lifecycle'; @@ -29,11 +29,8 @@ export class NotebookEditorModel extends EditorModel implements IWorkingCopy, IN private _dirty = false; protected readonly _onDidChangeDirty = this._register(new Emitter()); readonly onDidChangeDirty = this._onDidChangeDirty.event; - private readonly _onDidChangeCells = new Emitter(); - get onDidChangeCells(): Event { return this._onDidChangeCells.event; } private readonly _onDidChangeContent = this._register(new Emitter()); readonly onDidChangeContent: Event = this._onDidChangeContent.event; - private _notebook!: NotebookTextModel; get notebook() { @@ -78,12 +75,6 @@ export class NotebookEditorModel extends EditorModel implements IWorkingCopy, IN this._onDidChangeContent.fire(); })); - if (this._notebook.onDidChangeCells) { - this._register(this._notebook.onDidChangeCells((e) => { - this._onDidChangeCells.fire(e); - })); - } - return this; } @@ -91,30 +82,6 @@ export class NotebookEditorModel extends EditorModel implements IWorkingCopy, IN return this._dirty; } - getNotebook(): NotebookTextModel { - return this._notebook; - } - - insertCell(cell: ICell, index: number) { - let notebook = this.getNotebook(); - if (notebook) { - this.notebook.insertNewCell(index, [cell as NotebookCellTextModel]); - this._dirty = true; - this._onDidChangeDirty.fire(); - } - } - - deleteCell(index: number) { - let notebook = this.getNotebook(); - if (notebook) { - this.notebook.removeCell(index); - } - } - - moveCellToIdx(index: number, newIdx: number) { - this.notebook.moveCellToIdx(index, newIdx); - } - async save(): Promise { await this.notebookService.save(this.notebook.viewType, this.notebook.uri); this._dirty = false; diff --git a/src/vs/workbench/contrib/notebook/test/notebookViewModel.test.ts b/src/vs/workbench/contrib/notebook/test/notebookViewModel.test.ts index a1b399c52e6..4acc1060be1 100644 --- a/src/vs/workbench/contrib/notebook/test/notebookViewModel.test.ts +++ b/src/vs/workbench/contrib/notebook/test/notebookViewModel.test.ts @@ -26,7 +26,7 @@ suite('NotebookViewModel', () => { const notebook = new NotebookTextModel(0, 'notebook', URI.parse('test')); const model = new NotebookEditorTestModel(notebook); const eventDispatcher = new NotebookEventDispatcher(); - const viewModel = new NotebookViewModel('notebook', model, eventDispatcher, null, instantiationService, blukEditService, undoRedoService); + const viewModel = new NotebookViewModel('notebook', model.notebook, eventDispatcher, null, instantiationService, blukEditService, undoRedoService); assert.equal(viewModel.viewType, 'notebook'); }); diff --git a/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts b/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts index 47e46ec2cf2..7a757b8447b 100644 --- a/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts +++ b/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts @@ -4,7 +4,7 @@ *--------------------------------------------------------------------------------------------*/ import { URI } from 'vs/base/common/uri'; -import { CellKind, IOutput, CellUri, NotebookCellMetadata, NotebookCellTextModelSplice, ICell, INotebookEditorModel } from 'vs/workbench/contrib/notebook/common/notebookCommon'; +import { CellKind, IOutput, CellUri, NotebookCellMetadata, ICell, INotebookEditorModel } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { NotebookViewModel, IModelDecorationsChangeAccessor, CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { INotebookEditor, NotebookLayoutInfo, ICellViewModel, ICellRange, INotebookEditorMouseEvent, INotebookEditorContribution } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; @@ -208,9 +208,6 @@ export class NotebookEditorTestModel extends EditorModel implements INotebookEdi protected readonly _onDidChangeDirty = this._register(new Emitter()); readonly onDidChangeDirty = this._onDidChangeDirty.event; - private readonly _onDidChangeCells = new Emitter(); - get onDidChangeCells(): Event { return this._onDidChangeCells.event; } - private readonly _onDidChangeContent = this._register(new Emitter()); readonly onDidChangeContent: Event = this._onDidChangeContent.event; @@ -230,9 +227,6 @@ export class NotebookEditorTestModel extends EditorModel implements INotebookEdi this._onDidChangeDirty.fire(); this._onDidChangeContent.fire(); })); - this._register(_notebook.onDidChangeCells((e) => { - this._onDidChangeCells.fire(e); - })); } } @@ -244,29 +238,6 @@ export class NotebookEditorTestModel extends EditorModel implements INotebookEdi return this._notebook; } - insertCell(cell: ICell, index: number) { - let notebook = this.getNotebook(); - - if (notebook) { - this.notebook.insertNewCell(index, [cell as NotebookCellTextModel]); - this._dirty = true; - this._onDidChangeDirty.fire(); - - } - } - - deleteCell(index: number) { - let notebook = this.getNotebook(); - - if (notebook) { - this.notebook.removeCell(index); - } - } - - moveCellToIdx(index: number, newIdx: number) { - this.notebook.moveCellToIdx(index, newIdx); - } - async save(): Promise { if (this._notebook) { this._dirty = false; @@ -288,7 +259,7 @@ export function withTestNotebook(instantiationService: IInstantiationService, bl }); const model = new NotebookEditorTestModel(notebook); const eventDispatcher = new NotebookEventDispatcher(); - const viewModel = new NotebookViewModel(viewType, model, eventDispatcher, null, instantiationService, blukEditService, undoRedoService); + const viewModel = new NotebookViewModel(viewType, model.notebook, eventDispatcher, null, instantiationService, blukEditService, undoRedoService); callback(editor, viewModel, notebook); -- GitLab