Skip to content

Commit afa89c9

Browse files
committed
now handling edge case but need a huge clean up
1 parent f54ef3a commit afa89c9

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/client/repl/nativeRepl.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import {
66
NotebookControllerAffinity,
77
NotebookDocument,
88
QuickPickItem,
9-
TabInputText,
109
TextEditor,
1110
Uri,
1211
workspace,
1312
WorkspaceFolder,
1413
window,
15-
TabInputTextDiff,
1614
TabInputNotebook,
15+
TabInputTextDiff,
16+
TabInputText,
1717
} from 'vscode';
1818
import { Disposable } from 'vscode-jsonrpc';
1919
import * as path from 'path';
@@ -174,13 +174,25 @@ export class NativeRepl implements Disposable {
174174
);
175175
await this.context.globalState.update(NATIVE_REPL_URI_MEMENTO, this.replUri.toString());
176176
const myFileName = path.basename(this.replUri.fsPath);
177-
const whatever = 'hi';
178-
const tabNames = getOpenTabNames();
177+
178+
// const tabNames = getOpenTabNames();
179+
const tabLabel = getTabNameForUri(this.replUri);
180+
if (tabLabel !== 'Python REPL') {
181+
const regex = /^Untitled-\d+\.ipynb$/;
182+
const isUntitled = regex.test(myFileName);
183+
184+
this.replUri = undefined;
185+
mementoUri = undefined;
186+
187+
await this.context.globalState.update(NATIVE_REPL_URI_MEMENTO, undefined);
188+
this.notebookDocument = undefined;
189+
}
179190
}
180191
} else {
181192
this.replUri = undefined;
182193
mementoUri = undefined;
183194
await this.context.globalState.update(NATIVE_REPL_URI_MEMENTO, undefined);
195+
this.notebookDocument = undefined;
184196
}
185197

186198
const notebookEditor = await openInteractiveREPL(this.replController, this.notebookDocument, mementoUri);
@@ -208,10 +220,34 @@ function getOpenTabNames(): string[] {
208220
tabNames.push(tab.label);
209221
}
210222
}
211-
223+
// TODO if tabName includes 'Python REPL' and there are other 'Untitled-*.ipynb' then, we need to re-create REPL instance with new URI.
212224
return tabNames;
213225
}
214226

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+
215251
/**
216252
* Get Singleton Native REPL Instance
217253
* @param interpreter

src/client/repl/replCommandHandler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export async function openInteractiveREPL(
2525
): Promise<NotebookEditor> {
2626
let viewColumn = ViewColumn.Beside;
2727
if (mementoValue) {
28+
// also check if memento value URI tab has file name of Python REPL
2829
// Cachhed NotebookDocument exists.
2930
notebookDocument = await workspace.openNotebookDocument(mementoValue as Uri);
3031
} else if (notebookDocument) {

0 commit comments

Comments
 (0)