From 5ebd3b34608a7922e06a4755ba3181f06f401fdd Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Mon, 11 Sep 2017 18:50:11 +0200 Subject: [PATCH] Quick open history does not restore untitled files (fixes #34156) --- .../services/history/browser/history.ts | 48 ++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/vs/workbench/services/history/browser/history.ts b/src/vs/workbench/services/history/browser/history.ts index c4853ce1984..3b55591f9ff 100644 --- a/src/vs/workbench/services/history/browser/history.ts +++ b/src/vs/workbench/services/history/browser/history.ts @@ -79,9 +79,9 @@ export class TextEditorState { } } -interface ISerializedFileHistoryEntry { - resource?: string; - resourceJSON: object; +interface ISerializedEditorHistoryEntry { + resourceJSON?: object; + editorInputJSON?: { typeId: string; deserialized: string; }; } interface IEditorIdentifier { @@ -224,7 +224,7 @@ export class HistoryService extends BaseHistoryService implements IHistoryServic } private registerListeners(): void { - this.toUnbind.push(this.lifecycleService.onShutdown(reason => this.save())); + this.toUnbind.push(this.lifecycleService.onShutdown(reason => this.saveHistory())); this.toUnbind.push(this.editorGroupService.onEditorOpenFail(editor => this.remove(editor))); this.toUnbind.push(this.editorGroupService.getStacksModel().onEditorClosed(event => this.onEditorClosed(event))); this.toUnbind.push(this.fileService.onFileChanges(e => this.onFileChanges(e))); @@ -710,34 +710,58 @@ export class HistoryService extends BaseHistoryService implements IHistoryServic this.loaded = true; } - private save(): void { + private saveHistory(): void { if (!this.history) { return; // nothing to save because history was not used } - const entries: ISerializedFileHistoryEntry[] = this.history.map(input => { + const registry = Registry.as(Extensions.Editors); + + const entries: ISerializedEditorHistoryEntry[] = this.history.map(input => { + + // Editor input: try via factory if (input instanceof EditorInput) { - return void 0; // only file resource inputs are serializable currently + const factory = registry.getEditorInputFactory(input.getTypeId()); + if (factory) { + return { editorInputJSON: { typeId: input.getTypeId(), deserialized: factory.serialize(input) } } as ISerializedEditorHistoryEntry; + } + } + + // File resource: via URI.toJSON() + else { + return { resourceJSON: (input as IResourceInput).resource.toJSON() } as ISerializedEditorHistoryEntry; } - return { resourceJSON: (input as IResourceInput).resource.toJSON() }; + return void 0; }).filter(serialized => !!serialized); this.storageService.store(HistoryService.STORAGE_KEY, JSON.stringify(entries), StorageScope.WORKSPACE); } private loadHistory(): void { - let entries: ISerializedFileHistoryEntry[] = []; + let entries: ISerializedEditorHistoryEntry[] = []; const entriesRaw = this.storageService.get(HistoryService.STORAGE_KEY, StorageScope.WORKSPACE); if (entriesRaw) { entries = JSON.parse(entriesRaw); } + const registry = Registry.as(Extensions.Editors); + this.history = entries.map(entry => { - const serializedFileInput = entry as ISerializedFileHistoryEntry; - if (serializedFileInput.resource || serializedFileInput.resourceJSON) { - return { resource: !!serializedFileInput.resourceJSON ? URI.revive(serializedFileInput.resourceJSON) : URI.parse(serializedFileInput.resource) } as IResourceInput; + const serializedEditorHistoryEntry = entry as ISerializedEditorHistoryEntry; + + // File resource: via URI.revive() + if (serializedEditorHistoryEntry.resourceJSON) { + return { resource: URI.revive(serializedEditorHistoryEntry.resourceJSON) } as IResourceInput; + } + + // Editor input: via factory + if (serializedEditorHistoryEntry.editorInputJSON) { + const factory = registry.getEditorInputFactory(serializedEditorHistoryEntry.editorInputJSON.typeId); + if (factory) { + return factory.deserialize(this.instantiationService, serializedEditorHistoryEntry.editorInputJSON.deserialized); + } } return void 0; -- GitLab