Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make preference node renderers more robust against null values #11074

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class PreferenceArrayInputRenderer extends PreferenceLeafNodeRenderer<str

protected doHandleValueChange(): void {
this.updateInspection();
const values = this.getValue();
const values = this.getValue() ?? [];
const newValues = new Set(...values);
for (const [value, row] of this.existingValues.entries()) {
if (!newValues.has(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class PreferenceBooleanInputRenderer extends PreferenceLeafNodeRenderer<b
this.interactable = interactable;
interactable.type = 'checkbox';
interactable.classList.add('theia-input');
interactable.defaultChecked = this.getValue();
interactable.defaultChecked = Boolean(this.getValue());
interactable.onchange = this.handleUserInteraction.bind(this);
parent.appendChild(interactable);
}
Expand All @@ -46,7 +46,7 @@ export class PreferenceBooleanInputRenderer extends PreferenceLeafNodeRenderer<b
protected doHandleValueChange(): void {
const currentValue = this.interactable.checked;
this.updateInspection();
const newValue = this.getValue();
const newValue = Boolean(this.getValue());
this.updateModificationStatus(newValue);
if (newValue !== currentValue && document.activeElement !== this.interactable) {
this.interactable.checked = newValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ export abstract class PreferenceLeafNodeRenderer<ValueType extends JSONValue, In
return modifiedScopes;
}

protected getValue(): ValueType {
// Many preferences allow `null` and even use it as a default regardless of the declared type.
protected getValue(): ValueType | null {
let currentValue = Preference.getValueInScope(this.inspection, this.scopeTracker.currentScope.scope);
if (currentValue === undefined) {
currentValue = this.inspection?.defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class PreferenceNumberInputRenderer extends PreferenceLeafNodeRenderer<nu
this.interactable = interactable;
interactable.type = 'number';
interactable.classList.add('theia-input');
interactable.defaultValue = this.getValue().toString();
interactable.defaultValue = this.getValue()?.toString() ?? '';
interactable.oninput = this.handleUserInteraction.bind(this);
interactable.onblur = this.handleBlur.bind(this);
interactableWrapper.appendChild(interactable);
Expand Down Expand Up @@ -84,7 +84,7 @@ export class PreferenceNumberInputRenderer extends PreferenceLeafNodeRenderer<nu
const { value } = this.interactable;
const currentValue = value.length ? Number(value) : NaN;
this.updateInspection();
const newValue = this.getValue();
const newValue = this.getValue() ?? '';
this.updateModificationStatus(newValue);
if (newValue !== currentValue) {
if (document.activeElement !== this.interactable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class PreferenceStringInputRenderer extends PreferenceLeafNodeRenderer<st
interactable.type = 'text';
interactable.spellcheck = false;
interactable.classList.add('theia-input');
interactable.defaultValue = this.getValue();
interactable.defaultValue = this.getValue() ?? '';
interactable.oninput = this.handleUserInteraction.bind(this);
interactable.onblur = this.handleBlur.bind(this);
parent.appendChild(interactable);
Expand All @@ -40,7 +40,7 @@ export class PreferenceStringInputRenderer extends PreferenceLeafNodeRenderer<st
protected doHandleValueChange(): void {
const currentValue = this.interactable.value;
this.updateInspection();
const newValue = this.getValue();
const newValue = this.getValue() ?? '';
this.updateModificationStatus(newValue);
if (newValue !== currentValue) {
if (document.activeElement !== this.interactable) {
Expand Down