Skip to content

Commit 5f8d162

Browse files
compnerdz2oh
andcommitted
REPL: introduce a new jupyter notebook based REPL
VSCode has introduced a native jupyter notebook API. Wire up a "kernel" for Swift to provide a native REPL experience within VSCode. This is primarily a PoC implementation that demonstrates how such an implementation could work. The primary limitation in the current implementation is the IO processing in the LLDB REPL. We can apply a heuristic to capture the error message (termination on an empty string), but do not have such a heuristic for the output and currently only capture the first line of the output, leaving the remaining output in the buffer. Improving the stream handling would significantly improve the experience. Co-authored-by: Jeremy Day <[email protected]>
1 parent a85e784 commit 5f8d162

File tree

6 files changed

+290
-0
lines changed

6 files changed

+290
-0
lines changed

package.json

+20
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@
253253
"command": "swift.runAllTestsParallel",
254254
"title": "Run All Tests in Parallel",
255255
"category": "Test"
256+
},
257+
{
258+
"command": "swift.evaluate",
259+
"title": "Evaluate in REPL",
260+
"category": "Swift"
256261
}
257262
],
258263
"configuration": [
@@ -394,6 +399,16 @@
394399
"type": "boolean",
395400
"default": true,
396401
"markdownDescription": "Controls whether or not the extension will contribute environment variables defined in `Swift: Environment Variables` to the integrated terminal. If this is set to `true` and a custom `Swift: Path` is also set then the swift path is appended to the terminal's `PATH`."
402+
"order": 15
403+
},
404+
"swift.repl": {
405+
"type": "boolean",
406+
"default": false,
407+
"markdownDescription": "Use the native Swift REPL.",
408+
"order": 16,
409+
"tags": [
410+
"experimental"
411+
]
397412
}
398413
}
399414
},
@@ -831,6 +846,11 @@
831846
"command": "swift.cleanBuild",
832847
"group": "2_pkg@1",
833848
"when": "swift.hasPackage"
849+
},
850+
{
851+
"command": "swift.evaluate",
852+
"group": "1_file@6",
853+
"when": "editorFocus && editorLangId == swift && config.swift.repl"
834854
}
835855
],
836856
"view/title": [

src/WorkspaceContext.ts

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { SwiftBuildStatus } from "./ui/SwiftBuildStatus";
3434
import { SwiftToolchain } from "./toolchain/toolchain";
3535
import { DiagnosticsManager } from "./DiagnosticsManager";
3636
import { DocumentationManager } from "./documentation/DocumentationManager";
37+
import { REPL } from "./repl/REPL";
3738

3839
/**
3940
* Context for whole workspace. Holds array of contexts for each workspace folder
@@ -53,6 +54,7 @@ export class WorkspaceContext implements vscode.Disposable {
5354
public documentation: DocumentationManager;
5455
private lastFocusUri: vscode.Uri | undefined;
5556
private initialisationFinished = false;
57+
private repl: REPL | undefined;
5658

5759
private constructor(
5860
extensionContext: vscode.ExtensionContext,
@@ -663,6 +665,13 @@ export class WorkspaceContext implements vscode.Disposable {
663665

664666
private observers = new Set<(listener: FolderEvent) => unknown>();
665667
private swiftFileObservers = new Set<(listener: SwiftFileEvent) => unknown>();
668+
669+
public getRepl(): REPL {
670+
if (!this.repl) {
671+
this.repl = new REPL(this);
672+
}
673+
return this.repl;
674+
}
666675
}
667676

668677
/** Workspace Folder Operation types */

src/commands.ts

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { runPluginTask } from "./commands/runPluginTask";
3939
import { runTestMultipleTimes } from "./commands/testMultipleTimes";
4040
import { newSwiftFile } from "./commands/newFile";
4141
import { runAllTestsParallel } from "./commands/runParallelTests";
42+
import { evaluateExpression } from "./repl/command";
4243

4344
/**
4445
* References:
@@ -160,5 +161,6 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
160161
Commands.PREVIEW_DOCUMENTATION,
161162
async () => await ctx.documentation.launchDocumentationPreview()
162163
),
164+
vscode.commands.registerCommand("swift.evaluate", () => evaluateExpression(ctx)),
163165
];
164166
}

src/configuration.ts

+17
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ export interface FolderConfiguration {
6767
readonly disableAutoResolve: boolean;
6868
}
6969

70+
/** REPL configuration */
71+
export interface REPLConfiguration {
72+
/** Enable the native REPL */
73+
readonly enable: boolean;
74+
}
75+
7076
/**
7177
* Type-safe wrapper around configuration settings.
7278
*/
@@ -170,6 +176,17 @@ const configuration = {
170176
},
171177
};
172178
},
179+
180+
get repl(): REPLConfiguration {
181+
return {
182+
get enable(): boolean {
183+
return vscode.workspace
184+
.getConfiguration("swift.repl")
185+
.get<boolean>("enable", false);
186+
},
187+
};
188+
},
189+
173190
/** Files and directories to exclude from the code coverage. */
174191
get excludeFromCodeCoverage(): string[] {
175192
return vscode.workspace

src/repl/REPL.ts

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the VS Code Swift open source project
4+
//
5+
// Copyright (c) 2022 the VS Code Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import {
16+
Disposable,
17+
NotebookController,
18+
NotebookDocument,
19+
NotebookCellOutput,
20+
NotebookCellOutputItem,
21+
notebooks,
22+
workspace,
23+
NotebookEditor,
24+
ViewColumn,
25+
TabInputNotebook,
26+
commands,
27+
window,
28+
NotebookControllerAffinity,
29+
NotebookCellData,
30+
NotebookEdit,
31+
WorkspaceEdit,
32+
} from "vscode";
33+
import { ChildProcess, spawn } from "child_process";
34+
import { createInterface, Interface } from "readline";
35+
import { WorkspaceContext } from "../WorkspaceContext";
36+
import { NotebookCellKind } from "vscode-languageclient";
37+
38+
export interface ExecutionResult {
39+
status: boolean;
40+
output: string | undefined;
41+
}
42+
43+
export interface IREPL {
44+
execute(code: string): Promise<ExecutionResult | undefined>;
45+
interrupt(): void;
46+
}
47+
48+
class REPLConnection implements IREPL {
49+
private stdout: Interface;
50+
private stderr: Interface;
51+
52+
constructor(private repl: ChildProcess) {
53+
this.stdout = createInterface({ input: repl.stdout as NodeJS.ReadableStream });
54+
this.stderr = createInterface({ input: repl.stderr as NodeJS.ReadableStream });
55+
this.stdout.on("line", line => {
56+
console.log(`=> ${line}`);
57+
});
58+
this.stderr.on("line", line => {
59+
console.log(`=> ${line}`);
60+
});
61+
}
62+
63+
public async execute(code: string): Promise<ExecutionResult | undefined> {
64+
if (!code.endsWith("\n")) {
65+
code += "\n";
66+
}
67+
if (!this.repl.stdin?.write(code)) {
68+
return Promise.resolve({ status: false, output: undefined });
69+
}
70+
return new Promise((resolve, _reject) => {
71+
this.stdout.on("line", line => {
72+
return resolve({ status: true, output: line });
73+
});
74+
75+
const lines: string[] = [];
76+
this.stderr.on("line", line => {
77+
lines.push(line);
78+
if (!line) {
79+
return resolve({ status: false, output: lines.join("\n") });
80+
}
81+
});
82+
});
83+
}
84+
85+
public interrupt(): void {
86+
this.repl.stdin?.write(":q");
87+
}
88+
}
89+
90+
export class REPL implements Disposable {
91+
private repl: REPLConnection;
92+
private controller: NotebookController;
93+
private document: NotebookDocument | undefined;
94+
private listener: Disposable | undefined;
95+
96+
constructor(workspace: WorkspaceContext) {
97+
const repl = spawn(workspace.toolchain.getToolchainExecutable("swift"), ["repl"]);
98+
repl.on("exit", (code, _signal) => {
99+
console.error(`repl exited with code ${code}`);
100+
});
101+
repl.on("error", error => {
102+
console.error(`repl error: ${error}`);
103+
});
104+
105+
this.repl = new REPLConnection(repl);
106+
107+
this.controller = notebooks.createNotebookController(
108+
"SwiftREPL",
109+
"interactive",
110+
"Swift REPL"
111+
);
112+
this.controller.supportedLanguages = ["swift"];
113+
this.controller.supportsExecutionOrder = true;
114+
this.controller.description = "Swift REPL";
115+
this.controller.interruptHandler = async () => {
116+
this.repl.interrupt();
117+
};
118+
this.controller.executeHandler = async (cells, _notebook, controller) => {
119+
for (const cell of cells) {
120+
const execution = controller.createNotebookCellExecution(cell);
121+
execution.start(Date.now());
122+
123+
const result = await this.repl.execute(cell.document.getText());
124+
if (result?.output) {
125+
execution.replaceOutput([
126+
new NotebookCellOutput([
127+
NotebookCellOutputItem.text(result.output, "text/plain"),
128+
]),
129+
]);
130+
}
131+
132+
execution.end(result?.status);
133+
}
134+
};
135+
136+
this.watchNotebookClose();
137+
}
138+
139+
dispose(): void {
140+
this.controller.dispose();
141+
this.listener?.dispose();
142+
}
143+
144+
private watchNotebookClose() {
145+
this.listener = workspace.onDidCloseNotebookDocument(notebook => {
146+
if (notebook.uri.toString() === this.document?.uri.toString()) {
147+
this.document = undefined;
148+
}
149+
});
150+
}
151+
152+
private getNotebookColumn(): ViewColumn | undefined {
153+
const uri = this.document?.uri.toString();
154+
return window.tabGroups.all.flatMap(group => {
155+
return group.tabs.flatMap(tab => {
156+
if (tab.label === "Swift REPL") {
157+
if ((tab.input as TabInputNotebook)?.uri.toString() === uri) {
158+
return tab.group.viewColumn;
159+
}
160+
}
161+
return undefined;
162+
});
163+
})?.[0];
164+
}
165+
166+
public async evaluate(code: string): Promise<void> {
167+
let editor: NotebookEditor | undefined;
168+
if (this.document) {
169+
const column = this.getNotebookColumn() ?? ViewColumn.Beside;
170+
editor = await window.showNotebookDocument(this.document!, { viewColumn: column });
171+
} else {
172+
const notebook = (await commands.executeCommand(
173+
"interactive.open",
174+
{
175+
preserveFocus: true,
176+
viewColumn: ViewColumn.Beside,
177+
},
178+
undefined,
179+
this.controller.id,
180+
"Swift REPL"
181+
)) as { notebookEditor: NotebookEditor };
182+
editor = notebook.notebookEditor;
183+
this.document = editor.notebook;
184+
}
185+
186+
if (this.document) {
187+
this.controller.updateNotebookAffinity(
188+
this.document,
189+
NotebookControllerAffinity.Default
190+
);
191+
192+
await commands.executeCommand("notebook.selectKernel", {
193+
notebookEdtior: this.document,
194+
id: this.controller.id,
195+
extension: "sswg.swift-lang",
196+
});
197+
198+
const edit = new WorkspaceEdit();
199+
edit.set(this.document.uri, [
200+
NotebookEdit.insertCells(this.document.cellCount, [
201+
new NotebookCellData(NotebookCellKind.Code, code, "swift"),
202+
]),
203+
]);
204+
workspace.applyEdit(edit);
205+
206+
commands.executeCommand("notebook.cell.execute", {
207+
ranges: [{ start: this.document.cellCount, end: this.document.cellCount + 1 }],
208+
document: this.document.uri,
209+
});
210+
}
211+
}
212+
}

src/repl/command.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the VS Code Swift open source project
4+
//
5+
// Copyright (c) 2022 the VS Code Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import { window } from "vscode";
16+
import { WorkspaceContext } from "../WorkspaceContext";
17+
18+
export async function evaluateExpression(context: WorkspaceContext): Promise<void> {
19+
const editor = window.activeTextEditor;
20+
21+
// const multiline = !editor?.selection.isSingleLine ?? false;
22+
// const complete = true; // TODO(compnerd) determine if the input is complete
23+
const code = editor?.document.lineAt(editor?.selection.start.line).text;
24+
if (!code) {
25+
return;
26+
}
27+
28+
const repl = context.getRepl();
29+
await repl.evaluate(code);
30+
}

0 commit comments

Comments
 (0)