@@ -6,6 +6,7 @@ import { PythonManager } from "./pythonmanger";
66import * as cp from "child_process" ;
77import * as rpc from "vscode-jsonrpc/node" ;
88import { withTimeout } from "./utils" ;
9+ import { CONFIG_SECTION } from "./config" ;
910
1011interface RawNotebook {
1112 cells : RawNotebookCell [ ] ;
@@ -161,7 +162,7 @@ export class ReplServerClient {
161162
162163 connection : rpc . MessageConnection | undefined ;
163164 childProcess : cp . ChildProcessWithoutNullStreams | undefined ;
164- private _cancelationTokenSource : vscode . CancellationTokenSource | undefined ;
165+ private _cancelationTokenSources = new Map < vscode . CancellationTokenSource , vscode . NotebookCell > ( ) ;
165166
166167 dispose ( ) : void {
167168 this . exitClient ( ) . finally ( ( ) => { } ) ;
@@ -198,10 +199,11 @@ export class ReplServerClient {
198199 }
199200
200201 cancelCurrentExecution ( ) : void {
201- if ( this . _cancelationTokenSource ) {
202- this . _cancelationTokenSource . cancel ( ) ;
203- this . _cancelationTokenSource . dispose ( ) ;
202+ for ( const [ token ] of this . _cancelationTokenSources ) {
203+ token . cancel ( ) ;
204+ token . dispose ( ) ;
204205 }
206+ this . _cancelationTokenSources . clear ( ) ;
205207 }
206208
207209 async ensureInitialized ( ) : Promise < void > {
@@ -223,10 +225,14 @@ export class ReplServerClient {
223225
224226 const transport = await rpc . createClientPipeTransport ( pipeName , "utf-8" ) ;
225227
228+ const config = vscode . workspace . getConfiguration ( CONFIG_SECTION , folder ) ;
229+ const profiles = config . get < string [ ] > ( "profiles" , [ ] ) ;
230+
226231 const { pythonCommand, final_args } = await this . pythonManager . buildRobotCodeCommand (
227232 folder ,
228233 //["-v", "--debugpy", "--debugpy-wait-for-client", "repl-server", "--pipe", pipeName],
229234 [ "repl-server" , "--pipe" , pipeName , "--source" , this . document . uri . fsPath ] ,
235+ profiles ,
230236 undefined ,
231237 true ,
232238 true ,
@@ -279,16 +285,16 @@ export class ReplServerClient {
279285 this . connection = connection ;
280286 }
281287
282- async executeCell ( source : string ) : Promise < { success ?: boolean ; output : vscode . NotebookCellOutput } > {
283- this . _cancelationTokenSource = new vscode . CancellationTokenSource ( ) ;
284-
288+ async executeCell ( cell : vscode . NotebookCell ) : Promise < { success ?: boolean ; output : vscode . NotebookCellOutput } > {
289+ const _cancelationTokenSource = new vscode . CancellationTokenSource ( ) ;
290+ this . _cancelationTokenSources . set ( _cancelationTokenSource , cell ) ;
285291 try {
286292 await this . ensureInitialized ( ) ;
287293
288294 const result = await this . connection ?. sendRequest < ExecutionResult > (
289295 "executeCell" ,
290- { source } ,
291- this . _cancelationTokenSource . token ,
296+ { source : cell . document . getText ( ) , language_id : cell . document . languageId } ,
297+ _cancelationTokenSource . token ,
292298 ) ;
293299
294300 return {
@@ -306,10 +312,14 @@ export class ReplServerClient {
306312 ) ,
307313 } ;
308314 } finally {
309- this . _cancelationTokenSource . dispose ( ) ;
310- this . _cancelationTokenSource = undefined ;
315+ this . _cancelationTokenSources . delete ( _cancelationTokenSource ) ;
316+ _cancelationTokenSource . dispose ( ) ;
311317 }
312318 }
319+
320+ async interrupt ( ) : Promise < void > {
321+ await this . connection ?. sendRequest ( "interrupt" ) ;
322+ }
313323}
314324
315325export class REPLNotebookController {
@@ -320,7 +330,7 @@ export class REPLNotebookController {
320330 readonly description = "A Robot Framework REPL notebook controller" ;
321331 readonly supportsExecutionOrder = true ;
322332 readonly controller : vscode . NotebookController ;
323- readonly _clients = new Map < vscode . NotebookDocument , ReplServerClient > ( ) ;
333+ readonly clients = new Map < vscode . NotebookDocument , ReplServerClient > ( ) ;
324334
325335 _outputChannel : vscode . OutputChannel | undefined ;
326336
@@ -343,18 +353,23 @@ export class REPLNotebookController {
343353 this . controller . supportsExecutionOrder = true ;
344354 this . controller . description = "Robot Framework REPL" ;
345355 this . controller . interruptHandler = async ( notebook : vscode . NotebookDocument ) => {
346- this . _clients . get ( notebook ) ?. dispose ( ) ;
347- this . _clients . delete ( notebook ) ;
356+ this . clients . get ( notebook ) ?. interrupt ( ) ;
348357 } ;
349358 this . _disposables = vscode . Disposable . from (
350359 this . controller ,
351360 vscode . workspace . onDidCloseNotebookDocument ( ( document ) => {
352- this . _clients . get ( document ) ?. dispose ( ) ;
353- this . _clients . delete ( document ) ;
361+ this . disposeDocument ( document ) ;
354362 } ) ,
355363 ) ;
356364 }
357365
366+ disposeDocument ( notebook : vscode . NotebookDocument ) : void {
367+ const client = this . clients . get ( notebook ) ;
368+ client ?. interrupt ( ) ;
369+ client ?. dispose ( ) ;
370+ this . clients . delete ( notebook ) ;
371+ }
372+
358373 outputChannel ( ) : vscode . OutputChannel {
359374 if ( ! this . _outputChannel ) {
360375 this . _outputChannel = vscode . window . createOutputChannel ( "RobotCode REPL" ) ;
@@ -363,18 +378,18 @@ export class REPLNotebookController {
363378 }
364379
365380 dispose ( ) : void {
366- for ( const client of this . _clients . values ( ) ) {
381+ for ( const client of this . clients . values ( ) ) {
367382 client . dispose ( ) ;
368383 }
369384 this . _disposables . dispose ( ) ;
370385 }
371386
372387 private getClient ( document : vscode . NotebookDocument ) : ReplServerClient {
373- let client = this . _clients . get ( document ) ;
388+ let client = this . clients . get ( document ) ;
374389 if ( ! client ) {
375390 client = new ReplServerClient ( document , this . extensionContext , this . pythonManager , this . outputChannel ( ) ) ;
376391 this . finalizeRegistry . register ( document , client ) ;
377- this . _clients . set ( document , client ) ;
392+ this . clients . set ( document , client ) ;
378393 }
379394 return client ;
380395 }
@@ -398,8 +413,7 @@ export class REPLNotebookController {
398413
399414 execution . start ( Date . now ( ) ) ;
400415 try {
401- const source = cell . document . getText ( ) ;
402- const result = await client . executeCell ( source ) ;
416+ const result = await client . executeCell ( cell ) ;
403417 if ( result !== undefined ) {
404418 success = result . success ;
405419
@@ -445,6 +459,20 @@ export class NotebookManager {
445459 ) ;
446460 await vscode . commands . executeCommand ( "vscode.openWith" , newNotebook . uri , "robotframework-repl" ) ;
447461 } ) ,
462+ vscode . commands . registerCommand ( "robotcode.notebookEditor.restartKernel" , ( ) => {
463+ const notebook = vscode . window . activeNotebookEditor ?. notebook ;
464+ if ( notebook ) {
465+ vscode . window . withProgress (
466+ {
467+ location : vscode . ProgressLocation . Notification ,
468+ title : "Restarting kernel..." ,
469+ } ,
470+ async ( _progress , _token ) => {
471+ this . _notebookController . disposeDocument ( notebook ) ;
472+ } ,
473+ ) ;
474+ }
475+ } ) ,
448476 ) ;
449477 }
450478
0 commit comments