-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Handle reloading of REPL Window #24148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 23 commits
cfa19b0
139f374
e080a03
112c0f4
6b46945
74a31ef
640e976
48c8686
4a278e7
3a0c4fd
f54ef3a
afa89c9
44bd14d
fc966f5
79a1478
140771b
418c1a3
5698f49
6bb4056
60fbabc
8d7e2ac
992c799
dc6dcb5
435c200
639caaf
c3648f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { | |
| NotebookDocument, | ||
| QuickPickItem, | ||
| TextEditor, | ||
| Uri, | ||
| workspace, | ||
| WorkspaceFolder, | ||
| } from 'vscode'; | ||
|
|
@@ -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; | ||
|
|
@@ -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); | ||
| } | ||
| }), | ||
| ); | ||
|
|
@@ -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(); | ||
|
||
| 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); | ||
anthonykim1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'; | ||
|
|
||
| /** | ||
|
|
@@ -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, | ||
|
||
| 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); | ||
|
||
| } | ||
| } 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, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.