11// Native Repl class that holds instance of pythonServer and replController
22
33import {
4- ExtensionContext ,
54 NotebookController ,
65 NotebookControllerAffinity ,
76 NotebookDocument ,
@@ -24,6 +23,7 @@ import { sendTelemetryEvent } from '../telemetry';
2423import { VariablesProvider } from './variables/variablesProvider' ;
2524import { VariableRequester } from './variables/variableRequester' ;
2625import { getTabNameForUri } from './replUtils' ;
26+ import { getWorkspaceStateValue , updateWorkspaceStateValue } from '../common/persistentState' ;
2727
2828export const NATIVE_REPL_URI_MEMENTO = 'nativeReplUri' ;
2929let nativeRepl : NativeRepl | undefined ;
@@ -43,17 +43,14 @@ export class NativeRepl implements Disposable {
4343
4444 public newReplSession : boolean | undefined = true ;
4545
46- private context : ExtensionContext ;
47-
4846 // TODO: In the future, could also have attribute of URI for file specific REPL.
49- private constructor ( context : ExtensionContext ) {
47+ private constructor ( ) {
5048 this . watchNotebookClosed ( ) ;
51- this . context = context ;
5249 }
5350
5451 // Static async factory method to handle asynchronous initialization
55- public static async create ( interpreter : PythonEnvironment , context : ExtensionContext ) : Promise < NativeRepl > {
56- const nativeRepl = new NativeRepl ( context ) ;
52+ public static async create ( interpreter : PythonEnvironment ) : Promise < NativeRepl > {
53+ const nativeRepl = new NativeRepl ( ) ;
5754 nativeRepl . interpreter = interpreter ;
5855 await nativeRepl . setReplDirectory ( ) ;
5956 nativeRepl . pythonServer = createPythonServer ( [ interpreter . path as string ] , nativeRepl . cwd ) ;
@@ -76,7 +73,8 @@ export class NativeRepl implements Disposable {
7673 if ( this . notebookDocument && nb . uri . toString ( ) === this . notebookDocument . uri . toString ( ) ) {
7774 this . notebookDocument = undefined ;
7875 this . newReplSession = true ;
79- await this . context . globalState . update ( NATIVE_REPL_URI_MEMENTO , undefined ) ;
76+ // await this.context.globalState.update(NATIVE_REPL_URI_MEMENTO, undefined);
77+ updateWorkspaceStateValue < string | undefined > ( NATIVE_REPL_URI_MEMENTO , undefined ) ;
8078 }
8179 } ) ,
8280 ) ;
@@ -154,38 +152,29 @@ export class NativeRepl implements Disposable {
154152 * Function that opens interactive repl, selects kernel, and send/execute code to the native repl.
155153 */
156154 public async sendToNativeRepl ( code ?: string | undefined , preserveFocus : boolean = true ) : Promise < void > {
157- const mementoValue = ( await this . context . globalState . get ( NATIVE_REPL_URI_MEMENTO ) ) as string | undefined ;
158- let mementoUri = mementoValue ? Uri . parse ( mementoValue ) : undefined ;
159- const openNotebookDocuments = workspace . notebookDocuments . map ( ( doc ) => doc . uri ) ;
160-
161- if ( mementoUri ) {
162- const replTabBeforeReload = openNotebookDocuments . find ( ( uri ) => uri . fsPath === mementoUri ?. fsPath ) ;
163- if ( replTabBeforeReload ) {
164- this . notebookDocument = workspace . notebookDocuments . find (
165- ( doc ) => doc . uri . fsPath === replTabBeforeReload . fsPath ,
166- ) ;
167- await this . context . globalState . update ( NATIVE_REPL_URI_MEMENTO , replTabBeforeReload . toString ( ) ) ;
168-
169- // If repl URI does not have tabLabel 'Python REPL', something has changed:
170- // e.g. creation of untitled notebook without Python extension knowing.
171- if ( getTabNameForUri ( replTabBeforeReload ) !== 'Python REPL' ) {
172- mementoUri = undefined ;
173- await this . cleanRepl ( ) ;
174- }
155+ let wsMementoUri : Uri | undefined ;
156+
157+ if ( ! this . notebookDocument ) {
158+ const wsMemento = getWorkspaceStateValue < string > ( NATIVE_REPL_URI_MEMENTO ) ;
159+ wsMementoUri = wsMemento ? Uri . parse ( wsMemento ) : undefined ;
160+
161+ if ( ! wsMementoUri || getTabNameForUri ( wsMementoUri ) !== 'Python REPL' ) {
162+ await this . cleanRepl ( ) ;
163+ wsMementoUri = undefined ;
164+ this . notebookDocument = undefined ;
175165 }
176- } else {
177- await this . cleanRepl ( ) ;
178166 }
179167
180168 const notebookEditor = await openInteractiveREPL (
181169 this . replController ,
182170 this . notebookDocument ,
183- mementoUri ,
171+ wsMementoUri ,
184172 preserveFocus ,
185173 ) ;
186174
187175 this . notebookDocument = notebookEditor . notebook ;
188- await this . context . globalState . update ( NATIVE_REPL_URI_MEMENTO , this . notebookDocument . uri . toString ( ) ) ;
176+ // await this.context.globalState.update(NATIVE_REPL_URI_MEMENTO, this.notebookDocument.uri.toString());
177+ updateWorkspaceStateValue < string | undefined > ( NATIVE_REPL_URI_MEMENTO , this . notebookDocument . uri . toString ( ) ) ;
189178
190179 if ( this . notebookDocument ) {
191180 this . replController . updateNotebookAffinity ( this . notebookDocument , NotebookControllerAffinity . Default ) ;
@@ -202,7 +191,8 @@ export class NativeRepl implements Disposable {
202191 */
203192 private async cleanRepl ( ) : Promise < void > {
204193 this . notebookDocument = undefined ;
205- await this . context . globalState . update ( NATIVE_REPL_URI_MEMENTO , undefined ) ;
194+ // await this.context.globalState.update(NATIVE_REPL_URI_MEMENTO, undefined);
195+ updateWorkspaceStateValue < string | undefined > ( NATIVE_REPL_URI_MEMENTO , undefined ) ;
206196 }
207197}
208198
@@ -211,13 +201,9 @@ export class NativeRepl implements Disposable {
211201 * @param interpreter
212202 * @returns Native REPL instance
213203 */
214- export async function getNativeRepl (
215- interpreter : PythonEnvironment ,
216- disposables : Disposable [ ] ,
217- context : ExtensionContext ,
218- ) : Promise < NativeRepl > {
204+ export async function getNativeRepl ( interpreter : PythonEnvironment , disposables : Disposable [ ] ) : Promise < NativeRepl > {
219205 if ( ! nativeRepl ) {
220- nativeRepl = await NativeRepl . create ( interpreter , context ) ;
206+ nativeRepl = await NativeRepl . create ( interpreter ) ;
221207 disposables . push ( nativeRepl ) ;
222208 }
223209 if ( nativeRepl && nativeRepl . newReplSession ) {
0 commit comments