diff --git a/src/vs/workbench/contrib/notebook/browser/contrib/notebookActions.ts b/src/vs/workbench/contrib/notebook/browser/contrib/notebookActions.ts index fcb8f406517479fc771720b30c0c7f8d30ad0cfa..d505d096d93abb26b82a05cc54dfa45fc10c30d6 100644 --- a/src/vs/workbench/contrib/notebook/browser/contrib/notebookActions.ts +++ b/src/vs/workbench/contrib/notebook/browser/contrib/notebookActions.ts @@ -697,7 +697,7 @@ registerAction2(class extends Action2 { }); } - run(accessor: ServicesAccessor, context?: INotebookCellActionContext) { + async run(accessor: ServicesAccessor, context?: INotebookCellActionContext) { if (!isCellActionContext(context)) { context = getActiveCellContext(accessor); if (!context) { @@ -705,14 +705,28 @@ registerAction2(class extends Action2 { } } - return context.notebookEditor.deleteNotebookCell(context.cell); + const index = context.notebookEditor.viewModel!.getCellIndex(context.cell); + const result = await context.notebookEditor.deleteNotebookCell(context.cell); + + if (result) { + // deletion succeeds, move focus to the next cell + + if (index < context.notebookEditor.viewModel!.length) { + context.notebookEditor.focusNotebookCell(context.notebookEditor.viewModel!.viewCells[index], false); + } + } } }); async function moveCell(context: INotebookCellActionContext, direction: 'up' | 'down'): Promise { - direction === 'up' ? + const result = direction === 'up' ? context.notebookEditor.moveCellUp(context.cell) : context.notebookEditor.moveCellDown(context.cell); + + if (result) { + // move cell command only works when the cell container has focus + context.notebookEditor.focusNotebookCell(context.cell, false); + } } async function copyCell(context: INotebookCellActionContext, direction: 'up' | 'down'): Promise { diff --git a/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts b/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts index 8716504c9ec8740ff2b34bd79b010ba1691c4472..50e694cf7cc93a0926dbb67b29a9ed00bf320d30 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts @@ -134,22 +134,22 @@ export interface INotebookEditor { /** * Delete a cell from the notebook */ - deleteNotebookCell(cell: ICellViewModel): void; + deleteNotebookCell(cell: ICellViewModel): Promise; /** * Move a cell up one spot */ - moveCellUp(cell: ICellViewModel): void; + moveCellUp(cell: ICellViewModel): Promise; /** * Move a cell down one spot */ - moveCellDown(cell: ICellViewModel): void; + moveCellDown(cell: ICellViewModel): Promise; /** * Move a cell above or below another cell */ - moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise; + moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise; /** * Switch the cell into editing mode. diff --git a/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts b/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts index 0ff2c47bfc30fec437b9e98f97fd2a5490ae7987..a280a65bb7d084f6090ca9fc0223961ad093752c 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookEditor.ts @@ -675,35 +675,36 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor { return new Promise(resolve => { r = resolve; }); } - async deleteNotebookCell(cell: ICellViewModel): Promise { + async deleteNotebookCell(cell: ICellViewModel): Promise { (cell as CellViewModel).save(); const index = this.notebookViewModel!.getCellIndex(cell); this.notebookViewModel!.deleteCell(index, true); + return true; } - async moveCellDown(cell: ICellViewModel): Promise { + async moveCellDown(cell: ICellViewModel): Promise { const index = this.notebookViewModel!.getCellIndex(cell); if (index === this.notebookViewModel!.length - 1) { - return; + return false; } const newIdx = index + 1; return this.moveCellToIndex(index, newIdx); } - async moveCellUp(cell: ICellViewModel): Promise { + async moveCellUp(cell: ICellViewModel): Promise { const index = this.notebookViewModel!.getCellIndex(cell); if (index === 0) { - return; + return false; } const newIdx = index - 1; return this.moveCellToIndex(index, newIdx); } - async moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise { + async moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise { if (cell === relativeToCell) { - return; + return false; } const originalIdx = this.notebookViewModel!.getCellIndex(cell); @@ -713,15 +714,15 @@ export class NotebookEditor extends BaseEditor implements INotebookEditor { return this.moveCellToIndex(originalIdx, newIdx); } - private async moveCellToIndex(index: number, newIdx: number): Promise { + private async moveCellToIndex(index: number, newIdx: number): Promise { if (!this.notebookViewModel!.moveCellToIdx(index, newIdx, true)) { - return; + throw new Error('Notebook Editor move cell, index out of range'); } - let r: () => void; + let r: (val: boolean) => void; DOM.scheduleAtNextAnimationFrame(() => { this.list?.revealElementInView(this.notebookViewModel!.viewCells[newIdx]); - r(); + r(true); }); return new Promise(resolve => { r = resolve; }); diff --git a/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts b/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts index 14ffa70f66d74d521541db70c923f11c2d1b5249..dad32eeeab5f4d1b39a836792761c8f5421ed78a 100644 --- a/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts +++ b/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts @@ -95,15 +95,15 @@ export class TestNotebookEditor implements INotebookEditor { throw new Error('Method not implemented.'); } - moveCellDown(cell: CellViewModel): void { + moveCellDown(cell: CellViewModel): Promise { throw new Error('Method not implemented.'); } - moveCellUp(cell: CellViewModel): void { + moveCellUp(cell: CellViewModel): Promise { throw new Error('Method not implemented.'); } - moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise { + moveCell(cell: ICellViewModel, relativeToCell: ICellViewModel, direction: 'above' | 'below'): Promise { throw new Error('Method not implemented.'); } @@ -153,8 +153,8 @@ export class TestNotebookEditor implements INotebookEditor { async insertNotebookCell(cell: CellViewModel, type: CellKind, direction: 'above' | 'below'): Promise { throw new Error('Method not implemented.'); } - deleteNotebookCell(cell: CellViewModel): void { - // throw new Error('Method not implemented.'); + deleteNotebookCell(cell: CellViewModel): Promise { + throw new Error('Method not implemented.'); } editNotebookCell(cell: CellViewModel): void { // throw new Error('Method not implemented.');