Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/extensionRecommendations/common/extensionIgnoredRecommendationsService.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 { distinct } from '../../../../base/common/arrays.js';
7
import { Emitter } from '../../../../base/common/event.js';
8
import { Disposable } from '../../../../base/common/lifecycle.js';
9
import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js';
10
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
11
import { IExtensionIgnoredRecommendationsService, IgnoredRecommendationChangeNotification } from './extensionRecommendations.js';
12
import { IWorkspaceExtensionsConfigService } from './workspaceExtensionsConfig.js';
13
14
const ignoredRecommendationsStorageKey = 'extensionsAssistant/ignored_recommendations';
15
16
export class ExtensionIgnoredRecommendationsService extends Disposable implements IExtensionIgnoredRecommendationsService {
17
18
declare readonly _serviceBrand: undefined;
19
20
private _onDidChangeIgnoredRecommendations = this._register(new Emitter<void>());
21
readonly onDidChangeIgnoredRecommendations = this._onDidChangeIgnoredRecommendations.event;
22
23
// Global Ignored Recommendations
24
private _globalIgnoredRecommendations: string[] = [];
25
get globalIgnoredRecommendations(): string[] { return [...this._globalIgnoredRecommendations]; }
26
private _onDidChangeGlobalIgnoredRecommendation = this._register(new Emitter<IgnoredRecommendationChangeNotification>());
27
readonly onDidChangeGlobalIgnoredRecommendation = this._onDidChangeGlobalIgnoredRecommendation.event;
28
29
// Ignored Workspace Recommendations
30
private ignoredWorkspaceRecommendations: string[] = [];
31
32
get ignoredRecommendations(): string[] { return distinct([...this.globalIgnoredRecommendations, ...this.ignoredWorkspaceRecommendations]); }
33
34
constructor(
35
@IWorkspaceExtensionsConfigService private readonly workspaceExtensionsConfigService: IWorkspaceExtensionsConfigService,
36
@IStorageService private readonly storageService: IStorageService,
37
) {
38
super();
39
this._globalIgnoredRecommendations = this.getCachedIgnoredRecommendations();
40
this._register(this.storageService.onDidChangeValue(StorageScope.PROFILE, ignoredRecommendationsStorageKey, this._store)(() => this.onDidStorageChange()));
41
42
this.initIgnoredWorkspaceRecommendations();
43
}
44
45
private async initIgnoredWorkspaceRecommendations(): Promise<void> {
46
this.ignoredWorkspaceRecommendations = await this.workspaceExtensionsConfigService.getUnwantedRecommendations();
47
this._onDidChangeIgnoredRecommendations.fire();
48
this._register(this.workspaceExtensionsConfigService.onDidChangeExtensionsConfigs(async () => {
49
this.ignoredWorkspaceRecommendations = await this.workspaceExtensionsConfigService.getUnwantedRecommendations();
50
this._onDidChangeIgnoredRecommendations.fire();
51
}));
52
}
53
54
toggleGlobalIgnoredRecommendation(extensionId: string, shouldIgnore: boolean): void {
55
extensionId = extensionId.toLowerCase();
56
const ignored = this._globalIgnoredRecommendations.indexOf(extensionId) !== -1;
57
if (ignored === shouldIgnore) {
58
return;
59
}
60
61
this._globalIgnoredRecommendations = shouldIgnore ? [...this._globalIgnoredRecommendations, extensionId] : this._globalIgnoredRecommendations.filter(id => id !== extensionId);
62
this.storeCachedIgnoredRecommendations(this._globalIgnoredRecommendations);
63
this._onDidChangeGlobalIgnoredRecommendation.fire({ extensionId, isRecommended: !shouldIgnore });
64
this._onDidChangeIgnoredRecommendations.fire();
65
}
66
67
private getCachedIgnoredRecommendations(): string[] {
68
const ignoredRecommendations: string[] = JSON.parse(this.ignoredRecommendationsValue);
69
return ignoredRecommendations.map(e => e.toLowerCase());
70
}
71
72
private onDidStorageChange(): void {
73
if (this.ignoredRecommendationsValue !== this.getStoredIgnoredRecommendationsValue() /* This checks if current window changed the value or not */) {
74
this._ignoredRecommendationsValue = undefined;
75
this._globalIgnoredRecommendations = this.getCachedIgnoredRecommendations();
76
this._onDidChangeIgnoredRecommendations.fire();
77
}
78
}
79
80
private storeCachedIgnoredRecommendations(ignoredRecommendations: string[]): void {
81
this.ignoredRecommendationsValue = JSON.stringify(ignoredRecommendations);
82
}
83
84
private _ignoredRecommendationsValue: string | undefined;
85
private get ignoredRecommendationsValue(): string {
86
if (!this._ignoredRecommendationsValue) {
87
this._ignoredRecommendationsValue = this.getStoredIgnoredRecommendationsValue();
88
}
89
90
return this._ignoredRecommendationsValue;
91
}
92
93
private set ignoredRecommendationsValue(ignoredRecommendationsValue: string) {
94
if (this.ignoredRecommendationsValue !== ignoredRecommendationsValue) {
95
this._ignoredRecommendationsValue = ignoredRecommendationsValue;
96
this.setStoredIgnoredRecommendationsValue(ignoredRecommendationsValue);
97
}
98
}
99
100
private getStoredIgnoredRecommendationsValue(): string {
101
return this.storageService.get(ignoredRecommendationsStorageKey, StorageScope.PROFILE, '[]');
102
}
103
104
private setStoredIgnoredRecommendationsValue(value: string): void {
105
this.storageService.store(ignoredRecommendationsStorageKey, value, StorageScope.PROFILE, StorageTarget.USER);
106
}
107
108
}
109
110
registerSingleton(IExtensionIgnoredRecommendationsService, ExtensionIgnoredRecommendationsService, InstantiationType.Delayed);
111
112