Skip to content

Commit 841ef45

Browse files
committed
More CharCode adoption
1 parent 34a8572 commit 841ef45

5 files changed

Lines changed: 36 additions & 33 deletions

File tree

src/vs/base/common/charCode.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Please leave the const keyword such that it gets inlined when compiled to JavaScript!
1212
*/
1313
export const enum CharCode {
14-
14+
Null = 0,
1515
Tab = 9,
1616
LineFeed = 10,
1717
CarriageReturn = 13,
@@ -212,4 +212,17 @@ export const enum CharCode {
212212
* The `~` character.
213213
*/
214214
Tilde = 126,
215+
216+
/**
217+
* Unicode Character 'LINE SEPARATOR' (U+2028)
218+
* http://www.fileformat.info/info/unicode/char/2028/index.htm
219+
*/
220+
LINE_SEPARATOR_2028 = 8232,
221+
222+
/**
223+
* UTF-8 BOM
224+
* Unicode Character 'ZERO WIDTH NO-BREAK SPACE' (U+FEFF)
225+
* http://www.fileformat.info/info/unicode/char/feff/index.htm
226+
*/
227+
UTF8_BOM = 65279
215228
}

src/vs/base/common/strings.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,12 +550,10 @@ export function removeAnsiEscapeCodes(str: string): string {
550550

551551
// -- UTF-8 BOM
552552

553-
const __utf8_bom = 65279;
554-
555-
export const UTF8_BOM_CHARACTER = String.fromCharCode(__utf8_bom);
553+
export const UTF8_BOM_CHARACTER = String.fromCharCode(CharCode.UTF8_BOM);
556554

557555
export function startsWithUTF8BOM(str: string): boolean {
558-
return (str && str.length > 0 && str.charCodeAt(0) === __utf8_bom);
556+
return (str && str.length > 0 && str.charCodeAt(0) === CharCode.UTF8_BOM);
559557
}
560558

561559
/**

src/vs/editor/common/viewLayout/viewLineParts.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {Arrays} from 'vs/editor/common/core/arrays';
99
import {Range} from 'vs/editor/common/core/range';
1010
import {ViewLineToken, ViewLineTokens} from 'vs/editor/common/core/viewLineToken';
1111
import {InlineDecoration} from 'vs/editor/common/viewModel/viewModel';
12+
import {CharCode} from 'vs/base/common/charCode';
1213

1314
function cmpLineDecorations(a:InlineDecoration, b:InlineDecoration): number {
1415
return Range.compareRangesUsingStarts(a.range, b.range);
@@ -93,9 +94,6 @@ function trimEmptyTrailingPart(parts: ViewLineToken[], lineContent: string): Vie
9394
return parts.slice(0, parts.length - 1);
9495
}
9596

96-
const _tab = '\t'.charCodeAt(0);
97-
const _space = ' '.charCodeAt(0);
98-
9997
function insertOneCustomLineDecoration(dest:InlineDecoration[], lineNumber:number, startColumn:number, endColumn:number, className:string): void {
10098
dest.push(new InlineDecoration(new Range(lineNumber, startColumn, lineNumber, endColumn), className));
10199
}
@@ -135,8 +133,8 @@ function insertWhitespaceLineDecorations(lineNumber:number, lineContent: string,
135133
let hasTab = false;
136134

137135
for (let i = Math.max(firstNonWhitespaceIndex, fauxIndentLength); i <= lastNonWhitespaceIndex; ++i) {
138-
let currentCharIsTab = lineContent.charCodeAt(i) === _tab;
139-
if (currentCharIsTab || lineContent.charCodeAt(i) === _space) {
136+
let currentCharIsTab = lineContent.charCodeAt(i) === CharCode.Tab;
137+
if (currentCharIsTab || lineContent.charCodeAt(i) === CharCode.Space) {
140138
if (currentCharIsTab) {
141139
hasTab = true;
142140
}
@@ -185,7 +183,7 @@ function insertCustomLineDecorationsWithStateMachine(lineNumber:number, lineCont
185183
for (let index = 0; index < lineLength; index++) {
186184
let chCode = lineContent.charCodeAt(index);
187185

188-
if (chCode === _tab) {
186+
if (chCode === CharCode.Tab) {
189187
tmpIndent = tabSize;
190188
} else {
191189
tmpIndent++;

src/vs/editor/common/viewLayout/viewLineRenderer.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'use strict';
66

77
import {ViewLineToken} from 'vs/editor/common/core/viewLineToken';
8+
import {CharCode} from 'vs/base/common/charCode';
89

910
export class RenderLineInput {
1011
_renderLineInputBrand: void;
@@ -49,16 +50,6 @@ export class RenderLineOutput {
4950
}
5051
}
5152

52-
const _space = ' '.charCodeAt(0);
53-
const _tab = '\t'.charCodeAt(0);
54-
const _lowerThan = '<'.charCodeAt(0);
55-
const _greaterThan = '>'.charCodeAt(0);
56-
const _ampersand = '&'.charCodeAt(0);
57-
const _carriageReturn = '\r'.charCodeAt(0);
58-
const _controlCharacterSequenceConversionStart = 9216;
59-
const _lineSeparator = '\u2028'.charCodeAt(0); //http://www.fileformat.info/info/unicode/char/2028/index.htm
60-
const _bom = 65279;
61-
6253
export function renderLine(input:RenderLineInput): RenderLineOutput {
6354
const lineText = input.lineContent;
6455
const lineTextLength = lineText.length;
@@ -92,6 +83,8 @@ function isWhitespace(type:string): boolean {
9283
function isControlCharacter(characterCode: number): boolean {
9384
return characterCode < 32;
9485
}
86+
87+
const _controlCharacterSequenceConversionStart = 9216;
9588
function controlCharacterToPrintable(characterCode: number): string {
9689
return String.fromCharCode(_controlCharacterSequenceConversionStart + characterCode);
9790
}
@@ -128,7 +121,7 @@ function renderLineActual(lineText: string, lineTextLength: number, tabSize: num
128121
charOffsetInPartArr[charIndex] = charOffsetInPart;
129122
let charCode = lineText.charCodeAt(charIndex);
130123

131-
if (charCode === _tab) {
124+
if (charCode === CharCode.Tab) {
132125
let insertSpacesCount = tabSize - (charIndex + tabsCharDelta) % tabSize;
133126
tabsCharDelta += insertSpacesCount - 1;
134127
charOffsetInPart += insertSpacesCount - 1;
@@ -143,7 +136,7 @@ function renderLineActual(lineText: string, lineTextLength: number, tabSize: num
143136
insertSpacesCount--;
144137
}
145138
} else {
146-
// must be _space
139+
// must be CharCode.Space
147140
partContent += '&middot;';
148141
partContentCnt++;
149142
}
@@ -175,7 +168,7 @@ function renderLineActual(lineText: string, lineTextLength: number, tabSize: num
175168
let charCode = lineText.charCodeAt(charIndex);
176169

177170
switch (charCode) {
178-
case _tab:
171+
case CharCode.Tab:
179172
let insertSpacesCount = tabSize - (charIndex + tabsCharDelta) % tabSize;
180173
tabsCharDelta += insertSpacesCount - 1;
181174
charOffsetInPart += insertSpacesCount - 1;
@@ -185,32 +178,32 @@ function renderLineActual(lineText: string, lineTextLength: number, tabSize: num
185178
}
186179
break;
187180

188-
case _space:
181+
case CharCode.Space:
189182
out += '&nbsp;';
190183
break;
191184

192-
case _lowerThan:
185+
case CharCode.LessThan:
193186
out += '&lt;';
194187
break;
195188

196-
case _greaterThan:
189+
case CharCode.GreaterThan:
197190
out += '&gt;';
198191
break;
199192

200-
case _ampersand:
193+
case CharCode.Ampersand:
201194
out += '&amp;';
202195
break;
203196

204-
case 0:
197+
case CharCode.Null:
205198
out += '&#00;';
206199
break;
207200

208-
case _bom:
209-
case _lineSeparator:
201+
case CharCode.UTF8_BOM:
202+
case CharCode.LINE_SEPARATOR_2028:
210203
out += '\ufffd';
211204
break;
212205

213-
case _carriageReturn:
206+
case CharCode.CarriageReturn:
214207
// zero width space, because carriage return would introduce a line break
215208
out += '&#8203';
216209
break;

src/vs/editor/test/common/viewLayout/viewLineRenderer.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import * as assert from 'assert';
88
import {renderLine, RenderLineInput} from 'vs/editor/common/viewLayout/viewLineRenderer';
99
import {ViewLineToken} from 'vs/editor/common/core/viewLineToken';
10+
import {CharCode} from 'vs/base/common/charCode';
1011

1112
suite('viewLineRenderer.renderLine', () => {
1213

@@ -43,7 +44,7 @@ suite('viewLineRenderer.renderLine', () => {
4344

4445
test('replaces some bad characters', () => {
4546
assertCharacterReplacement('a\0b', 4, 'a&#00;b', [0, 1, 2, 3]);
46-
assertCharacterReplacement('a' + String.fromCharCode(65279) + 'b', 4, 'a\ufffdb', [0, 1, 2, 3]);
47+
assertCharacterReplacement('a' + String.fromCharCode(CharCode.UTF8_BOM) + 'b', 4, 'a\ufffdb', [0, 1, 2, 3]);
4748
assertCharacterReplacement('a\u2028b', 4, 'a\ufffdb', [0, 1, 2, 3]);
4849
assertCharacterReplacement('a\rb', 4, 'a&#8203b', [0, 1, 2, 3]);
4950
});

0 commit comments

Comments
 (0)