提交 e1a43609 编写于 作者: K katainaka0503

Rename autoDetectEncoding to autoGuessEncoding

上级 3de68cda
......@@ -97,22 +97,22 @@ export function detectEncodingByBOM(file: string): TPromise<string> {
const IGNORE_ENCODINGS = ['ascii', 'utf-8', 'utf-16', 'utf-32'];
/**
* Detects the encoding from buffer.
* Guesses the encoding from buffer.
*/
export function detectEncodingByBuffer(buffer: NodeBuffer): string {
let detected = jschardet.detect(buffer);
if (!detected || !detected.encoding) {
export function guessEncodingByBuffer(buffer: NodeBuffer): string {
let guessed = jschardet.detect(buffer);
if (!guessed || !guessed.encoding) {
return null;
}
let enc = detected.encoding.toLowerCase();
let enc = guessed.encoding.toLowerCase();
// Ignore encodings that cannot detect correctly
// Ignore encodings that cannot guess correctly
// (http://chardet.readthedocs.io/en/latest/supported-encodings.html)
if (0 <= IGNORE_ENCODINGS.indexOf(enc)) {
return null;
}
return detected.encoding;
return guessed.encoding;
}
/**
* The encodings that are allowed in a settings file don't match the canonical encoding labels specified by WHATWG.
......
......@@ -58,19 +58,19 @@ export interface IMimeAndEncoding {
mimes: string[];
}
function doDetectMimesFromStream(instream: streams.Readable, autoDetectEncoding: boolean): TPromise<IMimeAndEncoding> {
function doDetectMimesFromStream(instream: streams.Readable, autoGuessEncoding: boolean): TPromise<IMimeAndEncoding> {
return stream.readExactlyByStream(instream, BUFFER_READ_MAX_LEN).then((readResult: stream.ReadResult) => {
return detectMimeAndEncodingFromBuffer(readResult, autoDetectEncoding);
return detectMimeAndEncodingFromBuffer(readResult, autoGuessEncoding);
});
}
function doDetectMimesFromFile(absolutePath: string, autoDetectEncoding: boolean): TPromise<IMimeAndEncoding> {
function doDetectMimesFromFile(absolutePath: string, autoGuessEncoding: boolean): TPromise<IMimeAndEncoding> {
return stream.readExactlyByFile(absolutePath, BUFFER_READ_MAX_LEN).then((readResult: stream.ReadResult) => {
return detectMimeAndEncodingFromBuffer(readResult, autoDetectEncoding);
return detectMimeAndEncodingFromBuffer(readResult, autoGuessEncoding);
});
}
export function detectMimeAndEncodingFromBuffer({buffer, bytesRead}: stream.ReadResult, autoDetectEncoding: boolean): IMimeAndEncoding {
export function detectMimeAndEncodingFromBuffer({ buffer, bytesRead }: stream.ReadResult, autoGuessEncoding: boolean): IMimeAndEncoding {
let enc = encoding.detectEncodingByBOMFromBuffer(buffer, bytesRead);
// Detect 0 bytes to see if file is binary (ignore for UTF 16 though)
......@@ -83,8 +83,8 @@ export function detectMimeAndEncodingFromBuffer({buffer, bytesRead}: stream.Read
}
}
}
if (autoDetectEncoding && isText && !enc) {
enc = encoding.detectEncodingByBuffer(buffer);
if (autoGuessEncoding && isText && !enc) {
enc = encoding.guessEncodingByBuffer(buffer);
}
return {
......@@ -123,8 +123,8 @@ 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, autoDetectEncoding: boolean): TPromise<IMimeAndEncoding> {
return doDetectMimesFromStream(instream, autoDetectEncoding).then(encoding =>
export function detectMimesFromStream(instream: streams.Readable, nameHint: string, autoGuessEncoding: boolean): TPromise<IMimeAndEncoding> {
return doDetectMimesFromStream(instream, autoGuessEncoding).then(encoding =>
handleMimeResult(nameHint, encoding)
);
}
......@@ -133,8 +133,8 @@ export function detectMimesFromStream(instream: streams.Readable, nameHint: stri
* 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, autoDetectEncoding: boolean): TPromise<IMimeAndEncoding> {
return doDetectMimesFromFile(absolutePath, autoDetectEncoding).then(encoding =>
export function detectMimesFromFile(absolutePath: string, autoGuessEncoding: boolean): TPromise<IMimeAndEncoding> {
return doDetectMimesFromFile(absolutePath, autoGuessEncoding).then(encoding =>
handleMimeResult(absolutePath, encoding)
);
}
......
......@@ -61,7 +61,7 @@ suite('Mime', () => {
}, done);
});
test('autoDetectEncoding (ShiftJIS)', function (done: () => void) {
test('autoGuessEncoding (ShiftJIS)', function (done: () => void) {
const file = require.toUrl('./fixtures/some.shiftjis.txt');
mime.detectMimesFromFile(file, true).then(mimes => {
assert.deepEqual(mimes.encoding, 'SHIFT_JIS');
......
......@@ -575,7 +575,7 @@ export interface IFilesConfiguration {
exclude: glob.IExpression;
watcherExclude: { [filepattern: string]: boolean };
encoding: string;
autoDetectEncoding: boolean;
autoGuessEncoding: boolean;
trimTrailingWhitespace: boolean;
autoSave: string;
autoSaveDelay: number;
......
......@@ -240,7 +240,7 @@ const configurationValueWhitelist = [
'editor.acceptSuggestionOnCommitCharacter',
'workbench.editor.showTabs',
'files.encoding',
'files.autoDetectEncoding',
'files.autoGuessEncoding',
'editor.quickSuggestionsDelay',
'editor.snippetSuggestions',
'editor.selectionHighlight',
......
......@@ -212,10 +212,10 @@ configurationRegistry.registerConfiguration({
'default': 'utf8',
'description': nls.localize('encoding', "The default character set encoding to use when reading and writing files."),
},
'files.autoDetectEncoding': {
'files.autoGuessEncoding': {
'type': 'boolean',
'default': false,
'description': nls.localize('autoDetetEncoding', "whem enabled, will use detected encoding in opening a text file.")
'description': nls.localize('autoGuessEncoding', "When enabled, will attempt to guess the character set encoding when opening files")
},
'files.eol': {
'type': 'string',
......
......@@ -81,7 +81,7 @@ export class FileService implements IFileService {
const fileServiceConfig: IFileServiceOptions = {
errorLogger: (msg: string) => this.onFileServiceError(msg),
encoding: configuration.files && configuration.files.encoding,
autoDetectEncoding: configuration.files && configuration.files.autoDetectEncoding,
autoGuessEncoding: configuration.files && configuration.files.autoGuessEncoding,
encodingOverride,
watcherIgnoredPatterns,
verboseLogging: environmentService.verbose,
......
......@@ -44,7 +44,7 @@ export interface IFileServiceOptions {
tmpDir?: string;
errorLogger?: (msg: string) => void;
encoding?: string;
autoDetectEncoding?: boolean;
autoGuessEncoding?: boolean;
bom?: string;
encodingOverride?: IEncodingOverride[];
watcherIgnoredPatterns?: string[];
......@@ -206,7 +206,7 @@ export class FileService implements IFileService {
}
// 2.) detect mimes
return mime.detectMimesFromFile(absolutePath, this.options.autoDetectEncoding).then((detected: mime.IMimeAndEncoding) => {
return mime.detectMimesFromFile(absolutePath, this.options.autoGuessEncoding).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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册