Skip to content

Commit bee68ce

Browse files
authored
allow workspace edit to "create" untitled files (microsoft#191779)
microsoft/vscode-copilot#1261
1 parent eef56ce commit bee68ce

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

extensions/vscode-api-tests/src/singlefolder-tests/workspace.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,22 @@ suite('vscode API - workspace', () => {
977977
assert.strictEqual(document.getText(), expected);
978978
});
979979

980+
981+
test('[Bug] Failed to create new test file when in an untitled file #1261', async function () {
982+
const uri = vscode.Uri.parse('untitled:Untitled-5.test');
983+
const contents = `Hello Test File ${uri.toString()}`;
984+
const we = new vscode.WorkspaceEdit();
985+
we.createFile(uri, { ignoreIfExists: true });
986+
we.replace(uri, new vscode.Range(0, 0, 0, 0), contents);
987+
988+
const success = await vscode.workspace.applyEdit(we);
989+
990+
assert.ok(success);
991+
992+
const doc = await vscode.workspace.openTextDocument(uri);
993+
assert.strictEqual(doc.getText(), contents);
994+
});
995+
980996
test('Should send a single FileWillRenameEvent instead of separate events when moving multiple files at once#111867, 1/3', async function () {
981997

982998
const file1 = await createRandomFile();

src/vs/workbench/contrib/bulkEdit/browser/bulkFileEdits.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { ResourceFileEdit } from 'vs/editor/browser/services/bulkEditService';
1818
import { CancellationToken } from 'vs/base/common/cancellation';
1919
import { tail } from 'vs/base/common/arrays';
2020
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
21+
import { Schemas } from 'vs/base/common/network';
2122

2223
interface IFileOperation {
2324
uris: URI[];
@@ -173,6 +174,9 @@ class CreateOperation implements IFileOperation {
173174
const undoes: DeleteEdit[] = [];
174175

175176
for (const edit of this._edits) {
177+
if (edit.newUri.scheme === Schemas.untitled) {
178+
continue; // ignore, will be handled by a later edit
179+
}
176180
if (edit.options.overwrite === undefined && edit.options.ignoreIfExists && await this._fileService.exists(edit.newUri)) {
177181
continue; // not overwriting, but ignoring, and the target file exists
178182
}

0 commit comments

Comments
 (0)