66 NotebookDocument ,
77 QuickPickItem ,
88 TextEditor ,
9+ Uri ,
910 workspace ,
1011 WorkspaceFolder ,
1112} from 'vscode' ;
@@ -21,8 +22,11 @@ import { EventName } from '../telemetry/constants';
2122import { sendTelemetryEvent } from '../telemetry' ;
2223import { VariablesProvider } from './variables/variablesProvider' ;
2324import { VariableRequester } from './variables/variableRequester' ;
25+ import { getTabNameForUri } from './replUtils' ;
26+ import { getWorkspaceStateValue , updateWorkspaceStateValue } from '../common/persistentState' ;
2427
25- let nativeRepl : NativeRepl | undefined ; // In multi REPL scenario, hashmap of URI to Repl.
28+ export const NATIVE_REPL_URI_MEMENTO = 'nativeReplUri' ;
29+ let nativeRepl : NativeRepl | undefined ;
2630export class NativeRepl implements Disposable {
2731 // Adding ! since it will get initialized in create method, not the constructor.
2832 private pythonServer ! : PythonServer ;
@@ -65,10 +69,11 @@ export class NativeRepl implements Disposable {
6569 */
6670 private watchNotebookClosed ( ) : void {
6771 this . disposables . push (
68- workspace . onDidCloseNotebookDocument ( ( nb ) => {
72+ workspace . onDidCloseNotebookDocument ( async ( nb ) => {
6973 if ( this . notebookDocument && nb . uri . toString ( ) === this . notebookDocument . uri . toString ( ) ) {
7074 this . notebookDocument = undefined ;
7175 this . newReplSession = true ;
76+ await updateWorkspaceStateValue < string | undefined > ( NATIVE_REPL_URI_MEMENTO , undefined ) ;
7277 }
7378 } ) ,
7479 ) ;
@@ -145,15 +150,37 @@ export class NativeRepl implements Disposable {
145150 /**
146151 * Function that opens interactive repl, selects kernel, and send/execute code to the native repl.
147152 */
148- public async sendToNativeRepl ( code ?: string ) : Promise < void > {
149- const notebookEditor = await openInteractiveREPL ( this . replController , this . notebookDocument ) ;
150- this . notebookDocument = notebookEditor . notebook ;
151-
152- if ( this . notebookDocument ) {
153- this . replController . updateNotebookAffinity ( this . notebookDocument , NotebookControllerAffinity . Default ) ;
154- await selectNotebookKernel ( notebookEditor , this . replController . id , PVSC_EXTENSION_ID ) ;
155- if ( code ) {
156- await executeNotebookCell ( notebookEditor , code ) ;
153+ public async sendToNativeRepl ( code ?: string | undefined , preserveFocus : boolean = true ) : Promise < void > {
154+ let wsMementoUri : Uri | undefined ;
155+
156+ if ( ! this . notebookDocument ) {
157+ const wsMemento = getWorkspaceStateValue < string > ( NATIVE_REPL_URI_MEMENTO ) ;
158+ wsMementoUri = wsMemento ? Uri . parse ( wsMemento ) : undefined ;
159+
160+ if ( ! wsMementoUri || getTabNameForUri ( wsMementoUri ) !== 'Python REPL' ) {
161+ await updateWorkspaceStateValue < string | undefined > ( NATIVE_REPL_URI_MEMENTO , undefined ) ;
162+ wsMementoUri = undefined ;
163+ }
164+ }
165+
166+ const notebookEditor = await openInteractiveREPL (
167+ this . replController ,
168+ this . notebookDocument ?? wsMementoUri ,
169+ preserveFocus ,
170+ ) ;
171+ if ( notebookEditor ) {
172+ this . notebookDocument = notebookEditor . notebook ;
173+ await updateWorkspaceStateValue < string | undefined > (
174+ NATIVE_REPL_URI_MEMENTO ,
175+ this . notebookDocument . uri . toString ( ) ,
176+ ) ;
177+
178+ if ( this . notebookDocument ) {
179+ this . replController . updateNotebookAffinity ( this . notebookDocument , NotebookControllerAffinity . Default ) ;
180+ await selectNotebookKernel ( notebookEditor , this . replController . id , PVSC_EXTENSION_ID ) ;
181+ if ( code ) {
182+ await executeNotebookCell ( notebookEditor , code ) ;
183+ }
157184 }
158185 }
159186 }
0 commit comments