From 910aceb48ea5d6e4df8a604d9d627c7442195ea6 Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Mon, 14 Aug 2023 08:16:57 -0700 Subject: [PATCH] Make Settings inspect lazier (#190100) --- .../preferences/browser/settingsTree.ts | 5 + .../preferences/browser/settingsTreeModels.ts | 183 ++++++++++-------- 2 files changed, 109 insertions(+), 79 deletions(-) diff --git a/src/vs/workbench/contrib/preferences/browser/settingsTree.ts b/src/vs/workbench/contrib/preferences/browser/settingsTree.ts index f5e9dddd5af92..e370cf8382a30 100644 --- a/src/vs/workbench/contrib/preferences/browser/settingsTree.ts +++ b/src/vs/workbench/contrib/preferences/browser/settingsTree.ts @@ -874,6 +874,11 @@ export abstract class AbstractSettingRenderer extends Disposable implements ITre protected renderSettingElement(node: ITreeNode, index: number, template: ISettingItemTemplate | ISettingBoolItemTemplate): void { const element = node.element; + + // The element must inspect itself to get information for + // the modified indicator and the overridden Settings indicators. + element.inspectSelf(); + template.context = element; template.toolbar.context = element; const actions = this.disposableActionFactory(element.setting, element.settingsTarget); diff --git a/src/vs/workbench/contrib/preferences/browser/settingsTreeModels.ts b/src/vs/workbench/contrib/preferences/browser/settingsTreeModels.ts index 95142f4dbef38..c5278298cc5b0 100644 --- a/src/vs/workbench/contrib/preferences/browser/settingsTreeModels.ts +++ b/src/vs/workbench/contrib/preferences/browser/settingsTreeModels.ts @@ -167,17 +167,21 @@ export class SettingsTreeSettingElement extends SettingsTreeElement { constructor( setting: ISetting, parent: SettingsTreeGroupElement, - inspectResult: IInspectResult, - isWorkspaceTrusted: boolean, readonly settingsTarget: SettingsTarget, + private readonly isWorkspaceTrusted: boolean, + private readonly languageFilter: string | undefined, private readonly languageService: ILanguageService, - private readonly productService: IProductService + private readonly productService: IProductService, + private readonly userDataProfileService: IUserDataProfileService, + private readonly configurationService: IWorkbenchConfigurationService, ) { super(sanitizeId(parent.id + '_' + setting.key)); this.setting = setting; this.parent = parent; - this.update(inspectResult, isWorkspaceTrusted); + // Make sure description and valueType are initialized + this.initSettingDescription(); + this.initSettingValueType(); } get displayCategory(): string { @@ -207,7 +211,80 @@ export class SettingsTreeSettingElement extends SettingsTreeElement { this._displayCategory = displayKeyFormat.category; } - update(inspectResult: IInspectResult, isWorkspaceTrusted: boolean): void { + private initSettingDescription() { + if (this.setting.description.length > SettingsTreeSettingElement.MAX_DESC_LINES) { + const truncatedDescLines = this.setting.description.slice(0, SettingsTreeSettingElement.MAX_DESC_LINES); + truncatedDescLines.push('[...]'); + this.description = truncatedDescLines.join('\n'); + } else { + this.description = this.setting.description.join('\n'); + } + } + + private initSettingValueType() { + if (isExtensionToggleSetting(this.setting, this.productService)) { + this.valueType = SettingValueType.ExtensionToggle; + } else if (this.setting.enum && (!this.setting.type || settingTypeEnumRenderable(this.setting.type))) { + this.valueType = SettingValueType.Enum; + } else if (this.setting.type === 'string') { + if (this.setting.editPresentation === EditPresentationTypes.Multiline) { + this.valueType = SettingValueType.MultilineString; + } else { + this.valueType = SettingValueType.String; + } + } else if (isExcludeSetting(this.setting)) { + this.valueType = SettingValueType.Exclude; + } else if (isIncludeSetting(this.setting)) { + this.valueType = SettingValueType.Include; + } else if (this.setting.type === 'integer') { + this.valueType = SettingValueType.Integer; + } else if (this.setting.type === 'number') { + this.valueType = SettingValueType.Number; + } else if (this.setting.type === 'boolean') { + this.valueType = SettingValueType.Boolean; + } else if (this.setting.type === 'array' && this.setting.arrayItemType && + ['string', 'enum', 'number', 'integer'].includes(this.setting.arrayItemType)) { + this.valueType = SettingValueType.Array; + } else if (Array.isArray(this.setting.type) && this.setting.type.includes(SettingValueType.Null) && this.setting.type.length === 2) { + if (this.setting.type.includes(SettingValueType.Integer)) { + this.valueType = SettingValueType.NullableInteger; + } else if (this.setting.type.includes(SettingValueType.Number)) { + this.valueType = SettingValueType.NullableNumber; + } else { + this.valueType = SettingValueType.Complex; + } + } else if (isObjectSetting(this.setting)) { + if (this.setting.allKeysAreBoolean) { + this.valueType = SettingValueType.BooleanObject; + } else { + this.valueType = SettingValueType.Object; + } + } else if (this.setting.isLanguageTagSetting) { + this.valueType = SettingValueType.LanguageTag; + } else { + this.valueType = SettingValueType.Complex; + } + } + + inspectSelf() { + const targetToInspect = this.getTargetToInspect(this.setting); + const inspectResult = inspectSetting(this.setting.key, targetToInspect, this.languageFilter, this.configurationService); + this.update(inspectResult, this.isWorkspaceTrusted); + } + + private getTargetToInspect(setting: ISetting): SettingsTarget { + if (!this.userDataProfileService.currentProfile.isDefault) { + if (setting.scope === ConfigurationScope.APPLICATION) { + return ConfigurationTarget.APPLICATION; + } + if (this.configurationService.isSettingAppliedForAllProfiles(setting.key) && this.settingsTarget === ConfigurationTarget.USER_LOCAL) { + return ConfigurationTarget.APPLICATION; + } + } + return this.settingsTarget; + } + + private update(inspectResult: IInspectResult, isWorkspaceTrusted: boolean): void { let { isConfigured, inspected, targetSelector, inspectedLanguageOverrides, languageSelector } = inspectResult; switch (targetSelector) { @@ -302,57 +379,6 @@ export class SettingsTreeSettingElement extends SettingsTreeElement { this.tags.add(POLICY_SETTING_TAG); } } - - if (this.setting.description.length > SettingsTreeSettingElement.MAX_DESC_LINES) { - const truncatedDescLines = this.setting.description.slice(0, SettingsTreeSettingElement.MAX_DESC_LINES); - truncatedDescLines.push('[...]'); - this.description = truncatedDescLines.join('\n'); - } else { - this.description = this.setting.description.join('\n'); - } - - if (isExtensionToggleSetting(this.setting, this.productService)) { - this.valueType = SettingValueType.ExtensionToggle; - } else if (this.setting.enum && (!this.setting.type || settingTypeEnumRenderable(this.setting.type))) { - this.valueType = SettingValueType.Enum; - } else if (this.setting.type === 'string') { - if (this.setting.editPresentation === EditPresentationTypes.Multiline) { - this.valueType = SettingValueType.MultilineString; - } else { - this.valueType = SettingValueType.String; - } - } else if (isExcludeSetting(this.setting)) { - this.valueType = SettingValueType.Exclude; - } else if (isIncludeSetting(this.setting)) { - this.valueType = SettingValueType.Include; - } else if (this.setting.type === 'integer') { - this.valueType = SettingValueType.Integer; - } else if (this.setting.type === 'number') { - this.valueType = SettingValueType.Number; - } else if (this.setting.type === 'boolean') { - this.valueType = SettingValueType.Boolean; - } else if (this.setting.type === 'array' && this.setting.arrayItemType && - ['string', 'enum', 'number', 'integer'].includes(this.setting.arrayItemType)) { - this.valueType = SettingValueType.Array; - } else if (Array.isArray(this.setting.type) && this.setting.type.includes(SettingValueType.Null) && this.setting.type.length === 2) { - if (this.setting.type.includes(SettingValueType.Integer)) { - this.valueType = SettingValueType.NullableInteger; - } else if (this.setting.type.includes(SettingValueType.Number)) { - this.valueType = SettingValueType.NullableNumber; - } else { - this.valueType = SettingValueType.Complex; - } - } else if (isObjectSetting(this.setting)) { - if (this.setting.allKeysAreBoolean) { - this.valueType = SettingValueType.BooleanObject; - } else { - this.valueType = SettingValueType.Object; - } - } else if (this.setting.isLanguageTagSetting) { - this.valueType = SettingValueType.LanguageTag; - } else { - this.valueType = SettingValueType.Complex; - } } matchesAllTags(tagFilters?: Set): boolean { @@ -360,6 +386,12 @@ export class SettingsTreeSettingElement extends SettingsTreeElement { return true; } + if (!this.tags) { + // The setting must inspect itself to get tag information + // including for the hasPolicy tag. + this.inspectSelf(); + } + if (this.tags) { let hasFilteredTag = true; tagFilters.forEach(tag => { @@ -477,11 +509,11 @@ function createSettingMatchRegExp(pattern: string): RegExp { export class SettingsTreeModel { protected _root!: SettingsTreeGroupElement; - private _treeElementsBySettingName = new Map(); private _tocRoot!: ITOCEntry; + private readonly _treeElementsBySettingName = new Map(); constructor( - protected _viewState: ISettingsEditorViewState, + protected readonly _viewState: ISettingsEditorViewState, private _isWorkspaceTrusted: boolean, @IWorkbenchConfigurationService private readonly _configurationService: IWorkbenchConfigurationService, @ILanguageService private readonly _languageService: ILanguageService, @@ -538,30 +570,16 @@ export class SettingsTreeModel { return; } - this.updateSettings(this._treeElementsBySettingName.get(name)!); + this.reinspectSettings(this._treeElementsBySettingName.get(name)!); } private updateRequireTrustedTargetElements(): void { - this.updateSettings([...this._treeElementsBySettingName.values()].flat().filter(s => s.isUntrusted)); - } - - private getTargetToInspect(setting: ISetting): SettingsTarget { - if (!this._userDataProfileService.currentProfile.isDefault) { - if (setting.scope === ConfigurationScope.APPLICATION) { - return ConfigurationTarget.APPLICATION; - } - if (this._configurationService.isSettingAppliedForAllProfiles(setting.key) && this._viewState.settingsTarget === ConfigurationTarget.USER_LOCAL) { - return ConfigurationTarget.APPLICATION; - } - } - return this._viewState.settingsTarget; + this.reinspectSettings([...this._treeElementsBySettingName.values()].flat().filter(s => s.isUntrusted)); } - private updateSettings(settings: SettingsTreeSettingElement[]): void { + private reinspectSettings(settings: SettingsTreeSettingElement[]): void { for (const element of settings) { - const target = this.getTargetToInspect(element.setting); - const inspectResult = inspectSetting(element.setting.key, target, this._viewState.languageFilter, this._configurationService); - element.update(inspectResult, this._isWorkspaceTrusted); + element.inspectSelf(); } } @@ -596,9 +614,16 @@ export class SettingsTreeModel { } private createSettingsTreeSettingElement(setting: ISetting, parent: SettingsTreeGroupElement): SettingsTreeSettingElement { - const target = this.getTargetToInspect(setting); - const inspectResult = inspectSetting(setting.key, target, this._viewState.languageFilter, this._configurationService); - const element = new SettingsTreeSettingElement(setting, parent, inspectResult, this._isWorkspaceTrusted, this._viewState.settingsTarget, this._languageService, this._productService); + const element = new SettingsTreeSettingElement( + setting, + parent, + this._viewState.settingsTarget, + this._isWorkspaceTrusted, + this._viewState.languageFilter, + this._languageService, + this._productService, + this._userDataProfileService, + this._configurationService); const nameElements = this._treeElementsBySettingName.get(setting.key) || []; nameElements.push(element);