From bc46b93326cbe84f425b1555b23580470f98e941 Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Wed, 9 Aug 2023 11:37:23 -0700 Subject: [PATCH 1/2] Make Settings inspect lazier --- .../preferences/browser/settingsTree.ts | 2 + .../preferences/browser/settingsTreeModels.ts | 181 ++++++++++-------- 2 files changed, 104 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..957a1f13c7380 100644 --- a/src/vs/workbench/contrib/preferences/browser/settingsTree.ts +++ b/src/vs/workbench/contrib/preferences/browser/settingsTree.ts @@ -874,6 +874,8 @@ export abstract class AbstractSettingRenderer extends Disposable implements ITre protected renderSettingElement(node: ITreeNode, index: number, template: ISettingItemTemplate | ISettingBoolItemTemplate): void { const element = node.element; + 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 e55eba8fa7754..09f0aac117194 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,10 @@ export class SettingsTreeSettingElement extends SettingsTreeElement { return true; } + if (!this.tags) { + this.inspectSelf(); + } + if (this.tags) { let hasFilteredTag = true; tagFilters.forEach(tag => { @@ -477,11 +507,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 +568,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 +612,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); From 66826df79935eeacba595818957e7546a1df788c Mon Sep 17 00:00:00 2001 From: Raymond Zhao <7199958+rzhao271@users.noreply.github.com> Date: Wed, 9 Aug 2023 14:42:20 -0700 Subject: [PATCH 2/2] Add explainer comments --- src/vs/workbench/contrib/preferences/browser/settingsTree.ts | 3 +++ .../contrib/preferences/browser/settingsTreeModels.ts | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/vs/workbench/contrib/preferences/browser/settingsTree.ts b/src/vs/workbench/contrib/preferences/browser/settingsTree.ts index 957a1f13c7380..e370cf8382a30 100644 --- a/src/vs/workbench/contrib/preferences/browser/settingsTree.ts +++ b/src/vs/workbench/contrib/preferences/browser/settingsTree.ts @@ -874,6 +874,9 @@ 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; diff --git a/src/vs/workbench/contrib/preferences/browser/settingsTreeModels.ts b/src/vs/workbench/contrib/preferences/browser/settingsTreeModels.ts index 09f0aac117194..8ec063327746c 100644 --- a/src/vs/workbench/contrib/preferences/browser/settingsTreeModels.ts +++ b/src/vs/workbench/contrib/preferences/browser/settingsTreeModels.ts @@ -387,6 +387,8 @@ export class SettingsTreeSettingElement extends SettingsTreeElement { } if (!this.tags) { + // The setting must inspect itself to get tag information + // including for the hasPolicy tag. this.inspectSelf(); }