UNPKG

@difizen/mana-core

Version:

500 lines (496 loc) 23.9 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /* eslint-disable @typescript-eslint/no-use-before-define */ /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { Emitter, types } from '@difizen/mana-common'; import * as nls from "../../../nls"; import { Extensions as JSONExtensions } from "../jsonContributionRegistry"; import { Registry } from "../platform"; export var Extensions = { Configuration: 'base.contributions.configuration' }; export var ConfigurationScope = /*#__PURE__*/function (ConfigurationScope) { /** * Application specific configuration, which can be configured only in local user settings. */ ConfigurationScope[ConfigurationScope["APPLICATION"] = 1] = "APPLICATION"; /** * Machine specific configuration, which can be configured only in local and remote user settings. */ ConfigurationScope[ConfigurationScope["MACHINE"] = 2] = "MACHINE"; /** * Window specific configuration, which can be configured in the user or workspace settings. */ ConfigurationScope[ConfigurationScope["WINDOW"] = 3] = "WINDOW"; /** * Resource specific configuration, which can be configured in the user, workspace or folder settings. */ ConfigurationScope[ConfigurationScope["RESOURCE"] = 4] = "RESOURCE"; /** * Resource specific configuration that can be configured in language specific settings */ ConfigurationScope[ConfigurationScope["LANGUAGE_OVERRIDABLE"] = 5] = "LANGUAGE_OVERRIDABLE"; /** * Machine specific configuration that can also be configured in workspace or folder settings. */ ConfigurationScope[ConfigurationScope["MACHINE_OVERRIDABLE"] = 6] = "MACHINE_OVERRIDABLE"; return ConfigurationScope; }({}); export var allSettings = { properties: {}, patternProperties: {} }; export var applicationSettings = { properties: {}, patternProperties: {} }; export var machineSettings = { properties: {}, patternProperties: {} }; export var machineOverridableSettings = { properties: {}, patternProperties: {} }; export var windowSettings = { properties: {}, patternProperties: {} }; export var resourceSettings = { properties: {}, patternProperties: {} }; export var resourceLanguageSettingsSchemaId = 'vscode://schemas/settings/resourceLanguage'; var contributionRegistry = Registry.as(JSONExtensions.JSONContribution); var ConfigurationRegistry = /*#__PURE__*/function () { function ConfigurationRegistry() { _classCallCheck(this, ConfigurationRegistry); this.overrideIdentifiers = new Set(); this._onDidSchemaChange = new Emitter(); this.onDidSchemaChange = this._onDidSchemaChange.event; this._onDidUpdateConfiguration = new Emitter(); this.onDidUpdateConfiguration = this._onDidUpdateConfiguration.event; this.defaultValues = {}; this.defaultLanguageConfigurationOverridesNode = { id: 'defaultOverrides', title: nls.localize('defaultLanguageConfigurationOverrides.title', 'Default Language Configuration Overrides'), properties: {} }; this.configurationContributors = [this.defaultLanguageConfigurationOverridesNode]; this.resourceLanguageSettingsSchema = { properties: {}, patternProperties: {}, additionalProperties: false, errorMessage: 'Unknown editor configuration setting', allowTrailingCommas: true, allowComments: true }; this.configurationProperties = {}; this.excludedConfigurationProperties = {}; contributionRegistry.registerSchema(resourceLanguageSettingsSchemaId, this.resourceLanguageSettingsSchema); } _createClass(ConfigurationRegistry, [{ key: "registerConfiguration", value: function registerConfiguration(configuration) { var validate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; this.registerConfigurations([configuration], validate); } }, { key: "registerConfigurations", value: function registerConfigurations(configurations) { var _this = this; var validate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; var properties = []; configurations.forEach(function (configuration) { properties.push.apply(properties, _toConsumableArray(_this.validateAndRegisterProperties(configuration, validate))); // fills in defaults _this.configurationContributors.push(configuration); _this.registerJSONConfiguration(configuration); }); contributionRegistry.registerSchema(resourceLanguageSettingsSchemaId, this.resourceLanguageSettingsSchema); this._onDidSchemaChange.fire(); this._onDidUpdateConfiguration.fire(properties); } }, { key: "deregisterConfigurations", value: function deregisterConfigurations(configurations) { var _this2 = this; var properties = []; var deregisterConfiguration = function deregisterConfiguration(configuration) { if (configuration.properties) { for (var key in configuration.properties) { properties.push(key); delete _this2.configurationProperties[key]; _this2.removeFromSchema(key, configuration.properties[key]); } } if (configuration.allOf) { configuration.allOf.forEach(function (node) { return deregisterConfiguration(node); }); } }; var _iterator = _createForOfIteratorHelper(configurations), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var _configuration = _step.value; deregisterConfiguration(_configuration); var index = this.configurationContributors.indexOf(_configuration); if (index !== -1) { this.configurationContributors.splice(index, 1); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } contributionRegistry.registerSchema(resourceLanguageSettingsSchemaId, this.resourceLanguageSettingsSchema); this._onDidSchemaChange.fire(); this._onDidUpdateConfiguration.fire(properties); } }, { key: "registerDefaultConfigurations", value: function registerDefaultConfigurations(defaultConfigurations) { var properties = []; var overrideIdentifiers = []; var _iterator2 = _createForOfIteratorHelper(defaultConfigurations), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var defaultConfiguration = _step2.value; for (var key in defaultConfiguration) { properties.push(key); if (OVERRIDE_PROPERTY_PATTERN.test(key)) { this.defaultValues[key] = _objectSpread(_objectSpread({}, this.defaultValues[key] || {}), defaultConfiguration[key]); var property = { type: 'object', default: this.defaultValues[key], description: nls.localize('defaultLanguageConfiguration.description', 'Configure settings to be overridden for {0} language.', key), $ref: resourceLanguageSettingsSchemaId }; overrideIdentifiers.push(overrideIdentifierFromKey(key)); this.configurationProperties[key] = property; this.defaultLanguageConfigurationOverridesNode.properties[key] = property; } else { this.defaultValues[key] = defaultConfiguration[key]; var _property = this.configurationProperties[key]; if (_property) { this.updatePropertyDefaultValue(key, _property); this.updateSchema(key, _property); } } } } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } this.registerOverrideIdentifiers(overrideIdentifiers); this._onDidSchemaChange.fire(); this._onDidUpdateConfiguration.fire(properties); } }, { key: "deregisterDefaultConfigurations", value: function deregisterDefaultConfigurations(defaultConfigurations) { var properties = []; var _iterator3 = _createForOfIteratorHelper(defaultConfigurations), _step3; try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { var defaultConfiguration = _step3.value; for (var key in defaultConfiguration) { properties.push(key); delete this.defaultValues[key]; if (OVERRIDE_PROPERTY_PATTERN.test(key)) { delete this.configurationProperties[key]; delete this.defaultLanguageConfigurationOverridesNode.properties[key]; } else { var property = this.configurationProperties[key]; if (property) { this.updatePropertyDefaultValue(key, property); this.updateSchema(key, property); } } } } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } this.updateOverridePropertyPatternKey(); this._onDidSchemaChange.fire(); this._onDidUpdateConfiguration.fire(properties); } }, { key: "notifyConfigurationSchemaUpdated", value: function notifyConfigurationSchemaUpdated() { this._onDidSchemaChange.fire(); } }, { key: "registerOverrideIdentifiers", value: function registerOverrideIdentifiers(overrideIdentifiers) { var _iterator4 = _createForOfIteratorHelper(overrideIdentifiers), _step4; try { for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) { var overrideIdentifier = _step4.value; this.overrideIdentifiers.add(overrideIdentifier); } } catch (err) { _iterator4.e(err); } finally { _iterator4.f(); } this.updateOverridePropertyPatternKey(); } }, { key: "validateAndRegisterProperties", value: function validateAndRegisterProperties(configuration) { var validate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; var scope = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ConfigurationScope.WINDOW; scope = types.isUndefinedOrNull(configuration.scope) ? scope : configuration.scope; var propertyKeys = []; var properties = configuration.properties; if (properties) { for (var key in properties) { if (validate && validateProperty(key)) { delete properties[key]; continue; } var property = properties[key]; // update default value this.updatePropertyDefaultValue(key, property); // update scope if (OVERRIDE_PROPERTY_PATTERN.test(key)) { property.scope = undefined; // No scope for overridable properties `[${identifier}]` } else { property.scope = types.isUndefinedOrNull(property.scope) ? scope : property.scope; } // Add to properties maps // Property is included by default if 'included' is unspecified if (Object.prototype.hasOwnProperty.call(properties[key], 'included') && !properties[key].included) { this.excludedConfigurationProperties[key] = properties[key]; delete properties[key]; continue; } else { this.configurationProperties[key] = properties[key]; } if (!properties[key].deprecationMessage && properties[key].markdownDeprecationMessage) { // If not set, default deprecationMessage to the markdown source properties[key].deprecationMessage = properties[key].markdownDeprecationMessage; } propertyKeys.push(key); } } var subNodes = configuration.allOf; if (subNodes) { var _iterator5 = _createForOfIteratorHelper(subNodes), _step5; try { for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) { var node = _step5.value; propertyKeys.push.apply(propertyKeys, _toConsumableArray(this.validateAndRegisterProperties(node, validate, scope))); } } catch (err) { _iterator5.e(err); } finally { _iterator5.f(); } } return propertyKeys; } }, { key: "getConfigurations", value: function getConfigurations() { return this.configurationContributors; } }, { key: "getConfigurationProperties", value: function getConfigurationProperties() { return this.configurationProperties; } }, { key: "getExcludedConfigurationProperties", value: function getExcludedConfigurationProperties() { return this.excludedConfigurationProperties; } }, { key: "registerJSONConfiguration", value: function registerJSONConfiguration(configuration) { var _this3 = this; var register = function register(configuration) { var properties = configuration.properties; if (properties) { for (var key in properties) { _this3.updateSchema(key, properties[key]); } } var subNodes = configuration.allOf; if (subNodes) { subNodes.forEach(register); } }; register(configuration); } }, { key: "updateSchema", value: function updateSchema(key, property) { allSettings.properties[key] = property; switch (property.scope) { case ConfigurationScope.APPLICATION: applicationSettings.properties[key] = property; break; case ConfigurationScope.MACHINE: machineSettings.properties[key] = property; break; case ConfigurationScope.MACHINE_OVERRIDABLE: machineOverridableSettings.properties[key] = property; break; case ConfigurationScope.WINDOW: windowSettings.properties[key] = property; break; case ConfigurationScope.RESOURCE: resourceSettings.properties[key] = property; break; case ConfigurationScope.LANGUAGE_OVERRIDABLE: resourceSettings.properties[key] = property; this.resourceLanguageSettingsSchema.properties[key] = property; break; } } }, { key: "removeFromSchema", value: function removeFromSchema(key, property) { delete allSettings.properties[key]; switch (property.scope) { case ConfigurationScope.APPLICATION: delete applicationSettings.properties[key]; break; case ConfigurationScope.MACHINE: delete machineSettings.properties[key]; break; case ConfigurationScope.MACHINE_OVERRIDABLE: delete machineOverridableSettings.properties[key]; break; case ConfigurationScope.WINDOW: delete windowSettings.properties[key]; break; case ConfigurationScope.RESOURCE: case ConfigurationScope.LANGUAGE_OVERRIDABLE: delete resourceSettings.properties[key]; break; } } }, { key: "updateOverridePropertyPatternKey", value: function updateOverridePropertyPatternKey() { var _iterator6 = _createForOfIteratorHelper(this.overrideIdentifiers.values()), _step6; try { for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) { var overrideIdentifier = _step6.value; var overrideIdentifierProperty = "[".concat(overrideIdentifier, "]"); var resourceLanguagePropertiesSchema = { type: 'object', description: nls.localize('overrideSettings.defaultDescription', 'Configure editor settings to be overridden for a language.'), errorMessage: nls.localize('overrideSettings.errorMessage', 'This setting does not support per-language configuration.'), $ref: resourceLanguageSettingsSchemaId }; this.updatePropertyDefaultValue(overrideIdentifierProperty, resourceLanguagePropertiesSchema); allSettings.properties[overrideIdentifierProperty] = resourceLanguagePropertiesSchema; applicationSettings.properties[overrideIdentifierProperty] = resourceLanguagePropertiesSchema; machineSettings.properties[overrideIdentifierProperty] = resourceLanguagePropertiesSchema; machineOverridableSettings.properties[overrideIdentifierProperty] = resourceLanguagePropertiesSchema; windowSettings.properties[overrideIdentifierProperty] = resourceLanguagePropertiesSchema; resourceSettings.properties[overrideIdentifierProperty] = resourceLanguagePropertiesSchema; } } catch (err) { _iterator6.e(err); } finally { _iterator6.f(); } this._onDidSchemaChange.fire(); } }, { key: "updatePropertyDefaultValue", value: function updatePropertyDefaultValue(key, property) { var defaultValue = this.defaultValues[key]; if (types.isUndefined(defaultValue)) { defaultValue = property.default; } if (types.isUndefined(defaultValue)) { defaultValue = getDefaultValue(property.type); } property.default = defaultValue; } }]); return ConfigurationRegistry; }(); var OVERRIDE_PROPERTY = '\\[.*\\]$'; export var OVERRIDE_PROPERTY_PATTERN = new RegExp(OVERRIDE_PROPERTY); export function overrideIdentifierFromKey(key) { return key.substring(1, key.length - 1); } export function getDefaultValue(type) { var t = Array.isArray(type) ? type[0] : type; switch (t) { case 'boolean': return false; case 'integer': case 'number': return 0; case 'string': return ''; case 'array': return []; case 'object': return {}; default: return null; } } var configurationRegistry = new ConfigurationRegistry(); Registry.add(Extensions.Configuration, configurationRegistry); export function validateProperty(property) { if (!property.trim()) { return nls.localize('config.property.empty', 'Cannot register an empty property'); } if (OVERRIDE_PROPERTY_PATTERN.test(property)) { return nls.localize('config.property.languageDefault', "Cannot register '{0}'. This matches property pattern '\\\\[.*\\\\]$' for describing language specific editor settings. Use 'configurationDefaults' contribution.", property); } if (configurationRegistry.getConfigurationProperties()[property] !== undefined) { return nls.localize('config.property.duplicate', "Cannot register '{0}'. This property is already registered.", property); } return null; } export function getScopes() { var scopes = []; var configurationProperties = configurationRegistry.getConfigurationProperties(); for (var _i = 0, _Object$keys = Object.keys(configurationProperties); _i < _Object$keys.length; _i++) { var key = _Object$keys[_i]; scopes.push([key, configurationProperties[key].scope]); } scopes.push(['launch', ConfigurationScope.RESOURCE]); scopes.push(['task', ConfigurationScope.RESOURCE]); return scopes; }