Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/extensions/browser/fileBasedRecommendations.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 { ExtensionRecommendations, GalleryExtensionRecommendation } from './extensionRecommendations.js';
7
import { EnablementState } from '../../../services/extensionManagement/common/extensionManagement.js';
8
import { ExtensionRecommendationReason, IExtensionIgnoredRecommendationsService } from '../../../services/extensionRecommendations/common/extensionRecommendations.js';
9
import { IExtensionsWorkbenchService, IExtension } from '../common/extensions.js';
10
import { localize } from '../../../../nls.js';
11
import { StorageScope, IStorageService, StorageTarget } from '../../../../platform/storage/common/storage.js';
12
import { IProductService } from '../../../../platform/product/common/productService.js';
13
import { IFileContentCondition, IFilePathCondition, IFileLanguageCondition, IFileOpenCondition } from '../../../../base/common/product.js';
14
import { IStringDictionary } from '../../../../base/common/collections.js';
15
import { ITextModel } from '../../../../editor/common/model.js';
16
import { Schemas } from '../../../../base/common/network.js';
17
import { basename, extname } from '../../../../base/common/resources.js';
18
import { match } from '../../../../base/common/glob.js';
19
import { URI } from '../../../../base/common/uri.js';
20
import { IModelService } from '../../../../editor/common/services/model.js';
21
import { ILanguageService } from '../../../../editor/common/languages/language.js';
22
import { IExtensionRecommendationNotificationService, RecommendationsNotificationResult, RecommendationSource } from '../../../../platform/extensionRecommendations/common/extensionRecommendations.js';
23
import { distinct } from '../../../../base/common/arrays.js';
24
import { DisposableStore } from '../../../../base/common/lifecycle.js';
25
import { CellUri } from '../../notebook/common/notebookCommon.js';
26
import { disposableTimeout } from '../../../../base/common/async.js';
27
import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js';
28
import { areSameExtensions } from '../../../../platform/extensionManagement/common/extensionManagementUtil.js';
29
import { isEmptyObject } from '../../../../base/common/types.js';
30
import { PLAINTEXT_LANGUAGE_ID } from '../../../../editor/common/languages/modesRegistry.js';
31
32
const promptedRecommendationsStorageKey = 'fileBasedRecommendations/promptedRecommendations';
33
const recommendationsStorageKey = 'extensionsAssistant/recommendations';
34
const milliSecondsInADay = 1000 * 60 * 60 * 24;
35
36
export class FileBasedRecommendations extends ExtensionRecommendations {
37
38
private readonly fileOpenRecommendations: IStringDictionary<IFileOpenCondition[]>;
39
private readonly recommendationsByPattern = new Map<string, IStringDictionary<IFileOpenCondition[]>>();
40
private readonly fileBasedRecommendations = new Map<string, { recommendedTime: number }>();
41
private readonly fileBasedImportantRecommendations = new Set<string>();
42
43
get recommendations(): ReadonlyArray<GalleryExtensionRecommendation> {
44
const recommendations: GalleryExtensionRecommendation[] = [];
45
[...this.fileBasedRecommendations.keys()]
46
.sort((a, b) => {
47
if (this.fileBasedRecommendations.get(a)!.recommendedTime === this.fileBasedRecommendations.get(b)!.recommendedTime) {
48
if (this.fileBasedImportantRecommendations.has(a)) {
49
return -1;
50
}
51
if (this.fileBasedImportantRecommendations.has(b)) {
52
return 1;
53
}
54
}
55
return this.fileBasedRecommendations.get(a)!.recommendedTime > this.fileBasedRecommendations.get(b)!.recommendedTime ? -1 : 1;
56
})
57
.forEach(extensionId => {
58
recommendations.push({
59
extension: extensionId,
60
reason: {
61
reasonId: ExtensionRecommendationReason.File,
62
reasonText: localize('fileBasedRecommendation', "This extension is recommended based on the files you recently opened.")
63
}
64
});
65
});
66
return recommendations;
67
}
68
69
get importantRecommendations(): ReadonlyArray<GalleryExtensionRecommendation> {
70
return this.recommendations.filter(e => this.fileBasedImportantRecommendations.has(e.extension));
71
}
72
73
get otherRecommendations(): ReadonlyArray<GalleryExtensionRecommendation> {
74
return this.recommendations.filter(e => !this.fileBasedImportantRecommendations.has(e.extension));
75
}
76
77
constructor(
78
@IExtensionsWorkbenchService private readonly extensionsWorkbenchService: IExtensionsWorkbenchService,
79
@IModelService private readonly modelService: IModelService,
80
@ILanguageService private readonly languageService: ILanguageService,
81
@IProductService productService: IProductService,
82
@IStorageService private readonly storageService: IStorageService,
83
@IExtensionRecommendationNotificationService private readonly extensionRecommendationNotificationService: IExtensionRecommendationNotificationService,
84
@IExtensionIgnoredRecommendationsService private readonly extensionIgnoredRecommendationsService: IExtensionIgnoredRecommendationsService,
85
@IWorkspaceContextService private readonly workspaceContextService: IWorkspaceContextService,
86
) {
87
super();
88
this.fileOpenRecommendations = {};
89
if (productService.extensionRecommendations) {
90
for (const [extensionId, recommendation] of Object.entries(productService.extensionRecommendations)) {
91
if (recommendation.onFileOpen) {
92
this.fileOpenRecommendations[extensionId.toLowerCase()] = recommendation.onFileOpen;
93
}
94
}
95
}
96
}
97
98
protected async doActivate(): Promise<void> {
99
if (isEmptyObject(this.fileOpenRecommendations)) {
100
return;
101
}
102
103
await this.extensionsWorkbenchService.whenInitialized;
104
105
const cachedRecommendations = this.getCachedRecommendations();
106
const now = Date.now();
107
// Retire existing recommendations if they are older than a week or are not part of this.productService.extensionTips anymore
108
Object.entries(cachedRecommendations).forEach(([key, value]) => {
109
const diff = (now - value) / milliSecondsInADay;
110
if (diff <= 7 && this.fileOpenRecommendations[key]) {
111
this.fileBasedRecommendations.set(key.toLowerCase(), { recommendedTime: value });
112
}
113
});
114
115
this._register(this.modelService.onModelAdded(model => this.onModelAdded(model)));
116
this.modelService.getModels().forEach(model => this.onModelAdded(model));
117
}
118
119
private onModelAdded(model: ITextModel): void {
120
const uri = model.uri.scheme === Schemas.vscodeNotebookCell ? CellUri.parse(model.uri)?.notebook : model.uri;
121
if (!uri) {
122
return;
123
}
124
125
const supportedSchemes = distinct([Schemas.untitled, Schemas.file, Schemas.vscodeRemote, ...this.workspaceContextService.getWorkspace().folders.map(folder => folder.uri.scheme)]);
126
if (!uri || !supportedSchemes.includes(uri.scheme)) {
127
return;
128
}
129
130
// re-schedule this bit of the operation to be off the critical path - in case glob-match is slow
131
disposableTimeout(() => this.promptImportantRecommendations(uri, model), 0, this._store);
132
}
133
134
/**
135
* Prompt the user to either install the recommended extension for the file type in the current editor model
136
* or prompt to search the marketplace if it has extensions that can support the file type
137
*/
138
private promptImportantRecommendations(uri: URI, model: ITextModel, extensionRecommendations?: IStringDictionary<IFileOpenCondition[]>): void {
139
if (model.isDisposed()) {
140
return;
141
}
142
143
const pattern = extname(uri).toLowerCase();
144
extensionRecommendations = extensionRecommendations ?? this.recommendationsByPattern.get(pattern) ?? this.fileOpenRecommendations;
145
const extensionRecommendationEntries = Object.entries(extensionRecommendations);
146
if (extensionRecommendationEntries.length === 0) {
147
return;
148
}
149
150
const processedPathGlobs = new Map<string, boolean>();
151
const installed = this.extensionsWorkbenchService.local;
152
const recommendationsByPattern: IStringDictionary<IFileOpenCondition[]> = {};
153
const matchedRecommendations: IStringDictionary<IFileOpenCondition[]> = {};
154
const unmatchedRecommendations: IStringDictionary<IFileOpenCondition[]> = {};
155
let listenOnLanguageChange = false;
156
const languageId = model.getLanguageId();
157
158
for (const [extensionId, conditions] of extensionRecommendationEntries) {
159
const conditionsByPattern: IFileOpenCondition[] = [];
160
const matchedConditions: IFileOpenCondition[] = [];
161
const unmatchedConditions: IFileOpenCondition[] = [];
162
for (const condition of conditions) {
163
let languageMatched = false;
164
let pathGlobMatched = false;
165
166
const isLanguageCondition = !!(<IFileLanguageCondition>condition).languages;
167
const isFileContentCondition = !!(<IFileContentCondition>condition).contentPattern;
168
if (isLanguageCondition || isFileContentCondition) {
169
conditionsByPattern.push(condition);
170
}
171
172
if (isLanguageCondition) {
173
if ((<IFileLanguageCondition>condition).languages.includes(languageId)) {
174
languageMatched = true;
175
}
176
}
177
178
if ((<IFilePathCondition>condition).pathGlob) {
179
const pathGlob = (<IFilePathCondition>condition).pathGlob;
180
if (processedPathGlobs.get(pathGlob) ?? match((<IFilePathCondition>condition).pathGlob, uri.with({ fragment: '' }).toString())) {
181
pathGlobMatched = true;
182
}
183
processedPathGlobs.set(pathGlob, pathGlobMatched);
184
}
185
186
let matched = languageMatched || pathGlobMatched;
187
188
// If the resource has pattern (extension) and not matched, then we don't need to check the other conditions
189
if (pattern && !matched) {
190
continue;
191
}
192
193
if (matched && condition.whenInstalled) {
194
if (!condition.whenInstalled.every(id => installed.some(local => areSameExtensions({ id }, local.identifier)))) {
195
matched = false;
196
}
197
}
198
199
if (matched && condition.whenNotInstalled) {
200
if (installed.some(local => condition.whenNotInstalled?.some(id => areSameExtensions({ id }, local.identifier)))) {
201
matched = false;
202
}
203
}
204
205
if (matched && isFileContentCondition) {
206
if (!model.findMatches((<IFileContentCondition>condition).contentPattern, false, true, false, null, false).length) {
207
matched = false;
208
}
209
}
210
211
if (matched) {
212
matchedConditions.push(condition);
213
conditionsByPattern.pop();
214
} else {
215
if (isLanguageCondition || isFileContentCondition) {
216
unmatchedConditions.push(condition);
217
if (isLanguageCondition) {
218
listenOnLanguageChange = true;
219
}
220
}
221
}
222
223
}
224
if (matchedConditions.length) {
225
matchedRecommendations[extensionId] = matchedConditions;
226
}
227
if (unmatchedConditions.length) {
228
unmatchedRecommendations[extensionId] = unmatchedConditions;
229
}
230
if (conditionsByPattern.length) {
231
recommendationsByPattern[extensionId] = conditionsByPattern;
232
}
233
}
234
235
if (pattern) {
236
this.recommendationsByPattern.set(pattern, recommendationsByPattern);
237
}
238
if (Object.keys(unmatchedRecommendations).length) {
239
if (listenOnLanguageChange) {
240
const disposables = new DisposableStore();
241
disposables.add(model.onDidChangeLanguage(() => {
242
// re-schedule this bit of the operation to be off the critical path - in case glob-match is slow
243
disposableTimeout(() => {
244
if (!disposables.isDisposed) {
245
this.promptImportantRecommendations(uri, model, unmatchedRecommendations);
246
disposables.dispose();
247
}
248
}, 0, disposables);
249
}));
250
disposables.add(model.onWillDispose(() => disposables.dispose()));
251
}
252
}
253
254
if (Object.keys(matchedRecommendations).length) {
255
this.promptFromRecommendations(uri, model, matchedRecommendations);
256
}
257
}
258
259
private promptFromRecommendations(uri: URI, model: ITextModel, extensionRecommendations: IStringDictionary<IFileOpenCondition[]>): void {
260
let isImportantRecommendationForLanguage = false;
261
const importantRecommendations = new Set<string>();
262
const fileBasedRecommendations = new Set<string>();
263
for (const [extensionId, conditions] of Object.entries(extensionRecommendations)) {
264
for (const condition of conditions) {
265
fileBasedRecommendations.add(extensionId);
266
if (condition.important) {
267
importantRecommendations.add(extensionId);
268
this.fileBasedImportantRecommendations.add(extensionId);
269
}
270
if ((<IFileLanguageCondition>condition).languages) {
271
isImportantRecommendationForLanguage = true;
272
}
273
}
274
}
275
276
// Update file based recommendations
277
for (const recommendation of fileBasedRecommendations) {
278
const filedBasedRecommendation = this.fileBasedRecommendations.get(recommendation) || { recommendedTime: Date.now(), sources: [] };
279
filedBasedRecommendation.recommendedTime = Date.now();
280
this.fileBasedRecommendations.set(recommendation, filedBasedRecommendation);
281
}
282
283
this.storeCachedRecommendations();
284
285
if (this.extensionRecommendationNotificationService.hasToIgnoreRecommendationNotifications()) {
286
return;
287
}
288
289
const language = model.getLanguageId();
290
const languageName = this.languageService.getLanguageName(language);
291
if (importantRecommendations.size &&
292
this.promptRecommendedExtensionForFileType(languageName && isImportantRecommendationForLanguage && language !== PLAINTEXT_LANGUAGE_ID ? localize('languageName', "the {0} language", languageName) : basename(uri), language, [...importantRecommendations])) {
293
return;
294
}
295
}
296
297
private promptRecommendedExtensionForFileType(name: string, language: string, recommendations: string[]): boolean {
298
recommendations = this.filterIgnoredOrNotAllowed(recommendations);
299
if (recommendations.length === 0) {
300
return false;
301
}
302
303
recommendations = this.filterInstalled(recommendations, this.extensionsWorkbenchService.local)
304
.filter(extensionId => this.fileBasedImportantRecommendations.has(extensionId));
305
306
const promptedRecommendations = language !== PLAINTEXT_LANGUAGE_ID ? this.getPromptedRecommendations()[language] : undefined;
307
if (promptedRecommendations) {
308
recommendations = recommendations.filter(extensionId => !promptedRecommendations.includes(extensionId));
309
}
310
311
if (recommendations.length === 0) {
312
return false;
313
}
314
315
this.promptImportantExtensionsInstallNotification(recommendations, name, language);
316
return true;
317
}
318
319
private async promptImportantExtensionsInstallNotification(extensions: string[], name: string, language: string): Promise<void> {
320
try {
321
const result = await this.extensionRecommendationNotificationService.promptImportantExtensionsInstallNotification({ extensions, name, source: RecommendationSource.FILE });
322
if (result === RecommendationsNotificationResult.Accepted) {
323
this.addToPromptedRecommendations(language, extensions);
324
}
325
} catch (error) { /* Ignore */ }
326
}
327
328
private getPromptedRecommendations(): IStringDictionary<string[]> {
329
return JSON.parse(this.storageService.get(promptedRecommendationsStorageKey, StorageScope.PROFILE, '{}'));
330
}
331
332
private addToPromptedRecommendations(language: string, extensions: string[]) {
333
const promptedRecommendations = this.getPromptedRecommendations();
334
promptedRecommendations[language] = distinct([...(promptedRecommendations[language] ?? []), ...extensions]);
335
this.storageService.store(promptedRecommendationsStorageKey, JSON.stringify(promptedRecommendations), StorageScope.PROFILE, StorageTarget.USER);
336
}
337
338
private filterIgnoredOrNotAllowed(recommendationsToSuggest: string[]): string[] {
339
const ignoredRecommendations = [...this.extensionIgnoredRecommendationsService.ignoredRecommendations, ...this.extensionRecommendationNotificationService.ignoredRecommendations];
340
return recommendationsToSuggest.filter(id => !ignoredRecommendations.includes(id));
341
}
342
343
private filterInstalled(recommendationsToSuggest: string[], installed: IExtension[]): string[] {
344
const installedExtensionsIds = installed.reduce((result, i) => {
345
if (i.enablementState !== EnablementState.DisabledByExtensionKind) {
346
result.add(i.identifier.id.toLowerCase());
347
}
348
return result;
349
}, new Set<string>());
350
return recommendationsToSuggest.filter(id => !installedExtensionsIds.has(id.toLowerCase()));
351
}
352
353
private getCachedRecommendations(): IStringDictionary<number> {
354
let storedRecommendations = JSON.parse(this.storageService.get(recommendationsStorageKey, StorageScope.PROFILE, '[]'));
355
if (Array.isArray(storedRecommendations)) {
356
storedRecommendations = storedRecommendations.reduce<IStringDictionary<number>>((result, id) => { result[id] = Date.now(); return result; }, {});
357
}
358
const result: IStringDictionary<number> = {};
359
Object.entries(storedRecommendations).forEach(([key, value]) => {
360
if (typeof value === 'number') {
361
result[key.toLowerCase()] = value;
362
}
363
});
364
return result;
365
}
366
367
private storeCachedRecommendations(): void {
368
const storedRecommendations: IStringDictionary<number> = {};
369
this.fileBasedRecommendations.forEach((value, key) => storedRecommendations[key] = value.recommendedTime);
370
this.storageService.store(recommendationsStorageKey, JSON.stringify(storedRecommendations), StorageScope.PROFILE, StorageTarget.MACHINE);
371
}
372
}
373
374