Skip to content

Commit b2421c6

Browse files
committed
Allow the new-folder-command to create files if the input name appears file-like, handling creation and opening of the new file
1 parent e7c0dfb commit b2421c6

1 file changed

Lines changed: 51 additions & 11 deletions

File tree

packages/vscode/src/commands/new-folder-command.ts

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,75 @@ export function new_folder_command() {
5959
return
6060
}
6161

62+
const is_file_like =
63+
folder_name.startsWith('.') || path.basename(folder_name).includes('.')
64+
6265
try {
63-
const new_folder_path = create_safe_path(parent_path, folder_name)
66+
const target_path = create_safe_path(parent_path, folder_name)
6467

65-
if (!new_folder_path) {
68+
if (!target_path) {
6669
vscode.window.showErrorMessage(
67-
dictionary.error_message.INVALID_FOLDER_NAME(folder_name)
70+
is_file_like
71+
? dictionary.error_message.INVALID_FILE_NAME(folder_name)
72+
: dictionary.error_message.INVALID_FOLDER_NAME(folder_name)
6873
)
6974
return
7075
}
7176

77+
if (is_file_like) {
78+
const fileUri = vscode.Uri.file(target_path)
79+
80+
try {
81+
await vscode.workspace.fs.stat(fileUri)
82+
vscode.window.showErrorMessage(
83+
dictionary.error_message.FILE_ALREADY_EXISTS(
84+
path.basename(target_path)
85+
)
86+
)
87+
return
88+
} catch {}
89+
90+
const directory = path.dirname(target_path)
91+
await vscode.workspace.fs.createDirectory(vscode.Uri.file(directory))
92+
93+
const edit = new vscode.WorkspaceEdit()
94+
edit.createFile(fileUri, {
95+
overwrite: false,
96+
contents: new Uint8Array()
97+
})
98+
const applied = await vscode.workspace.applyEdit(edit)
99+
if (!applied) {
100+
throw new Error('Failed to apply create file edit')
101+
}
102+
103+
const document = await vscode.workspace.openTextDocument(fileUri)
104+
await vscode.window.showTextDocument(document, { preview: false })
105+
return
106+
}
107+
72108
try {
73-
await vscode.workspace.fs.stat(vscode.Uri.file(new_folder_path))
109+
await vscode.workspace.fs.stat(vscode.Uri.file(target_path))
74110
vscode.window.showErrorMessage(
75111
dictionary.error_message.FOLDER_ALREADY_EXISTS(
76-
path.basename(new_folder_path)
112+
path.basename(target_path)
77113
)
78114
)
79115
return
80116
} catch {
81117
// Folder doesn't exist, which is what we want
82118
}
83119

84-
await vscode.workspace.fs.createDirectory(
85-
vscode.Uri.file(new_folder_path)
86-
)
120+
await vscode.workspace.fs.createDirectory(vscode.Uri.file(target_path))
87121
} catch (error: any) {
88-
vscode.window.showErrorMessage(
89-
dictionary.error_message.FAILED_TO_CREATE_FOLDER(error.message)
90-
)
122+
if (is_file_like) {
123+
vscode.window.showErrorMessage(
124+
dictionary.error_message.FAILED_TO_CREATE_FILE(error.message)
125+
)
126+
} else {
127+
vscode.window.showErrorMessage(
128+
dictionary.error_message.FAILED_TO_CREATE_FOLDER(error.message)
129+
)
130+
}
91131
}
92132
}
93133
)

0 commit comments

Comments
 (0)