Skip to content

Commit 0b6817f

Browse files
committed
Use WorkspaceEdit for file creation to reliably trigger VS Code file creation events
1 parent 81cf9e9 commit 0b6817f

1 file changed

Lines changed: 20 additions & 22 deletions

File tree

‎packages/vscode/src/commands/new-file-command.ts‎

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export function new_file_command() {
1010
async (item?: vscode.TreeItem | vscode.Uri) => {
1111
let parent_path: string | undefined
1212

13-
// Handle case when invoked from view/title (no item parameter)
1413
if (!item) {
1514
if (
1615
vscode.workspace.workspaceFolders &&
@@ -23,13 +22,9 @@ export function new_file_command() {
2322
)
2423
return
2524
}
26-
}
27-
// Handle case when invoked with URI (from view/title)
28-
else if (item instanceof vscode.Uri) {
25+
} else if (item instanceof vscode.Uri) {
2926
parent_path = item.fsPath
30-
}
31-
// Handle case when invoked with TreeItem (from context menu)
32-
else if (item.resourceUri) {
27+
} else if (item.resourceUri) {
3328
parent_path = item.resourceUri.fsPath
3429
}
3530

@@ -40,15 +35,12 @@ export function new_file_command() {
4035
return
4136
}
4237

43-
// Check if the parent_path is a file and not a directory
4438
try {
4539
const stats = fs.statSync(parent_path)
4640
if (!stats.isDirectory()) {
4741
parent_path = path.dirname(parent_path)
4842
}
49-
} catch (error) {
50-
// If the path doesn't exist, we'll create it later
51-
}
43+
} catch {}
5244

5345
const file_name = await vscode.window.showInputBox({
5446
prompt: 'Enter file name',
@@ -69,29 +61,35 @@ export function new_file_command() {
6961
return
7062
}
7163

72-
// Check if file already exists
64+
const fileUri = vscode.Uri.file(file_path)
65+
7366
try {
74-
await vscode.workspace.fs.stat(vscode.Uri.file(file_path))
67+
await vscode.workspace.fs.stat(fileUri)
7568
vscode.window.showErrorMessage(
7669
dictionary.error_message.FILE_ALREADY_EXISTS(
7770
path.basename(file_path)
7871
)
7972
)
8073
return
81-
} catch {
82-
// File doesn't exist, which is what we want
83-
}
74+
} catch {}
8475

76+
// Ensure parent directory exists (this does not trigger onDidCreateFiles)
8577
const directory = path.dirname(file_path)
8678
await vscode.workspace.fs.createDirectory(vscode.Uri.file(directory))
8779

88-
await vscode.workspace.fs.writeFile(
89-
vscode.Uri.file(file_path),
90-
new Uint8Array()
91-
)
80+
// Create the file via WorkspaceEdit to trigger onDidCreateFiles
81+
const edit = new vscode.WorkspaceEdit()
82+
edit.createFile(fileUri, {
83+
overwrite: false,
84+
contents: new Uint8Array()
85+
})
86+
const applied = await vscode.workspace.applyEdit(edit)
87+
if (!applied) {
88+
throw new Error('Failed to apply create file edit')
89+
}
9290

93-
const document = await vscode.workspace.openTextDocument(file_path)
94-
await vscode.window.showTextDocument(document)
91+
const document = await vscode.workspace.openTextDocument(fileUri)
92+
await vscode.window.showTextDocument(document, { preview: false })
9593
} catch (error: any) {
9694
vscode.window.showErrorMessage(
9795
dictionary.error_message.FAILED_TO_CREATE_FILE(error.message)

0 commit comments

Comments
 (0)