UNPKG

@sussudio/platform

Version:

Internal APIs for VS Code's service injection the base services.

158 lines (157 loc) 5.25 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import * as types from '@sussudio/base/common/types.mjs'; import { URI } from '@sussudio/base/common/uri.mjs'; import { createDecorator } from '../../instantiation/common/instantiation.mjs'; export const IConfigurationService = createDecorator('configurationService'); export function isConfigurationOverrides(thing) { return ( thing && typeof thing === 'object' && (!thing.overrideIdentifier || typeof thing.overrideIdentifier === 'string') && (!thing.resource || thing.resource instanceof URI) ); } export function isConfigurationUpdateOverrides(thing) { return ( thing && typeof thing === 'object' && (!thing.overrideIdentifiers || Array.isArray(thing.overrideIdentifiers)) && !thing.overrideIdentifier && (!thing.resource || thing.resource instanceof URI) ); } export function ConfigurationTargetToString(configurationTarget) { switch (configurationTarget) { case 1 /* ConfigurationTarget.APPLICATION */: return 'APPLICATION'; case 2 /* ConfigurationTarget.USER */: return 'USER'; case 3 /* ConfigurationTarget.USER_LOCAL */: return 'USER_LOCAL'; case 4 /* ConfigurationTarget.USER_REMOTE */: return 'USER_REMOTE'; case 5 /* ConfigurationTarget.WORKSPACE */: return 'WORKSPACE'; case 6 /* ConfigurationTarget.WORKSPACE_FOLDER */: return 'WORKSPACE_FOLDER'; case 7 /* ConfigurationTarget.DEFAULT */: return 'DEFAULT'; case 8 /* ConfigurationTarget.MEMORY */: return 'MEMORY'; } } export function toValuesTree(properties, conflictReporter) { const root = Object.create(null); for (const key in properties) { addToValueTree(root, key, properties[key], conflictReporter); } return root; } export function addToValueTree(settingsTreeRoot, key, value, conflictReporter) { const segments = key.split('.'); const last = segments.pop(); let curr = settingsTreeRoot; for (let i = 0; i < segments.length; i++) { const s = segments[i]; let obj = curr[s]; switch (typeof obj) { case 'undefined': obj = curr[s] = Object.create(null); break; case 'object': break; default: conflictReporter(`Ignoring ${key} as ${segments.slice(0, i + 1).join('.')} is ${JSON.stringify(obj)}`); return; } curr = obj; } if (typeof curr === 'object' && curr !== null) { try { curr[last] = value; // workaround https://github.com/microsoft/vscode/issues/13606 } catch (e) { conflictReporter(`Ignoring ${key} as ${segments.join('.')} is ${JSON.stringify(curr)}`); } } else { conflictReporter(`Ignoring ${key} as ${segments.join('.')} is ${JSON.stringify(curr)}`); } } export function removeFromValueTree(valueTree, key) { const segments = key.split('.'); doRemoveFromValueTree(valueTree, segments); } function doRemoveFromValueTree(valueTree, segments) { const first = segments.shift(); if (segments.length === 0) { // Reached last segment delete valueTree[first]; return; } if (Object.keys(valueTree).indexOf(first) !== -1) { const value = valueTree[first]; if (typeof value === 'object' && !Array.isArray(value)) { doRemoveFromValueTree(value, segments); if (Object.keys(value).length === 0) { delete valueTree[first]; } } } } /** * A helper function to get the configuration value with a specific settings path (e.g. config.some.setting) */ export function getConfigurationValue(config, settingPath, defaultValue) { function accessSetting(config, path) { let current = config; for (const component of path) { if (typeof current !== 'object' || current === null) { return undefined; } current = current[component]; } return current; } const path = settingPath.split('.'); const result = accessSetting(config, path); return typeof result === 'undefined' ? defaultValue : result; } export function merge(base, add, overwrite) { Object.keys(add).forEach((key) => { if (key !== '__proto__') { if (key in base) { if (types.isObject(base[key]) && types.isObject(add[key])) { merge(base[key], add[key], overwrite); } else if (overwrite) { base[key] = add[key]; } } else { base[key] = add[key]; } } }); } export function getMigratedSettingValue(configurationService, currentSettingName, legacySettingName) { const setting = configurationService.inspect(currentSettingName); const legacySetting = configurationService.inspect(legacySettingName); if ( typeof setting.userValue !== 'undefined' || typeof setting.workspaceValue !== 'undefined' || typeof setting.workspaceFolderValue !== 'undefined' ) { return setting.value; } else if ( typeof legacySetting.userValue !== 'undefined' || typeof legacySetting.workspaceValue !== 'undefined' || typeof legacySetting.workspaceFolderValue !== 'undefined' ) { return legacySetting.value; } else { return setting.defaultValue; } } export function getLanguageTagSettingPlainKey(settingKey) { return settingKey.replace(/[\[\]]/g, ''); }