fix(notebook): redo cell creation does not re-select the cell

This commit is contained in:
arnobl 2021-03-28 23:15:28 +02:00
parent c0fa3dfd3e
commit 7fbddb5522
3 changed files with 22 additions and 5 deletions

View file

@ -311,6 +311,25 @@ suite('Notebook API tests', function () {
assert.strictEqual(count, 0);
});
test('correct cell selection on undo/redo of cell creation', async function () {
const resource = await createRandomFile('', undefined, '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');
await vscode.commands.executeCommand('notebook.cell.insertCodeCellBelow');
await vscode.commands.executeCommand('undo');
const selectionUndo = [...vscode.window.activeNotebookEditor!.selections];
await vscode.commands.executeCommand('redo');
const selectionRedo = vscode.window.activeNotebookEditor!.selections;
// On undo, the selected cell must be the upper cell, ie the first one
assert.strictEqual(selectionUndo.length, 1);
assert.strictEqual(selectionUndo[0].start, 0);
assert.strictEqual(selectionUndo[0].end, 1);
// On redo, the selected cell must be the new cell, ie the second one
assert.strictEqual(selectionRedo.length, 1);
assert.strictEqual(selectionRedo[0].start, 1);
assert.strictEqual(selectionRedo[0].end, 2);
});
test('editor editing event 2', async function () {
const resource = await createRandomFile('', undefined, '.vsctestnb');
await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest');

View file

@ -771,10 +771,7 @@ abstract class InsertCellCommand extends NotebookAction {
}
async runWithContext(accessor: ServicesAccessor, context: INotebookActionContext): Promise<void> {
const newCell = context.notebookEditor.insertNotebookCell(context.cell, this.kind, this.direction, undefined, true);
if (newCell) {
context.notebookEditor.focusNotebookCell(newCell, 'editor');
}
context.notebookEditor.insertNotebookCell(context.cell, this.kind, this.direction, undefined, true);
}
}

View file

@ -695,6 +695,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
createCell(index: number, source: string, language: string, type: CellKind, metadata: NotebookCellMetadata | undefined, outputs: IOutputDto[], synchronous: boolean, pushUndoStop: boolean = true, previouslyPrimary: number | null = null, previouslyFocused: ICellViewModel[] = []): CellViewModel {
const beforeSelections = previouslyFocused.map(e => e.handle);
const endSelections: ISelectionState = { kind: SelectionStateType.Index, focus: { start: index, end: index + 1 }, selections: [{ start: index, end: index + 1 }] };
this._notebook.applyEdits([
{
editType: CellEditType.Replace,
@ -710,7 +711,7 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
}
]
}
], synchronous, { kind: SelectionStateType.Handle, primary: previouslyPrimary, selections: beforeSelections }, () => undefined, undefined);
], synchronous, { kind: SelectionStateType.Handle, primary: previouslyPrimary, selections: beforeSelections }, () => endSelections, undefined);
return this._viewCells[index];
}