Skip to content

Commit edb6387

Browse files
author
Benjamin Pasero
committed
Losing "Recently Opened" quickopen entries (fix microsoft#89257)
1 parent 7fc5d91 commit edb6387

1 file changed

Lines changed: 47 additions & 60 deletions

File tree

  • src/vs/workbench/services/history/browser

src/vs/workbench/services/history/browser/history.ts

Lines changed: 47 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import { addDisposableListener, EventType, EventHelper } from 'vs/base/browser/d
3434
import { IWorkspacesService } from 'vs/platform/workspaces/common/workspaces';
3535
import { Schemas } from 'vs/base/common/network';
3636
import { isEqual } from 'vs/base/common/resources';
37-
import { ILogService } from 'vs/platform/log/common/log';
3837

3938
/**
4039
* Stores the selection & view state of an editor and allows to compare it to other selection states.
@@ -112,8 +111,7 @@ export class HistoryService extends Disposable implements IHistoryService {
112111
@IWorkspacesService private readonly workspacesService: IWorkspacesService,
113112
@IInstantiationService private readonly instantiationService: IInstantiationService,
114113
@IWorkbenchLayoutService private readonly layoutService: IWorkbenchLayoutService,
115-
@IContextKeyService private readonly contextKeyService: IContextKeyService,
116-
@ILogService private readonly logService: ILogService
114+
@IContextKeyService private readonly contextKeyService: IContextKeyService
117115
) {
118116
super();
119117

@@ -277,7 +275,9 @@ export class HistoryService extends Disposable implements IHistoryService {
277275
}
278276

279277
clear(): void {
280-
this.ensureHistoryLoaded();
278+
279+
// History
280+
this.clearRecentlyOpened();
281281

282282
// Navigation (next, previous)
283283
this.navigationStackIndex = -1;
@@ -289,9 +289,6 @@ export class HistoryService extends Disposable implements IHistoryService {
289289
// Closed files
290290
this.recentlyClosedFiles = [];
291291

292-
// History
293-
this.clearRecentlyOpened();
294-
295292
// Context Keys
296293
this.updateContextKeys();
297294
}
@@ -719,8 +716,8 @@ export class HistoryService extends Disposable implements IHistoryService {
719716
private static readonly MAX_HISTORY_ITEMS = 200;
720717
private static readonly HISTORY_STORAGE_KEY = 'history.entries';
721718

722-
private history: Array<IEditorInput | IResourceInput> = [];
723-
private loaded = false;
719+
private history: Array<IEditorInput | IResourceInput> | undefined = undefined;
720+
724721
private readonly resourceFilter = this._register(this.instantiationService.createInstance(
725722
ResourceGlobMatcher,
726723
(root?: URI) => this.getExcludes(root),
@@ -741,11 +738,10 @@ export class HistoryService extends Disposable implements IHistoryService {
741738
return;
742739
}
743740

744-
this.ensureHistoryLoaded();
745-
746741
const historyInput = this.preferResourceInput(input);
747742

748743
// Remove any existing entry and add to the beginning
744+
this.ensureHistoryLoaded(this.history);
749745
this.removeFromHistory(input);
750746
this.history.unshift(historyInput);
751747

@@ -772,7 +768,7 @@ export class HistoryService extends Disposable implements IHistoryService {
772768
}
773769

774770
private removeExcludedFromHistory(): void {
775-
this.ensureHistoryLoaded();
771+
this.ensureHistoryLoaded(this.history);
776772

777773
this.history = this.history.filter(e => {
778774
const include = this.include(e);
@@ -787,7 +783,7 @@ export class HistoryService extends Disposable implements IHistoryService {
787783
}
788784

789785
private removeFromHistory(arg1: IEditorInput | IResourceInput | FileChangesEvent): void {
790-
this.ensureHistoryLoaded();
786+
this.ensureHistoryLoaded(this.history);
791787

792788
this.history = this.history.filter(e => {
793789
const matches = this.matches(arg1, e);
@@ -809,52 +805,18 @@ export class HistoryService extends Disposable implements IHistoryService {
809805
}
810806

811807
getHistory(): ReadonlyArray<IEditorInput | IResourceInput> {
812-
this.ensureHistoryLoaded();
808+
this.ensureHistoryLoaded(this.history);
813809

814810
return this.history.slice(0);
815811
}
816812

817-
private ensureHistoryLoaded(): void {
818-
if (!this.loaded) {
819-
this.loadHistory();
820-
}
821-
822-
this.loaded = true;
823-
}
824-
825-
private saveState(): void {
813+
private ensureHistoryLoaded(history: Array<IEditorInput | IResourceInput> | undefined): asserts history {
826814
if (!this.history) {
827-
return; // nothing to save because history was not used
815+
this.history = this.loadHistory();
828816
}
829-
830-
const registry = Registry.as<IEditorInputFactoryRegistry>(EditorExtensions.EditorInputFactories);
831-
832-
const entries: ISerializedEditorHistoryEntry[] = coalesce(this.history.map((input): ISerializedEditorHistoryEntry | undefined => {
833-
834-
// Editor input: try via factory
835-
if (input instanceof EditorInput) {
836-
const factory = registry.getEditorInputFactory(input.getTypeId());
837-
if (factory) {
838-
const deserialized = factory.serialize(input);
839-
if (deserialized) {
840-
return { editorInputJSON: { typeId: input.getTypeId(), deserialized } };
841-
}
842-
}
843-
}
844-
845-
// File resource: via URI.toJSON()
846-
else {
847-
return { resourceJSON: (input as IResourceInput).resource.toJSON() };
848-
}
849-
850-
return undefined;
851-
}));
852-
853-
this.logService.trace(`[editor history] saving ${entries.length} entries`);
854-
this.storageService.store(HistoryService.HISTORY_STORAGE_KEY, JSON.stringify(entries), StorageScope.WORKSPACE);
855817
}
856818

857-
private loadHistory(): void {
819+
private loadHistory(): Array<IEditorInput | IResourceInput> {
858820
let entries: ISerializedEditorHistoryEntry[] = [];
859821

860822
const entriesRaw = this.storageService.get(HistoryService.HISTORY_STORAGE_KEY, StorageScope.WORKSPACE);
@@ -864,17 +826,13 @@ export class HistoryService extends Disposable implements IHistoryService {
864826

865827
const registry = Registry.as<IEditorInputFactoryRegistry>(EditorExtensions.EditorInputFactories);
866828

867-
this.history = coalesce(entries.map(entry => {
829+
return coalesce(entries.map(entry => {
868830
try {
869831
return this.safeLoadHistoryEntry(registry, entry);
870832
} catch (error) {
871-
this.logService.error(`[editor history] error loading one editor history entry: ${error.toString()}`);
872-
873833
return undefined; // https://github.com/Microsoft/vscode/issues/60960
874834
}
875835
}));
876-
877-
this.logService.trace(`[editor history] loading ${this.history.length} entries`);
878836
}
879837

880838
private safeLoadHistoryEntry(registry: IEditorInputFactoryRegistry, entry: ISerializedEditorHistoryEntry): IEditorInput | IResourceInput | undefined {
@@ -902,6 +860,37 @@ export class HistoryService extends Disposable implements IHistoryService {
902860
return undefined;
903861
}
904862

863+
private saveState(): void {
864+
if (!this.history) {
865+
return; // nothing to save because history was not used
866+
}
867+
868+
const registry = Registry.as<IEditorInputFactoryRegistry>(EditorExtensions.EditorInputFactories);
869+
870+
const entries: ISerializedEditorHistoryEntry[] = coalesce(this.history.map((input): ISerializedEditorHistoryEntry | undefined => {
871+
872+
// Editor input: try via factory
873+
if (input instanceof EditorInput) {
874+
const factory = registry.getEditorInputFactory(input.getTypeId());
875+
if (factory) {
876+
const deserialized = factory.serialize(input);
877+
if (deserialized) {
878+
return { editorInputJSON: { typeId: input.getTypeId(), deserialized } };
879+
}
880+
}
881+
}
882+
883+
// File resource: via URI.toJSON()
884+
else {
885+
return { resourceJSON: (input as IResourceInput).resource.toJSON() };
886+
}
887+
888+
return undefined;
889+
}));
890+
891+
this.storageService.store(HistoryService.HISTORY_STORAGE_KEY, JSON.stringify(entries), StorageScope.WORKSPACE);
892+
}
893+
905894
//#endregion
906895

907896
//#region Last Active Workspace/File
@@ -925,8 +914,7 @@ export class HistoryService extends Disposable implements IHistoryService {
925914
}
926915

927916
// Multiple folders: find the last active one
928-
const history = this.getHistory();
929-
for (const input of history) {
917+
for (const input of this.getHistory()) {
930918
if (input instanceof EditorInput) {
931919
continue;
932920
}
@@ -954,8 +942,7 @@ export class HistoryService extends Disposable implements IHistoryService {
954942
}
955943

956944
getLastActiveFile(filterByScheme: string): URI | undefined {
957-
const history = this.getHistory();
958-
for (const input of history) {
945+
for (const input of this.getHistory()) {
959946
let resource: URI | undefined;
960947
if (input instanceof EditorInput) {
961948
resource = toResource(input, { filterByScheme });

0 commit comments

Comments
 (0)