Skip to content

Commit 782f159

Browse files
author
Benjamin Pasero
committed
1 parent 027e16f commit 782f159

4 files changed

Lines changed: 19 additions & 23 deletions

File tree

src/vs/base/node/encoding.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function toNodeEncoding(enc: string | null): string {
167167
return enc;
168168
}
169169

170-
export function detectEncodingByBOMFromBuffer(buffer: Buffer | VSBuffer | null, bytesRead: number): string | null {
170+
export function detectEncodingByBOMFromBuffer(buffer: Buffer | VSBuffer | null, bytesRead: number): typeof UTF8_with_bom | typeof UTF16le | typeof UTF16be | null {
171171
if (!buffer || bytesRead < UTF16be_BOM.length) {
172172
return null;
173173
}
@@ -193,7 +193,7 @@ export function detectEncodingByBOMFromBuffer(buffer: Buffer | VSBuffer | null,
193193

194194
// UTF-8
195195
if (b0 === UTF8_BOM[0] && b1 === UTF8_BOM[1] && b2 === UTF8_BOM[2]) {
196-
return UTF8;
196+
return UTF8_with_bom;
197197
}
198198

199199
return null;

src/vs/base/test/node/encoding/encoding.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as encoding from 'vs/base/node/encoding';
99
import { Readable } from 'stream';
1010
import { getPathFromAmdModule } from 'vs/base/common/amd';
1111

12-
export async function detectEncodingByBOM(file: string): Promise<string | null> {
12+
export async function detectEncodingByBOM(file: string): Promise<typeof encoding.UTF16be | typeof encoding.UTF16le | typeof encoding.UTF8_with_bom | null> {
1313
try {
1414
const { buffer, bytesRead } = await readExactlyByFile(file, 3);
1515

@@ -86,7 +86,7 @@ suite('Encoding', () => {
8686
const file = getPathFromAmdModule(require, './fixtures/some_utf8.css');
8787

8888
const detectedEncoding = await detectEncodingByBOM(file);
89-
assert.equal(detectedEncoding, 'utf8');
89+
assert.equal(detectedEncoding, 'utf8bom');
9090
});
9191

9292
test('detectBOM UTF-16 LE', async () => {

src/vs/workbench/services/textfile/electron-browser/nativeTextFileService.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ export class EncodingOracle extends Disposable implements IResourceEncodings {
375375
if (!overwriteEncoding && encoding === UTF8) {
376376
try {
377377
const buffer = (await this.fileService.readFile(resource, { length: UTF8_BOM.length })).value;
378-
if (detectEncodingByBOMFromBuffer(buffer, buffer.byteLength) === UTF8) {
378+
if (detectEncodingByBOMFromBuffer(buffer, buffer.byteLength) === UTF8_with_bom) {
379379
return { encoding, addBOM: true };
380380
}
381381
} catch (error) {
@@ -400,7 +400,7 @@ export class EncodingOracle extends Disposable implements IResourceEncodings {
400400

401401
// Encoding passed in as option
402402
if (options?.encoding) {
403-
if (detectedEncoding === UTF8 && options.encoding === UTF8) {
403+
if (detectedEncoding === UTF8_with_bom && options.encoding === UTF8) {
404404
preferredEncoding = UTF8_with_bom; // indicate the file has BOM if we are to resolve with UTF 8
405405
} else {
406406
preferredEncoding = options.encoding; // give passed in encoding highest priority
@@ -409,11 +409,7 @@ export class EncodingOracle extends Disposable implements IResourceEncodings {
409409

410410
// Encoding detected
411411
else if (detectedEncoding) {
412-
if (detectedEncoding === UTF8) {
413-
preferredEncoding = UTF8_with_bom; // if we detected UTF-8, it can only be because of a BOM
414-
} else {
415-
preferredEncoding = detectedEncoding;
416-
}
412+
preferredEncoding = detectedEncoding;
417413
}
418414

419415
// Encoding configured

src/vs/workbench/services/textfile/test/textFileService.io.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ suite('Files - TextFileService i/o', () => {
172172
assert.equal(await exists(resource.fsPath), true);
173173

174174
const detectedEncoding = await detectEncodingByBOM(resource.fsPath);
175-
assert.equal(detectedEncoding, UTF8);
175+
assert.equal(detectedEncoding, UTF8_with_bom);
176176
});
177177

178178
test('create - UTF 8 BOM - content provided', async () => {
@@ -183,7 +183,7 @@ suite('Files - TextFileService i/o', () => {
183183
assert.equal(await exists(resource.fsPath), true);
184184

185185
const detectedEncoding = await detectEncodingByBOM(resource.fsPath);
186-
assert.equal(detectedEncoding, UTF8);
186+
assert.equal(detectedEncoding, UTF8_with_bom);
187187
});
188188

189189
test('create - UTF 8 BOM - empty content - snapshot', async () => {
@@ -194,7 +194,7 @@ suite('Files - TextFileService i/o', () => {
194194
assert.equal(await exists(resource.fsPath), true);
195195

196196
const detectedEncoding = await detectEncodingByBOM(resource.fsPath);
197-
assert.equal(detectedEncoding, UTF8);
197+
assert.equal(detectedEncoding, UTF8_with_bom);
198198
});
199199

200200
test('create - UTF 8 BOM - content provided - snapshot', async () => {
@@ -205,7 +205,7 @@ suite('Files - TextFileService i/o', () => {
205205
assert.equal(await exists(resource.fsPath), true);
206206

207207
const detectedEncoding = await detectEncodingByBOM(resource.fsPath);
208-
assert.equal(detectedEncoding, UTF8);
208+
assert.equal(detectedEncoding, UTF8_with_bom);
209209
});
210210

211211
test('write - use encoding (UTF 16 BE) - small content as string', async () => {
@@ -325,12 +325,12 @@ suite('Files - TextFileService i/o', () => {
325325
await service.write(resource, content, { encoding: UTF8_with_bom });
326326

327327
detectedEncoding = await detectEncodingByBOM(resource.fsPath);
328-
assert.equal(detectedEncoding, UTF8);
328+
assert.equal(detectedEncoding, UTF8_with_bom);
329329

330330
// ensure BOM preserved
331331
await service.write(resource, content, { encoding: UTF8 });
332332
detectedEncoding = await detectEncodingByBOM(resource.fsPath);
333-
assert.equal(detectedEncoding, UTF8);
333+
assert.equal(detectedEncoding, UTF8_with_bom);
334334

335335
// allow to remove BOM
336336
await service.write(resource, content, { encoding: UTF8, overwriteEncoding: true });
@@ -353,12 +353,12 @@ suite('Files - TextFileService i/o', () => {
353353
await service.write(resource, model.createSnapshot(), { encoding: UTF8_with_bom });
354354

355355
detectedEncoding = await detectEncodingByBOM(resource.fsPath);
356-
assert.equal(detectedEncoding, UTF8);
356+
assert.equal(detectedEncoding, UTF8_with_bom);
357357

358358
// ensure BOM preserved
359359
await service.write(resource, model.createSnapshot(), { encoding: UTF8 });
360360
detectedEncoding = await detectEncodingByBOM(resource.fsPath);
361-
assert.equal(detectedEncoding, UTF8);
361+
assert.equal(detectedEncoding, UTF8_with_bom);
362362

363363
// allow to remove BOM
364364
await service.write(resource, model.createSnapshot(), { encoding: UTF8, overwriteEncoding: true });
@@ -375,11 +375,11 @@ suite('Files - TextFileService i/o', () => {
375375
const resource = URI.file(join(testDir, 'some_utf8_bom.txt'));
376376

377377
let detectedEncoding = await detectEncodingByBOM(resource.fsPath);
378-
assert.equal(detectedEncoding, UTF8);
378+
assert.equal(detectedEncoding, UTF8_with_bom);
379379

380380
await service.write(resource, 'Hello World');
381381
detectedEncoding = await detectEncodingByBOM(resource.fsPath);
382-
assert.equal(detectedEncoding, UTF8);
382+
assert.equal(detectedEncoding, UTF8_with_bom);
383383
});
384384

385385
test('write - ensure BOM in empty file - content as string', async () => {
@@ -388,7 +388,7 @@ suite('Files - TextFileService i/o', () => {
388388
await service.write(resource, '', { encoding: UTF8_with_bom });
389389

390390
let detectedEncoding = await detectEncodingByBOM(resource.fsPath);
391-
assert.equal(detectedEncoding, UTF8);
391+
assert.equal(detectedEncoding, UTF8_with_bom);
392392
});
393393

394394
test('write - ensure BOM in empty file - content as snapshot', async () => {
@@ -397,7 +397,7 @@ suite('Files - TextFileService i/o', () => {
397397
await service.write(resource, TextModel.createFromString('').createSnapshot(), { encoding: UTF8_with_bom });
398398

399399
let detectedEncoding = await detectEncodingByBOM(resource.fsPath);
400-
assert.equal(detectedEncoding, UTF8);
400+
assert.equal(detectedEncoding, UTF8_with_bom);
401401
});
402402

403403
test('readStream - small text', async () => {

0 commit comments

Comments
 (0)