Path: blob/main/src/vs/workbench/contrib/extensions/browser/extensionsViews.ts
5251 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 { localize } from '../../../../nls.js';6import { Disposable, DisposableStore, toDisposable } from '../../../../base/common/lifecycle.js';7import { Event, Emitter } from '../../../../base/common/event.js';8import { isCancellationError, getErrorMessage, CancellationError } from '../../../../base/common/errors.js';9import { PagedModel, IPagedModel, DelayedPagedModel, IPager } from '../../../../base/common/paging.js';10import { SortOrder, IQueryOptions as IGalleryQueryOptions, SortBy as GallerySortBy, InstallExtensionInfo, ExtensionGalleryErrorCode, ExtensionGalleryError } from '../../../../platform/extensionManagement/common/extensionManagement.js';11import { IExtensionManagementServer, IExtensionManagementServerService, EnablementState, IWorkbenchExtensionManagementService, IWorkbenchExtensionEnablementService } from '../../../services/extensionManagement/common/extensionManagement.js';12import { IExtensionRecommendationsService } from '../../../services/extensionRecommendations/common/extensionRecommendations.js';13import { areSameExtensions, getExtensionDependencies } from '../../../../platform/extensionManagement/common/extensionManagementUtil.js';14import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.js';15import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.js';16import { append, $ } from '../../../../base/browser/dom.js';17import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';18import { ExtensionResultsListFocused, ExtensionState, IExtension, IExtensionsViewState, IExtensionsWorkbenchService, IWorkspaceRecommendedExtensionsView } from '../common/extensions.js';19import { Query } from '../common/extensionQuery.js';20import { IExtensionService, toExtension } from '../../../services/extensions/common/extensions.js';21import { IThemeService } from '../../../../platform/theme/common/themeService.js';22import { IViewletViewOptions } from '../../../browser/parts/views/viewsViewlet.js';23import { ITelemetryService } from '../../../../platform/telemetry/common/telemetry.js';24import { CountBadge } from '../../../../base/browser/ui/countBadge/countBadge.js';25import { WorkbenchPagedList } from '../../../../platform/list/browser/listService.js';26import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';27import { INotificationService, Severity } from '../../../../platform/notification/common/notification.js';28import { ViewPane, IViewPaneOptions, ViewPaneShowActions } from '../../../browser/parts/views/viewPane.js';29import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js';30import { coalesce, distinct, range } from '../../../../base/common/arrays.js';31import { alert } from '../../../../base/browser/ui/aria/aria.js';32import { CancellationToken, CancellationTokenSource } from '../../../../base/common/cancellation.js';33import { ActionRunner } from '../../../../base/common/actions.js';34import { ExtensionIdentifier, ExtensionIdentifierMap, ExtensionUntrustedWorkspaceSupportType, ExtensionVirtualWorkspaceSupportType, IExtensionDescription, IExtensionIdentifier, isLanguagePackExtension } from '../../../../platform/extensions/common/extensions.js';35import { CancelablePromise, createCancelablePromise, ThrottledDelayer } from '../../../../base/common/async.js';36import { IProductService } from '../../../../platform/product/common/productService.js';37import { SeverityIcon } from '../../../../base/browser/ui/severityIcon/severityIcon.js';38import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js';39import { IViewDescriptorService } from '../../../common/views.js';40import { IOpenerService } from '../../../../platform/opener/common/opener.js';41import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';42import { IExtensionManifestPropertiesService } from '../../../services/extensions/common/extensionManifestPropertiesService.js';43import { isVirtualWorkspace } from '../../../../platform/workspace/common/virtualWorkspace.js';44import { IWorkspaceTrustManagementService } from '../../../../platform/workspace/common/workspaceTrust.js';45import { ILogService } from '../../../../platform/log/common/log.js';46import { isOfflineError } from '../../../../base/parts/request/common/request.js';47import { defaultCountBadgeStyles } from '../../../../platform/theme/browser/defaultStyles.js';48import { Registry } from '../../../../platform/registry/common/platform.js';49import { Extensions, IExtensionFeatureRenderer, IExtensionFeaturesManagementService, IExtensionFeaturesRegistry } from '../../../services/extensionManagement/common/extensionFeatures.js';50import { URI } from '../../../../base/common/uri.js';51import { isString } from '../../../../base/common/types.js';52import { IUriIdentityService } from '../../../../platform/uriIdentity/common/uriIdentity.js';53import { IHoverService } from '../../../../platform/hover/browser/hover.js';54import { ExtensionsList } from './extensionsViewer.js';5556export const NONE_CATEGORY = 'none';5758type Message = {59readonly text: string;60readonly severity: Severity;61};6263class ExtensionsViewState extends Disposable implements IExtensionsViewState {6465private readonly _onFocus: Emitter<IExtension> = this._register(new Emitter<IExtension>());66readonly onFocus: Event<IExtension> = this._onFocus.event;6768private readonly _onBlur: Emitter<IExtension> = this._register(new Emitter<IExtension>());69readonly onBlur: Event<IExtension> = this._onBlur.event;7071private currentlyFocusedItems: IExtension[] = [];7273filters: {74featureId?: string;75} = {};7677onFocusChange(extensions: IExtension[]): void {78this.currentlyFocusedItems.forEach(extension => this._onBlur.fire(extension));79this.currentlyFocusedItems = extensions;80this.currentlyFocusedItems.forEach(extension => this._onFocus.fire(extension));81}82}8384export interface ExtensionsListViewOptions {85server?: IExtensionManagementServer;86flexibleHeight?: boolean;87readonly onDidChangeTitle?: Event<string>;88hideBadge?: boolean;89}9091interface IQueryResult {92model: IPagedModel<IExtension>;93message?: { text: string; severity: Severity };94readonly onDidChangeModel?: Event<IPagedModel<IExtension>>;95readonly disposables: DisposableStore;96}9798const enum LocalSortBy {99UpdateDate = 'UpdateDate',100}101102function isLocalSortBy(value: any): value is LocalSortBy {103switch (value as LocalSortBy) {104case LocalSortBy.UpdateDate: return true;105}106}107108type SortBy = LocalSortBy | GallerySortBy;109type IQueryOptions = Omit<IGalleryQueryOptions, 'sortBy'> & { sortBy?: SortBy };110111export abstract class AbstractExtensionsListView<T> extends ViewPane {112abstract show(query: string, refresh?: boolean): Promise<IPagedModel<T>>;113}114115export class ExtensionsListView extends AbstractExtensionsListView<IExtension> {116117private static RECENT_UPDATE_DURATION = 7 * 24 * 60 * 60 * 1000; // 7 days118119private bodyTemplate: {120messageContainer: HTMLElement;121messageSeverityIcon: HTMLElement;122messageBox: HTMLElement;123extensionsList: HTMLElement;124} | undefined;125private badge: CountBadge | undefined;126private list: WorkbenchPagedList<IExtension> | null = null;127private queryRequest: { query: string; request: CancelablePromise<IPagedModel<IExtension>> } | null = null;128private queryResult: IQueryResult | undefined;129private extensionsViewState: ExtensionsViewState | undefined;130131private readonly contextMenuActionRunner = this._register(new ActionRunner());132133constructor(134protected readonly options: ExtensionsListViewOptions,135viewletViewOptions: IViewletViewOptions,136@INotificationService protected notificationService: INotificationService,137@IKeybindingService keybindingService: IKeybindingService,138@IContextMenuService contextMenuService: IContextMenuService,139@IInstantiationService instantiationService: IInstantiationService,140@IThemeService themeService: IThemeService,141@IExtensionService private readonly extensionService: IExtensionService,142@IExtensionsWorkbenchService protected extensionsWorkbenchService: IExtensionsWorkbenchService,143@IExtensionRecommendationsService protected extensionRecommendationsService: IExtensionRecommendationsService,144@ITelemetryService protected readonly telemetryService: ITelemetryService,145@IHoverService hoverService: IHoverService,146@IConfigurationService configurationService: IConfigurationService,147@IWorkspaceContextService protected contextService: IWorkspaceContextService,148@IExtensionManagementServerService protected readonly extensionManagementServerService: IExtensionManagementServerService,149@IExtensionManifestPropertiesService private readonly extensionManifestPropertiesService: IExtensionManifestPropertiesService,150@IWorkbenchExtensionManagementService protected readonly extensionManagementService: IWorkbenchExtensionManagementService,151@IWorkspaceContextService protected readonly workspaceService: IWorkspaceContextService,152@IProductService protected readonly productService: IProductService,153@IContextKeyService contextKeyService: IContextKeyService,154@IViewDescriptorService viewDescriptorService: IViewDescriptorService,155@IOpenerService openerService: IOpenerService,156@IStorageService private readonly storageService: IStorageService,157@IWorkspaceTrustManagementService private readonly workspaceTrustManagementService: IWorkspaceTrustManagementService,158@IWorkbenchExtensionEnablementService private readonly extensionEnablementService: IWorkbenchExtensionEnablementService,159@IExtensionFeaturesManagementService private readonly extensionFeaturesManagementService: IExtensionFeaturesManagementService,160@IUriIdentityService protected readonly uriIdentityService: IUriIdentityService,161@ILogService private readonly logService: ILogService162) {163super({164...(viewletViewOptions as IViewPaneOptions),165showActions: ViewPaneShowActions.Always,166maximumBodySize: options.flexibleHeight ? (storageService.getNumber(`${viewletViewOptions.id}.size`, StorageScope.PROFILE, 0) ? undefined : 0) : undefined167}, keybindingService, contextMenuService, configurationService, contextKeyService, viewDescriptorService, instantiationService, openerService, themeService, hoverService);168if (this.options.onDidChangeTitle) {169this._register(this.options.onDidChangeTitle(title => this.updateTitle(title)));170}171172this._register(this.contextMenuActionRunner.onDidRun(({ error }) => error && this.notificationService.error(error)));173this.registerActions();174}175176protected registerActions(): void { }177178protected override renderHeader(container: HTMLElement): void {179container.classList.add('extension-view-header');180super.renderHeader(container);181182if (!this.options.hideBadge) {183this.badge = this._register(new CountBadge(append(container, $('.count-badge-wrapper')), {}, defaultCountBadgeStyles));184}185}186187protected override renderBody(container: HTMLElement): void {188super.renderBody(container);189190const messageContainer = append(container, $('.message-container'));191const messageSeverityIcon = append(messageContainer, $(''));192const messageBox = append(messageContainer, $('.message'));193const extensionsList = append(container, $('.extensions-list'));194this.extensionsViewState = this._register(new ExtensionsViewState());195this.list = this._register(this.instantiationService.createInstance(ExtensionsList, extensionsList, this.id, {}, this.extensionsViewState)).list;196ExtensionResultsListFocused.bindTo(this.list.contextKeyService);197this._register(this.list.onDidChangeFocus(e => this.extensionsViewState?.onFocusChange(coalesce(e.elements)), this));198199this.bodyTemplate = {200extensionsList,201messageBox,202messageContainer,203messageSeverityIcon204};205206if (this.queryResult) {207this.setModel(this.queryResult.model);208}209}210211protected override layoutBody(height: number, width: number): void {212super.layoutBody(height, width);213if (this.bodyTemplate) {214this.bodyTemplate.extensionsList.style.height = height + 'px';215}216this.list?.layout(height, width);217}218219async show(query: string, refresh?: boolean): Promise<IPagedModel<IExtension>> {220if (this.queryRequest) {221if (!refresh && this.queryRequest.query === query) {222return this.queryRequest.request;223}224this.queryRequest.request.cancel();225this.queryRequest = null;226}227228if (this.queryResult) {229this.queryResult.disposables.dispose();230this.queryResult = undefined;231if (this.extensionsViewState) {232this.extensionsViewState.filters = {};233}234}235236const parsedQuery = Query.parse(query);237238const options: IQueryOptions = {239sortOrder: SortOrder.Default240};241242switch (parsedQuery.sortBy) {243case 'installs': options.sortBy = GallerySortBy.InstallCount; break;244case 'rating': options.sortBy = GallerySortBy.WeightedRating; break;245case 'name': options.sortBy = GallerySortBy.Title; break;246case 'publishedDate': options.sortBy = GallerySortBy.PublishedDate; break;247case 'updateDate': options.sortBy = LocalSortBy.UpdateDate; break;248}249250const request = createCancelablePromise(async token => {251try {252this.queryResult = await this.query(parsedQuery, options, token);253const model = this.queryResult.model;254this.setModel(model, this.queryResult.message);255if (this.queryResult.onDidChangeModel) {256this.queryResult.disposables.add(this.queryResult.onDidChangeModel(model => {257if (this.queryResult) {258this.queryResult.model = model;259this.updateModel(model);260}261}));262}263return model;264} catch (e) {265const model = new PagedModel([]);266if (!isCancellationError(e)) {267this.logService.error(e);268this.setModel(model, this.getMessage(e));269}270return this.list ? this.list.model : model;271}272});273274request.finally(() => this.queryRequest = null);275this.queryRequest = { query, request };276return request;277}278279count(): number {280return this.queryResult?.model.length ?? 0;281}282283protected showEmptyModel(): Promise<IPagedModel<IExtension>> {284const emptyModel = new PagedModel([]);285this.setModel(emptyModel);286return Promise.resolve(emptyModel);287}288289private async query(query: Query, options: IQueryOptions, token: CancellationToken): Promise<IQueryResult> {290const idRegex = /@id:(([a-z0-9A-Z][a-z0-9\-A-Z]*)\.([a-z0-9A-Z][a-z0-9\-A-Z]*))/g;291const ids: string[] = [];292let idMatch;293while ((idMatch = idRegex.exec(query.value)) !== null) {294const name = idMatch[1];295ids.push(name);296}297if (ids.length) {298const model = await this.queryByIds(ids, options, token);299return { model, disposables: new DisposableStore() };300}301302if (ExtensionsListView.isLocalExtensionsQuery(query.value, query.sortBy)) {303return this.queryLocal(query, options);304}305306if (ExtensionsListView.isSearchPopularQuery(query.value)) {307query.value = query.value.replace('@popular', '');308options.sortBy = !options.sortBy ? GallerySortBy.InstallCount : options.sortBy;309}310else if (ExtensionsListView.isSearchRecentlyPublishedQuery(query.value)) {311query.value = query.value.replace('@recentlyPublished', '');312options.sortBy = !options.sortBy ? GallerySortBy.PublishedDate : options.sortBy;313}314315const galleryQueryOptions: IGalleryQueryOptions = { ...options, sortBy: isLocalSortBy(options.sortBy) ? undefined : options.sortBy };316return this.queryGallery(query, galleryQueryOptions, token);317}318319private async queryByIds(ids: string[], options: IQueryOptions, token: CancellationToken): Promise<IPagedModel<IExtension>> {320const idsSet: Set<string> = ids.reduce((result, id) => { result.add(id.toLowerCase()); return result; }, new Set<string>());321const result = (await this.extensionsWorkbenchService.queryLocal(this.options.server))322.filter(e => idsSet.has(e.identifier.id.toLowerCase()));323324const galleryIds = result.length ? ids.filter(id => result.every(r => !areSameExtensions(r.identifier, { id }))) : ids;325326if (galleryIds.length) {327const galleryResult = await this.extensionsWorkbenchService.getExtensions(galleryIds.map(id => ({ id })), { source: 'queryById' }, token);328result.push(...galleryResult);329}330331return new PagedModel(result);332}333334private async queryLocal(query: Query, options: IQueryOptions): Promise<IQueryResult> {335const local = await this.extensionsWorkbenchService.queryLocal(this.options.server);336let { extensions, canIncludeInstalledExtensions, description } = await this.filterLocal(local, this.extensionService.extensions, query, options);337const disposables = new DisposableStore();338const onDidChangeModel = disposables.add(new Emitter<IPagedModel<IExtension>>());339340if (canIncludeInstalledExtensions) {341let isDisposed: boolean = false;342disposables.add(toDisposable(() => isDisposed = true));343disposables.add(Event.debounce(Event.any(344Event.filter(this.extensionsWorkbenchService.onChange, e => e?.state === ExtensionState.Installed),345this.extensionService.onDidChangeExtensions346), () => undefined)(async () => {347const local = this.options.server ? this.extensionsWorkbenchService.installed.filter(e => e.server === this.options.server) : this.extensionsWorkbenchService.local;348const { extensions: newExtensions } = await this.filterLocal(local, this.extensionService.extensions, query, options);349if (!isDisposed) {350const mergedExtensions = this.mergeAddedExtensions(extensions, newExtensions);351if (mergedExtensions) {352extensions = mergedExtensions;353onDidChangeModel.fire(new PagedModel(extensions));354}355}356}));357}358359return {360model: new PagedModel(extensions),361message: description ? { text: description, severity: Severity.Info } : undefined,362onDidChangeModel: onDidChangeModel.event,363disposables364};365}366367private async filterLocal(local: IExtension[], runningExtensions: readonly IExtensionDescription[], query: Query, options: IQueryOptions): Promise<{ extensions: IExtension[]; canIncludeInstalledExtensions: boolean; description?: string }> {368const value = query.value;369let extensions: IExtension[] = [];370let description: string | undefined;371const includeBuiltin = /@builtin/i.test(value);372const canIncludeInstalledExtensions = !includeBuiltin;373374if (/@installed/i.test(value)) {375extensions = this.filterInstalledExtensions(local, runningExtensions, query, options);376}377378else if (/@outdated/i.test(value)) {379extensions = this.filterOutdatedExtensions(local, query, options);380}381382else if (/@disabled/i.test(value)) {383extensions = this.filterDisabledExtensions(local, runningExtensions, query, options, includeBuiltin);384}385386else if (/@enabled/i.test(value)) {387extensions = this.filterEnabledExtensions(local, runningExtensions, query, options, includeBuiltin);388}389390else if (/@workspaceUnsupported/i.test(value)) {391extensions = this.filterWorkspaceUnsupportedExtensions(local, query, options);392}393394else if (/@deprecated/i.test(query.value)) {395extensions = await this.filterDeprecatedExtensions(local, query, options);396}397398else if (/@recentlyUpdated/i.test(query.value)) {399extensions = this.filterRecentlyUpdatedExtensions(local, query, options);400}401402else if (/@contribute:/i.test(query.value)) {403extensions = this.filterExtensionsByFeature(local, query);404}405406else if (includeBuiltin) {407extensions = this.filterBuiltinExtensions(local, query, options);408}409410return { extensions, canIncludeInstalledExtensions, description };411}412413private filterBuiltinExtensions(local: IExtension[], query: Query, options: IQueryOptions): IExtension[] {414let { value, includedCategories, excludedCategories } = this.parseCategories(query.value);415value = value.replaceAll(/@builtin/gi, '').replaceAll(/@sort:(\w+)(-\w*)?/g, '').trim().toLowerCase();416417const result = local418.filter(e => e.isBuiltin && (e.name.toLowerCase().indexOf(value) > -1 || e.displayName.toLowerCase().indexOf(value) > -1)419&& this.filterExtensionByCategory(e, includedCategories, excludedCategories));420421return this.sortExtensions(result, options);422}423424private filterExtensionByCategory(e: IExtension, includedCategories: string[], excludedCategories: string[]): boolean {425if (!includedCategories.length && !excludedCategories.length) {426return true;427}428if (e.categories.length) {429if (excludedCategories.length && e.categories.some(category => excludedCategories.includes(category.toLowerCase()))) {430return false;431}432return e.categories.some(category => includedCategories.includes(category.toLowerCase()));433} else {434return includedCategories.includes(NONE_CATEGORY);435}436}437438private parseCategories(value: string): { value: string; includedCategories: string[]; excludedCategories: string[] } {439const includedCategories: string[] = [];440const excludedCategories: string[] = [];441value = value.replace(/\bcategory:("([^"]*)"|([^"]\S*))(\s+|\b|$)/g, (_, quotedCategory, category) => {442const entry = (category || quotedCategory || '').toLowerCase();443if (entry.startsWith('-')) {444if (excludedCategories.indexOf(entry) === -1) {445excludedCategories.push(entry);446}447} else {448if (includedCategories.indexOf(entry) === -1) {449includedCategories.push(entry);450}451}452return '';453});454return { value, includedCategories, excludedCategories };455}456457private filterInstalledExtensions(local: IExtension[], runningExtensions: readonly IExtensionDescription[], query: Query, options: IQueryOptions): IExtension[] {458let { value, includedCategories, excludedCategories } = this.parseCategories(query.value);459460value = value.replace(/@installed/g, '').replace(/@sort:(\w+)(-\w*)?/g, '').trim().toLowerCase();461462const matchingText = (e: IExtension) => (e.name.toLowerCase().indexOf(value) > -1 || e.displayName.toLowerCase().indexOf(value) > -1 || e.description.toLowerCase().indexOf(value) > -1)463&& this.filterExtensionByCategory(e, includedCategories, excludedCategories);464let result;465466if (options.sortBy !== undefined) {467result = local.filter(e => !e.isBuiltin && matchingText(e));468result = this.sortExtensions(result, options);469} else {470result = local.filter(e => (!e.isBuiltin || e.outdated || e.runtimeState !== undefined) && matchingText(e));471const runningExtensionsById = runningExtensions.reduce((result, e) => { result.set(e.identifier.value, e); return result; }, new ExtensionIdentifierMap<IExtensionDescription>());472473const defaultSort = (e1: IExtension, e2: IExtension) => {474const running1 = runningExtensionsById.get(e1.identifier.id);475const isE1Running = !!running1 && this.extensionManagementServerService.getExtensionManagementServer(toExtension(running1)) === e1.server;476const running2 = runningExtensionsById.get(e2.identifier.id);477const isE2Running = running2 && this.extensionManagementServerService.getExtensionManagementServer(toExtension(running2)) === e2.server;478if ((isE1Running && isE2Running)) {479return e1.displayName.localeCompare(e2.displayName);480}481const isE1LanguagePackExtension = e1.local && isLanguagePackExtension(e1.local.manifest);482const isE2LanguagePackExtension = e2.local && isLanguagePackExtension(e2.local.manifest);483if (!isE1Running && !isE2Running) {484if (isE1LanguagePackExtension) {485return -1;486}487if (isE2LanguagePackExtension) {488return 1;489}490return e1.displayName.localeCompare(e2.displayName);491}492if ((isE1Running && isE2LanguagePackExtension) || (isE2Running && isE1LanguagePackExtension)) {493return e1.displayName.localeCompare(e2.displayName);494}495return isE1Running ? -1 : 1;496};497498const incompatible: IExtension[] = [];499const deprecated: IExtension[] = [];500const outdated: IExtension[] = [];501const actionRequired: IExtension[] = [];502const noActionRequired: IExtension[] = [];503504for (const e of result) {505if (e.enablementState === EnablementState.DisabledByInvalidExtension) {506incompatible.push(e);507}508else if (e.deprecationInfo) {509deprecated.push(e);510}511else if (e.outdated && this.extensionEnablementService.isEnabledEnablementState(e.enablementState)) {512outdated.push(e);513}514else if (e.runtimeState) {515actionRequired.push(e);516}517else {518noActionRequired.push(e);519}520}521522result = [523...incompatible.sort(defaultSort),524...deprecated.sort(defaultSort),525...outdated.sort(defaultSort),526...actionRequired.sort(defaultSort),527...noActionRequired.sort(defaultSort)528];529}530return result;531}532533private filterOutdatedExtensions(local: IExtension[], query: Query, options: IQueryOptions): IExtension[] {534let { value, includedCategories, excludedCategories } = this.parseCategories(query.value);535536value = value.replace(/@outdated/g, '').replace(/@sort:(\w+)(-\w*)?/g, '').trim().toLowerCase();537538const result = local539.sort((e1, e2) => e1.displayName.localeCompare(e2.displayName))540.filter(extension => extension.outdated541&& (extension.name.toLowerCase().indexOf(value) > -1 || extension.displayName.toLowerCase().indexOf(value) > -1)542&& this.filterExtensionByCategory(extension, includedCategories, excludedCategories));543544return this.sortExtensions(result, options);545}546547private filterDisabledExtensions(local: IExtension[], runningExtensions: readonly IExtensionDescription[], query: Query, options: IQueryOptions, includeBuiltin: boolean): IExtension[] {548let { value, includedCategories, excludedCategories } = this.parseCategories(query.value);549550value = value.replaceAll(/@disabled|@builtin/gi, '').replaceAll(/@sort:(\w+)(-\w*)?/g, '').trim().toLowerCase();551552if (includeBuiltin) {553local = local.filter(e => e.isBuiltin);554}555const result = local556.sort((e1, e2) => e1.displayName.localeCompare(e2.displayName))557.filter(e => runningExtensions.every(r => !areSameExtensions({ id: r.identifier.value, uuid: r.uuid }, e.identifier))558&& (e.name.toLowerCase().indexOf(value) > -1 || e.displayName.toLowerCase().indexOf(value) > -1)559&& this.filterExtensionByCategory(e, includedCategories, excludedCategories));560561return this.sortExtensions(result, options);562}563564private filterEnabledExtensions(local: IExtension[], runningExtensions: readonly IExtensionDescription[], query: Query, options: IQueryOptions, includeBuiltin: boolean): IExtension[] {565let { value, includedCategories, excludedCategories } = this.parseCategories(query.value);566567value = value ? value.replaceAll(/@enabled|@builtin/gi, '').replaceAll(/@sort:(\w+)(-\w*)?/g, '').trim().toLowerCase() : '';568569local = local.filter(e => e.isBuiltin === includeBuiltin);570const result = local571.sort((e1, e2) => e1.displayName.localeCompare(e2.displayName))572.filter(e => runningExtensions.some(r => areSameExtensions({ id: r.identifier.value, uuid: r.uuid }, e.identifier))573&& (e.name.toLowerCase().indexOf(value) > -1 || e.displayName.toLowerCase().indexOf(value) > -1)574&& this.filterExtensionByCategory(e, includedCategories, excludedCategories));575576return this.sortExtensions(result, options);577}578579private filterWorkspaceUnsupportedExtensions(local: IExtension[], query: Query, options: IQueryOptions): IExtension[] {580// shows local extensions which are restricted or disabled in the current workspace because of the extension's capability581582const queryString = query.value; // @sortby is already filtered out583584const match = queryString.match(/^\s*@workspaceUnsupported(?::(untrusted|virtual)(Partial)?)?(?:\s+([^\s]*))?/i);585if (!match) {586return [];587}588const type = match[1]?.toLowerCase();589const partial = !!match[2];590const nameFilter = match[3]?.toLowerCase();591592if (nameFilter) {593local = local.filter(extension => extension.name.toLowerCase().indexOf(nameFilter) > -1 || extension.displayName.toLowerCase().indexOf(nameFilter) > -1);594}595596const hasVirtualSupportType = (extension: IExtension, supportType: ExtensionVirtualWorkspaceSupportType) => {597return extension.local && this.extensionManifestPropertiesService.getExtensionVirtualWorkspaceSupportType(extension.local.manifest) === supportType;598};599600const hasRestrictedSupportType = (extension: IExtension, supportType: ExtensionUntrustedWorkspaceSupportType) => {601if (!extension.local) {602return false;603}604605const enablementState = this.extensionEnablementService.getEnablementState(extension.local);606if (enablementState !== EnablementState.EnabledGlobally && enablementState !== EnablementState.EnabledWorkspace &&607enablementState !== EnablementState.DisabledByTrustRequirement && enablementState !== EnablementState.DisabledByExtensionDependency) {608return false;609}610611if (this.extensionManifestPropertiesService.getExtensionUntrustedWorkspaceSupportType(extension.local.manifest) === supportType) {612return true;613}614615if (supportType === false) {616const dependencies = getExtensionDependencies(local.map(ext => ext.local!), extension.local);617return dependencies.some(ext => this.extensionManifestPropertiesService.getExtensionUntrustedWorkspaceSupportType(ext.manifest) === supportType);618}619620return false;621};622623const inVirtualWorkspace = isVirtualWorkspace(this.workspaceService.getWorkspace());624const inRestrictedWorkspace = !this.workspaceTrustManagementService.isWorkspaceTrusted();625626if (type === 'virtual') {627// show limited and disabled extensions unless disabled because of a untrusted workspace628local = local.filter(extension => inVirtualWorkspace && hasVirtualSupportType(extension, partial ? 'limited' : false) && !(inRestrictedWorkspace && hasRestrictedSupportType(extension, false)));629} else if (type === 'untrusted') {630// show limited and disabled extensions unless disabled because of a virtual workspace631local = local.filter(extension => hasRestrictedSupportType(extension, partial ? 'limited' : false) && !(inVirtualWorkspace && hasVirtualSupportType(extension, false)));632} else {633// show extensions that are restricted or disabled in the current workspace634local = local.filter(extension => inVirtualWorkspace && !hasVirtualSupportType(extension, true) || inRestrictedWorkspace && !hasRestrictedSupportType(extension, true));635}636return this.sortExtensions(local, options);637}638639private async filterDeprecatedExtensions(local: IExtension[], query: Query, options: IQueryOptions): Promise<IExtension[]> {640const value = query.value.replace(/@deprecated/g, '').replace(/@sort:(\w+)(-\w*)?/g, '').trim().toLowerCase();641const extensionsControlManifest = await this.extensionManagementService.getExtensionsControlManifest();642const deprecatedExtensionIds = Object.keys(extensionsControlManifest.deprecated);643local = local.filter(e => deprecatedExtensionIds.includes(e.identifier.id) && (!value || e.name.toLowerCase().indexOf(value) > -1 || e.displayName.toLowerCase().indexOf(value) > -1));644return this.sortExtensions(local, options);645}646647private filterRecentlyUpdatedExtensions(local: IExtension[], query: Query, options: IQueryOptions): IExtension[] {648let { value, includedCategories, excludedCategories } = this.parseCategories(query.value);649const currentTime = Date.now();650local = local.filter(e => !e.isBuiltin && !e.outdated && e.local?.updated && e.local?.installedTimestamp !== undefined && currentTime - e.local.installedTimestamp < ExtensionsListView.RECENT_UPDATE_DURATION);651652value = value.replace(/@recentlyUpdated/g, '').replace(/@sort:(\w+)(-\w*)?/g, '').trim().toLowerCase();653654const result = local.filter(e =>655(e.name.toLowerCase().indexOf(value) > -1 || e.displayName.toLowerCase().indexOf(value) > -1)656&& this.filterExtensionByCategory(e, includedCategories, excludedCategories));657658options.sortBy = options.sortBy ?? LocalSortBy.UpdateDate;659660return this.sortExtensions(result, options);661}662663private filterExtensionsByFeature(local: IExtension[], query: Query): IExtension[] {664const value = query.value.replace(/@contribute:/g, '').trim();665const featureId = value.split(' ')[0];666const feature = Registry.as<IExtensionFeaturesRegistry>(Extensions.ExtensionFeaturesRegistry).getExtensionFeature(featureId);667if (!feature) {668return [];669}670if (this.extensionsViewState) {671this.extensionsViewState.filters.featureId = featureId;672}673const renderer = feature.renderer ? this.instantiationService.createInstance<IExtensionFeatureRenderer>(feature.renderer) : undefined;674try {675const result: [IExtension, number][] = [];676for (const e of local) {677if (!e.local) {678continue;679}680const accessData = this.extensionFeaturesManagementService.getAccessData(new ExtensionIdentifier(e.identifier.id), featureId);681const shouldRender = renderer?.shouldRender(e.local.manifest);682if (accessData || shouldRender) {683result.push([e, accessData?.accessTimes.length ?? 0]);684}685}686return result.sort(([, a], [, b]) => b - a).map(([e]) => e);687} finally {688renderer?.dispose();689}690}691692private mergeAddedExtensions(extensions: IExtension[], newExtensions: IExtension[]): IExtension[] | undefined {693const oldExtensions = [...extensions];694const findPreviousExtensionIndex = (from: number): number => {695let index = -1;696const previousExtensionInNew = newExtensions[from];697if (previousExtensionInNew) {698index = oldExtensions.findIndex(e => areSameExtensions(e.identifier, previousExtensionInNew.identifier));699if (index === -1) {700return findPreviousExtensionIndex(from - 1);701}702}703return index;704};705706let hasChanged: boolean = false;707for (let index = 0; index < newExtensions.length; index++) {708const extension = newExtensions[index];709if (extensions.every(r => !areSameExtensions(r.identifier, extension.identifier))) {710hasChanged = true;711extensions.splice(findPreviousExtensionIndex(index - 1) + 1, 0, extension);712}713}714715return hasChanged ? extensions : undefined;716}717718private async queryGallery(query: Query, options: IGalleryQueryOptions, token: CancellationToken): Promise<IQueryResult> {719const hasUserDefinedSortOrder = options.sortBy !== undefined;720if (!hasUserDefinedSortOrder && !query.value.trim()) {721options.sortBy = GallerySortBy.InstallCount;722}723724if (this.isRecommendationsQuery(query)) {725const model = await this.queryRecommendations(query, options, token);726return { model, disposables: new DisposableStore() };727}728729const text = query.value;730731if (!text) {732options.source = 'viewlet';733const pager = await this.extensionsWorkbenchService.queryGallery(options, token);734return { model: new PagedModel(pager), disposables: new DisposableStore() };735}736737if (/\bext:([^\s]+)\b/g.test(text)) {738options.text = text;739options.source = 'file-extension-tags';740const pager = await this.extensionsWorkbenchService.queryGallery(options, token);741return { model: new PagedModel(pager), disposables: new DisposableStore() };742}743744options.text = text.substring(0, 350);745options.source = 'searchText';746747if (hasUserDefinedSortOrder || /\b(category|tag):([^\s]+)\b/gi.test(text) || /\bfeatured(\s+|\b|$)/gi.test(text)) {748const pager = await this.extensionsWorkbenchService.queryGallery(options, token);749return { model: new PagedModel(pager), disposables: new DisposableStore() };750}751752try {753const [pager, preferredExtensions] = await Promise.all([754this.extensionsWorkbenchService.queryGallery(options, token),755this.getPreferredExtensions(options.text.toLowerCase(), token).catch(() => [])756]);757758const model = preferredExtensions.length ? new PreferredExtensionsPagedModel(preferredExtensions, pager) : new PagedModel(pager);759return { model, disposables: new DisposableStore() };760} catch (error) {761if (isCancellationError(error)) {762throw error;763}764765if (!(error instanceof ExtensionGalleryError)) {766throw error;767}768769const searchText = options.text.toLowerCase();770const localExtensions = this.extensionsWorkbenchService.local.filter(e => !e.isBuiltin && (e.name.toLowerCase().indexOf(searchText) > -1 || e.displayName.toLowerCase().indexOf(searchText) > -1 || e.description.toLowerCase().indexOf(searchText) > -1));771if (localExtensions.length) {772const message = this.getMessage(error);773return { model: new PagedModel(localExtensions), disposables: new DisposableStore(), message: { text: localize('showing local extensions only', "{0} Showing local extensions.", message.text), severity: message.severity } };774}775776throw error;777}778}779780private async getPreferredExtensions(searchText: string, token: CancellationToken): Promise<IExtension[]> {781const preferredExtensions = this.extensionsWorkbenchService.local.filter(e => !e.isBuiltin && (e.name.toLowerCase().indexOf(searchText) > -1 || e.displayName.toLowerCase().indexOf(searchText) > -1 || e.description.toLowerCase().indexOf(searchText) > -1));782const preferredExtensionUUIDs = new Set<string>();783784if (preferredExtensions.length) {785// Update gallery data for preferred extensions if they are not yet fetched786const extesionsToFetch: IExtensionIdentifier[] = [];787for (const extension of preferredExtensions) {788if (extension.identifier.uuid) {789preferredExtensionUUIDs.add(extension.identifier.uuid);790}791if (!extension.gallery && extension.identifier.uuid) {792extesionsToFetch.push(extension.identifier);793}794}795if (extesionsToFetch.length) {796this.extensionsWorkbenchService.getExtensions(extesionsToFetch, CancellationToken.None).catch(e => null/*ignore error*/);797}798}799800const preferredResults: string[] = [];801try {802const manifest = await this.extensionManagementService.getExtensionsControlManifest();803if (Array.isArray(manifest.search)) {804for (const s of manifest.search) {805if (s.query && s.query.toLowerCase() === searchText && Array.isArray(s.preferredResults)) {806preferredResults.push(...s.preferredResults);807break;808}809}810}811if (preferredResults.length) {812const result = await this.extensionsWorkbenchService.getExtensions(preferredResults.map(id => ({ id })), token);813for (const extension of result) {814if (extension.identifier.uuid && !preferredExtensionUUIDs.has(extension.identifier.uuid)) {815preferredExtensions.push(extension);816}817}818}819} catch (e) {820this.logService.warn('Failed to get preferred results from the extensions control manifest.', e);821}822823return preferredExtensions;824}825826private sortExtensions(extensions: IExtension[], options: IQueryOptions): IExtension[] {827switch (options.sortBy) {828case GallerySortBy.InstallCount:829extensions = extensions.sort((e1, e2) => typeof e2.installCount === 'number' && typeof e1.installCount === 'number' ? e2.installCount - e1.installCount : NaN);830break;831case LocalSortBy.UpdateDate:832extensions = extensions.sort((e1, e2) =>833typeof e2.local?.installedTimestamp === 'number' && typeof e1.local?.installedTimestamp === 'number' ? e2.local.installedTimestamp - e1.local.installedTimestamp :834typeof e2.local?.installedTimestamp === 'number' ? 1 :835typeof e1.local?.installedTimestamp === 'number' ? -1 : NaN);836break;837case GallerySortBy.AverageRating:838case GallerySortBy.WeightedRating:839extensions = extensions.sort((e1, e2) => typeof e2.rating === 'number' && typeof e1.rating === 'number' ? e2.rating - e1.rating : NaN);840break;841default:842extensions = extensions.sort((e1, e2) => e1.displayName.localeCompare(e2.displayName));843break;844}845if (options.sortOrder === SortOrder.Descending) {846extensions = extensions.reverse();847}848return extensions;849}850851private isRecommendationsQuery(query: Query): boolean {852return ExtensionsListView.isWorkspaceRecommendedExtensionsQuery(query.value)853|| ExtensionsListView.isKeymapsRecommendedExtensionsQuery(query.value)854|| ExtensionsListView.isLanguageRecommendedExtensionsQuery(query.value)855|| ExtensionsListView.isExeRecommendedExtensionsQuery(query.value)856|| ExtensionsListView.isRemoteRecommendedExtensionsQuery(query.value)857|| /@recommended:all/i.test(query.value)858|| ExtensionsListView.isSearchRecommendedExtensionsQuery(query.value)859|| ExtensionsListView.isRecommendedExtensionsQuery(query.value);860}861862private async queryRecommendations(query: Query, options: IQueryOptions, token: CancellationToken): Promise<IPagedModel<IExtension>> {863// Workspace recommendations864if (ExtensionsListView.isWorkspaceRecommendedExtensionsQuery(query.value)) {865return this.getWorkspaceRecommendationsModel(query, options, token);866}867868// Keymap recommendations869if (ExtensionsListView.isKeymapsRecommendedExtensionsQuery(query.value)) {870return this.getKeymapRecommendationsModel(query, options, token);871}872873// Language recommendations874if (ExtensionsListView.isLanguageRecommendedExtensionsQuery(query.value)) {875return this.getLanguageRecommendationsModel(query, options, token);876}877878// Exe recommendations879if (ExtensionsListView.isExeRecommendedExtensionsQuery(query.value)) {880return this.getExeRecommendationsModel(query, options, token);881}882883// Remote recommendations884if (ExtensionsListView.isRemoteRecommendedExtensionsQuery(query.value)) {885return this.getRemoteRecommendationsModel(query, options, token);886}887888// All recommendations889if (/@recommended:all/i.test(query.value)) {890return this.getAllRecommendationsModel(options, token);891}892893// Search recommendations894if (ExtensionsListView.isSearchRecommendedExtensionsQuery(query.value) ||895(ExtensionsListView.isRecommendedExtensionsQuery(query.value) && options.sortBy !== undefined)) {896return this.searchRecommendations(query, options, token);897}898899// Other recommendations900if (ExtensionsListView.isRecommendedExtensionsQuery(query.value)) {901return this.getOtherRecommendationsModel(query, options, token);902}903904return new PagedModel([]);905}906907protected async getInstallableRecommendations(recommendations: Array<string | URI>, options: IQueryOptions, token: CancellationToken): Promise<IExtension[]> {908const result: IExtension[] = [];909if (recommendations.length) {910const galleryExtensions: string[] = [];911const resourceExtensions: URI[] = [];912for (const recommendation of recommendations) {913if (typeof recommendation === 'string') {914galleryExtensions.push(recommendation);915} else {916resourceExtensions.push(recommendation);917}918}919if (galleryExtensions.length) {920try {921const extensions = await this.extensionsWorkbenchService.getExtensions(galleryExtensions.map(id => ({ id })), { source: options.source }, token);922for (const extension of extensions) {923if (extension.gallery && !extension.deprecationInfo924&& await this.extensionManagementService.canInstall(extension.gallery) === true) {925result.push(extension);926}927}928} catch (error) {929if (!resourceExtensions.length || !this.isOfflineError(error)) {930throw error;931}932}933}934if (resourceExtensions.length) {935const extensions = await this.extensionsWorkbenchService.getResourceExtensions(resourceExtensions, true);936for (const extension of extensions) {937if (await this.extensionsWorkbenchService.canInstall(extension) === true) {938result.push(extension);939}940}941}942}943return result;944}945946protected async getWorkspaceRecommendations(): Promise<Array<string | URI>> {947const recommendations = await this.extensionRecommendationsService.getWorkspaceRecommendations();948const { important } = await this.extensionRecommendationsService.getConfigBasedRecommendations();949for (const configBasedRecommendation of important) {950if (!recommendations.find(extensionId => extensionId === configBasedRecommendation)) {951recommendations.push(configBasedRecommendation);952}953}954return recommendations;955}956957private async getWorkspaceRecommendationsModel(query: Query, options: IQueryOptions, token: CancellationToken): Promise<IPagedModel<IExtension>> {958const recommendations = await this.getWorkspaceRecommendations();959const installableRecommendations = (await this.getInstallableRecommendations(recommendations, { ...options, source: 'recommendations-workspace' }, token));960return new PagedModel(installableRecommendations);961}962963private async getKeymapRecommendationsModel(query: Query, options: IQueryOptions, token: CancellationToken): Promise<IPagedModel<IExtension>> {964const value = query.value.replace(/@recommended:keymaps/g, '').trim().toLowerCase();965const recommendations = this.extensionRecommendationsService.getKeymapRecommendations();966const installableRecommendations = (await this.getInstallableRecommendations(recommendations, { ...options, source: 'recommendations-keymaps' }, token))967.filter(extension => extension.identifier.id.toLowerCase().indexOf(value) > -1);968return new PagedModel(installableRecommendations);969}970971private async getLanguageRecommendationsModel(query: Query, options: IQueryOptions, token: CancellationToken): Promise<IPagedModel<IExtension>> {972const value = query.value.replace(/@recommended:languages/g, '').trim().toLowerCase();973const recommendations = this.extensionRecommendationsService.getLanguageRecommendations();974const installableRecommendations = (await this.getInstallableRecommendations(recommendations, { ...options, source: 'recommendations-languages' }, token))975.filter(extension => extension.identifier.id.toLowerCase().indexOf(value) > -1);976return new PagedModel(installableRecommendations);977}978979private async getRemoteRecommendationsModel(query: Query, options: IQueryOptions, token: CancellationToken): Promise<IPagedModel<IExtension>> {980const value = query.value.replace(/@recommended:remotes/g, '').trim().toLowerCase();981const recommendations = this.extensionRecommendationsService.getRemoteRecommendations();982const installableRecommendations = (await this.getInstallableRecommendations(recommendations, { ...options, source: 'recommendations-remotes' }, token))983.filter(extension => extension.identifier.id.toLowerCase().indexOf(value) > -1);984return new PagedModel(installableRecommendations);985}986987private async getExeRecommendationsModel(query: Query, options: IQueryOptions, token: CancellationToken): Promise<IPagedModel<IExtension>> {988const exe = query.value.replace(/@exe:/g, '').trim().toLowerCase();989const { important, others } = await this.extensionRecommendationsService.getExeBasedRecommendations(exe.startsWith('"') ? exe.substring(1, exe.length - 1) : exe);990const installableRecommendations = await this.getInstallableRecommendations([...important, ...others], { ...options, source: 'recommendations-exe' }, token);991return new PagedModel(installableRecommendations);992}993994private async getOtherRecommendationsModel(query: Query, options: IQueryOptions, token: CancellationToken): Promise<IPagedModel<IExtension>> {995const otherRecommendations = await this.getOtherRecommendations();996const installableRecommendations = await this.getInstallableRecommendations(otherRecommendations, { ...options, source: 'recommendations-other', sortBy: undefined }, token);997const result = coalesce(otherRecommendations.map(id => installableRecommendations.find(i => areSameExtensions(i.identifier, { id }))));998return new PagedModel(result);999}10001001private async getOtherRecommendations(): Promise<string[]> {1002const local = (await this.extensionsWorkbenchService.queryLocal(this.options.server))1003.map(e => e.identifier.id.toLowerCase());1004const workspaceRecommendations = (await this.getWorkspaceRecommendations())1005.map(extensionId => isString(extensionId) ? extensionId.toLowerCase() : extensionId);10061007return distinct(1008(await Promise.all([1009// Order is important1010this.extensionRecommendationsService.getImportantRecommendations(),1011this.extensionRecommendationsService.getFileBasedRecommendations(),1012this.extensionRecommendationsService.getOtherRecommendations()1013])).flat().filter(extensionId => !local.includes(extensionId.toLowerCase()) && !workspaceRecommendations.includes(extensionId.toLowerCase())1014), extensionId => extensionId.toLowerCase());1015}10161017// Get All types of recommendations, trimmed to show a max of 8 at any given time1018private async getAllRecommendationsModel(options: IQueryOptions, token: CancellationToken): Promise<IPagedModel<IExtension>> {1019const localExtensions = await this.extensionsWorkbenchService.queryLocal(this.options.server);1020const localExtensionIds = localExtensions.map(e => e.identifier.id.toLowerCase());10211022const allRecommendations = distinct(1023(await Promise.all([1024// Order is important1025this.getWorkspaceRecommendations(),1026this.extensionRecommendationsService.getImportantRecommendations(),1027this.extensionRecommendationsService.getFileBasedRecommendations(),1028this.extensionRecommendationsService.getOtherRecommendations()1029])).flat().filter(extensionId => {1030if (isString(extensionId)) {1031return !localExtensionIds.includes(extensionId.toLowerCase());1032}1033return !localExtensions.some(localExtension => localExtension.local && this.uriIdentityService.extUri.isEqual(localExtension.local.location, extensionId));1034}));10351036const installableRecommendations = await this.getInstallableRecommendations(allRecommendations, { ...options, source: 'recommendations-all', sortBy: undefined }, token);10371038const result: IExtension[] = [];1039for (let i = 0; i < installableRecommendations.length && result.length < 8; i++) {1040const recommendation = allRecommendations[i];1041if (isString(recommendation)) {1042const extension = installableRecommendations.find(extension => areSameExtensions(extension.identifier, { id: recommendation }));1043if (extension) {1044result.push(extension);1045}1046} else {1047const extension = installableRecommendations.find(extension => extension.resourceExtension && this.uriIdentityService.extUri.isEqual(extension.resourceExtension.location, recommendation));1048if (extension) {1049result.push(extension);1050}1051}1052}10531054return new PagedModel(result);1055}10561057private async searchRecommendations(query: Query, options: IQueryOptions, token: CancellationToken): Promise<IPagedModel<IExtension>> {1058const value = query.value.replace(/@recommended/g, '').trim().toLowerCase();1059const recommendations = distinct([...await this.getWorkspaceRecommendations(), ...await this.getOtherRecommendations()]);1060const installableRecommendations = (await this.getInstallableRecommendations(recommendations, { ...options, source: 'recommendations', sortBy: undefined }, token))1061.filter(extension => extension.identifier.id.toLowerCase().indexOf(value) > -1);1062return new PagedModel(this.sortExtensions(installableRecommendations, options));1063}10641065private setModel(model: IPagedModel<IExtension>, message?: Message, donotResetScrollTop?: boolean) {1066if (this.list) {1067this.list.model = new DelayedPagedModel(model);1068this.updateBody(message);1069if (!donotResetScrollTop) {1070this.list.scrollTop = 0;1071}1072}1073if (this.badge) {1074this.badge.setCount(this.count());1075}1076}10771078private updateModel(model: IPagedModel<IExtension>) {1079if (this.list) {1080this.list.model = new DelayedPagedModel(model);1081this.updateBody();1082}1083if (this.badge) {1084this.badge.setCount(this.count());1085}1086}10871088private updateBody(message?: Message): void {1089if (this.bodyTemplate) {10901091const count = this.count();1092this.bodyTemplate.extensionsList.classList.toggle('hidden', count === 0);1093this.bodyTemplate.messageContainer.classList.toggle('hidden', !message && count > 0);10941095if (this.isBodyVisible()) {1096if (message) {1097this.bodyTemplate.messageSeverityIcon.className = SeverityIcon.className(message.severity);1098this.bodyTemplate.messageBox.textContent = message.text;1099} else if (this.count() === 0) {1100this.bodyTemplate.messageSeverityIcon.className = '';1101this.bodyTemplate.messageBox.textContent = localize('no extensions found', "No extensions found.");1102}1103if (this.bodyTemplate.messageBox.textContent) {1104alert(this.bodyTemplate.messageBox.textContent);1105}1106}1107}11081109this.updateSize();1110}11111112private getMessage(error: any): Message {1113if (this.isOfflineError(error)) {1114return { text: localize('offline error', "Unable to search the Marketplace when offline, please check your network connection."), severity: Severity.Warning };1115} else {1116return { text: localize('error', "Error while fetching extensions. {0}", getErrorMessage(error)), severity: Severity.Error };1117}1118}11191120private isOfflineError(error: Error): boolean {1121if (error instanceof ExtensionGalleryError) {1122return error.code === ExtensionGalleryErrorCode.Offline;1123}1124return isOfflineError(error);1125}11261127protected updateSize() {1128if (this.options.flexibleHeight) {1129this.maximumBodySize = this.list?.model.length ? Number.POSITIVE_INFINITY : 0;1130this.storageService.store(`${this.id}.size`, this.list?.model.length || 0, StorageScope.PROFILE, StorageTarget.MACHINE);1131}1132}11331134override dispose(): void {1135super.dispose();1136if (this.queryRequest) {1137this.queryRequest.request.cancel();1138this.queryRequest = null;1139}1140if (this.queryResult) {1141this.queryResult.disposables.dispose();1142this.queryResult = undefined;1143}1144this.list = null;1145}11461147static isLocalExtensionsQuery(query: string, sortBy?: string): boolean {1148return this.isInstalledExtensionsQuery(query)1149|| this.isSearchInstalledExtensionsQuery(query)1150|| this.isOutdatedExtensionsQuery(query)1151|| this.isEnabledExtensionsQuery(query)1152|| this.isDisabledExtensionsQuery(query)1153|| this.isBuiltInExtensionsQuery(query)1154|| this.isSearchBuiltInExtensionsQuery(query)1155|| this.isBuiltInGroupExtensionsQuery(query)1156|| this.isSearchDeprecatedExtensionsQuery(query)1157|| this.isSearchWorkspaceUnsupportedExtensionsQuery(query)1158|| this.isSearchRecentlyUpdatedQuery(query)1159|| this.isSearchExtensionUpdatesQuery(query)1160|| this.isSortInstalledExtensionsQuery(query, sortBy)1161|| this.isFeatureExtensionsQuery(query);1162}11631164static isSearchBuiltInExtensionsQuery(query: string): boolean {1165return /@builtin\s.+|.+\s@builtin/i.test(query);1166}11671168static isBuiltInExtensionsQuery(query: string): boolean {1169return /^@builtin$/i.test(query.trim());1170}11711172static isBuiltInGroupExtensionsQuery(query: string): boolean {1173return /^@builtin:.+$/i.test(query.trim());1174}11751176static isSearchWorkspaceUnsupportedExtensionsQuery(query: string): boolean {1177return /^\s*@workspaceUnsupported(:(untrusted|virtual)(Partial)?)?(\s|$)/i.test(query);1178}11791180static isInstalledExtensionsQuery(query: string): boolean {1181return /@installed$/i.test(query);1182}11831184static isSearchInstalledExtensionsQuery(query: string): boolean {1185return /@installed\s./i.test(query) || this.isFeatureExtensionsQuery(query);1186}11871188static isOutdatedExtensionsQuery(query: string): boolean {1189return /@outdated/i.test(query);1190}11911192static isEnabledExtensionsQuery(query: string): boolean {1193return /@enabled/i.test(query) && !/@builtin/i.test(query);1194}11951196static isDisabledExtensionsQuery(query: string): boolean {1197return /@disabled/i.test(query) && !/@builtin/i.test(query);1198}11991200static isSearchDeprecatedExtensionsQuery(query: string): boolean {1201return /@deprecated\s?.*/i.test(query);1202}12031204static isRecommendedExtensionsQuery(query: string): boolean {1205return /^@recommended$/i.test(query.trim());1206}12071208static isSearchRecommendedExtensionsQuery(query: string): boolean {1209return /@recommended\s.+/i.test(query);1210}12111212static isWorkspaceRecommendedExtensionsQuery(query: string): boolean {1213return /@recommended:workspace/i.test(query);1214}12151216static isExeRecommendedExtensionsQuery(query: string): boolean {1217return /@exe:.+/i.test(query);1218}12191220static isRemoteRecommendedExtensionsQuery(query: string): boolean {1221return /@recommended:remotes/i.test(query);1222}12231224static isKeymapsRecommendedExtensionsQuery(query: string): boolean {1225return /@recommended:keymaps/i.test(query);1226}12271228static isLanguageRecommendedExtensionsQuery(query: string): boolean {1229return /@recommended:languages/i.test(query);1230}12311232static isSortInstalledExtensionsQuery(query: string, sortBy?: string): boolean {1233return (sortBy !== undefined && sortBy !== '' && query === '') || (!sortBy && /^@sort:\S*$/i.test(query));1234}12351236static isSearchPopularQuery(query: string): boolean {1237return /@popular/i.test(query);1238}12391240static isSearchRecentlyPublishedQuery(query: string): boolean {1241return /@recentlyPublished/i.test(query);1242}12431244static isSearchRecentlyUpdatedQuery(query: string): boolean {1245return /@recentlyUpdated/i.test(query);1246}12471248static isSearchExtensionUpdatesQuery(query: string): boolean {1249return /@updates/i.test(query);1250}12511252static isSortUpdateDateQuery(query: string): boolean {1253return /@sort:updateDate/i.test(query);1254}12551256static isFeatureExtensionsQuery(query: string): boolean {1257return /@contribute:/i.test(query);1258}12591260override focus(): void {1261super.focus();1262if (!this.list) {1263return;1264}12651266if (!(this.list.getFocus().length || this.list.getSelection().length)) {1267this.list.focusNext();1268}1269this.list.domFocus();1270}1271}12721273export class DefaultPopularExtensionsView extends ExtensionsListView {12741275override async show(): Promise<IPagedModel<IExtension>> {1276const query = this.extensionManagementServerService.webExtensionManagementServer && !this.extensionManagementServerService.localExtensionManagementServer && !this.extensionManagementServerService.remoteExtensionManagementServer ? '@web' : '';1277return super.show(query);1278}12791280}12811282export class ServerInstalledExtensionsView extends ExtensionsListView {12831284override async show(query: string): Promise<IPagedModel<IExtension>> {1285query = query ? query : '@installed';1286if (!ExtensionsListView.isLocalExtensionsQuery(query) || ExtensionsListView.isSortInstalledExtensionsQuery(query)) {1287query = query += ' @installed';1288}1289return super.show(query.trim());1290}12911292}12931294export class EnabledExtensionsView extends ExtensionsListView {12951296override async show(query: string): Promise<IPagedModel<IExtension>> {1297query = query || '@enabled';1298return ExtensionsListView.isEnabledExtensionsQuery(query) ? super.show(query) :1299ExtensionsListView.isSortInstalledExtensionsQuery(query) ? super.show('@enabled ' + query) : this.showEmptyModel();1300}1301}13021303export class DisabledExtensionsView extends ExtensionsListView {13041305override async show(query: string): Promise<IPagedModel<IExtension>> {1306query = query || '@disabled';1307return ExtensionsListView.isDisabledExtensionsQuery(query) ? super.show(query) :1308ExtensionsListView.isSortInstalledExtensionsQuery(query) ? super.show('@disabled ' + query) : this.showEmptyModel();1309}1310}13111312export class OutdatedExtensionsView extends ExtensionsListView {13131314override async show(query: string): Promise<IPagedModel<IExtension>> {1315query = query ? query : '@outdated';1316if (ExtensionsListView.isSearchExtensionUpdatesQuery(query)) {1317query = query.replace('@updates', '@outdated');1318}1319return super.show(query.trim());1320}13211322protected override updateSize() {1323super.updateSize();1324this.setExpanded(this.count() > 0);1325}13261327}13281329export class RecentlyUpdatedExtensionsView extends ExtensionsListView {13301331override async show(query: string): Promise<IPagedModel<IExtension>> {1332query = query ? query : '@recentlyUpdated';1333if (ExtensionsListView.isSearchExtensionUpdatesQuery(query)) {1334query = query.replace('@updates', '@recentlyUpdated');1335}1336return super.show(query.trim());1337}13381339}13401341export interface StaticQueryExtensionsViewOptions extends ExtensionsListViewOptions {1342readonly query: string;1343}13441345export class StaticQueryExtensionsView extends ExtensionsListView {13461347constructor(1348protected override readonly options: StaticQueryExtensionsViewOptions,1349viewletViewOptions: IViewletViewOptions,1350@INotificationService notificationService: INotificationService,1351@IKeybindingService keybindingService: IKeybindingService,1352@IContextMenuService contextMenuService: IContextMenuService,1353@IInstantiationService instantiationService: IInstantiationService,1354@IThemeService themeService: IThemeService,1355@IExtensionService extensionService: IExtensionService,1356@IExtensionsWorkbenchService extensionsWorkbenchService: IExtensionsWorkbenchService,1357@IExtensionRecommendationsService extensionRecommendationsService: IExtensionRecommendationsService,1358@ITelemetryService telemetryService: ITelemetryService,1359@IHoverService hoverService: IHoverService,1360@IConfigurationService configurationService: IConfigurationService,1361@IWorkspaceContextService contextService: IWorkspaceContextService,1362@IExtensionManagementServerService extensionManagementServerService: IExtensionManagementServerService,1363@IExtensionManifestPropertiesService extensionManifestPropertiesService: IExtensionManifestPropertiesService,1364@IWorkbenchExtensionManagementService extensionManagementService: IWorkbenchExtensionManagementService,1365@IWorkspaceContextService workspaceService: IWorkspaceContextService,1366@IProductService productService: IProductService,1367@IContextKeyService contextKeyService: IContextKeyService,1368@IViewDescriptorService viewDescriptorService: IViewDescriptorService,1369@IOpenerService openerService: IOpenerService,1370@IStorageService storageService: IStorageService,1371@IWorkspaceTrustManagementService workspaceTrustManagementService: IWorkspaceTrustManagementService,1372@IWorkbenchExtensionEnablementService extensionEnablementService: IWorkbenchExtensionEnablementService,1373@IExtensionFeaturesManagementService extensionFeaturesManagementService: IExtensionFeaturesManagementService,1374@IUriIdentityService uriIdentityService: IUriIdentityService,1375@ILogService logService: ILogService1376) {1377super(options, viewletViewOptions, notificationService, keybindingService, contextMenuService, instantiationService, themeService, extensionService,1378extensionsWorkbenchService, extensionRecommendationsService, telemetryService, hoverService, configurationService, contextService, extensionManagementServerService,1379extensionManifestPropertiesService, extensionManagementService, workspaceService, productService, contextKeyService, viewDescriptorService, openerService,1380storageService, workspaceTrustManagementService, extensionEnablementService, extensionFeaturesManagementService,1381uriIdentityService, logService);1382}13831384override show(): Promise<IPagedModel<IExtension>> {1385return super.show(this.options.query);1386}1387}13881389function toSpecificWorkspaceUnsupportedQuery(query: string, qualifier: string): string | undefined {1390if (!query) {1391return '@workspaceUnsupported:' + qualifier;1392}1393const match = query.match(new RegExp(`@workspaceUnsupported(:${qualifier})?(\\s|$)`, 'i'));1394if (match) {1395if (!match[1]) {1396return query.replace(/@workspaceUnsupported/gi, '@workspaceUnsupported:' + qualifier);1397}1398return query;1399}1400return undefined;1401}140214031404export class UntrustedWorkspaceUnsupportedExtensionsView extends ExtensionsListView {1405override async show(query: string): Promise<IPagedModel<IExtension>> {1406const updatedQuery = toSpecificWorkspaceUnsupportedQuery(query, 'untrusted');1407return updatedQuery ? super.show(updatedQuery) : this.showEmptyModel();1408}1409}14101411export class UntrustedWorkspacePartiallySupportedExtensionsView extends ExtensionsListView {1412override async show(query: string): Promise<IPagedModel<IExtension>> {1413const updatedQuery = toSpecificWorkspaceUnsupportedQuery(query, 'untrustedPartial');1414return updatedQuery ? super.show(updatedQuery) : this.showEmptyModel();1415}1416}14171418export class VirtualWorkspaceUnsupportedExtensionsView extends ExtensionsListView {1419override async show(query: string): Promise<IPagedModel<IExtension>> {1420const updatedQuery = toSpecificWorkspaceUnsupportedQuery(query, 'virtual');1421return updatedQuery ? super.show(updatedQuery) : this.showEmptyModel();1422}1423}14241425export class VirtualWorkspacePartiallySupportedExtensionsView extends ExtensionsListView {1426override async show(query: string): Promise<IPagedModel<IExtension>> {1427const updatedQuery = toSpecificWorkspaceUnsupportedQuery(query, 'virtualPartial');1428return updatedQuery ? super.show(updatedQuery) : this.showEmptyModel();1429}1430}14311432export class DeprecatedExtensionsView extends ExtensionsListView {1433override async show(query: string): Promise<IPagedModel<IExtension>> {1434return ExtensionsListView.isSearchDeprecatedExtensionsQuery(query) ? super.show(query) : this.showEmptyModel();1435}1436}14371438export class SearchMarketplaceExtensionsView extends ExtensionsListView {14391440private readonly reportSearchFinishedDelayer = this._register(new ThrottledDelayer(2000));1441private searchWaitPromise: Promise<void> = Promise.resolve();14421443override async show(query: string): Promise<IPagedModel<IExtension>> {1444const queryPromise = super.show(query);1445this.reportSearchFinishedDelayer.trigger(() => this.reportSearchFinished());1446this.searchWaitPromise = queryPromise.then(null, null);1447return queryPromise;1448}14491450private async reportSearchFinished(): Promise<void> {1451await this.searchWaitPromise;1452this.telemetryService.publicLog2('extensionsView:MarketplaceSearchFinished');1453}1454}14551456export class DefaultRecommendedExtensionsView extends ExtensionsListView {1457private readonly recommendedExtensionsQuery = '@recommended:all';14581459protected override renderBody(container: HTMLElement): void {1460super.renderBody(container);14611462this._register(this.extensionRecommendationsService.onDidChangeRecommendations(() => {1463this.show('');1464}));1465}14661467override async show(query: string): Promise<IPagedModel<IExtension>> {1468if (query && query.trim() !== this.recommendedExtensionsQuery) {1469return this.showEmptyModel();1470}1471const model = await super.show(this.recommendedExtensionsQuery);1472if (!this.extensionsWorkbenchService.local.some(e => !e.isBuiltin)) {1473// This is part of popular extensions view. Collapse if no installed extensions.1474this.setExpanded(model.length > 0);1475}1476return model;1477}14781479}14801481export class RecommendedExtensionsView extends ExtensionsListView {1482private readonly recommendedExtensionsQuery = '@recommended';14831484protected override renderBody(container: HTMLElement): void {1485super.renderBody(container);14861487this._register(this.extensionRecommendationsService.onDidChangeRecommendations(() => {1488this.show('');1489}));1490}14911492override async show(query: string): Promise<IPagedModel<IExtension>> {1493return (query && query.trim() !== this.recommendedExtensionsQuery) ? this.showEmptyModel() : super.show(this.recommendedExtensionsQuery);1494}1495}14961497export class WorkspaceRecommendedExtensionsView extends ExtensionsListView implements IWorkspaceRecommendedExtensionsView {1498private readonly recommendedExtensionsQuery = '@recommended:workspace';14991500protected override renderBody(container: HTMLElement): void {1501super.renderBody(container);15021503this._register(this.extensionRecommendationsService.onDidChangeRecommendations(() => this.show(this.recommendedExtensionsQuery)));1504this._register(this.contextService.onDidChangeWorkbenchState(() => this.show(this.recommendedExtensionsQuery)));1505}15061507override async show(query: string): Promise<IPagedModel<IExtension>> {1508const shouldShowEmptyView = query && query.trim() !== '@recommended' && query.trim() !== '@recommended:workspace';1509const model = await (shouldShowEmptyView ? this.showEmptyModel() : super.show(this.recommendedExtensionsQuery));1510this.setExpanded(model.length > 0);1511return model;1512}15131514private async getInstallableWorkspaceRecommendations(): Promise<IExtension[]> {1515const installed = (await this.extensionsWorkbenchService.queryLocal())1516.filter(l => l.enablementState !== EnablementState.DisabledByExtensionKind); // Filter extensions disabled by kind1517const recommendations = (await this.getWorkspaceRecommendations())1518.filter(recommendation => installed.every(local => isString(recommendation) ? !areSameExtensions({ id: recommendation }, local.identifier) : !this.uriIdentityService.extUri.isEqual(recommendation, local.local?.location)));1519return this.getInstallableRecommendations(recommendations, { source: 'install-all-workspace-recommendations' }, CancellationToken.None);1520}15211522async installWorkspaceRecommendations(): Promise<void> {1523const installableRecommendations = await this.getInstallableWorkspaceRecommendations();1524if (installableRecommendations.length) {1525const galleryExtensions: InstallExtensionInfo[] = [];1526const resourceExtensions: IExtension[] = [];1527for (const recommendation of installableRecommendations) {1528if (recommendation.gallery) {1529galleryExtensions.push({ extension: recommendation.gallery, options: {} });1530} else {1531resourceExtensions.push(recommendation);1532}1533}1534await Promise.all([1535this.extensionManagementService.installGalleryExtensions(galleryExtensions),1536...resourceExtensions.map(extension => this.extensionsWorkbenchService.install(extension))1537]);1538} else {1539this.notificationService.notify({1540severity: Severity.Info,1541message: localize('no local extensions', "There are no extensions to install.")1542});1543}1544}15451546}15471548export class PreferredExtensionsPagedModel implements IPagedModel<IExtension> {15491550private readonly resolved = new Map<number, IExtension>();1551private preferredGalleryExtensions = new Set<string>();1552private resolvedGalleryExtensionsFromQuery: IExtension[] = [];1553private readonly pages: Array<{1554promise: Promise<void> | null;1555cts: CancellationTokenSource | null;1556promiseIndexes: Set<number>;1557}>;15581559public readonly length: number;15601561get onDidIncrementLength(): Event<number> {1562return Event.None;1563}15641565constructor(1566private readonly preferredExtensions: IExtension[],1567private readonly pager: IPager<IExtension>,1568) {1569for (let i = 0; i < this.preferredExtensions.length; i++) {1570this.resolved.set(i, this.preferredExtensions[i]);1571}15721573for (const e of preferredExtensions) {1574if (e.identifier.uuid) {1575this.preferredGalleryExtensions.add(e.identifier.uuid);1576}1577}15781579// expected that all preferred gallery extensions will be part of the query results1580this.length = (preferredExtensions.length - this.preferredGalleryExtensions.size) + this.pager.total;15811582const totalPages = Math.ceil(this.pager.total / this.pager.pageSize);1583this.populateResolvedExtensions(0, this.pager.firstPage);1584this.pages = range(totalPages - 1).map(() => ({1585promise: null,1586cts: null,1587promiseIndexes: new Set<number>(),1588}));1589}15901591isResolved(index: number): boolean {1592return this.resolved.has(index);1593}15941595get(index: number): IExtension {1596return this.resolved.get(index)!;1597}15981599async resolve(index: number, cancellationToken: CancellationToken): Promise<IExtension> {1600if (cancellationToken.isCancellationRequested) {1601throw new CancellationError();1602}16031604if (this.isResolved(index)) {1605return this.get(index);1606}16071608const indexInPagedModel = index - this.preferredExtensions.length + this.resolvedGalleryExtensionsFromQuery.length;1609const pageIndex = Math.floor(indexInPagedModel / this.pager.pageSize);1610const page = this.pages[pageIndex];16111612if (!page.promise) {1613page.cts = new CancellationTokenSource();1614page.promise = this.pager.getPage(pageIndex, page.cts.token)1615.then(extensions => this.populateResolvedExtensions(pageIndex, extensions))1616.catch(e => { page.promise = null; throw e; })1617.finally(() => page.cts = null);1618}16191620const listener = cancellationToken.onCancellationRequested(() => {1621if (!page.cts) {1622return;1623}1624page.promiseIndexes.delete(index);1625if (page.promiseIndexes.size === 0) {1626page.cts.cancel();1627}1628});16291630page.promiseIndexes.add(index);16311632try {1633await page.promise;1634} finally {1635listener.dispose();1636}16371638return this.get(index);1639}16401641private populateResolvedExtensions(pageIndex: number, extensions: IExtension[]): void {1642let adjustIndexOfNextPagesBy = 0;1643const pageStartIndex = pageIndex * this.pager.pageSize;1644for (let i = 0; i < extensions.length; i++) {1645const e = extensions[i];1646if (e.gallery?.identifier.uuid && this.preferredGalleryExtensions.has(e.gallery.identifier.uuid)) {1647this.resolvedGalleryExtensionsFromQuery.push(e);1648adjustIndexOfNextPagesBy++;1649} else {1650this.resolved.set(this.preferredExtensions.length - this.resolvedGalleryExtensionsFromQuery.length + pageStartIndex + i, e);1651}1652}1653// If this page has preferred gallery extensions, then adjust the index of the next pages1654// by the number of preferred gallery extensions found in this page. Because these preferred extensions1655// are already in the resolved list and since we did not add them now, we need to adjust the indices of the next pages.1656// Skip first page as the preferred extensions are always in the first page1657if (pageIndex !== 0 && adjustIndexOfNextPagesBy) {1658const nextPageStartIndex = (pageIndex + 1) * this.pager.pageSize;1659const indices = [...this.resolved.keys()].sort();1660for (const index of indices) {1661if (index >= nextPageStartIndex) {1662const e = this.resolved.get(index);1663if (e) {1664this.resolved.delete(index);1665this.resolved.set(index - adjustIndexOfNextPagesBy, e);1666}1667}1668}1669}1670}1671}167216731674