Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 9 additions & 22 deletions src/client/repl/nativeRepl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<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);
}
if (result.documentCreated) {
await selectNotebookKernel(result.notebookEditor, this.replController.id, PVSC_EXTENSION_ID);
}
if (code) {
await executeNotebookCell(result.notebookEditor, code);
}
}
}
Expand Down
26 changes: 10 additions & 16 deletions src/client/repl/replCommandHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
NotebookController,
NotebookEditor,
ViewColumn,
NotebookDocument,
Expand All @@ -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';
Expand All @@ -19,46 +17,42 @@ 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<NotebookEditor | undefined> {
): 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);
} else if (notebookDocument) {
// 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,
});

// 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 };
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/client/repl/replCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}),
);
Expand Down
Loading