less use viewModel on NotebookEditor.

This commit is contained in:
rebornix 2021-06-11 14:59:13 -07:00
parent 5e75d958ec
commit c78c56245c
4 changed files with 35 additions and 39 deletions

View file

@ -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<INotebookService>() { 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<INotebookService>() { 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<INotebookService>() {
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<INotebookService>() {
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<INotebookService>() {
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<INotebookService>() {
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<INotebookService>() {
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);

View file

@ -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();

View file

@ -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);

View file

@ -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<R = any>(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<R = any>(cells: [source: string, lang: string, kind: CellKind, output?: IOutputDto[], metadata?: NotebookCellMetadata][], callback: (editor: IActiveNotebookEditor, accessor: TestInstantiationService) => Promise<R> | R, accessor?: TestInstantiationService): Promise<R> {
export async function withTestNotebook<R = any>(cells: [source: string, lang: string, kind: CellKind, output?: IOutputDto[], metadata?: NotebookCellMetadata][], callback: (editor: IActiveNotebookEditor, viewModel: NotebookViewModel, accessor: TestInstantiationService) => Promise<R> | R, accessor?: TestInstantiationService): Promise<R> {
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;
}