Skip to content

Commit 9a0b6b7

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 d801d41 commit 9a0b6b7

7 files changed

+299
-2
lines changed

.eslintrc.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@
1212
"no-throw-literal": "warn",
1313
"semi": "off",
1414
"@typescript-eslint/no-non-null-assertion": "off",
15-
"@typescript-eslint/semi": "error"
15+
"@typescript-eslint/semi": "error",
16+
"@typescript-eslint/no-unused-vars": [
17+
"error",
18+
{
19+
"argsIgnorePattern": "^_",
20+
"varsIgnorePattern": "^_",
21+
"caughtErrorsIgnorePattern": "^_"
22+
}
23+
]
1624
},
1725
"extends": [
1826
"eslint:recommended",

package.json

+20-1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@
211211
"command": "swift.runTestsUntilFailure",
212212
"title": "Run Until Failure...",
213213
"category": "Swift"
214+
},
215+
{
216+
"command": "swift.evaluate",
217+
"title": "Evaluate in REPL",
218+
"category": "Swift"
214219
}
215220
],
216221
"configuration": [
@@ -358,6 +363,15 @@
358363
"default": true,
359364
"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`.",
360365
"order": 15
366+
},
367+
"swift.repl": {
368+
"type": "boolean",
369+
"default": false,
370+
"markdownDescription": "Use the native Swift REPL.",
371+
"order": 16,
372+
"tags": [
373+
"experimental"
374+
]
361375
}
362376
}
363377
},
@@ -745,6 +759,11 @@
745759
"command": "swift.cleanBuild",
746760
"group": "2_pkg@1",
747761
"when": "swift.hasPackage"
762+
},
763+
{
764+
"command": "swift.evaluate",
765+
"group": "1_file@6",
766+
"when": "editorFocus && editorLangId == swift && config.swift.repl"
748767
}
749768
],
750769
"view/title": [
@@ -1276,4 +1295,4 @@
12761295
"vscode-languageclient": "^9.0.1",
12771296
"xml2js": "^0.6.2"
12781297
}
1279-
}
1298+
}

src/WorkspaceContext.ts

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { DebugAdapter } from "./debugger/debugAdapter";
3333
import { SwiftBuildStatus } from "./ui/SwiftBuildStatus";
3434
import { SwiftToolchain } from "./toolchain/toolchain";
3535
import { DiagnosticsManager } from "./DiagnosticsManager";
36+
import { REPL } from "./repl/REPL";
3637

3738
/**
3839
* Context for whole workspace. Holds array of contexts for each workspace folder
@@ -51,6 +52,7 @@ export class WorkspaceContext implements vscode.Disposable {
5152
public commentCompletionProvider: CommentCompletionProviders;
5253
private lastFocusUri: vscode.Uri | undefined;
5354
private initialisationFinished = false;
55+
private repl: REPL | undefined;
5456

5557
private constructor(
5658
public tempFolder: TemporaryFolder,
@@ -658,6 +660,13 @@ export class WorkspaceContext implements vscode.Disposable {
658660

659661
private observers: Set<WorkspaceFoldersObserver> = new Set();
660662
private swiftFileObservers: Set<SwiftFileObserver> = new Set();
663+
664+
public getRepl(): REPL {
665+
if (!this.repl) {
666+
this.repl = new REPL(this);
667+
}
668+
return this.repl;
669+
}
661670
}
662671

663672
/** Workspace Folder events */

src/commands.ts

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { captureDiagnostics } from "./commands/captureDiagnostics";
3535
import { reindexProjectRequest } from "./sourcekit-lsp/lspExtensions";
3636
import { TestRunner, TestRunnerTestRunState } from "./TestExplorer/TestRunner";
3737
import { TestKind } from "./TestExplorer/TestKind";
38+
import { evaluateExpression } from "./repl/command";
3839

3940
/**
4041
* References:
@@ -940,5 +941,6 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
940941
ctx.diagnostics.clear()
941942
),
942943
vscode.commands.registerCommand("swift.captureDiagnostics", () => captureDiagnostics(ctx)),
944+
vscode.commands.registerCommand("swift.evaluate", () => evaluateExpression(ctx)),
943945
];
944946
}

src/configuration.ts

+17
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ export interface FolderConfiguration {
6565
readonly disableAutoResolve: boolean;
6666
}
6767

68+
/** REPL configuration */
69+
export interface REPLConfiguration {
70+
/** Enable the native REPL */
71+
readonly enable: boolean;
72+
}
73+
6874
/**
6975
* Type-safe wrapper around configuration settings.
7076
*/
@@ -154,6 +160,17 @@ const configuration = {
154160
},
155161
};
156162
},
163+
164+
get repl(): REPLConfiguration {
165+
return {
166+
get enable(): boolean {
167+
return vscode.workspace
168+
.getConfiguration("swift.repl")
169+
.get<boolean>("enable", false);
170+
},
171+
};
172+
},
173+
157174
/** Files and directories to exclude from the code coverage. */
158175
get excludeFromCodeCoverage(): string[] {
159176
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)