diff --git a/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts b/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts index 5381bc67978..cea40a4c6ef 100644 --- a/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts +++ b/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts @@ -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'); diff --git a/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts b/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts index 9b16fd34c75..52dd7e26387 100644 --- a/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts +++ b/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts @@ -771,10 +771,7 @@ abstract class InsertCellCommand extends NotebookAction { } async runWithContext(accessor: ServicesAccessor, context: INotebookActionContext): Promise { - 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); } } diff --git a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts index 9ae95f4fba8..4ac3e67b9bb 100644 --- a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts +++ b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts @@ -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]; }