Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/find/browser/findWidgetSearchHistory.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Emitter, Event } from '../../../../base/common/event.js';
7
import { IHistory } from '../../../../base/common/history.js';
8
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
9
10
export class FindWidgetSearchHistory implements IHistory<string> {
11
public static readonly FIND_HISTORY_KEY = 'workbench.find.history';
12
private inMemoryValues: Set<string> = new Set();
13
public onDidChange?: Event<string[]>;
14
private _onDidChangeEmitter: Emitter<string[]>;
15
16
private static _instance: FindWidgetSearchHistory | null = null;
17
18
static getOrCreate(
19
storageService: IStorageService,
20
): FindWidgetSearchHistory {
21
if (!FindWidgetSearchHistory._instance) {
22
FindWidgetSearchHistory._instance = new FindWidgetSearchHistory(storageService);
23
}
24
return FindWidgetSearchHistory._instance;
25
}
26
27
constructor(
28
@IStorageService private readonly storageService: IStorageService,
29
) {
30
this._onDidChangeEmitter = new Emitter<string[]>();
31
this.onDidChange = this._onDidChangeEmitter.event;
32
this.load();
33
}
34
35
delete(t: string): boolean {
36
const result = this.inMemoryValues.delete(t);
37
this.save();
38
return result;
39
}
40
41
add(t: string): this {
42
this.inMemoryValues.add(t);
43
this.save();
44
return this;
45
}
46
47
has(t: string): boolean {
48
return this.inMemoryValues.has(t);
49
}
50
51
clear(): void {
52
this.inMemoryValues.clear();
53
this.save();
54
}
55
56
forEach(callbackfn: (value: string, value2: string, set: Set<string>) => void, thisArg?: any): void {
57
// fetch latest from storage
58
this.load();
59
return this.inMemoryValues.forEach(callbackfn);
60
}
61
replace?(t: string[]): void {
62
this.inMemoryValues = new Set(t);
63
this.save();
64
}
65
66
load() {
67
let result: [] | undefined;
68
const raw = this.storageService.get(
69
FindWidgetSearchHistory.FIND_HISTORY_KEY,
70
StorageScope.WORKSPACE
71
);
72
73
if (raw) {
74
try {
75
result = JSON.parse(raw);
76
} catch (e) {
77
// Invalid data
78
}
79
}
80
81
this.inMemoryValues = new Set(result || []);
82
}
83
84
// Run saves async
85
save(): Promise<void> {
86
const elements: string[] = [];
87
this.inMemoryValues.forEach(e => elements.push(e));
88
return new Promise<void>(resolve => {
89
this.storageService.store(
90
FindWidgetSearchHistory.FIND_HISTORY_KEY,
91
JSON.stringify(elements),
92
StorageScope.WORKSPACE,
93
StorageTarget.USER,
94
);
95
this._onDidChangeEmitter.fire(elements);
96
resolve();
97
});
98
}
99
}
100
101