@@ -2,16 +2,8 @@ import * as messages from "./query-server/messages-shared";
22import * as legacyMessages from "./query-server/legacy-messages" ;
33import { DatabaseInfo , QueryMetadata } from "./common/interface-types" ;
44import { join , parse , dirname , basename } from "path" ;
5- import {
6- ConfigurationTarget ,
7- Range ,
8- TextDocument ,
9- TextEditor ,
10- Uri ,
11- window ,
12- } from "vscode" ;
13- import { isCanary , AUTOSAVE_SETTING } from "./config" ;
14- import { UserCancellationException } from "./common/vscode/progress" ;
5+ import { Range , TextEditor , Uri , window , workspace } from "vscode" ;
6+ import { isCanary , VSCODE_SAVE_BEFORE_START_SETTING } from "./config" ;
157import {
168 pathExists ,
179 readFile ,
@@ -491,55 +483,41 @@ async function getSelectedPosition(
491483 } ;
492484}
493485
486+ type SaveBeforeStartMode =
487+ | "nonUntitledEditorsInActiveGroup"
488+ | "allEditorsInActiveGroup"
489+ | "none" ;
490+
494491/**
495- * Prompts the user to save `document` if it has unsaved changes.
496- *
497- * @param document The document to save.
498- *
499- * @returns true if we should save changes and false if we should continue without saving changes.
500- * @throws UserCancellationException if we should abort whatever operation triggered this prompt
492+ * Saves dirty files before running queries, based on the user's settings.
501493 */
502- export async function promptUserToSaveChanges (
503- document : TextDocument ,
504- ) : Promise < boolean > {
505- if ( document . isDirty ) {
506- if ( AUTOSAVE_SETTING . getValue ( ) ) {
507- return true ;
508- } else {
509- const yesItem = { title : "Yes" , isCloseAffordance : false } ;
510- const alwaysItem = { title : "Always Save" , isCloseAffordance : false } ;
511- const noItem = {
512- title : "No (run version on disk)" ,
513- isCloseAffordance : false ,
514- } ;
515- const cancelItem = { title : "Cancel" , isCloseAffordance : true } ;
516- const message = `Query file '${ basename (
517- document . uri . fsPath ,
518- ) } ' has unsaved changes. Save now?`;
519- const chosenItem = await window . showInformationMessage (
520- message ,
521- { modal : true } ,
522- yesItem ,
523- alwaysItem ,
524- noItem ,
525- cancelItem ,
526- ) ;
527-
528- if ( chosenItem === alwaysItem ) {
529- await AUTOSAVE_SETTING . updateValue ( true , ConfigurationTarget . Workspace ) ;
530- return true ;
531- }
532-
533- if ( chosenItem === yesItem ) {
534- return true ;
535- }
536-
537- if ( chosenItem === cancelItem ) {
538- throw new UserCancellationException ( "Query run cancelled." , true ) ;
539- }
540- }
494+ export async function saveBeforeStart ( ) : Promise < void > {
495+ const mode : SaveBeforeStartMode =
496+ ( VSCODE_SAVE_BEFORE_START_SETTING . getValue < string > ( {
497+ languageId : "ql" ,
498+ } ) as SaveBeforeStartMode ) ?? "nonUntitledEditorsInActiveGroup" ;
499+
500+ // Despite the names of the modes, the VS Code implementation doesn't restrict itself to the
501+ // current tab group. It saves all dirty files in all groups. We'll do the same.
502+ switch ( mode ) {
503+ case "nonUntitledEditorsInActiveGroup" :
504+ await workspace . saveAll ( false ) ;
505+ break ;
506+
507+ case "allEditorsInActiveGroup" :
508+ // The VS Code implementation of this mode only saves an untitled file if it is the document
509+ // in the active editor. That's too much work for us, so we'll just live with the inconsistency.
510+ await workspace . saveAll ( true ) ;
511+ break ;
512+
513+ case "none" :
514+ break ;
515+
516+ default :
517+ // Unexpected value. Fall back to the default behavior.
518+ await workspace . saveAll ( false ) ;
519+ break ;
541520 }
542- return false ;
543521}
544522
545523/**
@@ -566,7 +544,7 @@ async function convertToQlPath(filePath: string): Promise<string> {
566544 }
567545 }
568546 }
569- throw new Error ( `Can't convert path to form suitable for QL:${ filePath } ` ) ;
547+ throw new Error ( `Can't convert path to form suitable for QL: ${ filePath } ` ) ;
570548 } else {
571549 return filePath ;
572550 }
0 commit comments