11// Native Repl class that holds instance of pythonServer and replController
22
33import {
4+ ExtensionContext ,
45 NotebookController ,
56 NotebookControllerAffinity ,
67 NotebookDocument ,
78 QuickPickItem ,
89 TextEditor ,
10+ Uri ,
911 workspace ,
1012 WorkspaceFolder ,
1113} from 'vscode' ;
@@ -22,6 +24,7 @@ import { sendTelemetryEvent } from '../telemetry';
2224import { VariablesProvider } from './variables/variablesProvider' ;
2325import { VariableRequester } from './variables/variableRequester' ;
2426
27+ const NATIVE_REPL_URI_MEMENTO = 'nativeReplUri' ;
2528let nativeRepl : NativeRepl | undefined ; // In multi REPL scenario, hashmap of URI to Repl.
2629export class NativeRepl implements Disposable {
2730 // Adding ! since it will get initialized in create method, not the constructor.
@@ -39,14 +42,19 @@ export class NativeRepl implements Disposable {
3942
4043 public newReplSession : boolean | undefined = true ;
4144
45+ private replUri : Uri | undefined ;
46+
47+ private context : ExtensionContext ;
48+
4249 // TODO: In the future, could also have attribute of URI for file specific REPL.
43- private constructor ( ) {
50+ private constructor ( context : ExtensionContext ) {
4451 this . watchNotebookClosed ( ) ;
52+ this . context = context ;
4553 }
4654
4755 // Static async factory method to handle asynchronous initialization
48- public static async create ( interpreter : PythonEnvironment ) : Promise < NativeRepl > {
49- const nativeRepl = new NativeRepl ( ) ;
56+ public static async create ( interpreter : PythonEnvironment , context : ExtensionContext ) : Promise < NativeRepl > {
57+ const nativeRepl = new NativeRepl ( context ) ;
5058 nativeRepl . interpreter = interpreter ;
5159 await nativeRepl . setReplDirectory ( ) ;
5260 nativeRepl . pythonServer = createPythonServer ( [ interpreter . path as string ] , nativeRepl . cwd ) ;
@@ -65,10 +73,12 @@ export class NativeRepl implements Disposable {
6573 */
6674 private watchNotebookClosed ( ) : void {
6775 this . disposables . push (
68- workspace . onDidCloseNotebookDocument ( ( nb ) => {
76+ workspace . onDidCloseNotebookDocument ( async ( nb ) => {
6977 if ( this . notebookDocument && nb . uri . toString ( ) === this . notebookDocument . uri . toString ( ) ) {
7078 this . notebookDocument = undefined ;
7179 this . newReplSession = true ;
80+ this . replUri = undefined ;
81+ await this . context . globalState . update ( NATIVE_REPL_URI_MEMENTO , undefined ) ;
7282 }
7383 } ) ,
7484 ) ;
@@ -152,6 +162,8 @@ export class NativeRepl implements Disposable {
152162 public async sendToNativeRepl ( code ?: string ) : Promise < void > {
153163 const notebookEditor = await openInteractiveREPL ( this . replController , this . notebookDocument ) ;
154164 this . notebookDocument = notebookEditor . notebook ;
165+ this . replUri = this . notebookDocument . uri ;
166+ await this . context . globalState . update ( NATIVE_REPL_URI_MEMENTO , this . replUri ) ;
155167
156168 if ( this . notebookDocument ) {
157169 this . replController . updateNotebookAffinity ( this . notebookDocument , NotebookControllerAffinity . Default ) ;
@@ -168,9 +180,13 @@ export class NativeRepl implements Disposable {
168180 * @param interpreter
169181 * @returns Native REPL instance
170182 */
171- export async function getNativeRepl ( interpreter : PythonEnvironment , disposables : Disposable [ ] ) : Promise < NativeRepl > {
183+ export async function getNativeRepl (
184+ interpreter : PythonEnvironment ,
185+ disposables : Disposable [ ] ,
186+ context : ExtensionContext ,
187+ ) : Promise < NativeRepl > {
172188 if ( ! nativeRepl ) {
173- nativeRepl = await NativeRepl . create ( interpreter ) ;
189+ nativeRepl = await NativeRepl . create ( interpreter , context ) ;
174190 disposables . push ( nativeRepl ) ;
175191 }
176192 if ( nativeRepl && nativeRepl . newReplSession ) {
0 commit comments