From fac2a9acb592bb195be14338f26292c15249a749 Mon Sep 17 00:00:00 2001 From: Radon Rosborough Date: Mon, 6 Jul 2020 14:15:24 -0600 Subject: [PATCH] Additional LSP diagnostics on frontend --- backend/src/api.ts | 47 +++++++++++++++++++-------------------------- frontend/src/app.ts | 24 ++++++++++++++++++++++- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/backend/src/api.ts b/backend/src/api.ts index 951aec1..130af6e 100644 --- a/backend/src/api.ts +++ b/backend/src/api.ts @@ -55,21 +55,22 @@ export class Session { this.run().catch((err) => { this.log(`Error while setting up environment for pty`); console.log(err); - try { - this.ws.send(JSON.stringify({ event: "terminalClear" })); - this.ws.send( - JSON.stringify({ - event: "terminalOutput", - output: `Riju encountered an unexpected error: ${err} + this.send({ event: "terminalClear" }); + this.send({ + event: "terminalOutput", + output: `Riju encountered an unexpected error: ${err} \rYou may want to save your code and refresh the page. `, - }) - ); - } catch (err) { - // - } + }); }); } + send = (msg: any) => { + try { + this.ws.send(JSON.stringify(msg)); + } catch (err) { + // + } + }; handleClientMessage = (event: string) => { let msg: any; try { @@ -131,11 +132,7 @@ export class Session { this.term.pty.kill(); this.term.live = false; } - try { - this.ws.send(JSON.stringify({ event: "terminalClear" })); - } catch (err) { - // - } + this.send({ event: "terminalClear" }); if (this.homedir == null) { this.homedir = `/tmp/riju/${this.uuid}`; await callPrivileged(["setup", `${this.uid}`, this.uuid], this.log); @@ -221,13 +218,7 @@ export class Session { // Capture term in closure so that we don't keep sending output // from the old pty even after it's been killed (see ghci). if (term.live) { - try { - this.ws.send( - JSON.stringify({ event: "terminalOutput", output: data }) - ); - } catch (err) { - // - } + this.send({ event: "terminalOutput", output: data }); } }); if (lsp && this.lsp === null) { @@ -251,17 +242,19 @@ export class Session { const proc = spawn(lspArgs[0], lspArgs.slice(1), { env: getEnv(this.uuid), }); + proc.on("exit", (code) => this.send({ event: "lspCrashed", code })); + proc.stderr.on("data", (data) => + this.send({ event: "lspLog", output: data.toString("utf8") }) + ); this.lsp = { proc, reader: new rpc.StreamMessageReader(proc.stdout), writer: new rpc.StreamMessageWriter(proc.stdin), }; this.lsp.reader.listen((data) => { - this.ws.send(JSON.stringify({ event: "lspOutput", output: data })); + this.send({ event: "lspOutput", output: data }); }); - this.ws.send( - JSON.stringify({ event: "lspStarted", root: `/tmp/riju/${this.uuid}` }) - ); + this.send({ event: "lspStarted", root: `/tmp/riju/${this.uuid}` }); } }; cleanup = async () => { diff --git a/frontend/src/app.ts b/frontend/src/app.ts index 045affa..c12ab85 100644 --- a/frontend/src/app.ts +++ b/frontend/src/app.ts @@ -119,6 +119,7 @@ async function main() { function tryConnect() { let clientDisposable: Disposable | null = null; let servicesDisposable: Disposable | null = null; + let lspLogBuffer = ""; console.log("Connecting to server..."); socket = new WebSocket( (document.location.protocol === "http:" ? "ws://" : "wss://") + @@ -136,7 +137,11 @@ async function main() { console.error("Malformed message from server:", event.data); return; } - if (DEBUG && message?.event !== "lspOutput") { + if ( + DEBUG && + message?.event !== "lspOutput" && + message?.event !== "lspLog" + ) { console.log("RECEIVE:", message); } if (message?.event && message?.event !== "error") { @@ -198,6 +203,23 @@ async function main() { case "lspOutput": // Should be handled by RijuMessageReader return; + case "lspLog": + if (typeof message.output !== "string") { + console.error("Unexpected message from server:", message); + return; + } + if (DEBUG) { + lspLogBuffer += message.output; + while (lspLogBuffer.includes("\n")) { + const idx = lspLogBuffer.indexOf("\n"); + const line = lspLogBuffer.slice(0, idx); + lspLogBuffer = lspLogBuffer.slice(idx + 1); + console.log(`LSP || ${line}`); + } + } + return; + case "lspCrashed": + return; default: console.error("Unexpected message from server:", message); return;