diff --git a/src/vs/platform/files/common/files.ts b/src/vs/platform/files/common/files.ts index fe73cfd090e46abaa46802e4426106b5bd410542..6fba116d6303a6bfabb53f1766891a0888482eb0 100644 --- a/src/vs/platform/files/common/files.ts +++ b/src/vs/platform/files/common/files.ts @@ -464,6 +464,8 @@ export const AutoSaveConfiguration = { ON_WINDOW_CHANGE: 'onWindowChange' }; +export const CONTENT_CHANGE_EVENT_BUFFER_DELAY = 1000; + export interface IFilesConfiguration { files: { associations: { [filepattern: string]: string }; diff --git a/src/vs/workbench/common/editor/untitledEditorModel.ts b/src/vs/workbench/common/editor/untitledEditorModel.ts index 88b5dcc8a2083385e4db6e560f4fa236ebe28c2f..6d4507fa1f468a1efb52e7a4ac28f92133df105b 100644 --- a/src/vs/workbench/common/editor/untitledEditorModel.ts +++ b/src/vs/workbench/common/editor/untitledEditorModel.ts @@ -11,7 +11,7 @@ import { StringEditorModel } from 'vs/workbench/common/editor/stringEditorModel' import URI from 'vs/base/common/uri'; import { PLAINTEXT_MODE_ID } from 'vs/editor/common/modes/modesRegistry'; import { EndOfLinePreference } from 'vs/editor/common/editorCommon'; -import { IFilesConfiguration } from 'vs/platform/files/common/files'; +import { IFilesConfiguration, CONTENT_CHANGE_EVENT_BUFFER_DELAY } from 'vs/platform/files/common/files'; import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; import { IModeService } from 'vs/editor/common/services/modeService'; import { IModelService } from 'vs/editor/common/services/modelService'; @@ -23,7 +23,7 @@ import { ITextFileService } from 'vs/workbench/services/textfile/common/textfile export class UntitledEditorModel extends StringEditorModel implements IEncodingSupport { - public static DEFAULT_CONTENT_CHANGE_BUFFER_DELAY = 1000; + public static DEFAULT_CONTENT_CHANGE_BUFFER_DELAY = CONTENT_CHANGE_EVENT_BUFFER_DELAY; private textModelChangeListener: IDisposable; private configurationChangeListener: IDisposable; diff --git a/src/vs/workbench/parts/backup/common/backupModelTracker.ts b/src/vs/workbench/parts/backup/common/backupModelTracker.ts index 95a59514a3b2630122c90c8a023d2c83adc04219..c437b4d70f8756eeabb8ece2415444a6ffb38da4 100644 --- a/src/vs/workbench/parts/backup/common/backupModelTracker.ts +++ b/src/vs/workbench/parts/backup/common/backupModelTracker.ts @@ -13,18 +13,24 @@ import { ITextFileService, TextFileModelChangeEvent, StateChange } from 'vs/work import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService'; import { IEnvironmentService } from 'vs/platform/environment/common/environment'; import { IWorkbenchContribution } from 'vs/workbench/common/contributions'; +import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; +import { IFilesConfiguration, AutoSaveConfiguration, CONTENT_CHANGE_EVENT_BUFFER_DELAY } from 'vs/platform/files/common/files'; + +const AUTO_SAVE_AFTER_DELAY_DISABLED_TIME = CONTENT_CHANGE_EVENT_BUFFER_DELAY + 500; export class BackupModelTracker implements IWorkbenchContribution { public _serviceBrand: any; + private configuredAutoSaveAfterDelay: boolean; private toDispose: IDisposable[]; constructor( @IBackupFileService private backupFileService: IBackupFileService, @ITextFileService private textFileService: ITextFileService, @IUntitledEditorService private untitledEditorService: IUntitledEditorService, - @IEnvironmentService private environmentService: IEnvironmentService + @IEnvironmentService private environmentService: IEnvironmentService, + @IConfigurationService private configurationService: IConfigurationService ) { this.toDispose = []; @@ -44,14 +50,32 @@ export class BackupModelTracker implements IWorkbenchContribution { // Listen for untitled model changes this.toDispose.push(this.untitledEditorService.onDidChangeContent((e) => this.onUntitledModelChanged(e))); this.toDispose.push(this.untitledEditorService.onDidDisposeModel((e) => this.discardBackup(e))); + + // Listen to config changes + this.toDispose.push(this.configurationService.onDidUpdateConfiguration(e => this.onConfigurationChange(e.config))); + } + + private onConfigurationChange(configuration: IFilesConfiguration): void { + if (!configuration || !configuration.files) { + this.configuredAutoSaveAfterDelay = false; + return; + } + this.configuredAutoSaveAfterDelay = + (configuration.files.autoSave === AutoSaveConfiguration.AFTER_DELAY && + configuration.files.autoSaveDelay <= AUTO_SAVE_AFTER_DELAY_DISABLED_TIME); } private onTextFileModelChanged(event: TextFileModelChangeEvent): void { if (event.kind === StateChange.REVERTED) { + // This must proceed even if auto save after delay is configured in order to clean up + // any backups made before the config change this.discardBackup(event.resource); } else if (event.kind === StateChange.CONTENT_CHANGE) { - const model = this.textFileService.models.get(event.resource); - this.backupFileService.backupResource(model.getResource(), model.getValue(), model.getVersionId()).done(null, errors.onUnexpectedError); + // Do not backup when auto save after delay is configured + if (!this.configuredAutoSaveAfterDelay) { + const model = this.textFileService.models.get(event.resource); + this.backupFileService.backupResource(model.getResource(), model.getValue(), model.getVersionId()).done(null, errors.onUnexpectedError); + } } } diff --git a/src/vs/workbench/services/textfile/common/textFileEditorModel.ts b/src/vs/workbench/services/textfile/common/textFileEditorModel.ts index af093986eb13833f5eabf1d9f42671979e641eb4..1f77ec2ae4e7e659d9c62dd883e1e49e089513ef 100644 --- a/src/vs/workbench/services/textfile/common/textFileEditorModel.ts +++ b/src/vs/workbench/services/textfile/common/textFileEditorModel.ts @@ -23,7 +23,7 @@ import { ITextFileService, IAutoSaveConfiguration, ModelState, ITextFileEditorMo import { EncodingMode, EditorModel } from 'vs/workbench/common/editor'; import { BaseTextEditorModel } from 'vs/workbench/common/editor/textEditorModel'; import { IBackupFileService, BACKUP_FILE_RESOLVE_OPTIONS } from 'vs/workbench/services/backup/common/backup'; -import { IFileService, IFileStat, IFileOperationResult, FileOperationResult, IContent } from 'vs/platform/files/common/files'; +import { IFileService, IFileStat, IFileOperationResult, FileOperationResult, IContent, CONTENT_CHANGE_EVENT_BUFFER_DELAY } from 'vs/platform/files/common/files'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { IMessageService, Severity } from 'vs/platform/message/common/message'; import { IModeService } from 'vs/editor/common/services/modeService'; @@ -38,7 +38,7 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil public static ID = 'workbench.editors.files.textFileEditorModel'; - public static DEFAULT_CONTENT_CHANGE_BUFFER_DELAY = 1000; + public static DEFAULT_CONTENT_CHANGE_BUFFER_DELAY = CONTENT_CHANGE_EVENT_BUFFER_DELAY; private static saveErrorHandler: ISaveErrorHandler; private static saveParticipant: ISaveParticipant;