@@ -10,13 +10,8 @@ import {
1010 Uri ,
1111 workspace ,
1212 WorkspaceFolder ,
13- window ,
14- TabInputNotebook ,
15- TabInputTextDiff ,
16- TabInputText ,
1713} from 'vscode' ;
1814import { Disposable } from 'vscode-jsonrpc' ;
19- import * as path from 'path' ;
2015import { PVSC_EXTENSION_ID } from '../common/constants' ;
2116import { showQuickPick } from '../common/vscodeApis/windowApis' ;
2217import { getWorkspaceFolders } from '../common/vscodeApis/workspaceApis' ;
@@ -28,6 +23,7 @@ import { EventName } from '../telemetry/constants';
2823import { sendTelemetryEvent } from '../telemetry' ;
2924import { VariablesProvider } from './variables/variablesProvider' ;
3025import { VariableRequester } from './variables/variableRequester' ;
26+ import { getTabNameForUri } from './replUtils' ;
3127
3228const NATIVE_REPL_URI_MEMENTO = 'nativeReplUri' ;
3329let nativeRepl : NativeRepl | undefined ; // In multi REPL scenario, hashmap of URI to Repl.
@@ -47,8 +43,6 @@ export class NativeRepl implements Disposable {
4743
4844 public newReplSession : boolean | undefined = true ;
4945
50- private replUri : Uri | undefined ;
51-
5246 private context : ExtensionContext ;
5347
5448 // TODO: In the future, could also have attribute of URI for file specific REPL.
@@ -82,7 +76,6 @@ export class NativeRepl implements Disposable {
8276 if ( this . notebookDocument && nb . uri . toString ( ) === this . notebookDocument . uri . toString ( ) ) {
8377 this . notebookDocument = undefined ;
8478 this . newReplSession = true ;
85- this . replUri = undefined ;
8679 await this . context . globalState . update ( NATIVE_REPL_URI_MEMENTO , undefined ) ;
8780 }
8881 } ) ,
@@ -168,28 +161,20 @@ export class NativeRepl implements Disposable {
168161 if ( mementoUri ) {
169162 const replTabBeforeReload = openNotebookDocuments . find ( ( uri ) => uri . fsPath === mementoUri ?. fsPath ) ;
170163 if ( replTabBeforeReload ) {
171- this . replUri = replTabBeforeReload ;
172164 this . notebookDocument = workspace . notebookDocuments . find (
173165 ( doc ) => doc . uri . fsPath === replTabBeforeReload . fsPath ,
174166 ) ;
175- await this . context . globalState . update ( NATIVE_REPL_URI_MEMENTO , this . replUri . toString ( ) ) ;
176- const myFileName = path . basename ( this . replUri . fsPath ) ;
177-
178- // const tabNames = getOpenTabNames();
179- const tabLabel = getTabNameForUri ( this . replUri ) ;
180- if ( tabLabel !== 'Python REPL' ) {
181- const regex = / ^ U n t i t l e d - \d + \. i p y n b $ / ;
182- const isUntitled = regex . test ( myFileName ) ;
167+ await this . context . globalState . update ( NATIVE_REPL_URI_MEMENTO , replTabBeforeReload . toString ( ) ) ;
183168
184- this . replUri = undefined ;
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' ) {
185172 mementoUri = undefined ;
186-
187173 await this . context . globalState . update ( NATIVE_REPL_URI_MEMENTO , undefined ) ;
188174 this . notebookDocument = undefined ;
189175 }
190176 }
191177 } else {
192- this . replUri = undefined ;
193178 mementoUri = undefined ;
194179 await this . context . globalState . update ( NATIVE_REPL_URI_MEMENTO , undefined ) ;
195180 this . notebookDocument = undefined ;
@@ -198,8 +183,7 @@ export class NativeRepl implements Disposable {
198183 const notebookEditor = await openInteractiveREPL ( this . replController , this . notebookDocument , mementoUri ) ;
199184
200185 this . notebookDocument = notebookEditor . notebook ;
201- this . replUri = this . notebookDocument . uri ;
202- await this . context . globalState . update ( NATIVE_REPL_URI_MEMENTO , this . replUri . toString ( ) ) ;
186+ await this . context . globalState . update ( NATIVE_REPL_URI_MEMENTO , this . notebookDocument . uri . toString ( ) ) ;
203187
204188 if ( this . notebookDocument ) {
205189 this . replController . updateNotebookAffinity ( this . notebookDocument , NotebookControllerAffinity . Default ) ;
@@ -211,43 +195,6 @@ export class NativeRepl implements Disposable {
211195 }
212196}
213197
214- function getOpenTabNames ( ) : string [ ] {
215- const tabNames : string [ ] = [ ] ;
216- const tabGroups = window . tabGroups . all ;
217-
218- for ( const tabGroup of tabGroups ) {
219- for ( const tab of tabGroup . tabs ) {
220- tabNames . push ( tab . label ) ;
221- }
222- }
223- // TODO if tabName includes 'Python REPL' and there are other 'Untitled-*.ipynb' then, we need to re-create REPL instance with new URI.
224- return tabNames ;
225- }
226-
227- function getTabNameForUri ( uri : Uri ) : string | undefined {
228- const tabGroups = window . tabGroups . all ;
229-
230- for ( const tabGroup of tabGroups ) {
231- for ( const tab of tabGroup . tabs ) {
232- if ( tab . input instanceof TabInputText && tab . input . uri . toString ( ) === uri . toString ( ) ) {
233- return tab . label ;
234- }
235- if ( tab . input instanceof TabInputTextDiff ) {
236- if (
237- tab . input . original . toString ( ) === uri . toString ( ) ||
238- tab . input . modified . toString ( ) === uri . toString ( )
239- ) {
240- return tab . label ;
241- }
242- } else if ( tab . input instanceof TabInputNotebook && tab . input . uri . toString ( ) === uri . toString ( ) ) {
243- return tab . label ;
244- }
245- }
246- }
247-
248- return undefined ;
249- }
250-
251198/**
252199 * Get Singleton Native REPL Instance
253200 * @param interpreter
0 commit comments