Skip to content

Commit 44bd14d

Browse files
committed
remove this.replUri to make my life easier
1 parent afa89c9 commit 44bd14d

File tree

3 files changed

+27
-61
lines changed

3 files changed

+27
-61
lines changed

src/client/repl/nativeRepl.ts

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ import {
1010
Uri,
1111
workspace,
1212
WorkspaceFolder,
13-
window,
14-
TabInputNotebook,
15-
TabInputTextDiff,
16-
TabInputText,
1713
} from 'vscode';
1814
import { Disposable } from 'vscode-jsonrpc';
19-
import * as path from 'path';
2015
import { PVSC_EXTENSION_ID } from '../common/constants';
2116
import { showQuickPick } from '../common/vscodeApis/windowApis';
2217
import { getWorkspaceFolders } from '../common/vscodeApis/workspaceApis';
@@ -28,6 +23,7 @@ import { EventName } from '../telemetry/constants';
2823
import { sendTelemetryEvent } from '../telemetry';
2924
import { VariablesProvider } from './variables/variablesProvider';
3025
import { VariableRequester } from './variables/variableRequester';
26+
import { getTabNameForUri } from './replUtils';
3127

3228
const NATIVE_REPL_URI_MEMENTO = 'nativeReplUri';
3329
let 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 = /^Untitled-\d+\.ipynb$/;
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

src/client/repl/replCommandHandler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ export async function openInteractiveREPL(
2626
let viewColumn = ViewColumn.Beside;
2727
if (mementoValue) {
2828
// also check if memento value URI tab has file name of Python REPL
29-
// Cachhed NotebookDocument exists.
29+
// Cached NotebookDocument exists.
3030
notebookDocument = await workspace.openNotebookDocument(mementoValue as Uri);
3131
} else if (notebookDocument) {
3232
// Case where NotebookDocument (REPL document already exists in the tab)
3333
const existingReplViewColumn = getExistingReplViewColumn(notebookDocument);
3434
viewColumn = existingReplViewColumn ?? viewColumn;
3535
} else if (!notebookDocument) {
36-
// Case where NotebookDocument doesnt exist, create a blank one.
36+
// Case where NotebookDocument doesnt exist, or
37+
// became outdated (untitled.ipynb created without Python extension knowing, effectively taking over original Python REPL's URI)
3738
notebookDocument = await workspace.openNotebookDocument('jupyter-notebook');
3839
}
3940
const editor = window.showNotebookDocument(notebookDocument!, {

src/client/repl/replUtils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,21 @@ export function getExistingReplViewColumn(notebookDocument: NotebookDocument): V
9999
}
100100
return undefined;
101101
}
102+
/**
103+
* Function that will return tab name for before reloading VS Code
104+
* This is so we can make sure tab name is still 'Python REPL' after reloading VS Code,
105+
* and make sure Python REPL does not get 'merged' into unaware untitled.ipynb tab.
106+
*/
107+
export function getTabNameForUri(uri: Uri): string | undefined {
108+
const tabGroups = window.tabGroups.all;
109+
110+
for (const tabGroup of tabGroups) {
111+
for (const tab of tabGroup.tabs) {
112+
if (tab.input instanceof TabInputNotebook && tab.input.uri.toString() === uri.toString()) {
113+
return tab.label;
114+
}
115+
}
116+
}
117+
118+
return undefined;
119+
}

0 commit comments

Comments
 (0)