Skip to content

Commit 50421e2

Browse files
committed
- notebookViewModel.deleteCell
1 parent adee8fa commit 50421e2

File tree

4 files changed

+18
-128
lines changed

4 files changed

+18
-128
lines changed

src/vs/workbench/contrib/notebook/browser/view/notebookCellList.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ export class NotebookCellList extends WorkbenchList<CellViewModel> implements ID
327327
for (let i = diff.start; i < diff.start + diff.deleteCount; i++) {
328328
const cell = this.element(i);
329329
if (cell.cellKind === CellKind.Code) {
330-
if (this._viewModel!.hasCell(cell.handle)) {
330+
if (this._viewModel!.hasCell(cell)) {
331331
hiddenOutputs.push(...cell?.outputsViewModels);
332332
} else {
333333
deletedOutputs.push(...cell?.outputsViewModels);
@@ -447,9 +447,9 @@ export class NotebookCellList extends WorkbenchList<CellViewModel> implements ID
447447
}
448448

449449
const selectionsLeft = [];
450-
this.getSelectedElements().map(el => el.handle).forEach(handle => {
451-
if (this._viewModel!.hasCell(handle)) {
452-
selectionsLeft.push(handle);
450+
this.getSelectedElements().forEach(el => {
451+
if (this._viewModel!.hasCell(el)) {
452+
selectionsLeft.push(el.handle);
453453
}
454454
});
455455

src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts

Lines changed: 4 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ import { clamp } from 'vs/base/common/numbers';
1111
import * as strings from 'vs/base/common/strings';
1212
import { URI } from 'vs/base/common/uri';
1313
import { IBulkEditService, ResourceEdit, ResourceTextEdit } from 'vs/editor/browser/services/bulkEditService';
14-
import { IPosition, Position } from 'vs/editor/common/core/position';
1514
import { Range } from 'vs/editor/common/core/range';
1615
import * as editorCommon from 'vs/editor/common/editorCommon';
17-
import { EndOfLinePreference, IModelDecorationOptions, IModelDeltaDecoration, IReadonlyTextBuffer, TrackedRangeStickiness } from 'vs/editor/common/model';
16+
import { IModelDecorationOptions, IModelDeltaDecoration, TrackedRangeStickiness } from 'vs/editor/common/model';
1817
import { MultiModelEditStackElement, SingleModelEditStackElement } from 'vs/editor/common/model/editStack';
1918
import { IntervalNode, IntervalTree } from 'vs/editor/common/model/intervalTree';
2019
import { ModelDecorationOptions } from 'vs/editor/common/model/textModel';
@@ -31,7 +30,7 @@ import { MarkupCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewM
3130
import { ViewContext } from 'vs/workbench/contrib/notebook/browser/viewModel/viewContext';
3231
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
3332
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
34-
import { CellEditType, CellKind, ICell, INotebookSearchOptions, IOutputDto, ISelectionState, NotebookCellMetadata, NotebookCellsChangeType, NotebookCellTextModelSplice, SelectionStateType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
33+
import { CellEditType, CellKind, ICell, INotebookSearchOptions, ISelectionState, NotebookCellsChangeType, NotebookCellTextModelSplice, SelectionStateType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
3534
import { cellIndexesToRanges, cellRangesToIndexes, ICellRange, reduceRanges } from 'vs/workbench/contrib/notebook/common/notebookRange';
3635

3736
export interface INotebookEditorViewState {
@@ -589,8 +588,8 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
589588
return index + 1;
590589
}
591590

592-
hasCell(handle: number) {
593-
return this._handleToViewCellMapping.has(handle);
591+
hasCell(cell: ICellViewModel) {
592+
return this._handleToViewCellMapping.has(cell.handle);
594593
}
595594

596595
getVersionId() {
@@ -774,58 +773,6 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
774773
}
775774
}
776775

777-
createCell(index: number, source: string, language: string, type: CellKind, metadata: NotebookCellMetadata | undefined, outputs: IOutputDto[], synchronous: boolean, pushUndoStop: boolean = true, previouslyPrimary: number | null = null, previouslyFocused: ICellViewModel[] = []): CellViewModel {
778-
const beforeSelections = previouslyFocused.map(e => e.handle);
779-
const endSelections: ISelectionState = { kind: SelectionStateType.Index, focus: { start: index, end: index + 1 }, selections: [{ start: index, end: index + 1 }] };
780-
this._notebook.applyEdits([
781-
{
782-
editType: CellEditType.Replace,
783-
index,
784-
count: 0,
785-
cells: [
786-
{
787-
cellKind: type,
788-
language: language,
789-
mime: undefined,
790-
outputs: outputs,
791-
metadata: metadata,
792-
source: source
793-
}
794-
]
795-
}
796-
], synchronous, { kind: SelectionStateType.Handle, primary: previouslyPrimary, selections: beforeSelections }, () => endSelections, undefined);
797-
return this._viewCells[index];
798-
}
799-
800-
deleteCell(index: number, synchronous: boolean, pushUndoStop: boolean = true) {
801-
const focusSelectionIndex = this.getFocus()?.start ?? null;
802-
let endPrimarySelection: number | null = null;
803-
804-
if (index === focusSelectionIndex) {
805-
if (focusSelectionIndex < this.length - 1) {
806-
endPrimarySelection = this._viewCells[focusSelectionIndex + 1].handle;
807-
} else if (focusSelectionIndex === this.length - 1 && this.length > 1) {
808-
endPrimarySelection = this._viewCells[focusSelectionIndex - 1].handle;
809-
}
810-
}
811-
812-
let endSelections: number[] = this.selectionHandles.filter(handle => handle !== endPrimarySelection && handle !== this._viewCells[index]?.handle);
813-
814-
this._notebook.applyEdits([
815-
{
816-
editType: CellEditType.Replace,
817-
index: index,
818-
count: 1,
819-
cells: []
820-
}],
821-
synchronous,
822-
{ kind: SelectionStateType.Index, focus: this.getFocus(), selections: this.getSelections() },
823-
() => ({ kind: SelectionStateType.Handle, primary: endPrimarySelection, selections: endSelections }),
824-
undefined,
825-
pushUndoStop
826-
);
827-
}
828-
829776
/**
830777
*
831778
* @param index
@@ -851,65 +798,6 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
851798
return true;
852799
}
853800

854-
private _pushIfAbsent(positions: IPosition[], p: IPosition) {
855-
const last = positions.length > 0 ? positions[positions.length - 1] : undefined;
856-
if (!last || last.lineNumber !== p.lineNumber || last.column !== p.column) {
857-
positions.push(p);
858-
}
859-
}
860-
861-
/**
862-
* Add split point at the beginning and the end;
863-
* Move end of line split points to the beginning of the next line;
864-
* Avoid duplicate split points
865-
*/
866-
private _splitPointsToBoundaries(splitPoints: IPosition[], textBuffer: IReadonlyTextBuffer): IPosition[] | null {
867-
const boundaries: IPosition[] = [];
868-
const lineCnt = textBuffer.getLineCount();
869-
const getLineLen = (lineNumber: number) => {
870-
return textBuffer.getLineLength(lineNumber);
871-
};
872-
873-
// split points need to be sorted
874-
splitPoints = splitPoints.sort((l, r) => {
875-
const lineDiff = l.lineNumber - r.lineNumber;
876-
const columnDiff = l.column - r.column;
877-
return lineDiff !== 0 ? lineDiff : columnDiff;
878-
});
879-
880-
for (let sp of splitPoints) {
881-
if (getLineLen(sp.lineNumber) + 1 === sp.column && sp.column !== 1 /** empty line */ && sp.lineNumber < lineCnt) {
882-
sp = new Position(sp.lineNumber + 1, 1);
883-
}
884-
this._pushIfAbsent(boundaries, sp);
885-
}
886-
887-
if (boundaries.length === 0) {
888-
return null;
889-
}
890-
891-
// boundaries already sorted and not empty
892-
const modelStart = new Position(1, 1);
893-
const modelEnd = new Position(lineCnt, getLineLen(lineCnt) + 1);
894-
return [modelStart, ...boundaries, modelEnd];
895-
}
896-
897-
computeCellLinesContents(cell: ICellViewModel, splitPoints: IPosition[]): string[] | null {
898-
const rangeBoundaries = this._splitPointsToBoundaries(splitPoints, cell.textBuffer);
899-
if (!rangeBoundaries) {
900-
return null;
901-
}
902-
const newLineModels: string[] = [];
903-
for (let i = 1; i < rangeBoundaries.length; i++) {
904-
const start = rangeBoundaries[i - 1];
905-
const end = rangeBoundaries[i];
906-
907-
newLineModels.push(cell.textBuffer.getValueInRange(new Range(start.lineNumber, start.column, end.lineNumber, end.column), EndOfLinePreference.TextDefined));
908-
}
909-
910-
return newLineModels;
911-
}
912-
913801
getEditorViewState(): INotebookEditorViewState {
914802
const editingCells: { [key: number]: boolean; } = {};
915803
this._viewCells.forEach((cell, i) => {

src/vs/workbench/contrib/notebook/test/notebookSelection.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import * as assert from 'assert';
77
import { IModeService } from 'vs/editor/common/services/modeService';
88
import { FoldingModel, updateFoldingStateAtIndex } from 'vs/workbench/contrib/notebook/browser/contrib/fold/foldingModel';
9+
import { runDeleteAction } from 'vs/workbench/contrib/notebook/browser/controller/cellOperations';
910
import { NotebookCellSelectionCollection } from 'vs/workbench/contrib/notebook/browser/viewModel/cellSelectionCollection';
1011
import { CellEditType, CellKind, SelectionStateType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
1112
import { createNotebookCellList, setupInstantiationService, TestCell, withTestNotebook } from 'vs/workbench/contrib/notebook/test/testNotebookEditor';
@@ -288,9 +289,10 @@ suite('NotebookCellList focus/selection', () => {
288289
],
289290
(editor, viewModel) => {
290291
viewModel.updateSelectionsState({ kind: SelectionStateType.Index, focus: { start: 1, end: 2 }, selections: [{ start: 1, end: 2 }] });
291-
viewModel.deleteCell(1, true, false);
292+
runDeleteAction(editor, viewModel.cellAt(1)!);
293+
// viewModel.deleteCell(1, true, false);
292294
assert.deepStrictEqual(viewModel.getFocus(), { start: 0, end: 1 });
293-
assert.deepStrictEqual(viewModel.getSelections(), []);
295+
assert.deepStrictEqual(viewModel.getSelections(), [{ start: 0, end: 1 }]);
294296
});
295297
});
296298

src/vs/workbench/contrib/notebook/test/notebookViewModel.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { TestConfigurationService } from 'vs/platform/configuration/test/common/
1515
import { IThemeService } from 'vs/platform/theme/common/themeService';
1616
import { TestThemeService } from 'vs/platform/theme/test/common/testThemeService';
1717
import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo';
18-
import { insertCellAtIndex } from 'vs/workbench/contrib/notebook/browser/controller/cellOperations';
18+
import { insertCellAtIndex, runDeleteAction } from 'vs/workbench/contrib/notebook/browser/controller/cellOperations';
1919
import { reduceCellRanges } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
2020
import { NotebookEventDispatcher } from 'vs/workbench/contrib/notebook/browser/viewModel/eventDispatcher';
2121
import { NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
@@ -57,7 +57,7 @@ suite('NotebookViewModel', () => {
5757
assert.strictEqual(viewModel.notebookDocument.cells.length, 3);
5858
assert.strictEqual(viewModel.getCellIndex(cell), 1);
5959

60-
viewModel.deleteCell(1, true);
60+
runDeleteAction(editor, viewModel.cellAt(1)!);
6161
assert.strictEqual(viewModel.length, 2);
6262
assert.strictEqual(viewModel.notebookDocument.cells.length, 2);
6363
assert.strictEqual(viewModel.getCellIndex(cell), -1);
@@ -129,7 +129,7 @@ suite('NotebookViewModel', () => {
129129
const cell = insertCellAtIndex(viewModel, insertIndex, 'var c = 3;', 'javascript', CellKind.Code, {}, [], true);
130130

131131
const addedCellIndex = viewModel.getCellIndex(cell);
132-
viewModel.deleteCell(addedCellIndex, true);
132+
runDeleteAction(editor, viewModel.cellAt(addedCellIndex)!);
133133

134134
const secondInsertIndex = viewModel.getCellIndex(lastViewCell) + 1;
135135
const cell2 = insertCellAtIndex(viewModel, secondInsertIndex, 'var d = 4;', 'javascript', CellKind.Code, {}, [], true);
@@ -192,7 +192,7 @@ suite('NotebookViewModel Decorations', () => {
192192
end: 3
193193
});
194194

195-
viewModel.deleteCell(0, true);
195+
runDeleteAction(editor, viewModel.cellAt(0)!);
196196
assert.deepStrictEqual(viewModel.getTrackedRange(trackedId!), {
197197
start: 1,
198198

@@ -206,14 +206,14 @@ suite('NotebookViewModel Decorations', () => {
206206
end: 3
207207
});
208208

209-
viewModel.deleteCell(3, true);
209+
runDeleteAction(editor, viewModel.cellAt(3)!);
210210
assert.deepStrictEqual(viewModel.getTrackedRange(trackedId!), {
211211
start: 1,
212212

213213
end: 2
214214
});
215215

216-
viewModel.deleteCell(1, true);
216+
runDeleteAction(editor, viewModel.cellAt(1)!);
217217
assert.deepStrictEqual(viewModel.getTrackedRange(trackedId!), {
218218
start: 0,
219219

0 commit comments

Comments
 (0)