From da460dc73cecdae4068a064922e42cb7742c1a43 Mon Sep 17 00:00:00 2001 From: IllusionMH Date: Tue, 1 Sep 2020 22:52:50 +0300 Subject: [PATCH 1/2] Allow 1-1000 as strings. Improve description and error. --- src/vs/editor/common/config/editorOptions.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/vs/editor/common/config/editorOptions.ts b/src/vs/editor/common/config/editorOptions.ts index 63e389c45ccb8..3cbfe5bbe7928 100644 --- a/src/vs/editor/common/config/editorOptions.ts +++ b/src/vs/editor/common/config/editorOptions.ts @@ -1490,7 +1490,7 @@ class EditorFontSize extends SimpleEditorOption { //#region fontWeight class EditorFontWeight extends BaseEditorOption { - private static ENUM_VALUES = ['normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900']; + private static SUGGESTION_VALUES = ['normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900']; private static MINIMUM_VALUE = 1; private static MAXIMUM_VALUE = 1000; @@ -1502,23 +1502,29 @@ class EditorFontWeight extends BaseEditorOption { type: 'number', minimum: EditorFontWeight.MINIMUM_VALUE, - maximum: EditorFontWeight.MAXIMUM_VALUE + maximum: EditorFontWeight.MAXIMUM_VALUE, + errorMessage: nls.localize('fontWeightErrorMessage', "Only \"normal\" and \"bold\" keywords or numbers between 1 and 1000 are allowed.") }, { - enum: EditorFontWeight.ENUM_VALUES + type: 'string', + pattern: '^(normal|bold|1000|[1-9][0-9]{0,2})$' + }, + { + enum: EditorFontWeight.SUGGESTION_VALUES } ], default: EDITOR_FONT_DEFAULTS.fontWeight, - description: nls.localize('fontWeight', "Controls the font weight.") + description: nls.localize('fontWeight', "Controls the font weight. Accepts \"normal\" and \"bold\" keywords or numbers between 1 and 1000.") } ); } public validate(input: any): string { - if (typeof input === 'number') { - return EditorFontWeight.MINIMUM_VALUE <= input && input <= EditorFontWeight.MAXIMUM_VALUE ? String(input) : EDITOR_FONT_DEFAULTS.fontWeight; + if (input === 'normal' || input === 'bold') { + return input; } - return EditorStringEnumOption.stringSet(input, EDITOR_FONT_DEFAULTS.fontWeight, EditorFontWeight.ENUM_VALUES); + const numericValue = Number(input); + return EditorFontWeight.MINIMUM_VALUE <= numericValue && numericValue <= EditorFontWeight.MAXIMUM_VALUE ? String(numericValue) : EDITOR_FONT_DEFAULTS.fontWeight; } } From 1250b086da682f7b1df4d7f04c65ff6c6f8ca436 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Wed, 2 Sep 2020 09:47:22 +0200 Subject: [PATCH 2/2] small tweaks --- src/vs/editor/common/config/editorOptions.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/vs/editor/common/config/editorOptions.ts b/src/vs/editor/common/config/editorOptions.ts index 3cbfe5bbe7928..ec9fa7c336eee 100644 --- a/src/vs/editor/common/config/editorOptions.ts +++ b/src/vs/editor/common/config/editorOptions.ts @@ -852,15 +852,13 @@ class EditorBooleanOption extends SimpleEditorOption extends SimpleEditorOption { - public static clampedInt(value: any, defaultValue: number, minimum: number, maximum: number): number { - let r: number; + public static clampedInt(value: any, defaultValue: T, minimum: number, maximum: number): number | T { if (typeof value === 'undefined') { - r = defaultValue; - } else { - r = parseInt(value, 10); - if (isNaN(r)) { - r = defaultValue; - } + return defaultValue; + } + let r = parseInt(value, 10); + if (isNaN(r)) { + return defaultValue; } r = Math.max(minimum, r); r = Math.min(maximum, r); @@ -1523,8 +1521,7 @@ class EditorFontWeight extends BaseEditorOption if (input === 'normal' || input === 'bold') { return input; } - const numericValue = Number(input); - return EditorFontWeight.MINIMUM_VALUE <= numericValue && numericValue <= EditorFontWeight.MAXIMUM_VALUE ? String(numericValue) : EDITOR_FONT_DEFAULTS.fontWeight; + return String(EditorIntOption.clampedInt(input, EDITOR_FONT_DEFAULTS.fontWeight, EditorFontWeight.MINIMUM_VALUE, EditorFontWeight.MAXIMUM_VALUE)); } }