From cd79fe9f27f0df765c4d09a12e9f93e83a5ec9be Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Fri, 21 Apr 2023 09:04:32 +0200 Subject: [PATCH] cleanup save API (#180476) --- .../api/browser/mainThreadWorkspace.ts | 21 +++++++------------ .../workbench/api/common/extHost.protocol.ts | 3 +-- .../workbench/api/common/extHostWorkspace.ts | 4 ++-- .../vscode.proposed.saveEditor.d.ts | 8 +++++-- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/vs/workbench/api/browser/mainThreadWorkspace.ts b/src/vs/workbench/api/browser/mainThreadWorkspace.ts index 55cecb6cd4f..1932e5633b4 100644 --- a/src/vs/workbench/api/browser/mainThreadWorkspace.ts +++ b/src/vs/workbench/api/browser/mainThreadWorkspace.ts @@ -203,25 +203,20 @@ export class MainThreadWorkspace implements MainThreadWorkspaceShape { // --- save & edit resources --- - async $save(uriComponents: UriComponents): Promise { + async $save(uriComponents: UriComponents, options: { saveAs: boolean }): Promise { const uri = URI.revive(uriComponents); const editors = [...this._editorService.findEditors(uri, { supportSideBySide: SideBySideEditor.PRIMARY })]; - const result = await this._editorService.save(editors, { reason: SaveReason.EXPLICIT, force: true /* force save even when non-dirty */ }); + const result = await this._editorService.save(editors, { + reason: SaveReason.EXPLICIT, + saveAs: options.saveAs, + force: !options.saveAs + }); - return firstOrDefault(this.saveResultToUris(result)); + return firstOrDefault(this._saveResultToUris(result)); } - async $saveAs(uriComponents: UriComponents): Promise { - const uri = URI.revive(uriComponents); - - const editors = [...this._editorService.findEditors(uri, { supportSideBySide: SideBySideEditor.PRIMARY })]; - const result = await this._editorService.save(editors, { reason: SaveReason.EXPLICIT, saveAs: true }); - - return firstOrDefault(this.saveResultToUris(result)); - } - - private saveResultToUris(result: ISaveEditorsResult): URI[] { + private _saveResultToUris(result: ISaveEditorsResult): URI[] { if (!result.success) { return []; } diff --git a/src/vs/workbench/api/common/extHost.protocol.ts b/src/vs/workbench/api/common/extHost.protocol.ts index cab1774b57f..b3693869658 100644 --- a/src/vs/workbench/api/common/extHost.protocol.ts +++ b/src/vs/workbench/api/common/extHost.protocol.ts @@ -1169,8 +1169,7 @@ export interface MainThreadWorkspaceShape extends IDisposable { $startFileSearch(includePattern: string | null, includeFolder: UriComponents | null, excludePatternOrDisregardExcludes: string | false | null, maxResults: number | null, token: CancellationToken): Promise; $startTextSearch(query: search.IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise; $checkExists(folders: readonly UriComponents[], includes: string[], token: CancellationToken): Promise; - $save(uri: UriComponents): Promise; - $saveAs(uri: UriComponents): Promise; + $save(uri: UriComponents, options: { saveAs: boolean }): Promise; $saveAll(includeUntitled?: boolean): Promise; $updateWorkspaceFolders(extensionName: string, index: number, deleteCount: number, workspaceFoldersToAdd: { uri: UriComponents; name?: string }[]): Promise; $resolveProxy(url: string): Promise; diff --git a/src/vs/workbench/api/common/extHostWorkspace.ts b/src/vs/workbench/api/common/extHostWorkspace.ts index 88069f0fb2d..b691389a3b6 100644 --- a/src/vs/workbench/api/common/extHostWorkspace.ts +++ b/src/vs/workbench/api/common/extHostWorkspace.ts @@ -558,13 +558,13 @@ export class ExtHostWorkspace implements ExtHostWorkspaceShape, IExtHostWorkspac } async save(uri: URI): Promise { - const result = await this._proxy.$save(uri); + const result = await this._proxy.$save(uri, { saveAs: false }); return URI.revive(result); } async saveAs(uri: URI): Promise { - const result = await this._proxy.$saveAs(uri); + const result = await this._proxy.$save(uri, { saveAs: true }); return URI.revive(result); } diff --git a/src/vscode-dts/vscode.proposed.saveEditor.d.ts b/src/vscode-dts/vscode.proposed.saveEditor.d.ts index 9b113aad70c..9088939a464 100644 --- a/src/vscode-dts/vscode.proposed.saveEditor.d.ts +++ b/src/vscode-dts/vscode.proposed.saveEditor.d.ts @@ -13,7 +13,9 @@ declare module 'vscode' { * Saves the editor identified by the given resource and returns the resulting resource or `undefined` * if save was not successful. * - * @param uri the associated uri for the editor to save. + * **Note** that an editor with the provided resource must be opened in order to be saved. + * + * @param uri the associated uri for the opened editor to save. * @return A thenable that resolves when the save operation has finished. */ export function save(uri: Uri): Thenable; @@ -22,7 +24,9 @@ declare module 'vscode' { * Saves the editor identified by the given resource to a new file name as provided by the user and * returns the resulting resource or `undefined` if save was not successful or cancelled. * - * @param uri the associated uri for the editor to save as. + * **Note** that an editor with the provided resource must be opened in order to be saved as. + * + * @param uri the associated uri for the opened editor to save as. * @return A thenable that resolves when the save-as operation has finished. */ export function saveAs(uri: Uri): Thenable;