forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathnativeRepl.ts
More file actions
181 lines (158 loc) · 6.92 KB
/
nativeRepl.ts
File metadata and controls
181 lines (158 loc) · 6.92 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Native Repl class that holds instance of pythonServer and replController
import {
NotebookController,
NotebookControllerAffinity,
NotebookDocument,
QuickPickItem,
TextEditor,
workspace,
WorkspaceFolder,
} from 'vscode';
import { Disposable } from 'vscode-jsonrpc';
import { PVSC_EXTENSION_ID } from '../common/constants';
import { showQuickPick } from '../common/vscodeApis/windowApis';
import { getWorkspaceFolders } from '../common/vscodeApis/workspaceApis';
import { PythonEnvironment } from '../pythonEnvironments/info';
import { createPythonServer, PythonServer } from './pythonServer';
import { executeNotebookCell, openInteractiveREPL, selectNotebookKernel } from './replCommandHandler';
import { createReplController } from './replController';
import { EventName } from '../telemetry/constants';
import { sendTelemetryEvent } from '../telemetry';
import { VariablesProvider } from './variables/variablesProvider';
import { VariableRequester } from './variables/variableRequester';
let nativeRepl: NativeRepl | undefined; // In multi REPL scenario, hashmap of URI to Repl.
export class NativeRepl implements Disposable {
// Adding ! since it will get initialized in create method, not the constructor.
private pythonServer!: PythonServer;
private cwd: string | undefined;
private interpreter!: PythonEnvironment;
private disposables: Disposable[] = [];
private replController!: NotebookController;
private notebookDocument: NotebookDocument | undefined;
public newReplSession: boolean | undefined = true;
// TODO: In the future, could also have attribute of URI for file specific REPL.
private constructor() {
this.watchNotebookClosed();
}
// Static async factory method to handle asynchronous initialization
public static async create(interpreter: PythonEnvironment): Promise<NativeRepl> {
const nativeRepl = new NativeRepl();
nativeRepl.interpreter = interpreter;
await nativeRepl.setReplDirectory();
nativeRepl.pythonServer = createPythonServer([interpreter.path as string], nativeRepl.cwd);
nativeRepl.setReplController();
return nativeRepl;
}
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;
this.newReplSession = true;
}
}),
);
}
/**
* Function that set up desired directory for REPL.
* If there is multiple workspaces, prompt the user to choose
* which directory we should set in context of native REPL.
*/
private async setReplDirectory(): Promise<void> {
// Figure out uri via workspaceFolder as uri parameter always
// seem to be undefined from parameter when trying to access from replCommands.ts
const workspaces: readonly WorkspaceFolder[] | undefined = getWorkspaceFolders();
if (workspaces) {
// eslint-disable-next-line no-shadow
const workspacesQuickPickItems: QuickPickItem[] = workspaces.map((workspace) => ({
label: workspace.name,
description: workspace.uri.fsPath,
}));
if (workspacesQuickPickItems.length === 0) {
this.cwd = process.cwd(); // Yields '/' on no workspace scenario.
} else if (workspacesQuickPickItems.length === 1) {
this.cwd = workspacesQuickPickItems[0].description;
} else {
// Show choices of workspaces for user to choose from.
const selection = (await showQuickPick(workspacesQuickPickItems, {
placeHolder: 'Select current working directory for new REPL',
matchOnDescription: true,
ignoreFocusOut: true,
})) as QuickPickItem;
this.cwd = selection?.description;
}
}
}
/**
* Function that check if NotebookController for REPL exists, and returns it in Singleton manner.
* @returns NotebookController
*/
public setReplController(): NotebookController {
if (!this.replController) {
this.replController = createReplController(this.interpreter!.path, this.disposables, this.cwd);
this.replController.variableProvider = new VariablesProvider(
new VariableRequester(this.pythonServer),
() => this.notebookDocument,
this.pythonServer.onCodeExecuted,
);
}
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);
if (code) {
await executeNotebookCell(notebookEditor, code);
}
}
}
}
/**
* Get Singleton Native REPL Instance
* @param interpreter
* @returns Native REPL instance
*/
export async function getNativeRepl(interpreter: PythonEnvironment, disposables: Disposable[]): Promise<NativeRepl> {
if (!nativeRepl) {
nativeRepl = await NativeRepl.create(interpreter);
disposables.push(nativeRepl);
}
if (nativeRepl && nativeRepl.newReplSession) {
sendTelemetryEvent(EventName.REPL, undefined, { replType: 'Native' });
nativeRepl.newReplSession = false;
}
return nativeRepl;
}