Return resulting URI from commands that save the active editor (fix #178713) (#179091)

* Return resulting `URI` from commands that save the active editor (fix #178713)

* 💄

* address feedback

* change to real proposed API

* cleanup
This commit is contained in:
Benjamin Pasero
2023-04-20 18:00:12 +02:00
committed by GitHub
parent 5ea57c3b48
commit 1ed110b6be
15 changed files with 173 additions and 31 deletions

View File

@@ -1171,7 +1171,6 @@ suite('vscode API - workspace', () => {
assert.deepStrictEqual(edt.selections, [new vscode.Selection(0, 0, 0, 3)]);
});
test('Support creating binary files in a WorkspaceEdit', async function (): Promise<any> {
const fileUri = vscode.Uri.parse(`${testFs.scheme}:/${rndName()}`);
@@ -1187,4 +1186,46 @@ suite('vscode API - workspace', () => {
assert.deepStrictEqual(actual, data);
});
test('saveAll', async () => {
await testSave(true);
});
test('save', async () => {
await testSave(false);
});
async function testSave(saveAll: boolean) {
const file = await createRandomFile();
const disposables: vscode.Disposable[] = [];
await revertAllDirty(); // needed for a clean state for `onDidSaveTextDocument` (#102365)
const onDidSaveTextDocument = new Set<vscode.TextDocument>();
disposables.push(vscode.workspace.onDidSaveTextDocument(e => {
onDidSaveTextDocument.add(e);
}));
const doc = await vscode.workspace.openTextDocument(file);
await vscode.window.showTextDocument(doc);
if (saveAll) {
const edit = new vscode.WorkspaceEdit();
edit.insert(doc.uri, new vscode.Position(0, 0), 'Hello World');
await vscode.workspace.applyEdit(edit);
assert.ok(doc.isDirty);
await vscode.workspace.saveAll(false); // requires dirty documents
} else {
const res = await vscode.workspace.save(doc.uri); // enforces to save even when not dirty
assert.ok(res?.toString() === doc.uri.toString());
}
assert.ok(onDidSaveTextDocument);
assert.ok(Array.from(onDidSaveTextDocument).find(e => e.uri.toString() === file.toString()), 'did Save: ' + file.toString());
disposeAll(disposables);
return deleteFile(file);
}
});