Skip to content
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cfa19b0
Handle reloading REPL Window
anthonykim1 Sep 21, 2024
139f374
progress, still draft mode - exploring
anthonykim1 Sep 22, 2024
e080a03
Merge branch 'main' into reloading921
anthonykim1 Nov 13, 2024
112c0f4
bunch of TODOs
anthonykim1 Nov 13, 2024
6b46945
WIP, workspace.textDocuments.map returns notebookcell document => inv…
anthonykim1 Nov 13, 2024
74a31ef
properly reload via fsPath. TODO: could use workspace.notebookDocumen…
anthonykim1 Nov 13, 2024
640e976
use watching of notebookDocument instead of textEditor
anthonykim1 Nov 14, 2024
48c8686
remove unncessary todo
anthonykim1 Nov 14, 2024
4a278e7
tests
anthonykim1 Nov 14, 2024
3a0c4fd
tab groups are returning same untitled-1-ipynb for notebook and REPL
anthonykim1 Nov 15, 2024
f54ef3a
use tab.label to differentiate untitled notebook vs. native repl as s…
anthonykim1 Nov 15, 2024
afa89c9
now handling edge case but need a huge clean up
anthonykim1 Nov 16, 2024
44bd14d
remove this.replUri to make my life easier
anthonykim1 Nov 16, 2024
fc966f5
more refactoring to make my life easier
anthonykim1 Nov 16, 2024
79a1478
remove unused
anthonykim1 Nov 16, 2024
140771b
co-authored from @amunger via #24451
anthonykim1 Nov 18, 2024
418c1a3
test
anthonykim1 Nov 18, 2024
5698f49
remove context, unncessary openNB check, use workspace memento
anthonykim1 Nov 18, 2024
6bb4056
remove dup
anthonykim1 Nov 18, 2024
60fbabc
add different test for chagning to workspace memento from globalstate
anthonykim1 Nov 19, 2024
8d7e2ac
make compiler happy
anthonykim1 Nov 19, 2024
992c799
do not leave leftover comments
anthonykim1 Nov 19, 2024
dc6dcb5
better sanity check, more test
anthonykim1 Nov 19, 2024
435c200
make sure to use await
anthonykim1 Nov 19, 2024
639caaf
leverage notebookDocument parameter typing to be nb|Uri|undefined for…
anthonykim1 Nov 19, 2024
c3648f5
get rid of unncessary cleanRepl()
anthonykim1 Nov 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 48 additions & 11 deletions src/client/repl/nativeRepl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
NotebookDocument,
QuickPickItem,
TextEditor,
Uri,
workspace,
WorkspaceFolder,
} from 'vscode';
Expand All @@ -21,8 +22,11 @@ import { EventName } from '../telemetry/constants';
import { sendTelemetryEvent } from '../telemetry';
import { VariablesProvider } from './variables/variablesProvider';
import { VariableRequester } from './variables/variableRequester';
import { getTabNameForUri } from './replUtils';
import { getWorkspaceStateValue, updateWorkspaceStateValue } from '../common/persistentState';

let nativeRepl: NativeRepl | undefined; // In multi REPL scenario, hashmap of URI to Repl.
export const NATIVE_REPL_URI_MEMENTO = 'nativeReplUri';
let nativeRepl: NativeRepl | undefined;
export class NativeRepl implements Disposable {
// Adding ! since it will get initialized in create method, not the constructor.
private pythonServer!: PythonServer;
Expand Down Expand Up @@ -65,10 +69,11 @@ export class NativeRepl implements Disposable {
*/
private watchNotebookClosed(): void {
this.disposables.push(
workspace.onDidCloseNotebookDocument((nb) => {
workspace.onDidCloseNotebookDocument(async (nb) => {
if (this.notebookDocument && nb.uri.toString() === this.notebookDocument.uri.toString()) {
this.notebookDocument = undefined;
this.newReplSession = true;
updateWorkspaceStateValue<string | undefined>(NATIVE_REPL_URI_MEMENTO, undefined);
}
}),
);
Expand Down Expand Up @@ -145,17 +150,49 @@ export class NativeRepl implements Disposable {
/**
* Function that opens interactive repl, selects kernel, and send/execute code to the native repl.
*/
public async sendToNativeRepl(code?: string): Promise<void> {
const notebookEditor = await openInteractiveREPL(this.replController, this.notebookDocument);
this.notebookDocument = notebookEditor.notebook;

if (this.notebookDocument) {
this.replController.updateNotebookAffinity(this.notebookDocument, NotebookControllerAffinity.Default);
await selectNotebookKernel(notebookEditor, this.replController.id, PVSC_EXTENSION_ID);
if (code) {
await executeNotebookCell(notebookEditor, code);
public async sendToNativeRepl(code?: string | undefined, preserveFocus: boolean = true): Promise<void> {
let wsMementoUri: Uri | undefined;

if (!this.notebookDocument) {
const wsMemento = getWorkspaceStateValue<string>(NATIVE_REPL_URI_MEMENTO);
wsMementoUri = wsMemento ? Uri.parse(wsMemento) : undefined;

if (!wsMementoUri || getTabNameForUri(wsMementoUri) !== 'Python REPL') {
await this.cleanRepl();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are in here because this.notebookDocument is undefined. So why are you clearing it again? also, it does not seem like it gets set or used here (in this if block) so why is this being checked.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is for case: #24148 (comment)
This case, memento URI will be referencing old native REPL, whereas in reality the untitled notebook have already taken over the URI of the old native REPL. In this case, to prevent native REPL creeping into the untitled jupyter notebook, we have to explicitly check for if the URI belongs to Python REPL, and properly clean up everything so that new native REPL can be created with different URI.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my question here was about this.notebookDocument. Its state does not seem to change within this if, so why are we clearing it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh I read this wrong, I understand this now. Yeah it wont change inside the if block, so no need at all:
c3648f5

wsMementoUri = undefined;
}
}

const notebookEditor = await openInteractiveREPL(
this.replController,
this.notebookDocument,
wsMementoUri,
preserveFocus,
);
if (notebookEditor) {
this.notebookDocument = notebookEditor.notebook;
updateWorkspaceStateValue<string | undefined>(
NATIVE_REPL_URI_MEMENTO,
this.notebookDocument.uri.toString(),
);

if (this.notebookDocument) {
this.replController.updateNotebookAffinity(this.notebookDocument, NotebookControllerAffinity.Default);
await selectNotebookKernel(notebookEditor, this.replController.id, PVSC_EXTENSION_ID);
if (code) {
await executeNotebookCell(notebookEditor, code);
}
}
}
}

/**
* Properly clean up notebook document stored inside Native REPL.
* Also remove the Native REPL URI from memento to prepare for brand new REPL creation.
*/
private async cleanRepl(): Promise<void> {
this.notebookDocument = undefined;
updateWorkspaceStateValue<string | undefined>(NATIVE_REPL_URI_MEMENTO, undefined);
}
}

Expand Down
34 changes: 26 additions & 8 deletions src/client/repl/replCommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import {
NotebookEdit,
WorkspaceEdit,
workspace,
Uri,
} from 'vscode';
import { getExistingReplViewColumn } from './replUtils';
import { getExistingReplViewColumn, getTabNameForUri } from './replUtils';
import { PVSC_EXTENSION_ID } from '../common/constants';

/**
Expand All @@ -20,22 +21,39 @@ import { PVSC_EXTENSION_ID } from '../common/constants';
export async function openInteractiveREPL(
notebookController: NotebookController,
notebookDocument: NotebookDocument | undefined,
): Promise<NotebookEditor> {
mementoValue: Uri | undefined,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like this should not be passed to this function. The function already takes a notebookDocument. You can make notebookDocument arg flexible NotebookDocument | Uri | undefined . If notebook document is instance of Uri, use openNotebookDocument, else use the notebookDocument.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this, 639caaf

preserveFocus: boolean = true,
): Promise<NotebookEditor | undefined> {
let viewColumn = ViewColumn.Beside;

// Case where NotebookDocument (REPL document already exists in the tab)
if (notebookDocument) {
if (mementoValue) {
if (!notebookDocument) {
notebookDocument = await workspace.openNotebookDocument(mementoValue as Uri);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid depending directly on workspace, window, commands. if you want to use our Unit Test framework effectively. One of the things you do with Unit testing is control the boundary. These APIs are the boundary for the extension, and using them directly like this can break that boundary too often. Also, this is a broad namespace, makes it hard to mock.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10000% agree. I created #24426
I think I should go through major refactoring for native REPL code soon, and replace all these workspace, window APIs

}
} else if (notebookDocument) {
// Case where NotebookDocument (REPL document already exists in the tab)
const existingReplViewColumn = getExistingReplViewColumn(notebookDocument);
viewColumn = existingReplViewColumn ?? viewColumn;
} else if (!notebookDocument) {
// Case where NotebookDocument doesnt exist, create a blank one.
// Case where NotebookDocument doesnt exist, or
// became outdated (untitled.ipynb created without Python extension knowing, effectively taking over original Python REPL's URI)
notebookDocument = await workspace.openNotebookDocument('jupyter-notebook');
}
const editor = window.showNotebookDocument(notebookDocument!, {
const editor = await window.showNotebookDocument(notebookDocument!, {
viewColumn,
asRepl: 'Python REPL',
preserveFocus: true,
preserveFocus,
});

// Sanity check that we opened a Native REPL from showNotebookDocument.
if (
!editor ||
!editor.notebook ||
!editor.notebook.uri ||
getTabNameForUri(editor.notebook.uri) !== 'Python REPL'
) {
return undefined;
}

await commands.executeCommand('notebook.selectKernel', {
editor,
id: notebookController.id,
Expand Down
2 changes: 1 addition & 1 deletion src/client/repl/replCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function registerStartNativeReplCommand(
if (interpreter) {
if (interpreter) {
const nativeRepl = await getNativeRepl(interpreter, disposables);
await nativeRepl.sendToNativeRepl();
await nativeRepl.sendToNativeRepl(undefined, false);
}
}
}),
Expand Down
19 changes: 19 additions & 0 deletions src/client/repl/replUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,22 @@ export function getExistingReplViewColumn(notebookDocument: NotebookDocument): V
}
return undefined;
}

/**
* Function that will return tab name for before reloading VS Code
* This is so we can make sure tab name is still 'Python REPL' after reloading VS Code,
* and make sure Python REPL does not get 'merged' into unaware untitled.ipynb tab.
*/
export function getTabNameForUri(uri: Uri): string | undefined {
const tabGroups = window.tabGroups.all;

for (const tabGroup of tabGroups) {
for (const tab of tabGroup.tabs) {
if (tab.input instanceof TabInputNotebook && tab.input.uri.toString() === uri.toString()) {
return tab.label;
}
}
}

return undefined;
}
32 changes: 30 additions & 2 deletions src/test/repl/nativeRepl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import { expect } from 'chai';
import { IInterpreterService } from '../../client/interpreter/contracts';
import { PythonEnvironment } from '../../client/pythonEnvironments/info';
import { getNativeRepl, NativeRepl } from '../../client/repl/nativeRepl';
import * as persistentState from '../../client/common/persistentState';

suite('REPL - Native REPL', () => {
let interpreterService: TypeMoq.IMock<IInterpreterService>;

let disposable: TypeMoq.IMock<Disposable>;
let disposableArray: Disposable[] = [];

let setReplDirectoryStub: sinon.SinonStub;
let setReplControllerSpy: sinon.SinonSpy;
let getWorkspaceStateValueStub: sinon.SinonStub;
let updateWorkspaceStateValueStub: sinon.SinonStub;

setup(() => {
interpreterService = TypeMoq.Mock.ofType<IInterpreterService>();
Expand All @@ -29,6 +31,7 @@ suite('REPL - Native REPL', () => {
setReplDirectoryStub = sinon.stub(NativeRepl.prototype as any, 'setReplDirectory').resolves(); // Stubbing private method
// Use a spy instead of a stub for setReplController
setReplControllerSpy = sinon.spy(NativeRepl.prototype, 'setReplController');
updateWorkspaceStateValueStub = sinon.stub(persistentState, 'updateWorkspaceStateValue').resolves();
});

teardown(() => {
Expand All @@ -37,7 +40,6 @@ suite('REPL - Native REPL', () => {
d.dispose();
}
});

disposableArray = [];
sinon.restore();
});
Expand All @@ -53,6 +55,32 @@ suite('REPL - Native REPL', () => {
expect(createMethodStub.calledOnce).to.be.true;
});

test('sendToNativeRepl should look for memento URI if notebook document is undefined', async () => {
getWorkspaceStateValueStub = sinon.stub(persistentState, 'getWorkspaceStateValue').returns(undefined);
interpreterService
.setup((i) => i.getActiveInterpreter(TypeMoq.It.isAny()))
.returns(() => Promise.resolve(({ path: 'ps' } as unknown) as PythonEnvironment));
const interpreter = await interpreterService.object.getActiveInterpreter();
const nativeRepl = await getNativeRepl(interpreter as PythonEnvironment, disposableArray);

nativeRepl.sendToNativeRepl(undefined, false);

expect(getWorkspaceStateValueStub.calledOnce).to.be.true;
});

test('sendToNativeRepl should call updateWorkspaceStateValue', async () => {
getWorkspaceStateValueStub = sinon.stub(persistentState, 'getWorkspaceStateValue').returns('myNameIsMemento');
interpreterService
.setup((i) => i.getActiveInterpreter(TypeMoq.It.isAny()))
.returns(() => Promise.resolve(({ path: 'ps' } as unknown) as PythonEnvironment));
const interpreter = await interpreterService.object.getActiveInterpreter();
const nativeRepl = await getNativeRepl(interpreter as PythonEnvironment, disposableArray);

nativeRepl.sendToNativeRepl(undefined, false);

expect(updateWorkspaceStateValueStub.calledOnce).to.be.true;
});

test('create should call setReplDirectory, setReplController', async () => {
const interpreter = await interpreterService.object.getActiveInterpreter();
interpreterService
Expand Down
1 change: 1 addition & 0 deletions src/test/repl/replCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ suite('REPL - register native repl command', () => {
let getNativeReplStub: sinon.SinonStub;
let disposable: TypeMoq.IMock<Disposable>;
let disposableArray: Disposable[] = [];

setup(() => {
interpreterService = TypeMoq.Mock.ofType<IInterpreterService>();
commandManager = TypeMoq.Mock.ofType<ICommandManager>();
Expand Down
Loading