|
| 1 | +// Native Repl class that holds instance of pythonServer and replController |
| 2 | + |
| 3 | +import { NotebookController, NotebookControllerAffinity, NotebookDocument, TextEditor, workspace } from 'vscode'; |
| 4 | +import { Disposable } from 'vscode-jsonrpc'; |
| 5 | +import { PVSC_EXTENSION_ID } from '../common/constants'; |
| 6 | +import { PythonEnvironment } from '../pythonEnvironments/info'; |
| 7 | +import { createPythonServer, PythonServer } from './pythonServer'; |
| 8 | +import { executeNotebookCell, openInteractiveREPL, selectNotebookKernel } from './replCommandHandler'; |
| 9 | +import { createReplController } from './replController'; |
| 10 | + |
| 11 | +export class NativeRepl implements Disposable { |
| 12 | + private pythonServer: PythonServer; |
| 13 | + |
| 14 | + private interpreter: PythonEnvironment; |
| 15 | + |
| 16 | + private disposables: Disposable[] = []; |
| 17 | + |
| 18 | + private replController: NotebookController; |
| 19 | + |
| 20 | + private notebookDocument: NotebookDocument | undefined; |
| 21 | + |
| 22 | + // TODO: In the future, could also have attribute of URI for file specific REPL. |
| 23 | + constructor(interpreter: PythonEnvironment) { |
| 24 | + this.interpreter = interpreter; |
| 25 | + |
| 26 | + this.pythonServer = createPythonServer([interpreter.path as string]); |
| 27 | + this.replController = this.setReplController(); |
| 28 | + |
| 29 | + this.watchNotebookClosed(); |
| 30 | + } |
| 31 | + |
| 32 | + dispose(): void { |
| 33 | + this.disposables.forEach((d) => d.dispose()); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Function that watches for Notebook Closed event. |
| 38 | + * This is for the purposes of correctly updating the notebookEditor and notebookDocument on close. |
| 39 | + */ |
| 40 | + private watchNotebookClosed(): void { |
| 41 | + this.disposables.push( |
| 42 | + workspace.onDidCloseNotebookDocument((nb) => { |
| 43 | + if (this.notebookDocument && nb.uri.toString() === this.notebookDocument.uri.toString()) { |
| 44 | + this.notebookDocument = undefined; |
| 45 | + } |
| 46 | + }), |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Function that check if NotebookController for REPL exists, and returns it in Singleton manner. |
| 52 | + * @returns NotebookController |
| 53 | + */ |
| 54 | + public setReplController(): NotebookController { |
| 55 | + if (!this.replController) { |
| 56 | + return createReplController(this.interpreter.path, this.disposables); |
| 57 | + } |
| 58 | + return this.replController; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Function that checks if native REPL's text input box contains complete code. |
| 63 | + * @param activeEditor |
| 64 | + * @param pythonServer |
| 65 | + * @returns Promise<boolean> - True if complete/Valid code is present, False otherwise. |
| 66 | + */ |
| 67 | + public async checkUserInputCompleteCode(activeEditor: TextEditor | undefined): Promise<boolean> { |
| 68 | + let completeCode = false; |
| 69 | + let userTextInput; |
| 70 | + if (activeEditor) { |
| 71 | + const { document } = activeEditor; |
| 72 | + userTextInput = document.getText(); |
| 73 | + } |
| 74 | + |
| 75 | + // Check if userTextInput is a complete Python command |
| 76 | + if (userTextInput) { |
| 77 | + completeCode = await this.pythonServer.checkValidCommand(userTextInput); |
| 78 | + } |
| 79 | + |
| 80 | + return completeCode; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Function that opens interactive repl, selects kernel, and send/execute code to the native repl. |
| 85 | + * @param code |
| 86 | + */ |
| 87 | + public async sendToNativeRepl(code: string): Promise<void> { |
| 88 | + const notebookEditor = await openInteractiveREPL(this.replController, this.notebookDocument); |
| 89 | + this.notebookDocument = notebookEditor.notebook; |
| 90 | + |
| 91 | + if (this.notebookDocument) { |
| 92 | + this.replController.updateNotebookAffinity(this.notebookDocument, NotebookControllerAffinity.Default); |
| 93 | + await selectNotebookKernel(notebookEditor, this.replController.id, PVSC_EXTENSION_ID); |
| 94 | + await executeNotebookCell(this.notebookDocument, code); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +let nativeRepl: NativeRepl | undefined; // In multi REPL scenario, hashmap of URI to Repl. |
| 100 | + |
| 101 | +/** |
| 102 | + * Get Singleton Native REPL Instance |
| 103 | + * @param interpreter |
| 104 | + * @returns Native REPL instance |
| 105 | + */ |
| 106 | +export function getNativeRepl(interpreter: PythonEnvironment, disposables: Disposable[]): NativeRepl { |
| 107 | + if (!nativeRepl) { |
| 108 | + nativeRepl = new NativeRepl(interpreter); |
| 109 | + disposables.push(nativeRepl); |
| 110 | + } |
| 111 | + return nativeRepl; |
| 112 | +} |
0 commit comments