diff --git a/src/vs/workbench/common/editor/untitledEditorInput.ts b/src/vs/workbench/common/editor/untitledEditorInput.ts index 3e7dab7fc96..17392c106b4 100644 --- a/src/vs/workbench/common/editor/untitledEditorInput.ts +++ b/src/vs/workbench/common/editor/untitledEditorInput.ts @@ -18,6 +18,7 @@ import { IModeService } from 'vs/editor/common/services/modeService'; import { IDisposable, dispose } from 'vs/base/common/lifecycle'; import Event, { Emitter } from 'vs/base/common/event'; import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles'; +import { IBackupFileService } from 'vs/workbench/services/backup/common/backup'; /** * An editor input to be used for untitled text buffers. @@ -28,8 +29,8 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput { public static SCHEMA: string = 'untitled'; private resource: URI; - private restoreResource: URI; private hasAssociatedFilePath: boolean; + private hasBackupToRestore: boolean; private modeId: string; private cachedModel: UntitledEditorModel; @@ -42,17 +43,18 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput { resource: URI, hasAssociatedFilePath: boolean, modeId: string, - restoreResource: URI, + hasBackupToRestore: boolean, @IInstantiationService private instantiationService: IInstantiationService, @IWorkspaceContextService private contextService: IWorkspaceContextService, @IModeService private modeService: IModeService, + @IBackupFileService private backupFileService: IBackupFileService, @ITextFileService private textFileService: ITextFileService ) { super(); this.resource = resource; - this.restoreResource = restoreResource; this.hasAssociatedFilePath = hasAssociatedFilePath; this.modeId = modeId; + this.hasBackupToRestore = hasBackupToRestore; this.toUnbind = []; this._onDidModelChangeContent = new Emitter(); this._onDidModelChangeEncoding = new Emitter(); @@ -87,8 +89,8 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput { return this.cachedModel.isDirty(); } - // untitled files with an associated path or restore resource are always dirty - return this.hasAssociatedFilePath || !!this.restoreResource; + // untitled files with an associated path or associated resource + return this.hasAssociatedFilePath || !!this.resource; } public confirmSave(): ConfirmResult { @@ -153,8 +155,12 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput { // Otherwise Create Model and load, restoring from backup if necessary let restorePromise: TPromise; - if (this.restoreResource) { - restorePromise = this.textFileService.resolveTextContent(this.restoreResource).then(rawTextContent => rawTextContent.value.lines.join('\n')); + if (this.hasBackupToRestore) { + // TODO: Pass in only Untitled-x into the constructor, evaluate whether there is a backup here. + const restoreResource = URI.from({ scheme: 'file', path: this.resource.fsPath }); + restorePromise = this.textFileService.resolveTextContent(restoreResource).then(rawTextContent => rawTextContent.value.lines.join('\n')); + this.resource = URI.from({ scheme: UntitledEditorInput.SCHEMA, path: paths.basename(this.resource.fsPath) }); + this.hasAssociatedFilePath = false; } else { restorePromise = TPromise.as(''); } diff --git a/src/vs/workbench/electron-browser/workbench.ts b/src/vs/workbench/electron-browser/workbench.ts index f9bbd8e4713..9c5252a8a40 100644 --- a/src/vs/workbench/electron-browser/workbench.ts +++ b/src/vs/workbench/electron-browser/workbench.ts @@ -291,7 +291,7 @@ export class Workbench implements IPartService { } else { if (this.workbenchParams.options.untitledToRestore && this.workbenchParams.options.untitledToRestore.length) { const untitledToRestoreInputs = this.workbenchParams.options.untitledToRestore.map(resourceInput => { - return this.untitledEditorService.createOrGet(null, null, resourceInput.resource); + return this.untitledEditorService.createOrGet(resourceInput.resource, null, true); }); editorOpenPromise = this.editorPart.restoreEditors(untitledToRestoreInputs); } else { diff --git a/src/vs/workbench/services/untitled/common/untitledEditorService.ts b/src/vs/workbench/services/untitled/common/untitledEditorService.ts index 1a2419a8e0b..946a6efc687 100644 --- a/src/vs/workbench/services/untitled/common/untitledEditorService.ts +++ b/src/vs/workbench/services/untitled/common/untitledEditorService.ts @@ -68,7 +68,7 @@ export interface IUntitledEditorService { * It is valid to pass in a file resource. In that case the path will be used as identifier. * The use case is to be able to create a new file with a specific path with VSCode. */ - createOrGet(resource?: URI, modeId?: string, restoreResource?: URI): UntitledEditorInput; + createOrGet(resource?: URI, modeId?: string, hasBackupToRestore?: boolean): UntitledEditorInput; /** * A check to find out if a untitled resource has a file path associated or not. @@ -154,7 +154,7 @@ export class UntitledEditorService implements IUntitledEditorService { .map((i) => i.getResource()); } - public createOrGet(resource?: URI, modeId?: string, restoreResource?: URI): UntitledEditorInput { + public createOrGet(resource?: URI, modeId?: string, hasBackupToRestore?: boolean): UntitledEditorInput { let hasAssociatedFilePath = false; if (resource) { hasAssociatedFilePath = (resource.scheme === 'file'); @@ -171,10 +171,10 @@ export class UntitledEditorService implements IUntitledEditorService { } // Create new otherwise - return this.doCreate(resource, hasAssociatedFilePath, modeId, restoreResource); + return this.doCreate(resource, hasAssociatedFilePath, modeId, hasBackupToRestore); } - private doCreate(resource?: URI, hasAssociatedFilePath?: boolean, modeId?: string, restoreResource?: URI): UntitledEditorInput { + private doCreate(resource?: URI, hasAssociatedFilePath?: boolean, modeId?: string, hasBackupToRestore?: boolean): UntitledEditorInput { if (!resource) { // Create new taking a resource URI that is not already taken @@ -185,7 +185,7 @@ export class UntitledEditorService implements IUntitledEditorService { } while (Object.keys(UntitledEditorService.CACHE).indexOf(resource.toString()) >= 0); } - const input = this.instantiationService.createInstance(UntitledEditorInput, resource, hasAssociatedFilePath, modeId, restoreResource); + const input = this.instantiationService.createInstance(UntitledEditorInput, resource, hasAssociatedFilePath, modeId, hasBackupToRestore); if (input.isDirty()) { setTimeout(() => { this._onDidChangeDirty.fire(resource);