Skip to content

Commit 50f4768

Browse files
committed
feat: support top-level config
1 parent 6d4ac4c commit 50f4768

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

demo/src/configs.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { defineConfigs } from 'reactive-vscode'
22

3+
// Scoped config
34
export const { message } = defineConfigs('reactive-vscode-demo', {
4-
message: 'string',
5+
message: String,
6+
})
7+
8+
// Top-level config
9+
export const { 'editor.fontSize': fontSize } = defineConfigs(null, {
10+
'editor.fontSize': Number,
511
})

packages/core/src/utils/defineConfigObject.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ export type ConfigObject<C extends object> = ShallowReactive<C & {
2525
*
2626
* @category lifecycle
2727
*/
28-
export function defineConfigObject<const C extends ConfigTypeOptions>(section: string, configs: C, scope?: Nullable<ConfigurationScope>): ConfigObject<ParseConfigTypeOptions<C>>
29-
export function defineConfigObject<C extends object>(section: string, configs: C, scope?: Nullable<ConfigurationScope>): ConfigObject<C>
30-
export function defineConfigObject(section: string, configs: Record<string, unknown>, scope?: Nullable<ConfigurationScope>) {
28+
export function defineConfigObject<const C extends ConfigTypeOptions>(section: Nullable<string>, configs: C, scope?: Nullable<ConfigurationScope>): ConfigObject<ParseConfigTypeOptions<C>>
29+
export function defineConfigObject<C extends object>(section: Nullable<string>, configs: C, scope?: Nullable<ConfigurationScope>): ConfigObject<C>
30+
export function defineConfigObject(section: Nullable<string>, configs: Record<string, unknown>, scope?: Nullable<ConfigurationScope>) {
3131
const configRefs = defineConfigs(section, configs, scope)
3232

3333
return shallowReactive({

packages/core/src/utils/defineConfigs.ts

+24-13
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ type ToConfigRefs<C extends object> = {
6060
*
6161
* @category lifecycle
6262
*/
63-
export function defineConfigs<const C extends ConfigTypeOptions>(section: string, configs: C, scope?: Nullable<ConfigurationScope>): ToConfigRefs<ParseConfigTypeOptions<C>>
64-
export function defineConfigs<C extends object>(section: string, configs: C, scope?: Nullable<ConfigurationScope>): ToConfigRefs<C>
65-
export function defineConfigs(section: string, configs: object, scope?: Nullable<ConfigurationScope>) {
66-
const workspaceConfig = workspace.getConfiguration(section, scope)
63+
export function defineConfigs<const C extends ConfigTypeOptions>(section: Nullable<string>, configs: C, scope?: Nullable<ConfigurationScope>): ToConfigRefs<ParseConfigTypeOptions<C>>
64+
export function defineConfigs<C extends object>(section: Nullable<string>, configs: C, scope?: Nullable<ConfigurationScope>): ToConfigRefs<C>
65+
export function defineConfigs(section: Nullable<string>, configs: object, scope?: Nullable<ConfigurationScope>) {
66+
const isTopLevel = !section
67+
const workspaceConfig = workspace.getConfiguration(isTopLevel ? undefined : section, scope)
6768

6869
function createConfigRef<T>(key: string, value: T): ConfigRef<T> {
6970
const data = shallowRef(value)
@@ -91,15 +92,25 @@ export function defineConfigs(section: string, configs: object, scope?: Nullable
9192
)
9293

9394
onActivate(() => {
94-
useDisposable(workspace.onDidChangeConfiguration((e) => {
95-
if (!e.affectsConfiguration(section))
96-
return
97-
const newWorkspaceConfig = workspace.getConfiguration(section)
98-
for (const key in configs) {
99-
if (e.affectsConfiguration(`${section}.${key}`))
100-
configRefs[key].set(newWorkspaceConfig.get(key) as any)
101-
}
102-
}))
95+
useDisposable(workspace.onDidChangeConfiguration(
96+
isTopLevel
97+
? (e) => {
98+
const newWorkspaceConfig = workspace.getConfiguration()
99+
for (const key in configs) {
100+
if (e.affectsConfiguration(key))
101+
configRefs[key].set(newWorkspaceConfig.get(key) as any)
102+
}
103+
}
104+
: (e) => {
105+
if (!e.affectsConfiguration(section))
106+
return
107+
const newWorkspaceConfig = workspace.getConfiguration(section)
108+
for (const key in configs) {
109+
if (e.affectsConfiguration(`${section}.${key}`))
110+
configRefs[key].set(newWorkspaceConfig.get(key) as any)
111+
}
112+
},
113+
))
103114
})
104115

105116
return configRefs

0 commit comments

Comments
 (0)