Skip to content

Commit 24f6cc6

Browse files
committed
polish movecells and insert cell
1 parent 960e1bd commit 24f6cc6

File tree

4 files changed

+12
-88
lines changed

4 files changed

+12
-88
lines changed

src/vs/workbench/contrib/notebook/browser/controller/cellOperations.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,10 @@ export function insertCell(
624624
const insertIndex = cell ?
625625
(direction === 'above' ? index : nextIndex) :
626626
index;
627-
return insertCellAtIndex(viewModel, insertIndex, initialText, language, type, undefined, [], true);
627+
return insertCellAtIndex(viewModel, insertIndex, initialText, language, type, undefined, [], true, true);
628628
}
629629

630-
export function insertCellAtIndex(viewModel: NotebookViewModel, index: number, source: string, language: string, type: CellKind, metadata: NotebookCellMetadata | undefined, outputs: IOutputDto[], synchronous: boolean, pushUndoStop: boolean = true): CellViewModel {
630+
export function insertCellAtIndex(viewModel: NotebookViewModel, index: number, source: string, language: string, type: CellKind, metadata: NotebookCellMetadata | undefined, outputs: IOutputDto[], synchronous: boolean, pushUndoStop: boolean): CellViewModel {
631631
const endSelections: ISelectionState = { kind: SelectionStateType.Index, focus: { start: index, end: index + 1 }, selections: [{ start: index, end: index + 1 }] };
632632
viewModel.notebookDocument.applyEdits([
633633
{
@@ -648,29 +648,3 @@ export function insertCellAtIndex(viewModel: NotebookViewModel, index: number, s
648648
], synchronous, { kind: SelectionStateType.Index, focus: viewModel.getFocus(), selections: viewModel.getSelections() }, () => endSelections, undefined, pushUndoStop);
649649
return viewModel.cellAt(index)!;
650650
}
651-
652-
653-
/**
654-
*
655-
* @param index
656-
* @param length
657-
* @param newIdx in an index scheme for the state of the tree after the current cell has been "removed"
658-
* @param synchronous
659-
* @param pushedToUndoStack
660-
*/
661-
export function moveCellToIdx(editor: IActiveNotebookEditor, index: number, length: number, newIdx: number, synchronous: boolean, pushedToUndoStack: boolean = true): boolean {
662-
const viewCell = editor.cellAt(index) as CellViewModel | undefined;
663-
if (!viewCell) {
664-
return false;
665-
}
666-
667-
editor.textModel.applyEdits([
668-
{
669-
editType: CellEditType.Move,
670-
index,
671-
length,
672-
newIdx
673-
}
674-
], synchronous, { kind: SelectionStateType.Index, focus: editor.getFocus(), selections: editor.getSelections() }, () => ({ kind: SelectionStateType.Index, focus: { start: newIdx, end: newIdx + 1 }, selections: [{ start: newIdx, end: newIdx + 1 }] }), undefined);
675-
return true;
676-
}

src/vs/workbench/contrib/notebook/test/browser/cellOperations.test.ts

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,14 @@
55

66
import * as assert from 'assert';
77
import { FoldingModel, updateFoldingStateAtIndex } from 'vs/workbench/contrib/notebook/browser/viewModel/foldingModel';
8-
import { changeCellToKind, computeCellLinesContents, copyCellRange, joinNotebookCells, moveCellRange, moveCellToIdx, runDeleteAction } from 'vs/workbench/contrib/notebook/browser/controller/cellOperations';
8+
import { changeCellToKind, computeCellLinesContents, copyCellRange, joinNotebookCells, moveCellRange, runDeleteAction } from 'vs/workbench/contrib/notebook/browser/controller/cellOperations';
99
import { CellEditType, CellKind, SelectionStateType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
1010
import { withTestNotebook } from 'vs/workbench/contrib/notebook/test/browser/testNotebookEditor';
1111
import { Range } from 'vs/editor/common/core/range';
1212
import { ResourceTextEdit } from 'vs/editor/browser/services/bulkEditService';
1313
import { ResourceNotebookCellEdit } from 'vs/workbench/contrib/bulkEdit/browser/bulkCellEdits';
1414

1515
suite('CellOperations', () => {
16-
test('move cells down', async function () {
17-
await withTestNotebook(
18-
[
19-
['//a', 'javascript', CellKind.Code, [], {}],
20-
['//b', 'javascript', CellKind.Code, [], {}],
21-
['//c', 'javascript', CellKind.Code, [], {}],
22-
],
23-
(editor, viewModel) => {
24-
moveCellToIdx(editor, 0, 1, 0, true);
25-
// no-op
26-
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//a');
27-
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//b');
28-
29-
moveCellToIdx(editor, 0, 1, 1, true);
30-
// b, a, c
31-
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//b');
32-
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//a');
33-
assert.strictEqual(viewModel.cellAt(2)?.getText(), '//c');
34-
35-
moveCellToIdx(editor, 0, 1, 2, true);
36-
// a, c, b
37-
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//a');
38-
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//c');
39-
assert.strictEqual(viewModel.cellAt(2)?.getText(), '//b');
40-
}
41-
);
42-
});
43-
44-
test('move cells up', async function () {
45-
await withTestNotebook(
46-
[
47-
['//a', 'javascript', CellKind.Code, [], {}],
48-
['//b', 'javascript', CellKind.Code, [], {}],
49-
['//c', 'javascript', CellKind.Code, [], {}],
50-
],
51-
(editor, viewModel) => {
52-
moveCellToIdx(editor, 1, 1, 0, true);
53-
// b, a, c
54-
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//b');
55-
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//a');
56-
57-
moveCellToIdx(editor, 2, 1, 0, true);
58-
// c, b, a
59-
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//c');
60-
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//b');
61-
assert.strictEqual(viewModel.cellAt(2)?.getText(), '//a');
62-
}
63-
);
64-
});
65-
6616
test('Move cells - single cell', async function () {
6717
await withTestNotebook(
6818
[

src/vs/workbench/contrib/notebook/test/browser/notebookExecutionService.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ suite('NotebookExecutionService', () => {
6868
async (viewModel) => {
6969
const executionService = instantiationService.createInstance(NotebookExecutionService);
7070

71-
const cell = insertCellAtIndex(viewModel, 1, 'var c = 3', 'javascript', CellKind.Code, {}, [], true);
71+
const cell = insertCellAtIndex(viewModel, 1, 'var c = 3', 'javascript', CellKind.Code, {}, [], true, true);
7272
await assertThrowsAsync(async () => await executionService.executeNotebookCell(cell));
7373
});
7474
});
@@ -80,7 +80,7 @@ suite('NotebookExecutionService', () => {
8080

8181
kernelService.registerKernel(new TestNotebookKernel({ languages: ['testlang'] }));
8282
const executionService = instantiationService.createInstance(NotebookExecutionService);
83-
const cell = insertCellAtIndex(viewModel, 1, 'var c = 3', 'javascript', CellKind.Code, {}, [], true);
83+
const cell = insertCellAtIndex(viewModel, 1, 'var c = 3', 'javascript', CellKind.Code, {}, [], true, true);
8484
await assertThrowsAsync(async () => await executionService.executeNotebookCell(cell));
8585

8686
});
@@ -96,7 +96,7 @@ suite('NotebookExecutionService', () => {
9696
const executeSpy = sinon.spy();
9797
kernel.executeNotebookCellsRequest = executeSpy;
9898

99-
const cell = insertCellAtIndex(viewModel, 0, 'var c = 3', 'javascript', CellKind.Code, {}, [], true);
99+
const cell = insertCellAtIndex(viewModel, 0, 'var c = 3', 'javascript', CellKind.Code, {}, [], true, true);
100100
await executionService.executeNotebookCells(viewModel.notebookDocument, [cell]);
101101
assert.strictEqual(executeSpy.calledOnce, true);
102102
});

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ suite('NotebookViewModel', () => {
9191
const lastViewCell = viewModel.cellAt(viewModel.length - 1)!;
9292

9393
const insertIndex = viewModel.getCellIndex(firstViewCell) + 1;
94-
const cell = insertCellAtIndex(viewModel, insertIndex, 'var c = 3;', 'javascript', CellKind.Code, {}, [], true);
94+
const cell = insertCellAtIndex(viewModel, insertIndex, 'var c = 3;', 'javascript', CellKind.Code, {}, [], true, true);
9595

9696
const addedCellIndex = viewModel.getCellIndex(cell);
9797
runDeleteAction(editor, viewModel.cellAt(addedCellIndex)!);
9898

9999
const secondInsertIndex = viewModel.getCellIndex(lastViewCell) + 1;
100-
const cell2 = insertCellAtIndex(viewModel, secondInsertIndex, 'var d = 4;', 'javascript', CellKind.Code, {}, [], true);
100+
const cell2 = insertCellAtIndex(viewModel, secondInsertIndex, 'var d = 4;', 'javascript', CellKind.Code, {}, [], true, true);
101101

102102
assert.strictEqual(viewModel.length, 3);
103103
assert.strictEqual(viewModel.notebookDocument.cells.length, 3);
@@ -150,7 +150,7 @@ suite('NotebookViewModel Decorations', () => {
150150
end: 2,
151151
});
152152

153-
insertCellAtIndex(viewModel, 0, 'var d = 6;', 'javascript', CellKind.Code, {}, [], true);
153+
insertCellAtIndex(viewModel, 0, 'var d = 6;', 'javascript', CellKind.Code, {}, [], true, true);
154154
assert.deepStrictEqual(viewModel.getTrackedRange(trackedId!), {
155155
start: 2,
156156

@@ -164,7 +164,7 @@ suite('NotebookViewModel Decorations', () => {
164164
end: 2
165165
});
166166

167-
insertCellAtIndex(viewModel, 3, 'var d = 7;', 'javascript', CellKind.Code, {}, [], true);
167+
insertCellAtIndex(viewModel, 3, 'var d = 7;', 'javascript', CellKind.Code, {}, [], true, true);
168168
assert.deepStrictEqual(viewModel.getTrackedRange(trackedId!), {
169169
start: 1,
170170

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

210-
insertCellAtIndex(viewModel, 5, 'var d = 9;', 'javascript', CellKind.Code, {}, [], true);
210+
insertCellAtIndex(viewModel, 5, 'var d = 9;', 'javascript', CellKind.Code, {}, [], true, true);
211211
assert.deepStrictEqual(viewModel.getTrackedRange(trackedId!), {
212212
start: 1,
213213

214214
end: 3
215215
});
216216

217-
insertCellAtIndex(viewModel, 4, 'var d = 10;', 'javascript', CellKind.Code, {}, [], true);
217+
insertCellAtIndex(viewModel, 4, 'var d = 10;', 'javascript', CellKind.Code, {}, [], true, true);
218218
assert.deepStrictEqual(viewModel.getTrackedRange(trackedId!), {
219219
start: 1,
220220

0 commit comments

Comments
 (0)