Skip to content

Commit 4e4941b

Browse files
gyzerokBenjamin Pasero
andauthored
fix microsoft#79275 enable encodings for web (microsoft#101706)
* fix microsoft#79275 enable encodings for web * fix one more place and prettify object * 💄 Co-authored-by: Benjamin Pasero <benjpas@microsoft.com>
1 parent ce14627 commit 4e4941b

7 files changed

Lines changed: 266 additions & 296 deletions

File tree

extensions/vscode-web-playground/src/exampleFiles.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,9 @@ export function getImageFile(): Uint8Array {
302302
const data = atob(`/9j/4AAQSkZJRgABAQAASABIAAD/2wCEAA4ODg4ODhcODhchFxcXIS0hISEhLTktLS0tLTlFOTk5OTk5RUVFRUVFRUVSUlJSUlJgYGBgYGxsbGxsbGxsbGwBERISGxkbLxkZL3FMP0xxcXFxcXFxcXFxcXFxcXFxcXFxcXFxcXFxcXFxcXFxcXFxcXFxcXFxcXFxcXFxcXFxcf/AABEIAFYAZAMBIgACEQEDEQH/xAB1AAACAwEBAQAAAAAAAAAAAAAABAMFBgIBBxAAAgIBAwMCBQQCAwAAAAAAAQIAAxEEBSESMUFRcRMiIzJhFIGRoQbBQlKxAQEBAQEAAAAAAAAAAAAAAAABAgADEQEBAQADAQEAAAAAAAAAAAAAARESITECQf/aAAwDAQACEQMRAD8A2LEZkLc/bKxbdYEHWoyfEze56zXpqRTTYUyPHiVrY2TVZyMzhFZMg8iYE6jcVXAusY98KMnj2lhRu+4aLoGuTNTYPV5APnyDNyPFp6EY3EsO3kxnVVLZVg8z2tw9YsXkGQpcbGIbxHQzep0vw8Jgc8n28CJJRY30lBwzf1iaa2ku/HmMV01VW/k/6hh0abTDTafpPcTytmckEewjeosAqJEj0yDo6yO/rFLzoGME5nIAXtGSM9uwnjLn8zFECw7QneITMWouR7gj9/Ep94061bjXa32WDGfzOGuCXKy9/wDc0FlFe5aX4OpHJHBHcSfT4w246bWJar6MsCwKnp9DOF0r6XRiu5snvg9hNK217vQeih0tXwzcED895R7voNfWoN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iKP8SnKg5U9xHQwsGV7jxF9QnWACMEcgwlUjT4ZUE+YRRLGRehwciEpLRMAAT6SALlIQkF4kl7HEIQLwuQfac9RPeEJi5H3TruvvmEJo1QOcgGQuvVg+sITM8rDKeDHVItXkQhKgqM6esnJEIQlJf//Z`);
303303
return Uint8Array.from([...data].map(x => x.charCodeAt(0)));
304304
}
305+
306+
// encoded from 'АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя'
307+
export const windows1251File = Uint8Array.from([192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]);
308+
309+
// encoded from '中国abc'
310+
export const gbkFile = Uint8Array.from([214, 208, 185, 250, 97, 98, 99]);

extensions/vscode-web-playground/src/memfs.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
Uri,
3030
workspace,
3131
} from 'vscode';
32-
import { largeTSFile, getImageFile, debuggableFile } from './exampleFiles';
32+
import { largeTSFile, getImageFile, debuggableFile, windows1251File, gbkFile } from './exampleFiles';
3333

3434
export class File implements FileStat {
3535

@@ -123,6 +123,19 @@ export class MemFS implements FileSystemProvider, FileSearchProvider, TextSearch
123123
this.writeFile(Uri.parse(`memfs:/sample-folder/xyz/UPPER.txt`), textEncoder.encode('UPPER'), { create: true, overwrite: true });
124124
this.writeFile(Uri.parse(`memfs:/sample-folder/xyz/upper.txt`), textEncoder.encode('upper'), { create: true, overwrite: true });
125125
this.writeFile(Uri.parse(`memfs:/sample-folder/xyz/def/foo.md`), textEncoder.encode('*MemFS*'), { create: true, overwrite: true });
126+
127+
// some files in different encodings
128+
this.createDirectory(Uri.parse(`memfs:/sample-folder/encodings/`));
129+
this.writeFile(
130+
Uri.parse(`memfs:/sample-folder/encodings/windows1251.txt`),
131+
windows1251File,
132+
{ create: true, overwrite: true }
133+
);
134+
this.writeFile(
135+
Uri.parse(`memfs:/sample-folder/encodings/gbk.txt`),
136+
gbkFile,
137+
{ create: true, overwrite: true }
138+
);
126139
}
127140

128141
root = new Directory(Uri.parse('memfs:/'), '');

src/vs/workbench/browser/parts/editor/editor.contribution.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { UntitledTextEditorInput } from 'vs/workbench/services/untitled/common/u
1515
import { ResourceEditorInput } from 'vs/workbench/common/editor/resourceEditorInput';
1616
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
1717
import { TextDiffEditor } from 'vs/workbench/browser/parts/editor/textDiffEditor';
18-
import { SUPPORTED_ENCODINGS } from 'vs/workbench/services/textfile/common/textfiles';
1918
import { BinaryResourceDiffEditor } from 'vs/workbench/browser/parts/editor/binaryDiffEditor';
2019
import { ChangeEncodingAction, ChangeEOLAction, ChangeModeAction, EditorStatus } from 'vs/workbench/browser/parts/editor/editorStatus';
2120
import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actions';
@@ -276,10 +275,7 @@ Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).regi
276275
const registry = Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions);
277276
registry.registerWorkbenchAction(SyncActionDescriptor.from(ChangeModeAction, { primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyCode.KEY_M) }), 'Change Language Mode');
278277
registry.registerWorkbenchAction(SyncActionDescriptor.from(ChangeEOLAction), 'Change End of Line Sequence');
279-
280-
if (Object.keys(SUPPORTED_ENCODINGS).length > 1) {
281-
registry.registerWorkbenchAction(SyncActionDescriptor.from(ChangeEncodingAction), 'Change File Encoding');
282-
}
278+
registry.registerWorkbenchAction(SyncActionDescriptor.from(ChangeEncodingAction), 'Change File Encoding');
283279

284280
// Register Editor Quick Access
285281
const quickAccessRegistry = Registry.as<IQuickAccessRegistry>(QuickAccessExtensions.Quickaccess);

src/vs/workbench/browser/parts/editor/editorStatus.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export class EditorStatus extends Disposable implements IWorkbenchContribution {
296296
private readonly screenRedearModeElement = this._register(new MutableDisposable<IStatusbarEntryAccessor>());
297297
private readonly indentationElement = this._register(new MutableDisposable<IStatusbarEntryAccessor>());
298298
private readonly selectionElement = this._register(new MutableDisposable<IStatusbarEntryAccessor>());
299-
private readonly encodingElement = Object.keys(SUPPORTED_ENCODINGS).length > 1 ? this._register(new MutableDisposable<IStatusbarEntryAccessor>()) : undefined;
299+
private readonly encodingElement = this._register(new MutableDisposable<IStatusbarEntryAccessor>());
300300
private readonly eolElement = this._register(new MutableDisposable<IStatusbarEntryAccessor>());
301301
private readonly modeElement = this._register(new MutableDisposable<IStatusbarEntryAccessor>());
302302
private readonly metadataElement = this._register(new MutableDisposable<IStatusbarEntryAccessor>());
@@ -483,10 +483,6 @@ export class EditorStatus extends Disposable implements IWorkbenchContribution {
483483
}
484484

485485
private updateEncodingElement(text: string | undefined): void {
486-
if (!this.encodingElement) {
487-
return; // return early if encoding should not show (e.g. in Web we only support utf8)
488-
}
489-
490486
if (!text) {
491487
this.encodingElement.clear();
492488
return;

src/vs/workbench/contrib/files/browser/files.contribution.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,13 @@ configurationRegistry.registerConfiguration({
248248
'default': 'utf8',
249249
'description': nls.localize('encoding', "The default character set encoding to use when reading and writing files. This setting can also be configured per language."),
250250
'scope': ConfigurationScope.LANGUAGE_OVERRIDABLE,
251-
'enumDescriptions': Object.keys(SUPPORTED_ENCODINGS).map(key => SUPPORTED_ENCODINGS[key].labelLong),
252-
'included': Object.keys(SUPPORTED_ENCODINGS).length > 1
251+
'enumDescriptions': Object.keys(SUPPORTED_ENCODINGS).map(key => SUPPORTED_ENCODINGS[key].labelLong)
253252
},
254253
'files.autoGuessEncoding': {
255254
'type': 'boolean',
256255
'default': false,
257256
'description': nls.localize('autoGuessEncoding', "When enabled, the editor will attempt to guess the character set encoding when opening files. This setting can also be configured per language."),
258-
'scope': ConfigurationScope.LANGUAGE_OVERRIDABLE,
259-
'included': Object.keys(SUPPORTED_ENCODINGS).length > 1
257+
'scope': ConfigurationScope.LANGUAGE_OVERRIDABLE
260258
},
261259
'files.eol': {
262260
'type': 'string',

src/vs/workbench/services/textfile/browser/browserTextFileService.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,13 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { AbstractTextFileService, EncodingOracle } from 'vs/workbench/services/textfile/browser/textFileService';
7-
import { ITextFileService, IResourceEncoding, TextFileEditorModelState } from 'vs/workbench/services/textfile/common/textfiles';
6+
import { AbstractTextFileService } from 'vs/workbench/services/textfile/browser/textFileService';
7+
import { ITextFileService, TextFileEditorModelState } from 'vs/workbench/services/textfile/common/textfiles';
88
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
99
import { ShutdownReason } from 'vs/platform/lifecycle/common/lifecycle';
1010

1111
export class BrowserTextFileService extends AbstractTextFileService {
1212

13-
private _browserEncoding: EncodingOracle | undefined;
14-
15-
get encoding(): EncodingOracle {
16-
if (!this._browserEncoding) {
17-
this._browserEncoding = this._register(this.instantiationService.createInstance(BrowserEncodingOracle));
18-
}
19-
20-
return this._browserEncoding;
21-
}
22-
2313
protected registerListeners(): void {
2414
super.registerListeners();
2515

@@ -38,18 +28,4 @@ export class BrowserTextFileService extends AbstractTextFileService {
3828
}
3929
}
4030

41-
class BrowserEncodingOracle extends EncodingOracle {
42-
async getPreferredWriteEncoding(): Promise<IResourceEncoding> {
43-
return { encoding: 'utf8', hasBOM: false };
44-
}
45-
46-
async getWriteEncoding(): Promise<{ encoding: string, addBOM: boolean }> {
47-
return { encoding: 'utf8', addBOM: false };
48-
}
49-
50-
async getReadEncoding(): Promise<string> {
51-
return 'utf8';
52-
}
53-
}
54-
5531
registerSingleton(ITextFileService, BrowserTextFileService);

0 commit comments

Comments
 (0)