Skip to content

Commit 469043a

Browse files
committed
allowedCharacters: string -> allowedCharacters: Record<string, bool>
1 parent 093aa3c commit 469043a

File tree

3 files changed

+59
-27
lines changed

3 files changed

+59
-27
lines changed

src/vs/editor/common/config/editorOptions.ts

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3312,9 +3312,9 @@ export interface IUnicodeHighlightOptions {
33123312
ambiguousCharacters?: boolean;
33133313
includeComments?: boolean | InUntrustedWorkspace;
33143314
/**
3315-
* A list of allowed code points in a single string.
3315+
* A map of allowed characters (true: allowed).
33163316
*/
3317-
allowedCharacters?: string;
3317+
allowedCharacters?: Record<string, true>;
33183318
}
33193319

33203320
/**
@@ -3340,7 +3340,7 @@ class UnicodeHighlight extends BaseEditorOption<EditorOption.unicodeHighlighting
33403340
invisibleCharacters: true,
33413341
ambiguousCharacters: true,
33423342
includeComments: inUntrustedWorkspace,
3343-
allowedCharacters: '',
3343+
allowedCharacters: {},
33443344
};
33453345

33463346
super(
@@ -3374,14 +3374,34 @@ class UnicodeHighlight extends BaseEditorOption<EditorOption.unicodeHighlighting
33743374
},
33753375
[unicodeHighlightConfigKeys.allowedCharacters]: {
33763376
restricted: true,
3377-
type: 'string',
3377+
type: 'object',
33783378
default: defaults.allowedCharacters,
3379-
description: nls.localize('unicodeHighlight.allowedCharacters', "Defines allowed characters that are not being highlighted.")
3379+
description: nls.localize('unicodeHighlight.allowedCharacters', "Defines allowed characters that are not being highlighted."),
3380+
additionalProperties: {
3381+
type: 'boolean'
3382+
}
33803383
},
33813384
}
33823385
);
33833386
}
33843387

3388+
public override applyUpdate(value: Required<Readonly<IUnicodeHighlightOptions>>, update: Required<Readonly<IUnicodeHighlightOptions>>): ApplyUpdateResult<Required<Readonly<IUnicodeHighlightOptions>>> {
3389+
let didChange = false;
3390+
if (update.allowedCharacters) {
3391+
// Treat allowedCharacters atomically
3392+
if (!objects.equals(value.allowedCharacters, update.allowedCharacters)) {
3393+
value = { ...value, allowedCharacters: update.allowedCharacters };
3394+
didChange = true;
3395+
}
3396+
}
3397+
3398+
const result = super.applyUpdate(value, update);
3399+
if (didChange) {
3400+
return new ApplyUpdateResult(result.newValue, true);
3401+
}
3402+
return result;
3403+
}
3404+
33853405
public validate(_input: any): InternalUnicodeHighlightOptions {
33863406
if (!_input || typeof _input !== 'object') {
33873407
return this.defaultValue;
@@ -3392,16 +3412,22 @@ class UnicodeHighlight extends BaseEditorOption<EditorOption.unicodeHighlighting
33923412
invisibleCharacters: boolean(input.invisibleCharacters, this.defaultValue.invisibleCharacters),
33933413
ambiguousCharacters: boolean(input.ambiguousCharacters, this.defaultValue.ambiguousCharacters),
33943414
includeComments: primitiveSet<boolean | InUntrustedWorkspace>(input.includeComments, inUntrustedWorkspace, [true, false, inUntrustedWorkspace]),
3395-
allowedCharacters: string(input.allowedCharacters, ''),
3415+
allowedCharacters: this.validateAllowedCharacters(_input.allowedCharacters, this.defaultValue.allowedCharacters),
33963416
};
33973417
}
3398-
}
33993418

3400-
function string(value: unknown, defaultValue: string): string {
3401-
if (typeof value !== 'string') {
3402-
return defaultValue;
3419+
private validateAllowedCharacters(map: unknown, defaultValue: Record<string, true>): Record<string, true> {
3420+
if ((typeof map !== 'object') || !map) {
3421+
return defaultValue;
3422+
}
3423+
const result: Record<string, true> = {};
3424+
for (const [key, value] of Object.entries(map)) {
3425+
if (value === true) {
3426+
result[key] = true;
3427+
}
3428+
}
3429+
return result;
34033430
}
3404-
return value;
34053431
}
34063432

34073433
//#endregion

src/vs/editor/contrib/unicodeHighlighter/unicodeHighlighter.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export class UnicodeHighlighter extends Disposable implements IEditorContributio
160160
ambiguousCharacters: options.ambiguousCharacters,
161161
invisibleCharacters: options.invisibleCharacters,
162162
includeComments: options.includeComments,
163-
allowedCodePoints: Array.from(options.allowedCharacters).map(c => c.codePointAt(0)!),
163+
allowedCodePoints: Object.keys(options.allowedCharacters).map(c => c.codePointAt(0)!),
164164
};
165165

166166
if (this._editorWorkerService.canComputeUnicodeHighlights(this._editor.getModel().uri)) {
@@ -191,7 +191,7 @@ function resolveOptions(trusted: boolean, options: InternalUnicodeHighlightOptio
191191
ambiguousCharacters: options.ambiguousCharacters,
192192
invisibleCharacters: options.invisibleCharacters,
193193
includeComments: options.includeComments === inUntrustedWorkspace ? !trusted : options.includeComments,
194-
allowedCharacters: options.allowedCharacters ?? [],
194+
allowedCharacters: options.allowedCharacters ?? {},
195195
};
196196
}
197197

@@ -640,18 +640,7 @@ export class ShowExcludeOptions extends EditorAction {
640640
const options: ExtendedOptions[] = [
641641
{
642642
label: getExcludeCharFromBeingHighlightedLabel(codePoint),
643-
run: async () => {
644-
const existingValue = configurationService.getValue(unicodeHighlightConfigKeys.allowedCharacters);
645-
let value: string;
646-
if (typeof existingValue === 'string') {
647-
value = existingValue;
648-
} else {
649-
value = '';
650-
}
651-
652-
value += char;
653-
await configurationService.updateValue(unicodeHighlightConfigKeys.allowedCharacters, value, ConfigurationTarget.USER);
654-
}
643+
run: () => excludeCharFromBeingHighlighted(configurationService, [codePoint])
655644
},
656645
];
657646

@@ -681,6 +670,23 @@ export class ShowExcludeOptions extends EditorAction {
681670
}
682671
}
683672

673+
async function excludeCharFromBeingHighlighted(configurationService: IConfigurationService, charCodes: number[]) {
674+
const existingValue = configurationService.getValue(unicodeHighlightConfigKeys.allowedCharacters);
675+
676+
let value: Record<string, boolean>;
677+
if ((typeof existingValue === 'object') && existingValue) {
678+
value = existingValue as any;
679+
} else {
680+
value = {};
681+
}
682+
683+
for (const charCode of charCodes) {
684+
value[String.fromCodePoint(charCode)] = true;
685+
}
686+
687+
await configurationService.updateValue(unicodeHighlightConfigKeys.allowedCharacters, value, ConfigurationTarget.USER);
688+
}
689+
684690
function expectNever(value: never) {
685691
throw new Error(`Unexpected value: ${value}`);
686692
}

src/vs/monaco.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3884,9 +3884,9 @@ declare namespace monaco.editor {
38843884
ambiguousCharacters?: boolean;
38853885
includeComments?: boolean | InUntrustedWorkspace;
38863886
/**
3887-
* A list of allowed code points in a single string.
3887+
* A map of allowed characters (true: allowed).
38883888
*/
3889-
allowedCharacters?: string;
3889+
allowedCharacters?: Record<string, true>;
38903890
}
38913891

38923892
export interface IInlineSuggestOptions {

0 commit comments

Comments
 (0)