From c78c56245c505f65297ea87026120310a43fe8b9 Mon Sep 17 00:00:00 2001 From: rebornix Date: Fri, 11 Jun 2021 14:59:13 -0700 Subject: [PATCH] less use viewModel on NotebookEditor. --- .../clipboard/test/notebookClipboard.test.ts | 27 ++++++----------- .../browser/contrib/find/test/find.test.ts | 8 ++--- .../undoRedo/test/notebookUndoRedo.test.ts | 9 ++---- .../notebook/test/testNotebookEditor.ts | 30 ++++++++++++------- 4 files changed, 35 insertions(+), 39 deletions(-) diff --git a/src/vs/workbench/contrib/notebook/browser/contrib/clipboard/test/notebookClipboard.test.ts b/src/vs/workbench/contrib/notebook/browser/contrib/clipboard/test/notebookClipboard.test.ts index 48b60081550..3d656a42033 100644 --- a/src/vs/workbench/contrib/notebook/browser/contrib/clipboard/test/notebookClipboard.test.ts +++ b/src/vs/workbench/contrib/notebook/browser/contrib/clipboard/test/notebookClipboard.test.ts @@ -42,12 +42,11 @@ suite('Notebook Clipboard', () => { ['paragraph 1', 'markdown', CellKind.Markup, [], {}], ['paragraph 2', 'markdown', CellKind.Markup, [], {}], ], - async (editor, accessor) => { + async (editor, viewModel, accessor) => { accessor.stub(INotebookService, new class extends mock() { override setToCopy() { } }); const clipboardContrib = new NotebookClipboardContribution(createEditorService(editor)); - const viewModel = editor.viewModel; viewModel.updateSelectionsState({ kind: SelectionStateType.Index, focus: { start: 0, end: 2 }, selections: [{ start: 0, end: 2 }] }, 'model'); assert.ok(clipboardContrib.runCutAction(accessor)); assert.deepStrictEqual(viewModel.getFocus(), { start: 0, end: 1 }); @@ -67,8 +66,7 @@ suite('Notebook Clipboard', () => { ['# header d', 'markdown', CellKind.Markup, [], {}], ['var e = 4;', 'javascript', CellKind.Code, [], {}], ], - async (editor, accessor) => { - const viewModel = editor.viewModel; + async (editor, viewModel, accessor) => { const foldingModel = new FoldingModel(); foldingModel.attachViewModel(viewModel); @@ -99,8 +97,7 @@ suite('Notebook Clipboard', () => { ['# header d', 'markdown', CellKind.Markup, [], {}], ['var e = 4;', 'javascript', CellKind.Code, [], {}], ], - async (editor, accessor) => { - const viewModel = editor.viewModel; + async (editor, viewModel, accessor) => { const foldingModel = new FoldingModel(); foldingModel.attachViewModel(viewModel); @@ -133,11 +130,10 @@ suite('Notebook Clipboard', () => { ['paragraph 1', 'markdown', CellKind.Markup, [], {}], ['paragraph 2', 'markdown', CellKind.Markup, [], {}], ], - async (editor, accessor) => { + async (editor, viewModel, accessor) => { accessor.stub(INotebookService, new class extends mock() { override setToCopy() { } }); const clipboardContrib = new NotebookClipboardContribution(createEditorService(editor)); - const viewModel = editor.viewModel; viewModel.updateSelectionsState({ kind: SelectionStateType.Index, focus: { start: 2, end: 3 }, selections: [{ start: 2, end: 3 }] }, 'model'); assert.ok(clipboardContrib.runCutAction(accessor)); // it should be the last cell, other than the first one. @@ -152,7 +148,7 @@ suite('Notebook Clipboard', () => { ['paragraph 1', 'markdown', CellKind.Markup, [], {}], ['paragraph 2', 'markdown', CellKind.Markup, [], {}], ], - async (editor, accessor) => { + async (editor, viewModel, accessor) => { accessor.stub(INotebookService, new class extends mock() { override setToCopy() { } override getToCopy() { @@ -167,7 +163,6 @@ suite('Notebook Clipboard', () => { const clipboardContrib = new NotebookClipboardContribution(createEditorService(editor)); - const viewModel = editor.viewModel; viewModel.updateSelectionsState({ kind: SelectionStateType.Index, focus: { start: 2, end: 3 }, selections: [{ start: 2, end: 3 }] }, 'model'); assert.ok(clipboardContrib.runPasteAction(accessor)); @@ -187,7 +182,7 @@ suite('Notebook Clipboard', () => { ['paragraph 1', 'markdown', CellKind.Markup, [], {}], ['paragraph 2', 'markdown', CellKind.Markup, [], {}], ], - async (editor, accessor) => { + async (editor, viewModel, accessor) => { let _toCopy: NotebookCellTextModel[] = []; accessor.stub(INotebookService, new class extends mock() { override setToCopy(toCopy: NotebookCellTextModel[]) { _toCopy = toCopy; } @@ -199,7 +194,6 @@ suite('Notebook Clipboard', () => { } }); - const viewModel = editor.viewModel; viewModel.updateSelectionsState({ kind: SelectionStateType.Index, focus: { start: 0, end: 1 }, selections: [{ start: 0, end: 2 }] }, 'model'); assert.ok(runCopyCells(accessor, editor, viewModel.cellAt(0))); assert.deepStrictEqual(_toCopy, [editor.viewModel.cellAt(0)!.model, editor.viewModel.cellAt(1)!.model]); @@ -218,7 +212,7 @@ suite('Notebook Clipboard', () => { ['paragraph 2', 'markdown', CellKind.Markup, [], {}], ['paragraph 3', 'markdown', CellKind.Markup, [], {}], ], - async (editor, accessor) => { + async (editor, viewModel, accessor) => { accessor.stub(INotebookService, new class extends mock() { override setToCopy() { } override getToCopy() { @@ -226,7 +220,6 @@ suite('Notebook Clipboard', () => { } }); - const viewModel = editor.viewModel; viewModel.updateSelectionsState({ kind: SelectionStateType.Index, focus: { start: 0, end: 1 }, selections: [{ start: 0, end: 2 }] }, 'model'); assert.ok(runCutCells(accessor, editor, viewModel.cellAt(0))); assert.strictEqual(viewModel.length, 2); @@ -260,7 +253,7 @@ suite('Notebook Clipboard', () => { ['paragraph 2', 'markdown', CellKind.Markup, [], {}], ['paragraph 3', 'markdown', CellKind.Markup, [], {}], ], - async (editor, accessor) => { + async (editor, viewModel, accessor) => { accessor.stub(INotebookService, new class extends mock() { override setToCopy() { } override getToCopy() { @@ -268,7 +261,6 @@ suite('Notebook Clipboard', () => { } }); - const viewModel = editor.viewModel; viewModel.updateSelectionsState({ kind: SelectionStateType.Index, focus: { start: 0, end: 1 }, selections: [{ start: 2, end: 4 }] }, 'model'); assert.ok(runCutCells(accessor, editor, undefined)); assert.strictEqual(viewModel.length, 3); @@ -285,7 +277,7 @@ suite('Notebook Clipboard', () => { ['paragraph 2', 'markdown', CellKind.Markup, [], {}], ['paragraph 3', 'markdown', CellKind.Markup, [], {}], ], - async (editor, accessor) => { + async (editor, viewModel, accessor) => { accessor.stub(INotebookService, new class extends mock() { override setToCopy() { } override getToCopy() { @@ -293,7 +285,6 @@ suite('Notebook Clipboard', () => { } }); - const viewModel = editor.viewModel; viewModel.updateSelectionsState({ kind: SelectionStateType.Index, focus: { start: 3, end: 4 }, selections: [{ start: 0, end: 2 }] }, 'model'); assert.ok(runCutCells(accessor, editor, undefined)); assert.strictEqual(viewModel.length, 3); diff --git a/src/vs/workbench/contrib/notebook/browser/contrib/find/test/find.test.ts b/src/vs/workbench/contrib/notebook/browser/contrib/find/test/find.test.ts index b70d58c31db..1ab397bcdc9 100644 --- a/src/vs/workbench/contrib/notebook/browser/contrib/find/test/find.test.ts +++ b/src/vs/workbench/contrib/notebook/browser/contrib/find/test/find.test.ts @@ -54,7 +54,7 @@ suite('Notebook Find', () => { ['paragraph 1', 'markdown', CellKind.Markup, [], {}], ['paragraph 2', 'markdown', CellKind.Markup, [], {}], ], - async (editor, accessor) => { + async (editor, viewModel, accessor) => { accessor.stub(IConfigurationService, configurationService); const state = new FindReplaceState(); const model = new FindModel(editor, state, accessor.get(IConfigurationService)); @@ -91,7 +91,7 @@ suite('Notebook Find', () => { ['paragraph 1.3', 'markdown', CellKind.Markup, [], {}], ['paragraph 2', 'markdown', CellKind.Markup, [], {}], ], - async (editor, accessor) => { + async (editor, viewModel, accessor) => { setupEditorForTest(editor); accessor.stub(IConfigurationService, configurationService); const state = new FindReplaceState(); @@ -134,7 +134,7 @@ suite('Notebook Find', () => { ['paragraph 1.3', 'markdown', CellKind.Markup, [], {}], ['paragraph 2', 'markdown', CellKind.Markup, [], {}], ], - async (editor, accessor) => { + async (editor, viewModel, accessor) => { setupEditorForTest(editor); accessor.stub(IConfigurationService, configurationService); const state = new FindReplaceState(); @@ -170,7 +170,7 @@ suite('Notebook Find', () => { ['paragraph 1.3', 'markdown', CellKind.Markup, [], {}], ['paragraph 2', 'markdown', CellKind.Markup, [], {}], ], - async (editor, accessor) => { + async (editor, viewModel, accessor) => { setupEditorForTest(editor); accessor.stub(IConfigurationService, configurationService); const state = new FindReplaceState(); diff --git a/src/vs/workbench/contrib/notebook/browser/contrib/undoRedo/test/notebookUndoRedo.test.ts b/src/vs/workbench/contrib/notebook/browser/contrib/undoRedo/test/notebookUndoRedo.test.ts index f9c76ebf4b9..a9b0869f105 100644 --- a/src/vs/workbench/contrib/notebook/browser/contrib/undoRedo/test/notebookUndoRedo.test.ts +++ b/src/vs/workbench/contrib/notebook/browser/contrib/undoRedo/test/notebookUndoRedo.test.ts @@ -15,9 +15,8 @@ suite('Notebook Undo/Redo', () => { ['# header 1', 'markdown', CellKind.Markup, [], {}], ['body', 'markdown', CellKind.Markup, [], {}], ], - async (editor, accessor) => { + async (editor, viewModel, accessor) => { const modeService = accessor.get(IModeService); - const viewModel = editor.viewModel; assert.strictEqual(viewModel.length, 2); assert.strictEqual(viewModel.getVersionId(), 0); assert.strictEqual(viewModel.getAlternativeId(), '0_0,1;1,1'); @@ -60,9 +59,8 @@ suite('Notebook Undo/Redo', () => { ['# header 1', 'markdown', CellKind.Markup, [], {}], ['body', 'markdown', CellKind.Markup, [], {}], ], - async (editor, accessor) => { + async (editor, viewModel, accessor) => { const modeService = accessor.get(IModeService); - const viewModel = editor.viewModel; viewModel.notebookDocument.applyEdits([{ editType: CellEditType.Replace, index: 0, count: 2, cells: [] }], true, undefined, () => undefined, undefined, true); @@ -103,9 +101,8 @@ suite('Notebook Undo/Redo', () => { ['# header 1', 'markdown', CellKind.Markup, [], {}], ['body', 'markdown', CellKind.Markup, [], {}], ], - async (editor, accessor) => { + async (editor, viewModel, accessor) => { const modeService = accessor.get(IModeService); - const viewModel = editor.viewModel; viewModel.notebookDocument.applyEdits([{ editType: CellEditType.Replace, index: 0, count: 2, cells: [] }], true, undefined, () => undefined, undefined, true); diff --git a/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts b/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts index 3dfe5b3e06e..dc6cbf70d2c 100644 --- a/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts +++ b/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts @@ -166,7 +166,7 @@ export function setupInstantiationService() { return instantiationService; } -function _createTestNotebookEditor(instantiationService: TestInstantiationService, cells: [source: string, lang: string, kind: CellKind, output?: IOutputDto[], metadata?: NotebookCellMetadata][]): IActiveNotebookEditor { +function _createTestNotebookEditor(instantiationService: TestInstantiationService, cells: [source: string, lang: string, kind: CellKind, output?: IOutputDto[], metadata?: NotebookCellMetadata][]): { editor: IActiveNotebookEditor, viewModel: NotebookViewModel; } { const viewType = 'notebook'; const notebook = instantiationService.createInstance(NotebookTextModel, viewType, URI.parse('test'), cells.map(cell => { @@ -242,10 +242,10 @@ function _createTestNotebookEditor(instantiationService: TestInstantiationServic override async removeInset() { } }; - return notebookEditor; + return { editor: notebookEditor, viewModel }; } -export function createTestNotebookEditor(cells: [source: string, lang: string, kind: CellKind, output?: IOutputDto[], metadata?: NotebookCellMetadata][]): IActiveNotebookEditor { +export function createTestNotebookEditor(cells: [source: string, lang: string, kind: CellKind, output?: IOutputDto[], metadata?: NotebookCellMetadata][]): { editor: IActiveNotebookEditor, viewModel: NotebookViewModel; } { return _createTestNotebookEditor(setupInstantiationService(), cells); } @@ -277,25 +277,33 @@ export async function withTestNotebookDiffModel(originalCells: [source: const res = await callback(model, instantiationService); if (res instanceof Promise) { res.finally(() => { - originalNotebook.dispose(); - modifiedNotebook.dispose(); + originalNotebook.editor.dispose(); + originalNotebook.viewModel.dispose(); + modifiedNotebook.editor.dispose(); + modifiedNotebook.viewModel.dispose(); }); } else { - originalNotebook.dispose(); - modifiedNotebook.dispose(); + originalNotebook.editor.dispose(); + originalNotebook.viewModel.dispose(); + modifiedNotebook.editor.dispose(); + modifiedNotebook.viewModel.dispose(); } return res; } -export async function withTestNotebook(cells: [source: string, lang: string, kind: CellKind, output?: IOutputDto[], metadata?: NotebookCellMetadata][], callback: (editor: IActiveNotebookEditor, accessor: TestInstantiationService) => Promise | R, accessor?: TestInstantiationService): Promise { +export async function withTestNotebook(cells: [source: string, lang: string, kind: CellKind, output?: IOutputDto[], metadata?: NotebookCellMetadata][], callback: (editor: IActiveNotebookEditor, viewModel: NotebookViewModel, accessor: TestInstantiationService) => Promise | R, accessor?: TestInstantiationService): Promise { const instantiationService = accessor ?? setupInstantiationService(); const notebookEditor = _createTestNotebookEditor(instantiationService, cells); - const res = await callback(notebookEditor, instantiationService); + const res = await callback(notebookEditor.editor, notebookEditor.viewModel, instantiationService); if (res instanceof Promise) { - res.finally(() => notebookEditor.dispose()); + res.finally(() => { + notebookEditor.editor.dispose(); + notebookEditor.viewModel.dispose(); + }); } else { - notebookEditor.dispose(); + notebookEditor.editor.dispose(); + notebookEditor.viewModel.dispose(); } return res; }