-notebookViewModel.moveCellToIdx

This commit is contained in:
rebornix
2021-09-19 17:06:26 -07:00
parent 50421e23a0
commit c28c86fcd1
4 changed files with 77 additions and 77 deletions
@@ -648,3 +648,28 @@ export function insertCellAtIndex(viewModel: NotebookViewModel, index: number, s
return viewModel.cellAt(index)!;
}
/**
*
* @param index
* @param length
* @param newIdx in an index scheme for the state of the tree after the current cell has been "removed"
* @param synchronous
* @param pushedToUndoStack
*/
export function moveCellToIdx(editor: IActiveNotebookEditor, index: number, length: number, newIdx: number, synchronous: boolean, pushedToUndoStack: boolean = true): boolean {
const viewCell = editor.cellAt(index) as CellViewModel | undefined;
if (!viewCell) {
return false;
}
editor.textModel.applyEdits([
{
editType: CellEditType.Move,
index,
length,
newIdx
}
], 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);
return true;
}
@@ -30,7 +30,7 @@ import { MarkupCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewM
import { ViewContext } from 'vs/workbench/contrib/notebook/browser/viewModel/viewContext';
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
import { CellEditType, CellKind, ICell, INotebookSearchOptions, ISelectionState, NotebookCellsChangeType, NotebookCellTextModelSplice, SelectionStateType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { CellKind, ICell, INotebookSearchOptions, ISelectionState, NotebookCellsChangeType, NotebookCellTextModelSplice, SelectionStateType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { cellIndexesToRanges, cellRangesToIndexes, ICellRange, reduceRanges } from 'vs/workbench/contrib/notebook/common/notebookRange';
export interface INotebookEditorViewState {
@@ -773,31 +773,6 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
}
}
/**
*
* @param index
* @param length
* @param newIdx in an index scheme for the state of the tree after the current cell has been "removed"
* @param synchronous
* @param pushedToUndoStack
*/
moveCellToIdx(index: number, length: number, newIdx: number, synchronous: boolean, pushedToUndoStack: boolean = true): boolean {
const viewCell = this.viewCells[index] as CellViewModel;
if (!viewCell) {
return false;
}
this._notebook.applyEdits([
{
editType: CellEditType.Move,
index,
length,
newIdx
}
], 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);
return true;
}
getEditorViewState(): INotebookEditorViewState {
const editingCells: { [key: number]: boolean; } = {};
this._viewCells.forEach((cell, i) => {
@@ -5,7 +5,7 @@
import * as assert from 'assert';
import { FoldingModel, updateFoldingStateAtIndex } from 'vs/workbench/contrib/notebook/browser/contrib/fold/foldingModel';
import { changeCellToKind, computeCellLinesContents, copyCellRange, joinNotebookCells, moveCellRange, runDeleteAction } from 'vs/workbench/contrib/notebook/browser/controller/cellOperations';
import { changeCellToKind, computeCellLinesContents, copyCellRange, joinNotebookCells, moveCellRange, moveCellToIdx, runDeleteAction } from 'vs/workbench/contrib/notebook/browser/controller/cellOperations';
import { CellEditType, CellKind, SelectionStateType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { withTestNotebook } from 'vs/workbench/contrib/notebook/test/testNotebookEditor';
import { Range } from 'vs/editor/common/core/range';
@@ -13,6 +13,56 @@ import { ResourceTextEdit } from 'vs/editor/browser/services/bulkEditService';
import { ResourceNotebookCellEdit } from 'vs/workbench/contrib/bulkEdit/browser/bulkCellEdits';
suite('CellOperations', () => {
test('move cells down', async function () {
await withTestNotebook(
[
['//a', 'javascript', CellKind.Code, [], {}],
['//b', 'javascript', CellKind.Code, [], {}],
['//c', 'javascript', CellKind.Code, [], {}],
],
(editor, viewModel) => {
moveCellToIdx(editor, 0, 1, 0, true);
// no-op
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//a');
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//b');
moveCellToIdx(editor, 0, 1, 1, true);
// b, a, c
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//b');
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//a');
assert.strictEqual(viewModel.cellAt(2)?.getText(), '//c');
moveCellToIdx(editor, 0, 1, 2, true);
// a, c, b
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//a');
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//c');
assert.strictEqual(viewModel.cellAt(2)?.getText(), '//b');
}
);
});
test('move cells up', async function () {
await withTestNotebook(
[
['//a', 'javascript', CellKind.Code, [], {}],
['//b', 'javascript', CellKind.Code, [], {}],
['//c', 'javascript', CellKind.Code, [], {}],
],
(editor, viewModel) => {
moveCellToIdx(editor, 1, 1, 0, true);
// b, a, c
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//b');
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//a');
moveCellToIdx(editor, 2, 1, 0, true);
// c, b, a
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//c');
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//b');
assert.strictEqual(viewModel.cellAt(2)?.getText(), '//a');
}
);
});
test('Move cells - single cell', async function () {
await withTestNotebook(
[
@@ -65,56 +65,6 @@ suite('NotebookViewModel', () => {
);
});
test('move cells down', async function () {
await withTestNotebook(
[
['//a', 'javascript', CellKind.Code, [], {}],
['//b', 'javascript', CellKind.Code, [], {}],
['//c', 'javascript', CellKind.Code, [], {}],
],
(editor, viewModel) => {
viewModel.moveCellToIdx(0, 1, 0, true);
// no-op
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//a');
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//b');
viewModel.moveCellToIdx(0, 1, 1, true);
// b, a, c
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//b');
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//a');
assert.strictEqual(viewModel.cellAt(2)?.getText(), '//c');
viewModel.moveCellToIdx(0, 1, 2, true);
// a, c, b
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//a');
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//c');
assert.strictEqual(viewModel.cellAt(2)?.getText(), '//b');
}
);
});
test('move cells up', async function () {
await withTestNotebook(
[
['//a', 'javascript', CellKind.Code, [], {}],
['//b', 'javascript', CellKind.Code, [], {}],
['//c', 'javascript', CellKind.Code, [], {}],
],
(editor, viewModel) => {
viewModel.moveCellToIdx(1, 1, 0, true);
// b, a, c
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//b');
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//a');
viewModel.moveCellToIdx(2, 1, 0, true);
// c, b, a
assert.strictEqual(viewModel.cellAt(0)?.getText(), '//c');
assert.strictEqual(viewModel.cellAt(1)?.getText(), '//b');
assert.strictEqual(viewModel.cellAt(2)?.getText(), '//a');
}
);
});
test('index', async function () {
await withTestNotebook(
[