@@ -7,6 +7,7 @@ import * as vscode from 'vscode';
77import { ErrorCodes , ResponseError } from 'vscode-languageclient' ;
88
99import * as cli from './cli' ;
10+ import * as config from './config' ;
1011import { DatabaseItem , getUpgradesDirectories } from './databases' ;
1112import * as helpers from './helpers' ;
1213import { DatabaseInfo , QueryMetadata , ResultsPaths } from './interface-types' ;
@@ -139,8 +140,8 @@ export class QueryInfo {
139140 target : this . quickEvalPosition ? {
140141 quickEval : { quickEvalPos : this . quickEvalPosition }
141142 } : {
142- query : { }
143- }
143+ query : { }
144+ }
144145 } ;
145146
146147 compiled = await helpers . withProgress ( {
@@ -259,7 +260,7 @@ async function checkDbschemeCompatibility(
259260
260261 if ( query . dbItem . contents !== undefined && query . dbItem . contents . dbSchemeUri !== undefined ) {
261262 const { scripts, finalDbscheme } = await cliServer . resolveUpgrades ( query . dbItem . contents . dbSchemeUri . fsPath , searchPath ) ;
262- const hash = async function ( filename : string ) : Promise < string > {
263+ const hash = async function ( filename : string ) : Promise < string > {
263264 return crypto . createHash ( 'sha256' ) . update ( await fs . readFile ( filename ) ) . digest ( 'hex' ) ;
264265 }
265266
@@ -294,14 +295,33 @@ async function checkDbschemeCompatibility(
294295 }
295296}
296297
297- /** Prompts the user to save `document` if it has unsaved changes. */
298- async function promptUserToSaveChanges ( document : vscode . TextDocument ) : Promise < void > {
298+ /**
299+ * Prompts the user to save `document` if it has unsaved changes.
300+ * Returns true if we should save changes.
301+ */
302+ async function promptUserToSaveChanges ( document : vscode . TextDocument ) : Promise < boolean > {
299303 if ( document . isDirty ) {
300- // TODO: add 'always save' button which records preference in configuration
301- if ( await helpers . showBinaryChoiceDialog ( 'Query file has unsaved changes. Save now?' ) ) {
302- await document . save ( ) ;
304+ if ( config . AUTOSAVE_SETTING . getValue ( ) ) {
305+ return true ;
306+ }
307+ else {
308+ const yesItem = { title : 'Yes' , isCloseAffordance : false } ;
309+ const alwaysItem = { title : 'Always Save' , isCloseAffordance : false } ;
310+ const noItem = { title : 'No' , isCloseAffordance : true }
311+ const message = 'Query file has unsaved changes. Save now?' ;
312+ const chosenItem = await vscode . window . showInformationMessage ( message , { modal : true } , yesItem , alwaysItem , noItem ) ;
313+
314+ if ( chosenItem === alwaysItem ) {
315+ await config . AUTOSAVE_SETTING . updateValue ( true , vscode . ConfigurationTarget . Workspace ) ;
316+ return true ;
317+ }
318+
319+ if ( chosenItem === yesItem ) {
320+ return true ;
321+ }
303322 }
304323 }
324+ return false ;
305325}
306326
307327type SelectedQuery = {
@@ -357,7 +377,9 @@ export async function determineSelectedQuery(selectedResourceUri: vscode.Uri | u
357377 // if the same file is open with unsaved changes in the active editor,
358378 // then prompt the user to save it first.
359379 if ( editor !== undefined && editor . document . uri . fsPath === queryPath ) {
360- await promptUserToSaveChanges ( editor . document ) ;
380+ if ( await promptUserToSaveChanges ( editor . document ) ) {
381+ editor . document . save ( ) ;
382+ }
361383 }
362384
363385 let quickEvalPosition : messages . Position | undefined = undefined ;
0 commit comments