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