Skip to content

Commit 17721fe

Browse files
committed
Avoid temp file and other code review feedback
1 parent 663190e commit 17721fe

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

client/src/extension.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
WorkspaceEdit,
1414
CodeActionKind,
1515
Diagnostic,
16+
ViewColumn,
1617
} from "vscode";
1718
import { ThemeColor } from "vscode";
1819

@@ -374,13 +375,28 @@ export function activate(context: ExtensionContext) {
374375
});
375376

376377
commands.registerCommand("rescript-vscode.dump-server-state", async () => {
377-
// Server handles everything: writing to disk and opening the file via window/showDocument
378378
try {
379-
await client.sendRequest("workspace/executeCommand", {
379+
const result = (await client.sendRequest("workspace/executeCommand", {
380380
command: "rescript/dumpServerState",
381+
})) as { content: string };
382+
383+
// Create an unsaved document with the server state content
384+
const document = await workspace.openTextDocument({
385+
content: result.content,
386+
language: "json",
387+
});
388+
389+
// Show the document in the editor
390+
await window.showTextDocument(document, {
391+
viewColumn: ViewColumn.Beside,
392+
preview: false,
381393
});
382394
} catch (e) {
383-
console.error("Failed to dump server state:", e);
395+
outputChannel.appendLine(`Failed to dump server state: ${String(e)}`);
396+
window.showErrorMessage(
397+
"Failed to dump server state. See 'Output' tab, 'ReScript Language Server' channel for details.",
398+
);
399+
outputChannel.show();
384400
}
385401
});
386402

server/src/server.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,8 +1273,11 @@ async function dumpServerState(
12731273
const packageJsonContent = await fsAsync.readFile(packageJsonPath, {
12741274
encoding: "utf-8",
12751275
});
1276-
const packageJson = JSON.parse(packageJsonContent);
1277-
serverVersion = packageJson.version;
1276+
const packageJson: { version?: unknown } = JSON.parse(packageJsonContent);
1277+
serverVersion =
1278+
typeof packageJson.version === "string"
1279+
? packageJson.version
1280+
: undefined;
12781281
} catch (e) {
12791282
// If we can't read the version, that's okay - we'll just omit it
12801283
serverVersion = undefined;
@@ -1311,28 +1314,12 @@ async function dumpServerState(
13111314
// have been converted to plain objects/arrays above
13121315
const formattedJson = JSON.stringify(state, null, 2);
13131316

1314-
// Write the file to disk on the server side
1315-
const outputFile = utils.createFileInTempDir("_server_state.json");
1316-
fs.writeFileSync(outputFile, formattedJson, { encoding: "utf-8" });
1317-
1318-
// Request the client to open the document
1319-
const fileUri = utils.pathToURI(outputFile);
1320-
const showDocumentRequest: p.RequestMessage = {
1321-
jsonrpc: c.jsonrpcVersion,
1322-
id: serverSentRequestIdCounter++,
1323-
method: "window/showDocument",
1324-
params: {
1325-
uri: fileUri,
1326-
external: false,
1327-
takeFocus: true,
1328-
},
1329-
};
1330-
send(showDocumentRequest);
1331-
1317+
// Return the content so the client can create an unsaved document
1318+
// This avoids creating temporary files that would never be cleaned up
13321319
let response: p.ResponseMessage = {
13331320
jsonrpc: c.jsonrpcVersion,
13341321
id: msg.id,
1335-
result: { uri: fileUri },
1322+
result: { content: formattedJson },
13361323
};
13371324
return response;
13381325
} catch (e) {

0 commit comments

Comments
 (0)