|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import lodash from 'lodash' |
| 3 | +import { Configuration } from './configurationType' |
| 4 | + |
| 5 | +const settingsToIgnore = [] as Array<keyof Configuration> |
| 6 | + |
| 7 | +export const mergeSettingsFromScopes = ( |
| 8 | + settings: Record<string, any>, |
| 9 | + language: string, |
| 10 | + packageJson: { contributes: { configuration: { properties: Record<string, any> } } }, |
| 11 | +) => { |
| 12 | + const { |
| 13 | + contributes: { |
| 14 | + configuration: { properties }, |
| 15 | + }, |
| 16 | + } = packageJson |
| 17 | + for (const [key, item] of Object.entries(properties)) { |
| 18 | + const isObject = item.type !== 'object' |
| 19 | + if ((isObject && item.type !== 'array') || settingsToIgnore.includes(key as keyof Configuration)) { |
| 20 | + continue |
| 21 | + } |
| 22 | + |
| 23 | + const value = getConfigValueFromAllScopes(key as keyof Configuration, language, isObject ? 'object' : 'array') |
| 24 | + lodash.set(settings, key, value) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +const getConfigValueFromAllScopes = <T extends keyof Configuration>(configKey: T, language: string, type: 'array' | 'object'): Configuration[T] => { |
| 29 | + const values = { ...vscode.workspace.getConfiguration(process.env.IDS_PREFIX, { languageId: language }).inspect<any[]>(configKey)! } |
| 30 | + const userValueKeys = Object.keys(values).filter(key => key.endsWith('Value') && !key.startsWith('default')) |
| 31 | + console.log(userValueKeys) |
| 32 | + for (const key of userValueKeys) { |
| 33 | + if (values[key] !== undefined) { |
| 34 | + continue |
| 35 | + } |
| 36 | + |
| 37 | + values[key] = type === 'array' ? [] : {} |
| 38 | + } |
| 39 | + |
| 40 | + return type === 'array' ? userValueKeys.flatMap(key => values[key]) : Object.assign({}, ...userValueKeys.map(key => values[key])) |
| 41 | +} |
0 commit comments