From 6c8e2bf8f31cec4d4222502ef370c00bd0cba27f Mon Sep 17 00:00:00 2001 From: Charles Gagnon Date: Fri, 4 Feb 2022 16:20:10 -0800 Subject: [PATCH] Fix export results default save location (#18224) * Fix export results default save location * cleanup --- src/sql/platform/common/pathUtilities.ts | 4 +--- .../services/query/common/resultSerializer.ts | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/sql/platform/common/pathUtilities.ts b/src/sql/platform/common/pathUtilities.ts index 8cf6c84044..e25631e39d 100644 --- a/src/sql/platform/common/pathUtilities.ts +++ b/src/sql/platform/common/pathUtilities.ts @@ -9,14 +9,12 @@ import { URI } from 'vs/base/common/uri'; import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace'; import { Schemas } from 'vs/base/common/network'; -export const FILE_SCHEMA: string = 'file'; - export function resolveCurrentDirectory(uri: string, rootPath?: string): string | undefined { let sqlUri = URI.parse(uri); let currentDirectory: string | undefined; // use current directory of the sql file if sql file is saved - if (sqlUri.scheme === FILE_SCHEMA) { + if (sqlUri.scheme === Schemas.file) { currentDirectory = dirname(sqlUri.fsPath); } else if (sqlUri.scheme === Schemas.untitled) { // if sql file is unsaved/untitled but a workspace is open use workspace root diff --git a/src/sql/workbench/services/query/common/resultSerializer.ts b/src/sql/workbench/services/query/common/resultSerializer.ts index ab560bfd80..4c0247a886 100644 --- a/src/sql/workbench/services/query/common/resultSerializer.ts +++ b/src/sql/workbench/services/query/common/resultSerializer.ts @@ -19,6 +19,7 @@ import { IConfigurationService } from 'vs/platform/configuration/common/configur import { IFileDialogService, FileFilter } from 'vs/platform/dialogs/common/dialogs'; import { IOpenerService } from 'vs/platform/opener/common/opener'; import { IQueryEditorConfiguration } from 'sql/platform/query/common/query'; +import { Schemas } from 'vs/base/common/network'; let prevSavePath: URI; @@ -99,23 +100,23 @@ export class ResultSerializer { return getRootPath(this._contextService); } - private promptForFilepath(format: SaveFormat, resourceUri: string): Promise { + private async promptForFilepath(format: SaveFormat, resourceUri: string): Promise { let filepathPlaceHolder = prevSavePath ? path.dirname(prevSavePath.fsPath) : resolveCurrentDirectory(resourceUri, this.rootPath); - if (filepathPlaceHolder) { - filepathPlaceHolder = path.join(filepathPlaceHolder, this.getResultsDefaultFilename(format)); + if (!filepathPlaceHolder) { + // If we haven't saved previously and there isn't a file path associated with this resource (e.g. for untitled files) + // then fall back to the system default + filepathPlaceHolder = (await this.fileDialogService.defaultFilePath(Schemas.file)).fsPath; } - - return this.fileDialogService.showSaveDialog({ + filepathPlaceHolder = path.join(filepathPlaceHolder, this.getResultsDefaultFilename(format)); + const fileUri = await this.fileDialogService.showSaveDialog({ title: nls.localize('resultsSerializer.saveAsFileTitle', "Choose Results File"), defaultUri: filepathPlaceHolder ? URI.file(filepathPlaceHolder) : undefined, filters: this.getResultsFileExtension(format) - }).then(filePath => { - if (filePath) { - prevSavePath = filePath; - return filePath; - } - return undefined; }); + if (fileUri) { + prevSavePath = fileUri; + } + return fileUri; } private getResultsDefaultFilename(format: SaveFormat): string {