Skip to content

Commit e24eeda

Browse files
author
Benjamin Pasero
committed
codicons - speed up stripCodicons
1 parent d2ae888 commit e24eeda

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/vs/base/common/codicon.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { matchesFuzzy, IMatch } from 'vs/base/common/filters';
77
import { ltrim } from 'vs/base/common/strings';
88

9-
const codiconStartMarker = '$(';
9+
export const codiconStartMarker = '$(';
1010

1111
export interface IParsedCodicons {
1212
readonly text: string;

src/vs/base/common/codicons.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
import { codiconStartMarker } from 'vs/base/common/codicon';
7+
68
const escapeCodiconsRegex = /(\\)?\$\([a-z0-9\-]+?(?:~[a-z0-9\-]*?)?\)/gi;
79
export function escapeCodicons(text: string): string {
810
return text.replace(escapeCodiconsRegex, (match, escaped) => escaped ? match : `\\${match}`);
@@ -30,5 +32,9 @@ export function renderCodicons(text: string): string {
3032

3133
const stripCodiconsRegex = /(\s)?(\\)?\$\([a-z0-9\-]+?(?:~[a-z0-9\-]*?)?\)(\s)?/gi;
3234
export function stripCodicons(text: string): string {
35+
if (text.indexOf(codiconStartMarker) === -1) {
36+
return text;
37+
}
38+
3339
return text.replace(stripCodiconsRegex, (match, preWhitespace, escaped, postWhitespace) => escaped ? match : preWhitespace || postWhitespace || '');
3440
}

src/vs/base/test/common/codicon.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5+
56
import * as assert from 'assert';
67
import { IMatch } from 'vs/base/common/filters';
78
import { matchesFuzzyCodiconAware, parseCodicons, IParsedCodicons } from 'vs/base/common/codicon';
9+
import { stripCodicons } from 'vs/base/common/codicons';
810

911
export interface ICodiconFilter {
1012
// Returns null if word doesn't match.
@@ -64,3 +66,13 @@ suite('Codicon', () => {
6466
]);
6567
});
6668
});
69+
70+
suite('Codicons', () => {
71+
72+
test('stripCodicons', () => {
73+
assert.equal(stripCodicons('Hello World'), 'Hello World');
74+
assert.equal(stripCodicons('$(Hello World'), '$(Hello World');
75+
assert.equal(stripCodicons('$(Hello) World'), ' World');
76+
assert.equal(stripCodicons('$(Hello) W$(oi)rld'), ' Wrld');
77+
});
78+
});

0 commit comments

Comments
 (0)