Skip to content

Commit 98ccdc7

Browse files
author
Benjamin Pasero
committed
encoding - only allow to detect UTF8 automatically
1 parent 8878e56 commit 98ccdc7

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

src/vs/base/node/encoding.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ export function detectEncodingByBOMFromBuffer(buffer: Buffer | VSBuffer | null,
199199
return null;
200200
}
201201

202+
// we explicitly ignore a specific set of encodings from auto guessing
203+
// - ASCII: we never want this encoding (most UTF-8 files would happily detect as
204+
// ASCII files and then you could not type non-ASCII characters anymore)
205+
// - UTF-16: we have our own detection logic for UTF-16
206+
// - UTF-32: we do not support this encoding in VSCode
207+
const IGNORE_ENCODINGS = ['ascii', 'utf-16', 'utf-32'];
208+
202209
/**
203210
* Guesses the encoding from buffer.
204211
*/
@@ -210,15 +217,9 @@ async function guessEncodingByBuffer(buffer: Buffer): Promise<string | null> {
210217
return null;
211218
}
212219

213-
// Ignore 'ascii' as guessed encoding because that
214-
// is almost never what we want, rather fallback
215-
// to the configured encoding then. Otherwise,
216-
// opening a ascii-only file with auto guessing
217-
// enabled will put the file into 'ascii' mode
218-
// and thus typing any special characters is
219-
// not possible anymore.
220-
if (guessed.encoding.toLowerCase() === 'ascii') {
221-
return null;
220+
const enc = guessed.encoding.toLowerCase();
221+
if (0 <= IGNORE_ENCODINGS.indexOf(enc)) {
222+
return null; // see comment above why we ignore some encodings
222223
}
223224

224225
return toIconvLiteEncoding(guessed.encoding);

0 commit comments

Comments
 (0)