From b24f2849dcca9d557e6f1ece73598781b8672647 Mon Sep 17 00:00:00 2001 From: Rob Lourens Date: Mon, 20 Apr 2020 09:57:01 -0500 Subject: [PATCH] Fix dragging cells down --- .../browser/viewModel/notebookViewModel.ts | 5 +- .../notebook/test/notebookViewModel.test.ts | 61 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts index d4086754492..f86ff239884 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 2db7b23d1e3..ea06d84fce0 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, -- GitLab