diff --git a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts index d4086754492941a32e5ade2a32fff0cd4ed0cda9..f86ff23988410b94fc35a543923ec8689e76502d 100644 --- a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts +++ b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts @@ -539,7 +539,10 @@ export class NotebookViewModel extends Disposable implements FoldingRegionDelega } moveCellToIdx(index: number, newIdx: number, synchronous: boolean, pushedToUndoStack: boolean = true): boolean { - // TODO roblou - works differently if index > or < newIdx, fix, write tests + if (index < newIdx) { + // newIdx will be one less after index has been removed + newIdx--; + } const viewCell = this.viewCells[index] as CellViewModel; if (!viewCell) { diff --git a/src/vs/workbench/contrib/notebook/test/notebookViewModel.test.ts b/src/vs/workbench/contrib/notebook/test/notebookViewModel.test.ts index 2db7b23d1e35a1b6bcd09cc36b89bbe26535c76e..ea06d84fce000b437841a8d6c8d8bb9ceaa8bd5a 100644 --- a/src/vs/workbench/contrib/notebook/test/notebookViewModel.test.ts +++ b/src/vs/workbench/contrib/notebook/test/notebookViewModel.test.ts @@ -57,6 +57,67 @@ suite('NotebookViewModel', () => { ); }); + test('move cells down', function () { + withTestNotebook( + instantiationService, + blukEditService, + undoRedoService, + [ + [['//a'], 'javascript', CellKind.Code, [], { editable: true }], + [['//b'], 'javascript', CellKind.Code, [], { editable: true }], + [['//c'], 'javascript', CellKind.Code, [], { editable: true }], + ], + (editor, viewModel) => { + viewModel.moveCellToIdx(0, 0, false); + // no-op + assert.equal(viewModel.viewCells[0].getText(), '//a'); + assert.equal(viewModel.viewCells[1].getText(), '//b'); + + viewModel.moveCellToIdx(0, 1, false); + // no-op (move to after this cell?) + assert.equal(viewModel.viewCells[0].getText(), '//a'); + assert.equal(viewModel.viewCells[1].getText(), '//b'); + + viewModel.moveCellToIdx(0, 2, false); + // b, a, c + assert.equal(viewModel.viewCells[0].getText(), '//b'); + assert.equal(viewModel.viewCells[1].getText(), '//a'); + assert.equal(viewModel.viewCells[2].getText(), '//c'); + + viewModel.moveCellToIdx(0, 3, false); + // a, c, b + assert.equal(viewModel.viewCells[0].getText(), '//a'); + assert.equal(viewModel.viewCells[1].getText(), '//c'); + assert.equal(viewModel.viewCells[2].getText(), '//b'); + } + ); + }); + + test('move cells up', function () { + withTestNotebook( + instantiationService, + blukEditService, + undoRedoService, + [ + [['//a'], 'javascript', CellKind.Code, [], { editable: true }], + [['//b'], 'javascript', CellKind.Code, [], { editable: true }], + [['//c'], 'javascript', CellKind.Code, [], { editable: true }], + ], + (editor, viewModel) => { + viewModel.moveCellToIdx(1, 0, false); + // b, a, c + assert.equal(viewModel.viewCells[0].getText(), '//b'); + assert.equal(viewModel.viewCells[1].getText(), '//a'); + + viewModel.moveCellToIdx(2, 0, false); + // c, b, a + assert.equal(viewModel.viewCells[0].getText(), '//c'); + assert.equal(viewModel.viewCells[1].getText(), '//b'); + assert.equal(viewModel.viewCells[2].getText(), '//a'); + } + ); + }); + test('index', function () { withTestNotebook( instantiationService,