Skip to content

Commit

Permalink
escape and unescape \n and \r\n
Browse files Browse the repository at this point in the history
Signed-off-by: Uni Sayo <unibtc@gmail.com>

move escape/unescape functions to core common strings.

Signed-off-by: Uni Sayo <unibtc@gmail.com>

use explicit string type

Signed-off-by: Uni Sayo <unibtc@gmail.com>

rename prefValue to value

Signed-off-by: Uni Sayo <unibtc@gmail.com>
  • Loading branch information
uniibu authored and svenefftinge committed Feb 1, 2019
1 parent 7cf3ca7 commit 57b8add
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
8 changes: 8 additions & 0 deletions packages/core/src/common/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ export function* split(s: string, splitter: string): IterableIterator<string> {
start = end + splitter.length;
}
}

export function escapeInvisibleChars(value: string ): string {
return value.replace(/\n/g, '\\n').replace(/\r/g, '\\r');
}

export function unescapeInvisibleChars(value: string): string {
return value.replace(/\\n/g, '\n').replace(/\\r/g, '\r');
}
5 changes: 4 additions & 1 deletion packages/preferences/src/browser/preferences-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { inject, injectable } from 'inversify';
import { Tree, TreeDecorator, TreeDecoration, PreferenceProperty, PreferenceService } from '@theia/core/lib/browser';
import { Emitter, Event, MaybePromise } from '@theia/core';
import { escapeInvisibleChars } from '@theia/core/lib/common/strings';

@injectable()
export class PreferencesDecorator implements TreeDecorator {
Expand Down Expand Up @@ -50,7 +51,8 @@ export class PreferencesDecorator implements TreeDecorator {
tooltip: preferenceValue.description,
captionSuffixes: [
{
data: storedValue !== undefined ? ': ' + storedValue : preferenceValue.default !== undefined ? ': ' + preferenceValue.default : undefined,
data: storedValue !== undefined && typeof storedValue === 'string' ? ': ' + escapeInvisibleChars(storedValue) :
preferenceValue.default !== undefined ? ': ' + preferenceValue.default : undefined,
},
{
data: ' ' + preferenceValue.description,
Expand All @@ -64,4 +66,5 @@ export class PreferencesDecorator implements TreeDecorator {
decorations(tree: Tree): MaybePromise<Map<string, TreeDecoration.Data>> {
return this.preferencesDecorations;
}

}
8 changes: 5 additions & 3 deletions packages/preferences/src/browser/preferences-menu-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { injectable } from 'inversify';
import { Menu } from '@phosphor/widgets';
import { CommandRegistry } from '@phosphor/commands';
import { PreferenceProperty } from '@theia/core/lib/browser';
import { escapeInvisibleChars, unescapeInvisibleChars } from '@theia/core/lib/common/strings';

@injectable()
export class PreferencesMenuFactory {
Expand All @@ -29,13 +30,14 @@ export class PreferencesMenuFactory {
if (property) {
const enumConst = property.enum;
if (enumConst) {
enumConst.forEach(enumValue => {
enumConst.map(escapeInvisibleChars)
.forEach(enumValue => {
const commandId = id + '-' + enumValue;
if (!commands.hasCommand(commandId)) {
commands.addCommand(commandId, {
label: enumValue,
iconClass: savedPreference === enumValue || !savedPreference && property.default === enumValue ? 'fa fa-check' : '',
execute: () => execute(id, enumValue)
iconClass: escapeInvisibleChars(savedPreference) === enumValue || !savedPreference && property.default === enumValue ? 'fa fa-check' : '',
execute: () => execute(id, unescapeInvisibleChars(enumValue))
});
menu.addItem({
type: 'command',
Expand Down

0 comments on commit 57b8add

Please sign in to comment.