1- /*---------------------------------------------------------
2- * Copyright (C) Microsoft Corporation. All rights reserved.
3- *--------------------------------------------------------*/
41
52import { EventEmitter } from 'events' ;
63import * as vscode from 'vscode' ;
@@ -80,22 +77,18 @@ export class DebugRuntime extends EventEmitter {
8077 public async initializeRequest ( response : DebugProtocol . InitializeResponse , args : MDebugProtocol . InitializeRequestArguments , request : MDebugProtocol . InitializeRequest ) {
8178 // This function initializes a debug session with the following steps:
8279 // 1. Handle arguments
83- // 2. Launch a child process
80+ // 2. Launch a child process running R
8481 // 3. Check that the child process started
8582 // 4. Load the R package vscDebugger
8683 // 5. Check that the R package is present and has a correct version
8784
8885
8986 //// (1) Handle arguments
90- if ( args . rStrings ) {
91- const rs1 = args . rStrings ;
92- const rs0 = this . rStrings ;
93- this . rStrings . prompt = rs1 . prompt || rs0 . prompt ;
94- this . rStrings . continue = rs1 . continue || rs0 . continue ;
95- this . rStrings . startup = rs1 . startup || rs0 . startup ;
96- this . rStrings . libraryNotFound = rs1 . libraryNotFound || rs0 . libraryNotFound ;
97- this . rStrings . packageName = rs1 . packageName || rs0 . packageName ;
98- }
87+ // update rStrings
88+ this . rStrings = {
89+ ...this . rStrings ,
90+ ...args . rStrings
91+ } ;
9992 args . rStrings = this . rStrings ;
10093
10194 // read settings from vsc-settings
@@ -106,15 +99,14 @@ export class DebugRuntime extends EventEmitter {
10699 this . outputModes [ "sinkSocket" ] = config ( ) . get < OutputMode > ( 'printSinkSocket' , 'filtered' ) ;
107100
108101 // start R in child process
109- const rStartupArguments : MDebugProtocol . RStartupArguments = await getRStartupArguments ( ) ;
102+ const rStartupArguments = await getRStartupArguments ( ) ;
110103 const openFolders = vscode . workspace . workspaceFolders ;
111104 if ( openFolders ) {
112105 rStartupArguments . cwd = openFolders [ 0 ] . uri . fsPath ;
113106 }
114107
115108 if ( ! rStartupArguments . path ) {
116109 const message = 'No R path was found in the settings/path/registry.\n(Can be changed in setting rdebugger.rterm.XXX)' ;
117- // const message = 'R path not working:\n' + rPath + '\n';
118110 await this . abortInitializeRequest ( response , message ) ;
119111 return false ;
120112 }
@@ -124,7 +116,6 @@ export class DebugRuntime extends EventEmitter {
124116 this . startOutputGroup ( 'Starting R session...' , true ) ;
125117 this . writeOutput ( 'R Startup:\n' + JSON . stringify ( rStartupArguments , undefined , 2 ) ) ;
126118
127-
128119 //// (2) Launch child process
129120 const tmpHandleLine : LineHandler = ( line : string , from : DataSource , isFullLine : boolean ) => {
130121 return this . handleLine ( line , from , isFullLine ) ;
@@ -143,7 +134,7 @@ export class DebugRuntime extends EventEmitter {
143134
144135 // read ports that were assigned to the child process and add to initialize args
145136 if ( this . rSession . jsonPort <= 0 || this . rSession . sinkPort <= 0 ) {
146- const message = 'Failed to connect to R process using ports !' ;
137+ const message = 'Failed to listen on port !' ;
147138 await this . abortInitializeRequest ( response , message ) ;
148139 return false ;
149140 } else {
@@ -160,7 +151,7 @@ export class DebugRuntime extends EventEmitter {
160151 const escapedStartupString = escapeStringForR ( this . rStrings . startup + '\n' ) ;
161152 const startupCmd = `base::cat(${ escapedStartupString } )` ;
162153 this . rSession . writeToStdin ( startupCmd ) ;
163- // this.rSession.callFunction('cat', this.rStrings.startup, '\n', true, 'base');
154+
164155 // `this.rSessionStartup` is notified when the output of the above `cat()` call is received
165156 await this . rSessionStartup . wait ( this . startupTimeout ) ;
166157 if ( this . rSessionReady ) {
@@ -220,7 +211,7 @@ export class DebugRuntime extends EventEmitter {
220211 return true ;
221212 }
222213
223- private async abortInitializeRequest ( response : DebugProtocol . InitializeResponse , message : string , endOutputGroup : boolean = true ) {
214+ protected async abortInitializeRequest ( response : DebugProtocol . InitializeResponse , message : string , endOutputGroup : boolean = true ) {
224215 // used to abort the debug session and return an unsuccessful InitializeResponse
225216 logger . error ( message ) ;
226217 if ( endOutputGroup ) {
@@ -241,7 +232,7 @@ export class DebugRuntime extends EventEmitter {
241232 // Output-handlers: (for output of the R process to stdout/stderr)
242233 //////////
243234
244- public handleLine ( line : string , from : DataSource , isFullLine : boolean ) : string {
235+ protected handleLine ( line : string , from : DataSource , isFullLine : boolean ) : string {
245236 // handle output from the R process line by line
246237 // is called by rSession.handleData()
247238
@@ -359,7 +350,7 @@ export class DebugRuntime extends EventEmitter {
359350 return line ;
360351 }
361352
362- private async handlePrompt ( which : "browser" | "topLevel" , text ?: string ) {
353+ protected async handlePrompt ( which : "browser" | "topLevel" , text ?: string ) {
363354 logger . debug ( "matches prompt: " + which ) ;
364355
365356 // wait for timeout to give json socket time to catch up
@@ -391,7 +382,7 @@ export class DebugRuntime extends EventEmitter {
391382 }
392383 }
393384
394- private sendShowingPromptRequest ( which : "browser" | "topLevel" , text ?: string ) {
385+ protected sendShowingPromptRequest ( which : "browser" | "topLevel" , text ?: string ) {
395386 const request : MDebugProtocol . ShowingPromptRequest = {
396387 command : "custom" ,
397388 arguments : {
@@ -405,7 +396,7 @@ export class DebugRuntime extends EventEmitter {
405396 this . dispatchRequest ( request ) ;
406397 }
407398
408- public handleJsonString ( json : string , from ?: DataSource , isFullLine : boolean = true ) {
399+ protected handleJsonString ( json : string , from ?: DataSource , isFullLine : boolean = true ) {
409400 if ( ! isFullLine ) {
410401 return json ;
411402 } else {
@@ -415,7 +406,7 @@ export class DebugRuntime extends EventEmitter {
415406 }
416407 }
417408
418- public handleJson ( json : any ) {
409+ protected handleJson ( json : any ) {
419410 if ( json . type === "response" ) {
420411 if ( json . command === "initialize" ) {
421412 this . rPackageFound = true ;
@@ -450,7 +441,12 @@ export class DebugRuntime extends EventEmitter {
450441 }
451442 }
452443
453- public handleWriteToStdinEvent ( args : MDebugProtocol . WriteToStdinBody ) {
444+ // send DAP message to the debugSession
445+ protected sendProtocolMessage ( message : DebugProtocol . ProtocolMessage ) {
446+ this . emit ( "protocolMessage" , message ) ;
447+ }
448+
449+ protected handleWriteToStdinEvent ( args : MDebugProtocol . WriteToStdinBody ) {
454450 let count : number = 0 ;
455451 if ( args . count !== 0 ) {
456452 count = args . count || 1 ;
@@ -491,7 +487,6 @@ export class DebugRuntime extends EventEmitter {
491487 public writeToStdin ( text : string ) {
492488 if ( text ) {
493489 logger . debug ( "Writing to stdin: " , text ) ;
494- // this.rSession.runCommand(text, [], true);
495490 this . rSession . writeToStdin ( text ) ;
496491 return true ;
497492 } else {
@@ -519,10 +514,6 @@ export class DebugRuntime extends EventEmitter {
519514 }
520515 }
521516
522- // send message to the debugSession
523- private sendProtocolMessage ( message : DebugProtocol . ProtocolMessage ) {
524- this . emit ( "protocolMessage" , message ) ;
525- }
526517
527518
528519 //////////////////////////////////////////////
@@ -536,6 +527,7 @@ export class DebugRuntime extends EventEmitter {
536527 data : object = { }
537528 ) : boolean {
538529 // writes output to the debug console (of the vsc instance runnning the R code)
530+ // used during start up to print info about errors/progress
539531 if ( text . slice ( - 1 ) !== '\n' && addNewline ) {
540532 text = text + '\n' ;
541533 }
@@ -565,7 +557,7 @@ export class DebugRuntime extends EventEmitter {
565557 this . sendProtocolMessage ( event ) ;
566558 return true ; // output event was sent
567559 }
568- private startOutputGroup ( text : string = "" , collapsed : boolean = false , addNewline = false , toStderr = false , line = 1 ) {
560+ public startOutputGroup ( text : string = "" , collapsed : boolean = false , addNewline = false , toStderr = false , line = 1 ) {
569561 const group = ( collapsed ? 'startCollapsed' : 'start' ) ;
570562 this . writeOutput ( text , addNewline , ( toStderr ? 'stderr' : 'stdout' ) , line , group ) ;
571563 }
0 commit comments