diff --git a/src/vs/workbench/api/common/extHostNotebookKernels.ts b/src/vs/workbench/api/common/extHostNotebookKernels.ts index eacc1e40cf0e49bca47c5cefcf90ab6125adb518..53df9db9f426ab30abec119da64599f1dd051996 100644 --- a/src/vs/workbench/api/common/extHostNotebookKernels.ts +++ b/src/vs/workbench/api/common/extHostNotebookKernels.ts @@ -19,6 +19,7 @@ import { ExtHostCell, ExtHostNotebookDocument } from 'vs/workbench/api/common/ex import { CellEditType, IImmediateCellEditOperation, NullablePartialNotebookCellMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { CancellationTokenSource } from 'vs/base/common/cancellation'; import { NotebookCellExecutionState } from 'vs/workbench/api/common/extHostTypes'; +import { asArray } from 'vs/base/common/arrays'; interface IKernelData { extensionId: ExtensionIdentifier, @@ -367,12 +368,14 @@ class NotebookCellExecutionTask extends Disposable { this.applyEdits([edit]); } - private cellIndexToHandle(cellIndex: number | undefined): number | undefined { - const cell = typeof cellIndex === 'number' ? this._document.getCellFromIndex(cellIndex) : this._cell; + private cellIndexToHandle(cellIndex: number | undefined): number { + if (typeof cellIndex !== 'number') { + return this._cell.handle; + } + const cell = this._document.getCellFromIndex(cellIndex); if (!cell) { - return; + throw new Error('INVALID cell index'); } - return cell.handle; } @@ -427,34 +430,26 @@ class NotebookCellExecutionTask extends Disposable { async appendOutput(outputs: vscode.NotebookCellOutput | vscode.NotebookCellOutput[], cellIndex?: number): Promise { that.verifyStateForOutput(); const handle = that.cellIndexToHandle(cellIndex); - if (typeof handle !== 'number') { - return; - } - - outputs = Array.isArray(outputs) ? outputs : [outputs]; + outputs = asArray(outputs); return that.applyEditSoon({ editType: CellEditType.Output, handle, append: true, outputs: outputs.map(extHostTypeConverters.NotebookCellOutput.from) }); }, async replaceOutput(outputs: vscode.NotebookCellOutput | vscode.NotebookCellOutput[], cellIndex?: number): Promise { that.verifyStateForOutput(); const handle = that.cellIndexToHandle(cellIndex); - if (typeof handle !== 'number') { - return; - } - - outputs = Array.isArray(outputs) ? outputs : [outputs]; + outputs = asArray(outputs); return that.applyEditSoon({ editType: CellEditType.Output, handle, outputs: outputs.map(extHostTypeConverters.NotebookCellOutput.from) }); }, async appendOutputItems(items: vscode.NotebookCellOutputItem | vscode.NotebookCellOutputItem[], outputId: string): Promise { that.verifyStateForOutput(); - items = Array.isArray(items) ? items : [items]; + items = asArray(items); return that.applyEditSoon({ editType: CellEditType.OutputItems, append: true, items: items.map(extHostTypeConverters.NotebookCellOutputItem.from), outputId }); }, async replaceOutputItems(items: vscode.NotebookCellOutputItem | vscode.NotebookCellOutputItem[], outputId: string): Promise { that.verifyStateForOutput(); - items = Array.isArray(items) ? items : [items]; + items = asArray(items); return that.applyEditSoon({ editType: CellEditType.OutputItems, items: items.map(extHostTypeConverters.NotebookCellOutputItem.from), outputId }); },