|
| 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 | +} |
0 commit comments