Skip to content

Commit

Permalink
Extension IDs listed for 'unwantedRecommendations' should be ignored.
Browse files Browse the repository at this point in the history
- Support 'unwantedRecommendations' in .vscode/extensions.json and in
  the '.code-workspace' (multi-root) files

Signed-off-by: Roland Grunberg <[email protected]>
  • Loading branch information
rgrunber committed Sep 11, 2024
1 parent 018eeaf commit 00705e1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/vscode/impl/recommendationServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Level, Recommendation, RecommendationModel, RecommendationsTelemetrySer
import { IRecommendationService } from "../recommendationService";
import { IStorageService } from "../storageService";
import { StorageServiceImpl } from "./storageServiceImpl";
import { getInstalledExtensionName, isExtensionInstalled, promptUserUtil, installExtensionUtil } from "./vscodeUtil";
import { getInstalledExtensionName, isExtensionInstalled, promptUserUtil, installExtensionUtil, isUnwantedRecommendation } from "./vscodeUtil";

export const filterUnique = (value: any, index: number, self: any[]): boolean => self.indexOf(value) === index;

Expand Down Expand Up @@ -114,7 +114,7 @@ export class RecommendationServiceImpl implements IRecommendationService {
// Show a single recommendation immediately, if certain conditions are met
// Specifically, if the recommender is installed, and the recommended is not installed,
// and the recommended has not been timelocked in this session or ignored by user previously
if( this.ignoreRecommendations()) {
if( this.ignoreRecommendations() || await isUnwantedRecommendation(toExtension)) {
return;
}

Expand Down Expand Up @@ -154,6 +154,7 @@ export class RecommendationServiceImpl implements IRecommendationService {
const recommendedExtension: string[] = model.recommendations
.map((x) => x.extensionId)
.filter(filterUnique)
.filter((x) => !isUnwantedRecommendation(x))
.filter((x) => !isExtensionInstalled(x));
for( let i = 0; i < recommendedExtension.length; i++ ) {
this.showStartupRecommendationsForSingleExtension(model, recommendedExtension[i]);
Expand Down
42 changes: 41 additions & 1 deletion src/vscode/impl/vscodeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the EPL v2.0 License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import {commands, Disposable, extensions, window} from 'vscode';
import {commands, Disposable, extensions, window, workspace} from 'vscode';
import { Level, UserChoice } from '../recommendationModel';
import path from 'path';
import { existsSync } from 'fs';
import { readFile } from './util/fsUtil';

export const promptUserUtil = async (message: string, level: Level, hideNever: boolean): Promise<UserChoice | undefined> => {
const actions: Array<string> = Object.keys(UserChoice).filter((x) => x !== UserChoice.Never || !hideNever);
Expand Down Expand Up @@ -55,4 +58,41 @@ export const getInstalledExtensionName = (id: string): string | undefined => {
}).finally(() => {
installListenerDisposable.dispose();
});
}

export const getExtensionConfigurationFile = (): string | undefined => {
if (workspace.workspaceFolders !== undefined) {
if (workspace.workspaceFolders.length == 1) {
const file = path.resolve(workspace.workspaceFolders[0].uri.path, '.vscode', 'extensions.json');
if (existsSync(file)) {
return file;
}
} else {
const file = workspace.workspaceFile?.path;
if (file !== undefined && existsSync(file)) {
return file;
}
}
}
return undefined;
}

export const isUnwantedRecommendation = async (toExtension: string): Promise<boolean> => {
const extensionConfigFile = getExtensionConfigurationFile();
if (extensionConfigFile !== undefined) {
try {
const jsonData = await readFile(extensionConfigFile);
if (jsonData) {
let json = JSON.parse(jsonData);
if (workspace.workspaceFile?.path !== undefined) {
json = json['extensions'];
}
const unwantedRecommendations = json['unwantedRecommendations'];
return !!unwantedRecommendations && unwantedRecommendations.length > 0 && unwantedRecommendations.includes(toExtension);
}
} catch (err) {
// continue
}
}
return false;
}

0 comments on commit 00705e1

Please sign in to comment.