Skip to content

Commit 1f8949c

Browse files
authored
Only select the python REPL kernel when creating/restoring the REPL (#25263)
fix #23971
1 parent 7607548 commit 1f8949c

File tree

3 files changed

+21
-42
lines changed

3 files changed

+21
-42
lines changed

src/client/repl/nativeRepl.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@
33

44
// Native Repl class that holds instance of pythonServer and replController
55

6-
import {
7-
NotebookController,
8-
NotebookControllerAffinity,
9-
NotebookDocument,
10-
QuickPickItem,
11-
TextEditor,
12-
Uri,
13-
WorkspaceFolder,
14-
} from 'vscode';
6+
import { NotebookController, NotebookDocument, QuickPickItem, TextEditor, Uri, WorkspaceFolder } from 'vscode';
157
import { Disposable } from 'vscode-jsonrpc';
168
import { PVSC_EXTENSION_ID } from '../common/constants';
179
import { showQuickPick } from '../common/vscodeApis/windowApis';
@@ -172,24 +164,19 @@ export class NativeRepl implements Disposable {
172164
}
173165
}
174166

175-
const notebookEditor = await openInteractiveREPL(
176-
this.replController,
177-
this.notebookDocument ?? wsMementoUri,
178-
preserveFocus,
179-
);
180-
if (notebookEditor) {
181-
this.notebookDocument = notebookEditor.notebook;
167+
const result = await openInteractiveREPL(this.notebookDocument ?? wsMementoUri, preserveFocus);
168+
if (result) {
169+
this.notebookDocument = result.notebookEditor.notebook;
182170
await updateWorkspaceStateValue<string | undefined>(
183171
NATIVE_REPL_URI_MEMENTO,
184172
this.notebookDocument.uri.toString(),
185173
);
186174

187-
if (this.notebookDocument) {
188-
this.replController.updateNotebookAffinity(this.notebookDocument, NotebookControllerAffinity.Default);
189-
await selectNotebookKernel(notebookEditor, this.replController.id, PVSC_EXTENSION_ID);
190-
if (code) {
191-
await executeNotebookCell(notebookEditor, code);
192-
}
175+
if (result.documentCreated) {
176+
await selectNotebookKernel(result.notebookEditor, this.replController.id, PVSC_EXTENSION_ID);
177+
}
178+
if (code) {
179+
await executeNotebookCell(result.notebookEditor, code);
193180
}
194181
}
195182
}

src/client/repl/replCommandHandler.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
NotebookController,
32
NotebookEditor,
43
ViewColumn,
54
NotebookDocument,
@@ -10,7 +9,6 @@ import {
109
Uri,
1110
} from 'vscode';
1211
import { getExistingReplViewColumn, getTabNameForUri } from './replUtils';
13-
import { PVSC_EXTENSION_ID } from '../common/constants';
1412
import { showNotebookDocument } from '../common/vscodeApis/windowApis';
1513
import { openNotebookDocument, applyEdit } from '../common/vscodeApis/workspaceApis';
1614
import { executeCommand } from '../common/vscodeApis/commandApis';
@@ -19,46 +17,42 @@ import { executeCommand } from '../common/vscodeApis/commandApis';
1917
* Function that opens/show REPL using IW UI.
2018
*/
2119
export async function openInteractiveREPL(
22-
notebookController: NotebookController,
2320
notebookDocument: NotebookDocument | Uri | undefined,
2421
preserveFocus: boolean = true,
25-
): Promise<NotebookEditor | undefined> {
22+
): Promise<{ notebookEditor: NotebookEditor; documentCreated: boolean } | undefined> {
2623
let viewColumn = ViewColumn.Beside;
24+
let alreadyExists = false;
2725
if (notebookDocument instanceof Uri) {
2826
// Case where NotebookDocument is undefined, but workspace mementoURI exists.
2927
notebookDocument = await openNotebookDocument(notebookDocument);
3028
} else if (notebookDocument) {
3129
// Case where NotebookDocument (REPL document already exists in the tab)
3230
const existingReplViewColumn = getExistingReplViewColumn(notebookDocument);
3331
viewColumn = existingReplViewColumn ?? viewColumn;
32+
alreadyExists = true;
3433
} else if (!notebookDocument) {
3534
// Case where NotebookDocument doesnt exist, or
3635
// became outdated (untitled.ipynb created without Python extension knowing, effectively taking over original Python REPL's URI)
3736
notebookDocument = await openNotebookDocument('jupyter-notebook');
3837
}
39-
const editor = await showNotebookDocument(notebookDocument!, {
38+
39+
const notebookEditor = await showNotebookDocument(notebookDocument!, {
4040
viewColumn,
4141
asRepl: 'Python REPL',
4242
preserveFocus,
4343
});
4444

4545
// Sanity check that we opened a Native REPL from showNotebookDocument.
4646
if (
47-
!editor ||
48-
!editor.notebook ||
49-
!editor.notebook.uri ||
50-
getTabNameForUri(editor.notebook.uri) !== 'Python REPL'
47+
!notebookEditor ||
48+
!notebookEditor.notebook ||
49+
!notebookEditor.notebook.uri ||
50+
getTabNameForUri(notebookEditor.notebook.uri) !== 'Python REPL'
5151
) {
5252
return undefined;
5353
}
5454

55-
await executeCommand('notebook.selectKernel', {
56-
editor,
57-
id: notebookController.id,
58-
extension: PVSC_EXTENSION_ID,
59-
});
60-
61-
return editor;
55+
return { notebookEditor, documentCreated: !alreadyExists };
6256
}
6357

6458
/**

src/client/repl/replCommands.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ export async function registerStartNativeReplCommand(
3131
sendTelemetryEvent(EventName.REPL, undefined, { replType: 'Native' });
3232
const interpreter = await getActiveInterpreter(uri, interpreterService);
3333
if (interpreter) {
34-
if (interpreter) {
35-
const nativeRepl = await getNativeRepl(interpreter, disposables);
36-
await nativeRepl.sendToNativeRepl(undefined, false);
37-
}
34+
const nativeRepl = await getNativeRepl(interpreter, disposables);
35+
await nativeRepl.sendToNativeRepl(undefined, false);
3836
}
3937
}),
4038
);

0 commit comments

Comments
 (0)