diff --git a/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts b/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts index fde26b3d53435a67d05f43539a23d3c98015d435..aaff06c8a8f0468c5328f61e74a2f4df57ab0094 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts @@ -507,9 +507,8 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor { const index = this.notebookViewModel!.getViewCellIndex(cell); const insertIndex = direction === 'above' ? index : index + 1; const newModeCell = await this.notebookService.createNotebookCell(this.viewType!, this.notebookViewModel!.uri, insertIndex, language, type); - const newCell = this.instantiationService.createInstance(CellViewModel, this.viewType!, this.notebookViewModel!.handle, newModeCell!); + const newCell = this.notebookViewModel!.insertCell(insertIndex, newModeCell!); - this.notebookViewModel!.insertCell(insertIndex, newCell); this.list?.splice(insertIndex, 0, [newCell]); this.list?.setFocus([insertIndex]); diff --git a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookCellViewModel.ts b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookCellViewModel.ts index 3367cd694a206aae6d339e81d368d0e1448b22b6..80ddd1f102810bfda03b3d2012457cccc28f6ad3 100644 --- a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookCellViewModel.ts +++ b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookCellViewModel.ts @@ -153,15 +153,6 @@ export class CellViewModel extends Disposable { }; } - stopFind(keepSelection?: boolean | undefined): void { - if (!this.assertTextModelAttached()) { - return; - } - } - - focus(): void { - } - assertTextModelAttached(): boolean { if (this._textModel && this._textEditor && this._textEditor.getModel() === this._textModel) { return true; diff --git a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts index e36047165d9ef5f1cedc170833e6d4e38b9079d6..45190a662dd08a5be26756ddacfb6928a2e6b668 100644 --- a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts +++ b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts @@ -9,7 +9,7 @@ import * as editorCommon from 'vs/editor/common/editorCommon'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { NotebookEditorModel } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput'; import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookCellViewModel'; -import { NotebookCellsSplice } from 'vs/workbench/contrib/notebook/common/notebookCommon'; +import { NotebookCellsSplice, ICell } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { IModelDeltaDecoration } from 'vs/editor/common/model'; import { onUnexpectedError } from 'vs/base/common/errors'; import { CellFindMatch } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; @@ -114,15 +114,19 @@ export class NotebookViewModel extends Disposable { return matches; } - insertCell(index: number, newCell: CellViewModel) { + insertCell(index: number, cell: ICell): CellViewModel { + const newCell = this.instantiationService.createInstance(CellViewModel, this.viewType, this.handle, cell); this.viewCells!.splice(index, 0, newCell); this._model.insertCell(newCell.cell, index); + this._localStore.add(newCell); + return newCell; } deleteCell(index: number) { let viewCell = this.viewCells[index]; this.viewCells.splice(index, 1); this._model.deleteCell(viewCell.cell); + viewCell.dispose(); } saveEditorViewState(): INotebookEditorViewState { diff --git a/src/vs/workbench/contrib/notebook/test/notebookViewModel.test.ts b/src/vs/workbench/contrib/notebook/test/notebookViewModel.test.ts index ae21f5697c3513a9c58efa7199566249d346460a..b7ded99e442331651c3353645c3582ada493eeaa 100644 --- a/src/vs/workbench/contrib/notebook/test/notebookViewModel.test.ts +++ b/src/vs/workbench/contrib/notebook/test/notebookViewModel.test.ts @@ -9,7 +9,7 @@ import { TestInstantiationService } from 'vs/platform/instantiation/test/common/ import { NotebookEditorModel } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput'; import { NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel'; import { CellKind } from 'vs/workbench/contrib/notebook/common/notebookCommon'; -import { createTestCellViewModel, TestNotebook, withTestNotebook } from 'vs/workbench/contrib/notebook/test/testNotebookEditor'; +import { TestNotebook, withTestNotebook, TestCell } from 'vs/workbench/contrib/notebook/test/testNotebookEditor'; suite('NotebookViewModel', () => { const instantiationService = new TestInstantiationService(); @@ -29,8 +29,7 @@ suite('NotebookViewModel', () => { [['var b = 2;'], 'javascript', CellKind.Code, []] ], (editor, viewModel) => { - const cell = createTestCellViewModel(instantiationService, viewModel.viewType, viewModel.handle, 0, ['var c = 3;'], 'javascript', CellKind.Code, []); - viewModel.insertCell(1, cell); + const cell = viewModel.insertCell(1, new TestCell(viewModel.viewType, 0, ['var c = 3;'], 'javascript', CellKind.Code, [])); assert.equal(viewModel.viewCells.length, 3); assert.equal(viewModel.notebookDocument.cells.length, 3); assert.equal(viewModel.getViewCellIndex(cell), 1); @@ -55,15 +54,13 @@ suite('NotebookViewModel', () => { const lastViewCell = viewModel.viewCells[viewModel.viewCells.length - 1]; const insertIndex = viewModel.getViewCellIndex(firstViewCell) + 1; - const cell = createTestCellViewModel(instantiationService, viewModel.viewType, viewModel.handle, 3, ['var c = 3;'], 'javascript', CellKind.Code, []); - viewModel.insertCell(insertIndex, cell); + const cell = viewModel.insertCell(insertIndex, new TestCell(viewModel.viewType, 3, ['var c = 3;'], 'javascript', CellKind.Code, [])); const addedCellIndex = viewModel.getViewCellIndex(cell); viewModel.deleteCell(addedCellIndex); const secondInsertIndex = viewModel.getViewCellIndex(lastViewCell) + 1; - const cell2 = createTestCellViewModel(instantiationService, viewModel.viewType, viewModel.handle, 4, ['var d = 4;'], 'javascript', CellKind.Code, []); - viewModel.insertCell(secondInsertIndex, cell2); + const cell2 = viewModel.insertCell(secondInsertIndex, new TestCell(viewModel.viewType, 4, ['var d = 4;'], 'javascript', CellKind.Code, [])); assert.equal(viewModel.viewCells.length, 3); assert.equal(viewModel.notebookDocument.cells.length, 3);