Path: blob/main/src/vs/workbench/services/extensionRecommendations/common/extensionIgnoredRecommendationsService.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { distinct } from '../../../../base/common/arrays.js';6import { Emitter } from '../../../../base/common/event.js';7import { Disposable } from '../../../../base/common/lifecycle.js';8import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js';9import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';10import { IExtensionIgnoredRecommendationsService, IgnoredRecommendationChangeNotification } from './extensionRecommendations.js';11import { IWorkspaceExtensionsConfigService } from './workspaceExtensionsConfig.js';1213const ignoredRecommendationsStorageKey = 'extensionsAssistant/ignored_recommendations';1415export class ExtensionIgnoredRecommendationsService extends Disposable implements IExtensionIgnoredRecommendationsService {1617declare readonly _serviceBrand: undefined;1819private _onDidChangeIgnoredRecommendations = this._register(new Emitter<void>());20readonly onDidChangeIgnoredRecommendations = this._onDidChangeIgnoredRecommendations.event;2122// Global Ignored Recommendations23private _globalIgnoredRecommendations: string[] = [];24get globalIgnoredRecommendations(): string[] { return [...this._globalIgnoredRecommendations]; }25private _onDidChangeGlobalIgnoredRecommendation = this._register(new Emitter<IgnoredRecommendationChangeNotification>());26readonly onDidChangeGlobalIgnoredRecommendation = this._onDidChangeGlobalIgnoredRecommendation.event;2728// Ignored Workspace Recommendations29private ignoredWorkspaceRecommendations: string[] = [];3031get ignoredRecommendations(): string[] { return distinct([...this.globalIgnoredRecommendations, ...this.ignoredWorkspaceRecommendations]); }3233constructor(34@IWorkspaceExtensionsConfigService private readonly workspaceExtensionsConfigService: IWorkspaceExtensionsConfigService,35@IStorageService private readonly storageService: IStorageService,36) {37super();38this._globalIgnoredRecommendations = this.getCachedIgnoredRecommendations();39this._register(this.storageService.onDidChangeValue(StorageScope.PROFILE, ignoredRecommendationsStorageKey, this._store)(() => this.onDidStorageChange()));4041this.initIgnoredWorkspaceRecommendations();42}4344private async initIgnoredWorkspaceRecommendations(): Promise<void> {45this.ignoredWorkspaceRecommendations = await this.workspaceExtensionsConfigService.getUnwantedRecommendations();46this._onDidChangeIgnoredRecommendations.fire();47this._register(this.workspaceExtensionsConfigService.onDidChangeExtensionsConfigs(async () => {48this.ignoredWorkspaceRecommendations = await this.workspaceExtensionsConfigService.getUnwantedRecommendations();49this._onDidChangeIgnoredRecommendations.fire();50}));51}5253toggleGlobalIgnoredRecommendation(extensionId: string, shouldIgnore: boolean): void {54extensionId = extensionId.toLowerCase();55const ignored = this._globalIgnoredRecommendations.indexOf(extensionId) !== -1;56if (ignored === shouldIgnore) {57return;58}5960this._globalIgnoredRecommendations = shouldIgnore ? [...this._globalIgnoredRecommendations, extensionId] : this._globalIgnoredRecommendations.filter(id => id !== extensionId);61this.storeCachedIgnoredRecommendations(this._globalIgnoredRecommendations);62this._onDidChangeGlobalIgnoredRecommendation.fire({ extensionId, isRecommended: !shouldIgnore });63this._onDidChangeIgnoredRecommendations.fire();64}6566private getCachedIgnoredRecommendations(): string[] {67const ignoredRecommendations: string[] = JSON.parse(this.ignoredRecommendationsValue);68return ignoredRecommendations.map(e => e.toLowerCase());69}7071private onDidStorageChange(): void {72if (this.ignoredRecommendationsValue !== this.getStoredIgnoredRecommendationsValue() /* This checks if current window changed the value or not */) {73this._ignoredRecommendationsValue = undefined;74this._globalIgnoredRecommendations = this.getCachedIgnoredRecommendations();75this._onDidChangeIgnoredRecommendations.fire();76}77}7879private storeCachedIgnoredRecommendations(ignoredRecommendations: string[]): void {80this.ignoredRecommendationsValue = JSON.stringify(ignoredRecommendations);81}8283private _ignoredRecommendationsValue: string | undefined;84private get ignoredRecommendationsValue(): string {85if (!this._ignoredRecommendationsValue) {86this._ignoredRecommendationsValue = this.getStoredIgnoredRecommendationsValue();87}8889return this._ignoredRecommendationsValue;90}9192private set ignoredRecommendationsValue(ignoredRecommendationsValue: string) {93if (this.ignoredRecommendationsValue !== ignoredRecommendationsValue) {94this._ignoredRecommendationsValue = ignoredRecommendationsValue;95this.setStoredIgnoredRecommendationsValue(ignoredRecommendationsValue);96}97}9899private getStoredIgnoredRecommendationsValue(): string {100return this.storageService.get(ignoredRecommendationsStorageKey, StorageScope.PROFILE, '[]');101}102103private setStoredIgnoredRecommendationsValue(value: string): void {104this.storageService.store(ignoredRecommendationsStorageKey, value, StorageScope.PROFILE, StorageTarget.USER);105}106107}108109registerSingleton(IExtensionIgnoredRecommendationsService, ExtensionIgnoredRecommendationsService, InstantiationType.Delayed);110111112