@@ -16,6 +16,7 @@ import type {
1616// tslint:disable-next-line: no-var-requires no-require-imports
1717const vscodeNotebookEnums = require ( 'vscode' ) as typeof import ( 'vscode-proposed' ) ;
1818import * as uuid from 'uuid/v4' ;
19+ import { NotebookCellRunState } from '../../../../typings/vscode-proposed' ;
1920import {
2021 concatMultilineStringInput ,
2122 concatMultilineStringOutput ,
@@ -90,24 +91,62 @@ export function cellToVSCNotebookCellData(cell: ICell): NotebookCellData | undef
9091 return ;
9192 }
9293
93- return {
94+ // tslint:disable-next-line: no-any
95+ const outputs = cellOutputsToVSCCellOutputs ( cell . data . outputs as any ) ;
96+
97+ // If we have an execution count & no errors, then success state.
98+ // If we have an execution count & errors, then error state.
99+ // Else idle state.
100+ const hasErrors = outputs . some ( ( output ) => output . outputKind === vscodeNotebookEnums . CellOutputKind . Error ) ;
101+ const hasExecutionCount = typeof cell . data . execution_count === 'number' && cell . data . execution_count > 0 ;
102+ let runState : NotebookCellRunState ;
103+ let statusMessage : string | undefined ;
104+ if ( ! hasExecutionCount ) {
105+ runState = vscodeNotebookEnums . NotebookCellRunState . Idle ;
106+ } else if ( hasErrors ) {
107+ runState = vscodeNotebookEnums . NotebookCellRunState . Error ;
108+ // Error details are stripped from the output, get raw output.
109+ // tslint:disable-next-line: no-any
110+ statusMessage = getCellStatusMessageBasedOnFirstErrorOutput ( cell . data . outputs as any ) ;
111+ } else {
112+ runState = vscodeNotebookEnums . NotebookCellRunState . Success ;
113+ }
114+
115+ const notebookCellData : NotebookCellData = {
94116 cellKind :
95117 cell . data . cell_type === 'code' ? vscodeNotebookEnums . CellKind . Code : vscodeNotebookEnums . CellKind . Markdown ,
96118 language : cell . data . cell_type === 'code' ? PYTHON_LANGUAGE : MARKDOWN_LANGUAGE ,
97119 metadata : {
98120 editable : true ,
99121 executionOrder : typeof cell . data . execution_count === 'number' ? cell . data . execution_count : undefined ,
100122 hasExecutionOrder : cell . data . cell_type === 'code' ,
101- runState : vscodeNotebookEnums . NotebookCellRunState . Idle ,
123+ runState,
102124 runnable : cell . data . cell_type === 'code' ,
103125 custom : {
104126 cellId : cell . id
105127 }
106128 } ,
107129 source : concatMultilineStringInput ( cell . data . source ) ,
108- // tslint:disable-next-line: no-any
109- outputs : cellOutputsToVSCCellOutputs ( cell . data . outputs as any )
130+ outputs
110131 } ;
132+
133+ if ( statusMessage ) {
134+ notebookCellData . metadata . statusMessage = statusMessage ;
135+ }
136+
137+ const startExecutionTime = cell . data . metadata . vscode ?. start_execution_time
138+ ? new Date ( Date . parse ( cell . data . metadata . vscode . start_execution_time ) ) . getTime ( )
139+ : undefined ;
140+ const endExecutionTime = cell . data . metadata . vscode ?. end_execution_time
141+ ? new Date ( Date . parse ( cell . data . metadata . vscode . end_execution_time ) ) . getTime ( )
142+ : undefined ;
143+
144+ if ( startExecutionTime && typeof endExecutionTime === 'number' ) {
145+ notebookCellData . metadata . runStartTime = startExecutionTime ;
146+ notebookCellData . metadata . lastRunDuration = endExecutionTime - startExecutionTime ;
147+ }
148+
149+ return notebookCellData ;
111150}
112151
113152export function cellOutputsToVSCCellOutputs ( outputs ?: nbformat . IOutput [ ] ) : CellOutput [ ] {
@@ -205,11 +244,31 @@ function translateStreamOutput(output: nbformat.IStream): CellStreamOutput | Cel
205244 }
206245 } ;
207246}
247+
248+ /**
249+ * We will display the error message in the status of the cell.
250+ * The `ename` & `evalue` is displayed at the top of the output by VS Code.
251+ * As we're displaying the error in the statusbar, we don't want this dup error in output.
252+ * Hence remove this.
253+ */
208254export function translateErrorOutput ( output : nbformat . IError ) : CellErrorOutput {
209255 return {
210- ename : output . ename ,
211- evalue : output . evalue ,
256+ ename : '' ,
257+ evalue : '' ,
212258 outputKind : vscodeNotebookEnums . CellOutputKind . Error ,
213259 traceback : output . traceback
214260 } ;
215261}
262+
263+ export function getCellStatusMessageBasedOnFirstErrorOutput ( outputs ?: nbformat . IOutput [ ] ) : string {
264+ if ( ! Array . isArray ( outputs ) ) {
265+ return '' ;
266+ }
267+ const errorOutput = ( outputs . find ( ( output ) => output . output_type === 'error' ) as unknown ) as
268+ | nbformat . IError
269+ | undefined ;
270+ if ( ! errorOutput ) {
271+ return '' ;
272+ }
273+ return `${ errorOutput . ename } ${ errorOutput . evalue ? ': ' : '' } ${ errorOutput . evalue } ` ;
274+ }
0 commit comments