Skip to content

Commit c28c86f

Browse files
committed
-notebookViewModel.moveCellToIdx
1 parent 50421e2 commit c28c86f

File tree

4 files changed

+77
-77
lines changed

4 files changed

+77
-77
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,3 +648,28 @@ export function insertCellAtIndex(viewModel: NotebookViewModel, index: number, s
648648
return viewModel.cellAt(index)!;
649649
}
650650

651+
652+
/**
653+
*
654+
* @param index
655+
* @param length
656+
* @param newIdx in an index scheme for the state of the tree after the current cell has been "removed"
657+
* @param synchronous
658+
* @param pushedToUndoStack
659+
*/
660+
export function moveCellToIdx(editor: IActiveNotebookEditor, index: number, length: number, newIdx: number, synchronous: boolean, pushedToUndoStack: boolean = true): boolean {
661+
const viewCell = editor.cellAt(index) as CellViewModel | undefined;
662+
if (!viewCell) {
663+
return false;
664+
}
665+
666+
editor.textModel.applyEdits([
667+
{
668+
editType: CellEditType.Move,
669+
index,
670+
length,
671+
newIdx
672+
}
673+
], 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);
674+
return true;
675+
}

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

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { MarkupCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewM
3030
import { ViewContext } from 'vs/workbench/contrib/notebook/browser/viewModel/viewContext';
3131
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
3232
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
33-
import { CellEditType, CellKind, ICell, INotebookSearchOptions, ISelectionState, NotebookCellsChangeType, NotebookCellTextModelSplice, SelectionStateType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
33+
import { CellKind, ICell, INotebookSearchOptions, ISelectionState, NotebookCellsChangeType, NotebookCellTextModelSplice, SelectionStateType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
3434
import { cellIndexesToRanges, cellRangesToIndexes, ICellRange, reduceRanges } from 'vs/workbench/contrib/notebook/common/notebookRange';
3535

3636
export interface INotebookEditorViewState {
@@ -773,31 +773,6 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
773773
}
774774
}
775775

776-
/**
777-
*
778-
* @param index
779-
* @param length
780-
* @param newIdx in an index scheme for the state of the tree after the current cell has been "removed"
781-
* @param synchronous
782-
* @param pushedToUndoStack
783-
*/
784-
moveCellToIdx(index: number, length: number, newIdx: number, synchronous: boolean, pushedToUndoStack: boolean = true): boolean {
785-
const viewCell = this.viewCells[index] as CellViewModel;
786-
if (!viewCell) {
787-
return false;
788-
}
789-
790-
this._notebook.applyEdits([
791-
{
792-
editType: CellEditType.Move,
793-
index,
794-
length,
795-
newIdx
796-
}
797-
], synchronous, { kind: SelectionStateType.Index, focus: this.getFocus(), selections: this.getSelections() }, () => ({ kind: SelectionStateType.Index, focus: { start: newIdx, end: newIdx + 1 }, selections: [{ start: newIdx, end: newIdx + 1 }] }), undefined);
798-
return true;
799-
}
800-
801776
getEditorViewState(): INotebookEditorViewState {
802777
const editingCells: { [key: number]: boolean; } = {};
803778
this._viewCells.forEach((cell, i) => {

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

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

66
import * as assert from 'assert';
77
import { FoldingModel, updateFoldingStateAtIndex } from 'vs/workbench/contrib/notebook/browser/contrib/fold/foldingModel';
8-
import { changeCellToKind, computeCellLinesContents, copyCellRange, joinNotebookCells, moveCellRange, runDeleteAction } from 'vs/workbench/contrib/notebook/browser/controller/cellOperations';
8+
import { changeCellToKind, computeCellLinesContents, copyCellRange, joinNotebookCells, moveCellRange, moveCellToIdx, 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/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+
1666
test('Move cells - single cell', async function () {
1767
await withTestNotebook(
1868
[

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

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -65,56 +65,6 @@ suite('NotebookViewModel', () => {
6565
);
6666
});
6767

68-
test('move cells down', async function () {
69-
await withTestNotebook(
70-
[
71-
['//a', 'javascript', CellKind.Code, [], {}],
72-
['//b', 'javascript', CellKind.Code, [], {}],
73-
['//c', 'javascript', CellKind.Code, [], {}],
74-
],
75-
(editor, viewModel) => {
76-
viewModel.moveCellToIdx(0, 1, 0, true);
77-
// no-op
78-
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//a');
79-
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//b');
80-
81-
viewModel.moveCellToIdx(0, 1, 1, true);
82-
// b, a, c
83-
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//b');
84-
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//a');
85-
assert.strictEqual(viewModel.cellAt(2)?.getText(), '//c');
86-
87-
viewModel.moveCellToIdx(0, 1, 2, true);
88-
// a, c, b
89-
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//a');
90-
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//c');
91-
assert.strictEqual(viewModel.cellAt(2)?.getText(), '//b');
92-
}
93-
);
94-
});
95-
96-
test('move cells up', async function () {
97-
await withTestNotebook(
98-
[
99-
['//a', 'javascript', CellKind.Code, [], {}],
100-
['//b', 'javascript', CellKind.Code, [], {}],
101-
['//c', 'javascript', CellKind.Code, [], {}],
102-
],
103-
(editor, viewModel) => {
104-
viewModel.moveCellToIdx(1, 1, 0, true);
105-
// b, a, c
106-
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//b');
107-
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//a');
108-
109-
viewModel.moveCellToIdx(2, 1, 0, true);
110-
// c, b, a
111-
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//c');
112-
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//b');
113-
assert.strictEqual(viewModel.cellAt(2)?.getText(), '//a');
114-
}
115-
);
116-
});
117-
11868
test('index', async function () {
11969
await withTestNotebook(
12070
[

0 commit comments

Comments
 (0)