Skip to content

Commit

Permalink
fix: defineConfigObject
Browse files Browse the repository at this point in the history
  • Loading branch information
kermanx committed Oct 26, 2024
1 parent 1aaa64a commit 1ea054b
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions packages/core/src/utils/defineConfigObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<C extends object> = UnwrapNestedRefs<C> & {
Expand All @@ -27,17 +26,66 @@ export type ConfigObject<C extends object> = UnwrapNestedRefs<C> & {
* @category lifecycle
*/
export function defineConfigObject<const C extends ConfigTypeOptions>(section: Nullable<string>, configs: C, scope?: Nullable<ConfigurationScope>): ConfigObject<ParseConfigTypeOptions<C>>
export function defineConfigObject<C extends object>(section: Nullable<string>, configs: C, scope?: Nullable<ConfigurationScope>): ConfigObject<C>
export function defineConfigObject<C extends object>(section: Nullable<string>, configs: Record<string, any>, scope?: Nullable<ConfigurationScope>): ConfigObject<C>
export function defineConfigObject(section: Nullable<string>, configs: Record<string, unknown>, scope?: Nullable<ConfigurationScope>) {
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<typeof configs>
}
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
}

0 comments on commit 1ea054b

Please sign in to comment.