Skip to content

Commit 76ccd83

Browse files
authored
Open Text File if File Does Not Exist (#963)
This will open new files as empty text files for editing.
1 parent 28f189b commit 76ccd83

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

cmd/wsh/cmd/wshcmd-view.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,14 @@ func viewRun(cmd *cobra.Command, args []string) {
5858
WriteStderr("[error] getting absolute path: %v\n", err)
5959
return
6060
}
61-
_, err = os.Stat(absFile)
61+
absParent, err := filepath.Abs(filepath.Dir(fileArg))
62+
if err != nil {
63+
WriteStderr("[error] getting absolute path of parent dir: %v\n", err)
64+
return
65+
}
66+
_, err = os.Stat(absParent)
6267
if err == fs.ErrNotExist {
63-
WriteStderr("[error] file does not exist: %q\n", absFile)
68+
WriteStderr("[error] parent directory does not exist: %q\n", absParent)
6469
return
6570
}
6671
if err != nil {

frontend/app/view/preview/preview.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,14 @@ export class PreviewModel implements ViewModel {
432432
const fileInfo = await getFn(this.statFile);
433433
const fileName = await getFn(this.statFilePath);
434434
const editMode = getFn(this.editMode);
435+
const parentFileInfo = await this.getParentInfo(fileInfo);
436+
console.log(parentFileInfo);
435437

438+
if (parentFileInfo?.notfound ?? false) {
439+
return { errorStr: `Parent Directory Not Found: ${fileInfo.path}` };
440+
}
436441
if (fileInfo?.notfound) {
437-
return { errorStr: `File Not Found: ${fileInfo.path}` };
442+
return { specializedView: "codeedit" };
438443
}
439444
if (mimeType == null) {
440445
return { errorStr: `Unable to determine mimetype for: ${fileInfo.path}` };
@@ -492,6 +497,18 @@ export class PreviewModel implements ViewModel {
492497
services.ObjectService.UpdateObjectMeta(blockOref, updateMeta);
493498
}
494499

500+
async getParentInfo(fileInfo: FileInfo): Promise<FileInfo | undefined> {
501+
const conn = globalStore.get(this.connection);
502+
try {
503+
const parentFileInfo = await RpcApi.RemoteFileJoinCommand(WindowRpcClient, [fileInfo.path, ".."], {
504+
route: makeConnRoute(conn),
505+
});
506+
return parentFileInfo;
507+
} catch {
508+
return undefined;
509+
}
510+
}
511+
495512
async goParentDirectory() {
496513
const fileInfo = await globalStore.get(this.statFile);
497514
if (fileInfo == null) {
@@ -500,7 +517,7 @@ export class PreviewModel implements ViewModel {
500517
}
501518
const conn = globalStore.get(this.connection);
502519
try {
503-
const newFileInfo = await RpcApi.RemoteFileJoinCommand(WindowRpcClient, [fileInfo.dir, ".."], {
520+
const newFileInfo = await RpcApi.RemoteFileJoinCommand(WindowRpcClient, [fileInfo.path, ".."], {
504521
route: makeConnRoute(conn),
505522
});
506523
console.log(newFileInfo.path);

0 commit comments

Comments
 (0)