Skip to content

Commit 420b4be

Browse files
committed
Allow creating the non-existing file
1 parent 11ff0d0 commit 420b4be

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

src/extension/providers/JudgeViewProvider.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as vscode from "vscode";
22
import * as v from "valibot";
33
import * as crypto from "crypto";
4-
import * as fs from "fs";
54

65
import {
76
TestcaseSchema,
@@ -23,6 +22,7 @@ import {
2322
getFileRunSettings,
2423
openInNewEditor,
2524
openInTerminalTab,
25+
openOrCreateFile,
2626
ReadonlyStringProvider,
2727
resolveVariables,
2828
showOpenRunSettingsErrorWindow,
@@ -1442,12 +1442,10 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
14421442
return;
14431443
}
14441444

1445-
if (settings.interactorFile && fs.existsSync(settings.interactorFile)) {
1446-
void vscode.window.showTextDocument(vscode.Uri.file(settings.interactorFile));
1445+
if (settings.interactorFile) {
1446+
void openOrCreateFile(settings.interactorFile);
14471447
} else {
1448-
void vscode.window.showWarningMessage(
1449-
`${settings.interactorFile ?? "Interactor file"} does not exist`
1450-
);
1448+
void vscode.window.showWarningMessage("Interactor file not specified");
14511449
}
14521450
}
14531451

src/extension/providers/StressViewProvider.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as vscode from "vscode";
22
import * as v from "valibot";
33
import * as crypto from "crypto";
4-
import * as fs from "fs";
54

65
import { StatusSchema, type Status } from "../../shared/enums";
76
import BaseViewProvider from "./BaseViewProvider";
@@ -17,6 +16,7 @@ import {
1716
getFileRunSettings,
1817
openInNewEditor,
1918
openInTerminalTab,
19+
openOrCreateFile,
2020
showOpenRunSettingsErrorWindow,
2121
TextHandler,
2222
type FileRunSettings,
@@ -706,11 +706,7 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
706706
});
707707
}
708708

709-
if (fs.existsSync(resolvedFile)) {
710-
void vscode.window.showTextDocument(vscode.Uri.file(resolvedFile));
711-
} else {
712-
void vscode.window.showWarningMessage(`${resolvedFile} does not exist`);
713-
}
709+
void openOrCreateFile(resolvedFile);
714710
}
715711

716712
private _open({ id }: v.InferOutput<typeof OpenMessageSchema>) {
@@ -738,11 +734,7 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
738734
return;
739735
}
740736

741-
if (fs.existsSync(resolvedFile)) {
742-
void vscode.window.showTextDocument(vscode.Uri.file(resolvedFile));
743-
} else {
744-
void vscode.window.showWarningMessage(`${resolvedFile} does not exist`);
745-
}
737+
void openOrCreateFile(resolvedFile);
746738
}
747739

748740
clear() {

src/extension/utils/vscode.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,33 @@ export function openInTerminalTab(content: string, title: string = "Output") {
234234
terminal.show();
235235
}
236236

237+
/**
238+
* Opens a file if it exists, otherwise prompts the user to create it.
239+
*/
240+
export async function openOrCreateFile(file: string): Promise<void> {
241+
if (fs.existsSync(file)) {
242+
const document = await vscode.workspace.openTextDocument(file);
243+
await vscode.window.showTextDocument(document);
244+
} else {
245+
const choice = await vscode.window.showWarningMessage(
246+
`${file} does not exist`,
247+
"Create File",
248+
"Cancel"
249+
);
250+
if (choice === "Create File") {
251+
try {
252+
fs.mkdirSync(path.dirname(file), { recursive: true });
253+
fs.writeFileSync(file, "");
254+
const document = await vscode.workspace.openTextDocument(file);
255+
await vscode.window.showTextDocument(document);
256+
} catch (error) {
257+
getLogger("vscode").error(`Failed to create file ${file}: ${error}`);
258+
vscode.window.showErrorMessage(`Failed to create file ${file}: ${error}`);
259+
}
260+
}
261+
}
262+
}
263+
237264
function getRelativeFileDirname(relativeFilePath: string | undefined): string {
238265
if (!relativeFilePath) return "";
239266
const dir = path.dirname(relativeFilePath);

0 commit comments

Comments
 (0)