import { PythonResult , UserError , PythonEvaluator } from "arepl-backend" ; import * as vscode from "vscode" ; import PythonInlinePreview from "./pythonInlinePreview" ; import PythonPanelPreview from "./pythonPanelPreview" ; import Reporter from "./telemetry" ; import { settings } from "./settings" ; /**
 * logic wrapper around html preview doc
 */ export class PreviewContainer { public errorDecorationType : vscode . TextEditorDecorationType ; public printResults : string [ ] ; private vars : { } ; pythonInlinePreview : PythonInlinePreview ; pythonEvaluator : PythonEvaluator ; constructor ( private reporter , context , htmlUpdateFrequency = 50 , private pythonPanelPreview ? ) { if ( ! this . pythonPanelPreview ) this . pythonPanelPreview = new PythonPanelPreview ( context , htmlUpdateFrequency ) ; this . pythonInlinePreview = new PythonInlinePreview ( reporter , context ) ; this . errorDecorationType = this . pythonInlinePreview . errorDecorationType ; } public start ( linkedFileName , pythonEvaluator ) { this . clearStoredData ( ) ; this . pythonEvaluator = pythonEvaluator ; return this . pythonPanelPreview . start ( linkedFileName ) ; } /**
     * clears stored data (preview gui is unaffected)
     */ public clearStoredData ( ) { this . vars = { } ; this . printResults = [ ] ; } public handleResult ( pythonResults ) { console . debug ( `Exec time: ${ pythonResults . execTime }` ) ; console . debug ( `Python time: ${ pythonResults . totalPyTime }` ) ; console . debug ( `Total time: ${ pythonResults . totalTime }` ) ; this . reporter . execTime += pythonResults . execTime ; this . reporter . totalPyTime += pythonResults . totalPyTime ; this . reporter . totalTime += pythonResults . totalTime ; try { if ( ! pythonResults . done ) { this . updateVarsWithDumpOutput ( pythonResults ) ; } else { this . pythonPanelPreview . updateTime ( pythonResults . execTime ) ; } this . vars = { ... this . vars , ... pythonResults . userVariables } ; if ( ! pythonResults . userErrorMsg ) { pythonResults . userErrorMsg = "" ; } const syntaxError = pythonResults . userError && 'msg' in pythonResults . userError ; if ( ! syntaxError ) { this . pythonPanelPreview . updateVars ( this . vars ) ; } if ( pythonResults . internalError ) { const lastLine = pythonResults . internalError . trimRight ( ) . split ( '\n' ) ; const firstWordOfLastLine = lastLine . pop ( ) . split ( ' ' ) [ 0 ] . replace ( ':' , '' ) ; const error = new Error ( firstWordOfLastLine ) ; error . stack = pythonResults . internalError ; this . reporter . sendError ( error , 0 , 'python.internal' ) ; pythonResults . userErrorMsg = pythonResults . internalError ; } if ( ! syntaxError && this . printResults . length == 0 ) this . pythonPanelPreview . clearPrint ( ) ; this . updateError ( pythonResults . userError , pythonResults . userErrorMsg , false ) ; this . pythonPanelPreview . injectCustomCSS ( settings ( ) . get ( 'customCSS' ) ) ; this . pythonPanelPreview . throttledUpdate ( ) ; } catch ( error ) { if ( error instanceof Error || error instanceof String ) { vscode . window . showErrorMessage ( "Internal AREPL Error: " + error . toString ( ) , "Report bug" ) . then ( ( action ) => { if ( action == "Report bug" ) { const bugReportLink = vscode . Uri . parse ( `https://github.com/Almenon/AREPL-vscode/issues/new?template=bug_report.md&title=${ error }` ) ; vscode . commands . executeCommand ( 'vscode.open' , bugReportLink ) ; } } ) ; } if ( error instanceof Error ) { this . reporter . sendError ( error ) ; } else { this . reporter . sendError ( new Error ( error ) ) ; } } } public handlePrint ( printResult ) { this . printResults . push ( printResult ) ; this . pythonPanelPreview . handlePrint ( this . printResults . join ( '' ) ) ; } public updateError ( userError , userErrorMsg , refresh ) { const cachedSettings = settings ( ) ; if ( ! cachedSettings . get ( 'showNameErrors' ) ) { if ( userError ?. exc_type ?. [ "py/type" ] ?. includes ( "NameError" ) ) { console . warn ( 'skipped showing name error - showNameErrors setting is off' ) ; return ; } } if ( ! cachedSettings . get ( 'showSyntaxErrors' ) ) { if ( userError ?. exc_type ?. [ "py/type" ] ?. includes ( "SyntaxError" ) ) { console . warn ( 'skipped showing syntax error - SyntaxError setting is off' ) ; return ; } } if ( cachedSettings . get ( 'inlineResults' ) ) { this . pythonInlinePreview . showInlineErrors ( userError , userErrorMsg ) ; } this . pythonPanelPreview . updateError ( userErrorMsg , refresh ) ; } public displayProcessError ( err ) { this . pythonPanelPreview . displayProcessError ( err ) ; } param pythonResults private updateVarsWithDumpOutput ( pythonResults ) { const lineKey = "line " + pythonResults . lineno ; if ( pythonResults . userVariables [ "dump output" ] != undefined ) { const dumpOutput = pythonResults . userVariables [ "dump output" ] ; pythonResults . userVariables = { } ; pythonResults . userVariables [ lineKey ] = dumpOutput ; } else { const v = pythonResults . userVariables ; pythonResults . userVariables = { } ; pythonResults . userVariables [ pythonResults . caller + " vars " + lineKey ] = v ; } } get onDidChange ( ) : vscode . Event < vscode . Uri > { return this . pythonPanelPreview . onDidChange ; } }