From 1ea054b8607a5731740799c93d2231be9dd7fc83 Mon Sep 17 00:00:00 2001 From: _Kerman Date: Sat, 26 Oct 2024 19:06:11 +0800 Subject: [PATCH] fix: `defineConfigObject` --- packages/core/src/utils/defineConfigObject.ts | 58 +++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/packages/core/src/utils/defineConfigObject.ts b/packages/core/src/utils/defineConfigObject.ts index e726fa5..a95d609 100644 --- a/packages/core/src/utils/defineConfigObject.ts +++ b/packages/core/src/utils/defineConfigObject.ts @@ -2,7 +2,6 @@ import type { UnwrapNestedRefs } from '@reactive-vscode/reactivity' import type { ConfigurationScope, ConfigurationTarget } from 'vscode' import type { ConfigTypeOptions, ParseConfigTypeOptions } from './defineConfigs' import type { Nullable } from './types' -import { reactive } from '@reactive-vscode/reactivity' import { defineConfigs } from './defineConfigs' export type ConfigObject = UnwrapNestedRefs & { @@ -27,17 +26,66 @@ export type ConfigObject = UnwrapNestedRefs & { * @category lifecycle */ export function defineConfigObject(section: Nullable, configs: C, scope?: Nullable): ConfigObject> -export function defineConfigObject(section: Nullable, configs: C, scope?: Nullable): ConfigObject +export function defineConfigObject(section: Nullable, configs: Record, scope?: Nullable): ConfigObject export function defineConfigObject(section: Nullable, configs: Record, scope?: Nullable) { const configRefs = defineConfigs(section, configs, scope) - return reactive({ - ...configRefs, + const nestedKeys: any = {} + const rawData: any = { $update(key: string, value: any, configurationTarget: any, overrideInLanguage: any) { return configRefs[key].update(value, configurationTarget, overrideInLanguage) }, $set(key: string, value: any) { return configRefs[key].set(value) }, - }) satisfies ConfigObject + } + for (const key in configs) { + const path = key.split('.') + let prefix = '' + let targetKeys = nestedKeys + let targetData = rawData + for (const p of path.slice(0, -1)) { + prefix = prefix ? `${prefix}.${p}` : p + const keys = targetKeys = targetKeys[p] ||= {} + if (targetData[p]) { + targetData = targetData[p] + } + else { + const innerData = {} + Object.defineProperty(targetData, p, { + enumerable: true, + get() { + return innerData + }, + set(newVal) { + function update(keys: any, path: string, data: any) { + if (keys) { + for (const key in keys) { + update(keys[key], path ? `${path}.${key}` : key, data?.[key]) + } + } + else { + configRefs[path].value = data + } + } + update(keys, prefix, newVal) + }, + }) + targetData = innerData + } + } + const lastPart = path[path.length - 1] + targetKeys[lastPart] = null + Object.defineProperty(targetData, lastPart, { + enumerable: true, + get() { + return configRefs[key].value + }, + set(newVal: any) { + configRefs[key].value = newVal + }, + }) + } + + return rawData }