提交 7f0f3103 编写于 作者: B Benjamin Pasero 提交者: GitHub

Merge pull request #21582 from katainaka0503/refactoring

Refactor to use Promise
......@@ -7,6 +7,7 @@
import stream = require('vs/base/node/stream');
import iconv = require('iconv-lite');
import { TPromise } from 'vs/base/common/winjs.base';
export const UTF8 = 'utf8';
export const UTF8_with_bom = 'utf8bom';
......@@ -87,14 +88,8 @@ export function detectEncodingByBOMFromBuffer(buffer: NodeBuffer, bytesRead: num
/**
* Detects the Byte Order Mark in a given file.
* If no BOM is detected, `encoding` will be null.
* If no BOM is detected, null will be passed to callback.
*/
export function detectEncodingByBOM(file: string, callback: (error: Error, encoding: string) => void): void {
stream.readExactlyByFile(file, 3, (err: Error, buffer: NodeBuffer, bytesRead: number) => {
if (err) {
return callback(err, null);
}
return callback(null, detectEncodingByBOMFromBuffer(buffer, bytesRead));
});
export function detectEncodingByBOM(file: string): TPromise<string> {
return stream.readExactlyByFile(file, 3).then(({buffer, bytesRead}) => detectEncodingByBOMFromBuffer(buffer, bytesRead));
}
\ No newline at end of file
......@@ -8,6 +8,7 @@
import streams = require('stream');
import mime = require('vs/base/common/mime');
import { TPromise } from 'vs/base/common/winjs.base';
import stream = require('vs/base/node/stream');
import encoding = require('vs/base/node/encoding');
......@@ -57,27 +58,15 @@ export interface IMimeAndEncoding {
mimes: string[];
}
function doDetectMimesFromStream(instream: streams.Readable, callback: (error: Error, result: IMimeAndEncoding) => void): void {
stream.readExactlyByStream(instream, BUFFER_READ_MAX_LEN, (err, buffer, bytesRead) => {
handleReadResult(err, buffer, bytesRead, callback);
});
function doDetectMimesFromStream(instream: streams.Readable): TPromise<IMimeAndEncoding> {
return stream.readExactlyByStream(instream, BUFFER_READ_MAX_LEN).then(detectMimeAndEncodingFromBuffer);
}
function doDetectMimesFromFile(absolutePath: string, callback: (error: Error, result: IMimeAndEncoding) => void): void {
stream.readExactlyByFile(absolutePath, BUFFER_READ_MAX_LEN, (err, buffer, bytesRead) => {
handleReadResult(err, buffer, bytesRead, callback);
});
function doDetectMimesFromFile(absolutePath: string): TPromise<IMimeAndEncoding> {
return stream.readExactlyByFile(absolutePath, BUFFER_READ_MAX_LEN).then(detectMimeAndEncodingFromBuffer);
}
function handleReadResult(err: Error, buffer: NodeBuffer, bytesRead: number, callback: (error: Error, result: IMimeAndEncoding) => void): void {
if (err) {
return callback(err, null);
}
return callback(null, detectMimeAndEncodingFromBuffer(buffer, bytesRead));
}
export function detectMimeAndEncodingFromBuffer(buffer: NodeBuffer, bytesRead: number): IMimeAndEncoding {
export function detectMimeAndEncodingFromBuffer({buffer, bytesRead}: stream.ReadResult): IMimeAndEncoding {
let enc = encoding.detectEncodingByBOMFromBuffer(buffer, bytesRead);
// Detect 0 bytes to see if file is binary (ignore for UTF 16 though)
......@@ -127,29 +116,26 @@ function filterAndSortMimes(detectedMimes: string[], guessedMimes: string[]): st
* @param instream the readable stream to detect the mime types from.
* @param nameHint an additional hint that can be used to detect a mime from a file extension.
*/
export function detectMimesFromStream(instream: streams.Readable, nameHint: string, callback: (error: Error, result: IMimeAndEncoding) => void): void {
doDetectMimesFromStream(instream, (error: Error, result: IMimeAndEncoding) => {
handleMimeResult(nameHint, error, result, callback);
});
export function detectMimesFromStream(instream: streams.Readable, nameHint: string): TPromise<IMimeAndEncoding> {
return doDetectMimesFromStream(instream).then(encoding =>
handleMimeResult(nameHint, encoding)
);
}
/**
* Opens the given file to detect its mime type. Returns an array of mime types sorted from most specific to unspecific.
* @param absolutePath the absolute path of the file.
*/
export function detectMimesFromFile(absolutePath: string, callback: (error: Error, result: IMimeAndEncoding) => void): void {
doDetectMimesFromFile(absolutePath, (error: Error, result: IMimeAndEncoding) => {
handleMimeResult(absolutePath, error, result, callback);
});
export function detectMimesFromFile(absolutePath: string): TPromise<IMimeAndEncoding> {
return doDetectMimesFromFile(absolutePath).then(encoding =>
handleMimeResult(absolutePath, encoding)
);
}
function handleMimeResult(nameHint: string, error: Error, result: IMimeAndEncoding, callback: (error: Error, result: IMimeAndEncoding) => void): void {
if (error) {
return callback(error, null);
}
function handleMimeResult(nameHint: string, result: IMimeAndEncoding): IMimeAndEncoding {
let filterAndSortedMimes = filterAndSortMimes(result.mimes, mime.guessMimeTypes(nameHint));
result.mimes = filterAndSortedMimes;
callback(null, result);
return result;
}
\ No newline at end of file
......@@ -8,93 +8,104 @@
import fs = require('fs');
import stream = require('stream');
import { TPromise } from 'vs/base/common/winjs.base';
export interface ReadResult {
buffer: NodeBuffer;
bytesRead: number;
}
/**
* Reads up to total bytes from the provided stream.
*/
export function readExactlyByStream(stream: stream.Readable, totalBytes: number, callback: (err: Error, buffer: NodeBuffer, bytesRead: number) => void): void {
let done = false;
let buffer = new Buffer(totalBytes);
let bytesRead = 0;
stream.on('data', (data: NodeBuffer) => {
let bytesToRead = Math.min(totalBytes - bytesRead, data.length);
data.copy(buffer, bytesRead, 0, bytesToRead);
bytesRead += bytesToRead;
if (bytesRead === totalBytes) {
stream.destroy(); // Will trigger the close event eventually
}
});
export function readExactlyByStream(stream: stream.Readable, totalBytes: number): TPromise<ReadResult> {
return new TPromise((complete, error) => {
let done = false;
let buffer = new Buffer(totalBytes);
let bytesRead = 0;
stream.on('error', (e: Error) => {
if (!done) {
done = true;
callback(e, null, null);
}
stream.on('data', (data: NodeBuffer) => {
let bytesToRead = Math.min(totalBytes - bytesRead, data.length);
data.copy(buffer, bytesRead, 0, bytesToRead);
bytesRead += bytesToRead;
if (bytesRead === totalBytes) {
stream.destroy(); // Will trigger the close event eventually
}
});
stream.on('error', (e: Error) => {
if (!done) {
done = true;
error(e);
}
});
let onSuccess = () => {
if (!done) {
done = true;
complete({ buffer, bytesRead });
}
};
stream.on('close', onSuccess);
});
let onSuccess = () => {
if (!done) {
done = true;
callback(null, buffer, bytesRead);
}
};
stream.on('close', onSuccess);
}
/**
* Reads totalBytes from the provided file.
*/
export function readExactlyByFile(file: string, totalBytes: number, callback: (error: Error, buffer: NodeBuffer, bytesRead: number) => void): void {
fs.open(file, 'r', null, (err, fd) => {
if (err) {
return callback(err, null, 0);
}
function end(err: Error, resultBuffer: NodeBuffer, bytesRead: number): void {
fs.close(fd, (closeError: Error) => {
if (closeError) {
return callback(closeError, null, bytesRead);
}
if (err && (<any>err).code === 'EISDIR') {
return callback(err, null, bytesRead); // we want to bubble this error up (file is actually a folder)
}
return callback(null, resultBuffer, bytesRead);
});
}
export function readExactlyByFile(file: string, totalBytes: number): TPromise<ReadResult> {
return new TPromise((complete, error) => {
fs.open(file, 'r', null, (err, fd) => {
if (err) {
return error(err);
}
function end(err: Error, resultBuffer: NodeBuffer, bytesRead: number): void {
fs.close(fd, (closeError: Error) => {
if (closeError) {
return error(closeError);
}
let buffer = new Buffer(totalBytes);
let bytesRead = 0;
let zeroAttempts = 0;
function loop(): void {
fs.read(fd, buffer, bytesRead, totalBytes - bytesRead, null, (err, moreBytesRead) => {
if (err) {
return end(err, null, 0);
}
// Retry up to N times in case 0 bytes where read
if (moreBytesRead === 0) {
if (++zeroAttempts === 10) {
return end(null, buffer, bytesRead);
if (err && (<any>err).code === 'EISDIR') {
return error(err); // we want to bubble this error up (file is actually a folder)
}
return loop();
}
return complete({ buffer: resultBuffer, bytesRead });
});
}
let buffer = new Buffer(totalBytes);
let bytesRead = 0;
let zeroAttempts = 0;
function loop(): void {
fs.read(fd, buffer, bytesRead, totalBytes - bytesRead, null, (err, moreBytesRead) => {
if (err) {
return end(err, null, 0);
}
// Retry up to N times in case 0 bytes where read
if (moreBytesRead === 0) {
if (++zeroAttempts === 10) {
return end(null, buffer, bytesRead);
}
return loop();
}
bytesRead += moreBytesRead;
bytesRead += moreBytesRead;
if (bytesRead === totalBytes) {
return end(null, buffer, bytesRead);
}
if (bytesRead === totalBytes) {
return end(null, buffer, bytesRead);
}
return loop();
});
}
return loop();
});
}
loop();
loop();
});
});
}
......@@ -107,59 +118,61 @@ export function readExactlyByFile(file: string, totalBytes: number, callback: (e
* @param maximumBytesToRead The maximum number of bytes to read before giving up.
* @param callback The finished callback.
*/
export function readToMatchingString(file: string, matchingString: string, chunkBytes: number, maximumBytesToRead: number, callback: (error: Error, result: string) => void): void {
fs.open(file, 'r', null, (err, fd) => {
if (err) {
return callback(err, null);
}
function end(err: Error, result: string): void {
fs.close(fd, (closeError: Error) => {
if (closeError) {
return callback(closeError, null);
}
if (err && (<any>err).code === 'EISDIR') {
return callback(err, null); // we want to bubble this error up (file is actually a folder)
}
return callback(null, result);
});
}
let buffer = new Buffer(maximumBytesToRead);
let bytesRead = 0;
let zeroAttempts = 0;
function loop(): void {
fs.read(fd, buffer, bytesRead, chunkBytes, null, (err, moreBytesRead) => {
if (err) {
return end(err, null);
}
// Retry up to N times in case 0 bytes where read
if (moreBytesRead === 0) {
if (++zeroAttempts === 10) {
return end(null, null);
export function readToMatchingString(file: string, matchingString: string, chunkBytes: number, maximumBytesToRead: number): TPromise<string> {
return new TPromise((complete, error) =>
fs.open(file, 'r', null, (err, fd) => {
if (err) {
return error(err);
}
function end(err: Error, result: string): void {
fs.close(fd, (closeError: Error) => {
if (closeError) {
return error(closeError);
}
return loop();
}
if (err && (<any>err).code === 'EISDIR') {
return error(err); // we want to bubble this error up (file is actually a folder)
}
return complete(result);
});
}
let buffer = new Buffer(maximumBytesToRead);
let bytesRead = 0;
let zeroAttempts = 0;
function loop(): void {
fs.read(fd, buffer, bytesRead, chunkBytes, null, (err, moreBytesRead) => {
if (err) {
return end(err, null);
}
bytesRead += moreBytesRead;
// Retry up to N times in case 0 bytes where read
if (moreBytesRead === 0) {
if (++zeroAttempts === 10) {
return end(null, null);
}
const newLineIndex = buffer.indexOf(matchingString);
if (newLineIndex >= 0) {
return end(null, buffer.toString('utf8').substr(0, newLineIndex));
}
return loop();
}
if (bytesRead >= maximumBytesToRead) {
return end(new Error(`Could not find ${matchingString} in first ${maximumBytesToRead} bytes of ${file}`), null);
}
bytesRead += moreBytesRead;
return loop();
});
}
const newLineIndex = buffer.indexOf(matchingString);
if (newLineIndex >= 0) {
return end(null, buffer.toString('utf8').substr(0, newLineIndex));
}
loop();
});
if (bytesRead >= maximumBytesToRead) {
return end(new Error(`Could not find ${matchingString} in first ${maximumBytesToRead} bytes of ${file}`), null);
}
return loop();
});
}
loop();
})
);
}
\ No newline at end of file
......@@ -10,58 +10,48 @@ import assert = require('assert');
import encoding = require('vs/base/node/encoding');
suite('Encoding', () => {
test('detectBOM UTF-8', function (done: () => void) {
test('detectBOM UTF-8', (done: (err?: any) => void) => {
const file = require.toUrl('./fixtures/some_utf8.css');
encoding.detectEncodingByBOM(file, (error: Error, encoding: string) => {
assert.equal(error, null);
encoding.detectEncodingByBOM(file).then((encoding: string) => {
assert.equal(encoding, 'utf8');
done();
});
}, done);
});
test('detectBOM UTF-16 LE', function (done: () => void) {
test('detectBOM UTF-16 LE', (done: (err?: any) => void) => {
const file = require.toUrl('./fixtures/some_utf16le.css');
encoding.detectEncodingByBOM(file, (error: Error, encoding: string) => {
assert.equal(error, null);
encoding.detectEncodingByBOM(file).then((encoding: string) => {
assert.equal(encoding, 'utf16le');
done();
});
}, done);
});
test('detectBOM UTF-16 BE', function (done: () => void) {
test('detectBOM UTF-16 BE', (done: (err?: any) => void) => {
const file = require.toUrl('./fixtures/some_utf16be.css');
encoding.detectEncodingByBOM(file, (error: Error, encoding: string) => {
assert.equal(error, null);
encoding.detectEncodingByBOM(file).then((encoding: string) => {
assert.equal(encoding, 'utf16be');
done();
});
}, done);
});
test('detectBOM ANSI', function (done: () => void) {
test('detectBOM ANSI', function (done: (err?: any) => void) {
const file = require.toUrl('./fixtures/some_ansi.css');
encoding.detectEncodingByBOM(file, (error: Error, encoding: string) => {
assert.equal(error, null);
encoding.detectEncodingByBOM(file).then((encoding: string) => {
assert.equal(encoding, null);
done();
});
}, done);
});
test('detectBOM ANSI', function (done: () => void) {
test('detectBOM ANSI', function (done: (err?: any) => void) {
const file = require.toUrl('./fixtures/empty.txt');
encoding.detectEncodingByBOM(file, (error: Error, encoding: string) => {
assert.equal(error, null);
encoding.detectEncodingByBOM(file).then((encoding: string) => {
assert.equal(encoding, null);
done();
});
}, done);
});
});
......@@ -12,64 +12,52 @@ import mime = require('vs/base/node/mime');
suite('Mime', () => {
test('detectMimesFromFile (JSON saved as PNG)', function (done: () => void) {
test('detectMimesFromFile (JSON saved as PNG)', function (done: (err?: any) => void) {
const file = require.toUrl('./fixtures/some.json.png');
mime.detectMimesFromFile(file, (error, mimes) => {
assert.equal(error, null);
mime.detectMimesFromFile(file).then(mimes => {
assert.deepEqual(mimes.mimes, ['text/plain']);
done();
});
}, done);
});
test('detectMimesFromFile (PNG saved as TXT)', function (done: () => void) {
test('detectMimesFromFile (PNG saved as TXT)', function (done: (err?: any) => void) {
mimeCommon.registerTextMime({ id: 'text', mime: 'text/plain', extension: '.txt' });
const file = require.toUrl('./fixtures/some.png.txt');
mime.detectMimesFromFile(file, (error, mimes) => {
assert.equal(error, null);
mime.detectMimesFromFile(file).then(mimes => {
assert.deepEqual(mimes.mimes, ['text/plain', 'application/octet-stream']);
done();
});
}, done);
});
test('detectMimesFromFile (XML saved as PNG)', function (done: () => void) {
test('detectMimesFromFile (XML saved as PNG)', function (done: (err?: any) => void) {
const file = require.toUrl('./fixtures/some.xml.png');
mime.detectMimesFromFile(file, (error, mimes) => {
assert.equal(error, null);
mime.detectMimesFromFile(file).then(mimes => {
assert.deepEqual(mimes.mimes, ['text/plain']);
done();
});
}, done);
});
test('detectMimesFromFile (QWOFF saved as TXT)', function (done: () => void) {
test('detectMimesFromFile (QWOFF saved as TXT)', function (done: (err?: any) => void) {
const file = require.toUrl('./fixtures/some.qwoff.txt');
mime.detectMimesFromFile(file, (error, mimes) => {
assert.equal(error, null);
mime.detectMimesFromFile(file).then(mimes => {
assert.deepEqual(mimes.mimes, ['text/plain', 'application/octet-stream']);
done();
});
}, done);
});
test('detectMimesFromFile (CSS saved as QWOFF)', function (done: () => void) {
test('detectMimesFromFile (CSS saved as QWOFF)', function (done: (err?: any) => void) {
const file = require.toUrl('./fixtures/some.css.qwoff');
mime.detectMimesFromFile(file, (error, mimes) => {
assert.equal(error, null);
mime.detectMimesFromFile(file).then(mimes => {
assert.deepEqual(mimes.mimes, ['text/plain']);
done();
});
}, done);
});
test('detectMimesFromFile (PDF)', function (done: () => void) {
const file = require.toUrl('./fixtures/some.pdf');
mime.detectMimesFromFile(file, (error, mimes) => {
assert.equal(error, null);
mime.detectMimesFromFile(file).then(mimes => {
assert.deepEqual(mimes.mimes, ['application/octet-stream']);
done();
});
}, done);
});
});
......@@ -11,72 +11,61 @@ import fs = require('fs');
import stream = require('vs/base/node/stream');
suite('Stream', () => {
test('readExactlyByFile - ANSI', function (done: () => void) {
test('readExactlyByFile - ANSI', function (done: (err?) => void) {
const file = require.toUrl('./fixtures/file.css');
stream.readExactlyByFile(file, 10, (error: Error, buffer: NodeBuffer, count: number) => {
assert.equal(error, null);
assert.equal(count, 10);
stream.readExactlyByFile(file, 10).then(({buffer, bytesRead}) => {
assert.equal(bytesRead, 10);
assert.equal(buffer.toString(), '/*--------');
done();
});
}, done);
});
test('readExactlyByFile - empty', function (done: () => void) {
test('readExactlyByFile - empty', function (done: (err?: any) => void) {
const file = require.toUrl('./fixtures/empty.txt');
stream.readExactlyByFile(file, 10, (error: Error, buffer: NodeBuffer, count: number) => {
assert.equal(error, null);
assert.equal(count, 0);
stream.readExactlyByFile(file, 10).then(({bytesRead}) => {
assert.equal(bytesRead, 0);
done();
});
}, done);
});
test('readExactlyByStream - ANSI', function (done: () => void) {
test('readExactlyByStream - ANSI', function (done: (err?: any) => void) {
const file = require.toUrl('./fixtures/file.css');
stream.readExactlyByStream(fs.createReadStream(file), 10, (error: Error, buffer: NodeBuffer, count: number) => {
assert.equal(error, null);
assert.equal(count, 10);
stream.readExactlyByStream(fs.createReadStream(file), 10).then(({buffer, bytesRead}) => {
assert.equal(bytesRead, 10);
assert.equal(buffer.toString(), '/*--------');
done();
});
}, done);
});
test('readExactlyByStream - empty', function (done: () => void) {
test('readExactlyByStream - empty', function (done: (err?: any) => void) {
const file = require.toUrl('./fixtures/empty.txt');
stream.readExactlyByStream(fs.createReadStream(file), 10, (error: Error, buffer: NodeBuffer, count: number) => {
assert.equal(error, null);
assert.equal(count, 0);
stream.readExactlyByStream(fs.createReadStream(file), 10).then(({bytesRead}) => {
assert.equal(bytesRead, 0);
done();
});
}, done);
});
test('readToMatchingString - ANSI', function (done: () => void) {
test('readToMatchingString - ANSI', function (done: (err?: any) => void) {
const file = require.toUrl('./fixtures/file.css');
stream.readToMatchingString(file, '\n', 10, 100, (error: Error, result: string) => {
assert.equal(error, null);
stream.readToMatchingString(file, '\n', 10, 100).then((result: string) => {
// \r may be present on Windows
assert.equal(result.replace('\r', ''), '/*---------------------------------------------------------------------------------------------');
done();
});
}, done);
});
test('readToMatchingString - empty', function (done: () => void) {
test('readToMatchingString - empty', function (done: (err?: any) => void) {
const file = require.toUrl('./fixtures/empty.txt');
stream.readToMatchingString(file, '\n', 10, 100, (error: Error, result: string) => {
assert.equal(error, null);
stream.readToMatchingString(file, '\n', 10, 100).then((result: string) => {
assert.equal(result, null);
done();
});
}, done);
});
});
\ No newline at end of file
......@@ -327,19 +327,13 @@ export class Repository {
return TPromise.wrapError(localize('errorBuffer', "Can't open file from git"));
}
return new Promise((c, e) => {
detectMimesFromStream(child.stdout, null, (err, result) => {
if (err) {
e(err);
} else if (isBinaryMime(result.mimes)) {
e(<IFileOperationResult>{
message: localize('fileBinaryError', "File seems to be binary and cannot be opened as text"),
fileOperationResult: FileOperationResult.FILE_IS_BINARY
});
} else {
c(this.doBuffer(object));
}
});
return detectMimesFromStream(child.stdout, null).then(result => {
return isBinaryMime(result.mimes) ?
TPromise.wrapError<string>(<IFileOperationResult>{
message: localize('fileBinaryError', "File seems to be binary and cannot be opened as text"),
fileOperationResult: FileOperationResult.FILE_IS_BINARY
}) :
this.doBuffer(object);
});
}
......
......@@ -165,22 +165,16 @@ export class RawGitService implements IRawGitService {
detectMimetypes(filePath: string, treeish?: string): TPromise<string[]> {
return exists(join(this.repo.path, filePath)).then((exists) => {
if (exists) {
return new TPromise<string[]>((c, e) => {
detectMimesFromFile(join(this.repo.path, filePath), (err, result) => {
if (err) { e(err); }
else { c(result.mimes); }
});
});
return detectMimesFromFile(join(this.repo.path, filePath))
.then(result => result.mimes);
}
const child = this.repo.show(treeish + ':' + filePath);
return new TPromise<string[]>((c, e) => {
detectMimesFromStream(child.stdout, filePath, (err, result) => {
if (err) { e(err); }
else { c(result.mimes); }
});
});
return new TPromise<string[]>((c, e) =>
detectMimesFromStream(child.stdout, filePath)
.then(result => result.mimes)
);
});
}
......
......@@ -229,15 +229,10 @@ export class BackupFileService implements IBackupFileService {
const readPromises: TPromise<Uri>[] = [];
model.get().forEach(fileBackup => {
readPromises.push(new TPromise<Uri>((c, e) => {
readToMatchingString(fileBackup.fsPath, BackupFileService.META_MARKER, 2000, 10000, (error, result) => {
if (result === null) {
e(error);
}
c(Uri.parse(result));
});
}));
readPromises.push(
readToMatchingString(fileBackup.fsPath, BackupFileService.META_MARKER, 2000, 10000)
.then(Uri.parse)
);
});
return TPromise.join(readPromises);
......
......@@ -205,7 +205,7 @@ export class FileService implements IFileService {
}
// 2.) detect mimes
return nfcall(mime.detectMimesFromFile, absolutePath).then((detected: mime.IMimeAndEncoding): TPromise<IStreamContent> => {
return mime.detectMimesFromFile(absolutePath).then((detected: mime.IMimeAndEncoding) => {
const isText = detected.mimes.indexOf(baseMime.MIME_BINARY) === -1;
// Return error early if client only accepts text and this is not text
......@@ -300,7 +300,7 @@ export class FileService implements IFileService {
if (options.overwriteEncoding) {
addBomPromise = TPromise.as(false); // if we are to overwrite the encoding, we do not preserve it if found
} else {
addBomPromise = nfcall(encoding.detectEncodingByBOM, absolutePath).then(enc => enc === encoding.UTF8); // otherwise preserve it if found
addBomPromise = encoding.detectEncodingByBOM(absolutePath).then(enc => enc === encoding.UTF8); // otherwise preserve it if found
}
}
......
......@@ -13,7 +13,6 @@ import assert = require('assert');
import { TPromise } from 'vs/base/common/winjs.base';
import { FileService, IEncodingOverride } from 'vs/workbench/services/files/node/fileService';
import { FileOperation, FileOperationEvent, FileChangesEvent, FileOperationResult, IFileOperationResult } from 'vs/platform/files/common/files';
import { nfcall } from 'vs/base/common/async';
import uri from 'vs/base/common/uri';
import uuid = require('vs/base/common/uuid');
import extfs = require('vs/base/node/extfs');
......@@ -542,7 +541,7 @@ suite('FileService', () => {
c.encoding = encoding;
return service.updateContent(c.resource, c.value, { encoding: encoding }).then(c => {
return nfcall(encodingLib.detectEncodingByBOM, c.resource.fsPath).then((enc) => {
return encodingLib.detectEncodingByBOM(c.resource.fsPath).then((enc) => {
assert.equal(enc, encodingLib.UTF16be);
return service.resolveContent(resource).then(c => {
......@@ -565,7 +564,7 @@ suite('FileService', () => {
c.value = 'Some updates';
return service.updateContent(c.resource, c.value, { encoding: encoding }).then(c => {
return nfcall(encodingLib.detectEncodingByBOM, c.resource.fsPath).then((enc) => {
return encodingLib.detectEncodingByBOM(c.resource.fsPath).then((enc) => {
assert.equal(enc, encodingLib.UTF16le);
return service.resolveContent(resource).then(c => {
......
......@@ -209,7 +209,7 @@ export class SearchWorkerEngine {
// Detect encoding and mime when this is the beginning of the file
if (isFirstRead) {
const mimeAndEncoding = detectMimeAndEncodingFromBuffer(buffer, bytesRead);
const mimeAndEncoding = detectMimeAndEncodingFromBuffer({ buffer, bytesRead });
if (mimeAndEncoding.mimes[mimeAndEncoding.mimes.length - 1] !== baseMime.MIME_TEXT) {
return clb(null); // skip files that seem binary
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册