|
| 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 | +} |
0 commit comments