UNPKG

4.85 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2020 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17/*---------------------------------------------------------------------------------------------
18 * Copyright (c) Microsoft Corporation. All rights reserved.
19 * Licensed under the MIT License. See License.txt in the project root for license information.
20 *--------------------------------------------------------------------------------------------*/
21var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
22 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
23 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
24 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
25 return c > 3 && r && Object.defineProperty(target, key, r), r;
26};
27var __metadata = (this && this.__metadata) || function (k, v) {
28 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
29};
30Object.defineProperty(exports, "__esModule", { value: true });
31exports.EncodingRegistry = void 0;
32// based on https://github.com/microsoft/vscode/blob/04c36be045a94fee58e5f8992d3e3fd980294a84/src/vs/workbench/services/textfile/browser/textFileService.ts#L491
33const inversify_1 = require("inversify");
34const disposable_1 = require("../common/disposable");
35const core_preferences_1 = require("./core-preferences");
36const encoding_service_1 = require("../common/encoding-service");
37const encodings_1 = require("../common/encodings");
38let EncodingRegistry = class EncodingRegistry {
39 constructor() {
40 this.encodingOverrides = [];
41 }
42 registerOverride(override) {
43 this.encodingOverrides.push(override);
44 return disposable_1.Disposable.create(() => {
45 const index = this.encodingOverrides.indexOf(override);
46 if (index !== -1) {
47 this.encodingOverrides.splice(index, 1);
48 }
49 });
50 }
51 getEncodingForResource(resource, preferredEncoding) {
52 let fileEncoding;
53 const override = this.getEncodingOverride(resource);
54 if (override) {
55 fileEncoding = override; // encoding override always wins
56 }
57 else if (preferredEncoding) {
58 fileEncoding = preferredEncoding; // preferred encoding comes second
59 }
60 else {
61 fileEncoding = this.preferences.get('files.encoding', undefined, resource.toString());
62 }
63 if (!fileEncoding || !this.encodingService.exists(fileEncoding)) {
64 return encodings_1.UTF8; // the default is UTF 8
65 }
66 return this.encodingService.toIconvEncoding(fileEncoding);
67 }
68 getEncodingOverride(resource) {
69 if (this.encodingOverrides && this.encodingOverrides.length) {
70 for (const override of this.encodingOverrides) {
71 if (override.parent && resource.isEqualOrParent(override.parent)) {
72 return override.encoding;
73 }
74 if (override.extension && resource.path.ext === `.${override.extension}`) {
75 return override.encoding;
76 }
77 if (override.scheme && override.scheme === resource.scheme) {
78 return override.encoding;
79 }
80 }
81 }
82 return undefined;
83 }
84};
85__decorate([
86 (0, inversify_1.inject)(core_preferences_1.CorePreferences),
87 __metadata("design:type", Object)
88], EncodingRegistry.prototype, "preferences", void 0);
89__decorate([
90 (0, inversify_1.inject)(encoding_service_1.EncodingService),
91 __metadata("design:type", encoding_service_1.EncodingService)
92], EncodingRegistry.prototype, "encodingService", void 0);
93EncodingRegistry = __decorate([
94 (0, inversify_1.injectable)()
95], EncodingRegistry);
96exports.EncodingRegistry = EncodingRegistry;
97//# sourceMappingURL=encoding-registry.js.map
\No newline at end of file