@@ -16,12 +16,13 @@ import { IDisposable, dispose } from 'vs/base/common/lifecycle';
1616import paths = require( 'vs/base/common/paths' ) ;
1717import diagnostics = require( 'vs/base/common/diagnostics' ) ;
1818import types = require( 'vs/base/common/types' ) ;
19- import { IModelContentChangedEvent } from 'vs/editor/common/editorCommon' ;
19+ import { IModelContentChangedEvent , IRawText } from 'vs/editor/common/editorCommon' ;
2020import { IMode } from 'vs/editor/common/modes' ;
2121import { ILifecycleService } from 'vs/platform/lifecycle/common/lifecycle' ;
2222import { ITextFileService , IAutoSaveConfiguration , ModelState , ITextFileEditorModel , IModelSaveOptions , ISaveErrorHandler , ISaveParticipant , StateChange , SaveReason } from 'vs/workbench/services/textfile/common/textfiles' ;
2323import { EncodingMode , EditorModel } from 'vs/workbench/common/editor' ;
2424import { BaseTextEditorModel } from 'vs/workbench/common/editor/textEditorModel' ;
25+ import { IBackupService } from 'vs/platform/backup/common/backup' ;
2526import { IFileService , IFileStat , IFileOperationResult , FileOperationResult } from 'vs/platform/files/common/files' ;
2627import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation' ;
2728import { IMessageService , Severity } from 'vs/platform/message/common/message' ;
@@ -40,7 +41,6 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
4041 private static saveParticipant : ISaveParticipant ;
4142
4243 private resource : URI ;
43- private restoreResource : URI ;
4444 private contentEncoding : string ; // encoding as reported from disk
4545 private preferredEncoding : string ; // encoding as chosen by the user
4646 private dirty : boolean ;
@@ -71,7 +71,8 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
7171 @ILifecycleService private lifecycleService : ILifecycleService ,
7272 @IInstantiationService private instantiationService : IInstantiationService ,
7373 @ITelemetryService private telemetryService : ITelemetryService ,
74- @ITextFileService private textFileService : ITextFileService
74+ @ITextFileService private textFileService : ITextFileService ,
75+ @IBackupService private backupService : IBackupService
7576 ) {
7677 super ( modelService , modeService ) ;
7778
@@ -183,10 +184,6 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
183184 } ) ;
184185 }
185186
186- public setRestoreResource ( resource : URI ) : void {
187- this . restoreResource = resource ;
188- }
189-
190187 public load ( force ?: boolean /* bypass any caches and really go to disk */ ) : TPromise < EditorModel > {
191188 diag ( 'load() - enter' , this . resource , new Date ( ) ) ;
192189
@@ -264,12 +261,20 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
264261 else {
265262 diag ( 'load() - created text editor model' , this . resource , new Date ( ) ) ;
266263
267- if ( this . restoreResource ) {
268- this . createTextEditorModelPromise = this . textFileService . resolveTextContent ( this . restoreResource , { acceptTextOnly : true , etag : etag , encoding : this . preferredEncoding } ) . then ( ( restoreContent ) => {
269- return this . createTextEditorModel ( restoreContent . value , content . resource ) . then ( ( ) => {
264+ return this . backupService . doesTextFileHaveBackup ( this . resource ) . then ( backupExists => {
265+ let getContentPromise : TPromise < IRawText > ;
266+ if ( backupExists ) {
267+ const restoreResource = this . backupService . getBackupResource ( this . resource ) ;
268+ getContentPromise = this . textFileService . resolveTextContent ( restoreResource , { acceptTextOnly : true , etag : etag , encoding : this . preferredEncoding } ) . then ( content => content . value ) ;
269+ } else {
270+ getContentPromise = TPromise . as ( content . value ) ;
271+ }
272+
273+ this . createTextEditorModelPromise = getContentPromise . then ( fileContent => {
274+ return this . createTextEditorModel ( fileContent , content . resource ) . then ( ( ) => {
270275 this . createTextEditorModelPromise = null ;
271276
272- this . setDirty ( true ) ;
277+ this . setDirty ( backupExists ) ; // Ensure we are not tracking a stale state
273278 this . toDispose . push ( this . textEditorModel . onDidChangeRawContent ( ( e : IModelContentChangedEvent ) => this . onModelContentChanged ( e ) ) ) ;
274279
275280 return this ;
@@ -279,22 +284,9 @@ export class TextFileEditorModel extends BaseTextEditorModel implements ITextFil
279284 return TPromise . wrapError ( error ) ;
280285 } ) ;
281286 } ) ;
282- } else {
283- this . createTextEditorModelPromise = this . createTextEditorModel ( content . value , content . resource ) . then ( ( ) => {
284- this . createTextEditorModelPromise = null ;
285-
286- this . setDirty ( false ) ; // Ensure we are not tracking a stale state
287- this . toDispose . push ( this . textEditorModel . onDidChangeRawContent ( ( e : IModelContentChangedEvent ) => this . onModelContentChanged ( e ) ) ) ;
288-
289- return this ;
290- } , ( error ) => {
291- this . createTextEditorModelPromise = null ;
292287
293- return TPromise . wrapError ( error ) ;
294- } ) ;
295- }
296-
297- return this . createTextEditorModelPromise ;
288+ return this . createTextEditorModelPromise ;
289+ } ) ;
298290 }
299291 } , ( error ) => {
300292
0 commit comments