Skip to content

Commit c58d4d4

Browse files
authored
Merge pull request #17 from KubrickCode/scope-configuration
Improved vscode user-defined scope settings
2 parents 93a3ecf + 177947d commit c58d4d4

10 files changed

Lines changed: 485 additions & 204 deletions

File tree

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
"title": "Open Configuration UI",
5252
"category": "Quick Commands",
5353
"icon": "$(gear)"
54+
},
55+
{
56+
"command": "quickCommandButtons.toggleConfigurationTarget",
57+
"title": "Toggle Configuration Target (Workspace/Global)",
58+
"category": "Quick Commands"
5459
}
5560
],
5661
"viewsContainers": {
@@ -120,6 +125,16 @@
120125
}
121126
}
122127
},
128+
"quickCommandButtons.configurationTarget": {
129+
"type": "string",
130+
"enum": ["workspace", "global"],
131+
"default": "workspace",
132+
"description": "Where to save button configurations: 'workspace' saves to .vscode/settings.json (project-specific), 'global' saves to user settings (shared across all projects)",
133+
"enumDescriptions": [
134+
"Save to workspace settings (.vscode/settings.json) - project-specific commands",
135+
"Save to user settings - shared across all projects"
136+
]
137+
},
123138
"quickCommandButtons.buttons": {
124139
"type": "array",
125140
"default": [
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as vscode from "vscode";
2+
3+
export const CONFIG_SECTION = "quickCommandButtons";
4+
5+
export const CONFIG_KEYS = {
6+
BUTTONS: "buttons",
7+
CONFIGURATION_TARGET: "configurationTarget",
8+
REFRESH_BUTTON: "refreshButton",
9+
} as const;
10+
11+
export const CONFIGURATION_TARGETS = {
12+
WORKSPACE: "workspace",
13+
GLOBAL: "global",
14+
} as const;
15+
16+
export const VS_CODE_CONFIGURATION_TARGETS = {
17+
[CONFIGURATION_TARGETS.WORKSPACE]: vscode.ConfigurationTarget.Workspace,
18+
[CONFIGURATION_TARGETS.GLOBAL]: vscode.ConfigurationTarget.Global,
19+
} as const;
20+
21+
export type ConfigurationTargetType =
22+
(typeof CONFIGURATION_TARGETS)[keyof typeof CONFIGURATION_TARGETS];
23+
export type ConfigKeyType = (typeof CONFIG_KEYS)[keyof typeof CONFIG_KEYS];
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import * as vscode from "vscode";
2+
import { ButtonConfig } from "./types";
3+
import {
4+
CONFIG_SECTION,
5+
CONFIG_KEYS,
6+
CONFIGURATION_TARGETS,
7+
VS_CODE_CONFIGURATION_TARGETS,
8+
ConfigurationTargetType,
9+
} from "./config-constants";
10+
11+
export class ConfigManager {
12+
static getCurrentConfigurationTarget(): ConfigurationTargetType {
13+
const config = vscode.workspace.getConfiguration(CONFIG_SECTION);
14+
return config.get<ConfigurationTargetType>(
15+
CONFIG_KEYS.CONFIGURATION_TARGET,
16+
CONFIGURATION_TARGETS.WORKSPACE
17+
);
18+
}
19+
20+
static getVSCodeConfigurationTarget(): vscode.ConfigurationTarget {
21+
const currentTarget = this.getCurrentConfigurationTarget();
22+
return VS_CODE_CONFIGURATION_TARGETS[currentTarget];
23+
}
24+
25+
static async updateConfigurationTarget(
26+
target: ConfigurationTargetType
27+
): Promise<void> {
28+
try {
29+
const config = vscode.workspace.getConfiguration(CONFIG_SECTION);
30+
await config.update(
31+
CONFIG_KEYS.CONFIGURATION_TARGET,
32+
target,
33+
vscode.ConfigurationTarget.Global // Configuration target setting itself should always be global
34+
);
35+
36+
const targetMessage =
37+
target === CONFIGURATION_TARGETS.GLOBAL
38+
? "user settings (shared across all projects)"
39+
: "workspace settings (project-specific)";
40+
41+
vscode.window.showInformationMessage(
42+
`Configuration target changed to: ${targetMessage}`
43+
);
44+
} catch (error) {
45+
console.error("Failed to update configuration target:", error);
46+
vscode.window.showErrorMessage(
47+
"Failed to update configuration target. Please try again."
48+
);
49+
}
50+
}
51+
52+
static async updateButtonConfiguration(
53+
buttons: ButtonConfig[]
54+
): Promise<void> {
55+
try {
56+
const config = vscode.workspace.getConfiguration(CONFIG_SECTION);
57+
const target = this.getVSCodeConfigurationTarget();
58+
59+
await config.update(CONFIG_KEYS.BUTTONS, buttons, target);
60+
61+
const currentTarget = this.getCurrentConfigurationTarget();
62+
const targetMessage =
63+
currentTarget === CONFIGURATION_TARGETS.GLOBAL
64+
? "user settings"
65+
: "workspace settings";
66+
67+
vscode.window.showInformationMessage(
68+
`Configuration updated successfully in ${targetMessage}!`
69+
);
70+
} catch (error) {
71+
console.error("Failed to update configuration:", error);
72+
vscode.window.showErrorMessage(
73+
"Failed to update configuration. Please try again."
74+
);
75+
}
76+
}
77+
78+
static getConfigDataForWebview(configReader: {
79+
getButtons(): ButtonConfig[];
80+
}): {
81+
buttons: ButtonConfig[];
82+
configurationTarget: ConfigurationTargetType;
83+
} {
84+
return {
85+
buttons: configReader.getButtons(),
86+
configurationTarget: this.getCurrentConfigurationTarget(),
87+
};
88+
}
89+
}

src/extension/src/main.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ describe("main", () => {
205205
refreshTreeCommand: "mockDisposable",
206206
showAllCommandsCommand: "mockDisposable",
207207
openConfigCommand: "mockDisposable",
208+
toggleConfigurationTargetCommand: "mockDisposable",
208209
});
209210
});
210211
});

src/extension/src/main.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
createVSCodeStatusBarCreator,
1212
createVSCodeQuickPickCreator,
1313
} from "./adapters";
14+
import { ConfigManager } from "./config-manager";
15+
import { CONFIGURATION_TARGETS } from "./config-constants";
1416

1517
export const registerCommands = (
1618
context: vscode.ExtensionContext,
@@ -67,13 +69,27 @@ export const registerCommands = (
6769
)
6870
);
6971

72+
const toggleConfigurationTargetCommand = vscode.commands.registerCommand(
73+
"quickCommandButtons.toggleConfigurationTarget",
74+
async () => {
75+
const currentTarget = ConfigManager.getCurrentConfigurationTarget();
76+
const newTarget =
77+
currentTarget === CONFIGURATION_TARGETS.WORKSPACE
78+
? CONFIGURATION_TARGETS.GLOBAL
79+
: CONFIGURATION_TARGETS.WORKSPACE;
80+
81+
await ConfigManager.updateConfigurationTarget(newTarget);
82+
}
83+
);
84+
7085
return {
7186
executeCommand,
7287
executeFromTreeCommand,
7388
refreshCommand,
7489
refreshTreeCommand,
7590
showAllCommandsCommand,
7691
openConfigCommand,
92+
toggleConfigurationTargetCommand,
7793
};
7894
};
7995

0 commit comments

Comments
 (0)