diff --git a/src/client/repl/nativeRepl.ts b/src/client/repl/nativeRepl.ts index 9b002655d714..62e314172da6 100644 --- a/src/client/repl/nativeRepl.ts +++ b/src/client/repl/nativeRepl.ts @@ -3,15 +3,7 @@ // Native Repl class that holds instance of pythonServer and replController -import { - NotebookController, - NotebookControllerAffinity, - NotebookDocument, - QuickPickItem, - TextEditor, - Uri, - WorkspaceFolder, -} from 'vscode'; +import { NotebookController, NotebookDocument, QuickPickItem, TextEditor, Uri, WorkspaceFolder } from 'vscode'; import { Disposable } from 'vscode-jsonrpc'; import { PVSC_EXTENSION_ID } from '../common/constants'; import { showQuickPick } from '../common/vscodeApis/windowApis'; @@ -172,24 +164,19 @@ export class NativeRepl implements Disposable { } } - const notebookEditor = await openInteractiveREPL( - this.replController, - this.notebookDocument ?? wsMementoUri, - preserveFocus, - ); - if (notebookEditor) { - this.notebookDocument = notebookEditor.notebook; + const result = await openInteractiveREPL(this.notebookDocument ?? wsMementoUri, preserveFocus); + if (result) { + this.notebookDocument = result.notebookEditor.notebook; await updateWorkspaceStateValue( 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); - } + if (result.documentCreated) { + await selectNotebookKernel(result.notebookEditor, this.replController.id, PVSC_EXTENSION_ID); + } + if (code) { + await executeNotebookCell(result.notebookEditor, code); } } } diff --git a/src/client/repl/replCommandHandler.ts b/src/client/repl/replCommandHandler.ts index f65580dd1e17..630eddfdd565 100644 --- a/src/client/repl/replCommandHandler.ts +++ b/src/client/repl/replCommandHandler.ts @@ -1,5 +1,4 @@ import { - NotebookController, NotebookEditor, ViewColumn, NotebookDocument, @@ -10,7 +9,6 @@ import { Uri, } from 'vscode'; import { getExistingReplViewColumn, getTabNameForUri } from './replUtils'; -import { PVSC_EXTENSION_ID } from '../common/constants'; import { showNotebookDocument } from '../common/vscodeApis/windowApis'; import { openNotebookDocument, applyEdit } from '../common/vscodeApis/workspaceApis'; import { executeCommand } from '../common/vscodeApis/commandApis'; @@ -19,11 +17,11 @@ import { executeCommand } from '../common/vscodeApis/commandApis'; * Function that opens/show REPL using IW UI. */ export async function openInteractiveREPL( - notebookController: NotebookController, notebookDocument: NotebookDocument | Uri | undefined, preserveFocus: boolean = true, -): Promise { +): Promise<{ notebookEditor: NotebookEditor; documentCreated: boolean } | undefined> { let viewColumn = ViewColumn.Beside; + let alreadyExists = false; if (notebookDocument instanceof Uri) { // Case where NotebookDocument is undefined, but workspace mementoURI exists. notebookDocument = await openNotebookDocument(notebookDocument); @@ -31,12 +29,14 @@ export async function openInteractiveREPL( // Case where NotebookDocument (REPL document already exists in the tab) const existingReplViewColumn = getExistingReplViewColumn(notebookDocument); viewColumn = existingReplViewColumn ?? viewColumn; + alreadyExists = true; } else if (!notebookDocument) { // 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 openNotebookDocument('jupyter-notebook'); } - const editor = await showNotebookDocument(notebookDocument!, { + + const notebookEditor = await showNotebookDocument(notebookDocument!, { viewColumn, asRepl: 'Python REPL', preserveFocus, @@ -44,21 +44,15 @@ export async function openInteractiveREPL( // Sanity check that we opened a Native REPL from showNotebookDocument. if ( - !editor || - !editor.notebook || - !editor.notebook.uri || - getTabNameForUri(editor.notebook.uri) !== 'Python REPL' + !notebookEditor || + !notebookEditor.notebook || + !notebookEditor.notebook.uri || + getTabNameForUri(notebookEditor.notebook.uri) !== 'Python REPL' ) { return undefined; } - await executeCommand('notebook.selectKernel', { - editor, - id: notebookController.id, - extension: PVSC_EXTENSION_ID, - }); - - return editor; + return { notebookEditor, documentCreated: !alreadyExists }; } /** diff --git a/src/client/repl/replCommands.ts b/src/client/repl/replCommands.ts index f260185988a8..993a0cc91b19 100644 --- a/src/client/repl/replCommands.ts +++ b/src/client/repl/replCommands.ts @@ -31,10 +31,8 @@ export async function registerStartNativeReplCommand( sendTelemetryEvent(EventName.REPL, undefined, { replType: 'Native' }); const interpreter = await getActiveInterpreter(uri, interpreterService); if (interpreter) { - if (interpreter) { - const nativeRepl = await getNativeRepl(interpreter, disposables); - await nativeRepl.sendToNativeRepl(undefined, false); - } + const nativeRepl = await getNativeRepl(interpreter, disposables); + await nativeRepl.sendToNativeRepl(undefined, false); } }), );