From 92b33116e2aa8c627ac5355c26a73e97aa0a5268 Mon Sep 17 00:00:00 2001 From: Aaron Powell Date: Tue, 2 Jul 2019 11:29:21 +1000 Subject: [PATCH] Refactoring logic to be a bit cleaner to read --- .gitignore | 2 +- src/extension.ts | 149 ++++++++++++++++++++++++++--------------------- 2 files changed, 85 insertions(+), 66 deletions(-) diff --git a/.gitignore b/.gitignore index 9f93d0a..2e94e98 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,5 @@ node_modules .vscode-test/ *.vsix .ionide -test-output.xml +test-results.xml .test-profiles \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index 860861a..f1d7830 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -3,92 +3,111 @@ import SettingsHelper from "./settingsHelper"; import ContributedCommands from "./commands"; import Config from "./services/config"; -export async function activate(context: vscode.ExtensionContext) { - let settingsHelper = new SettingsHelper(context); - let config = new Config(); +function selectProfile(config: Config, settingsHelper: SettingsHelper) { + return async () => { + let profiles = config.getProfiles(); - context.subscriptions.push( - vscode.commands.registerCommand(ContributedCommands.SelectProfile, async () => { - let profiles = config.getProfiles(); + if (!profiles.length) { + await vscode.window.showInformationMessage( + "There are no profiles saved to switch to. First save a profile and then you can pick it" + ); + return; + } - if (!profiles.length) { - await vscode.window.showInformationMessage( - "There are no profiles saved to switch to. First save a profile and then you can pick it" - ); - return; - } + let profile = await vscode.window.showQuickPick(profiles, { + placeHolder: "Select a profile" + }); + + if (!profile) { + return; + } + + let profileSettings = config.getProfileSettings(profile); + + await settingsHelper.updateUserSettings(profileSettings); + }; +} + +function saveProfile(config: Config, settingsHelper: SettingsHelper) { + return async () => { + let profiles = config.getProfiles(); - let profile = await vscode.window.showQuickPick(profiles, { + let profile = await vscode.window.showQuickPick( + [...profiles, "New profile"], + { placeHolder: "Select a profile" + } + ); + + if (!profile || profile === "New profile") { + profile = await vscode.window.showInputBox({ + placeHolder: "Enter the profile name" }); if (!profile) { return; } - let profileSettings = config.getProfileSettings(profile); + await config.addProfile(profile); + } - await settingsHelper.updateUserSettings(profileSettings); - }) - ); + let userSettings = await settingsHelper.getUserSettings(); + await config.addProfileSettings(profile, userSettings); - context.subscriptions.push( - vscode.commands.registerCommand(ContributedCommands.SaveProfile, async () => { - let profiles = config.getProfiles(); - - let profile = await vscode.window.showQuickPick( - [...profiles, "New profile"], - { - placeHolder: "Select a profile" - } - ); - - if (!profile || profile === "New profile") { - profile = await vscode.window.showInputBox({ - placeHolder: "Enter the profile name" - }); + vscode.window.showInformationMessage(`Profile ${profile} has been saved.`); + }; +} - if (!profile) { - return; - } +function deleteProfile(config: Config) { + return async () => { + let profiles = config.getProfiles(); - await config.addProfile(profile); - } + if (!profiles.length) { + await vscode.window.showInformationMessage( + "There are no profiles saved." + ); + return; + } - let userSettings = await settingsHelper.getUserSettings(); - await config.addProfileSettings(profile, userSettings); + let profile = await vscode.window.showQuickPick(profiles, { + placeHolder: "Select a profile" + }); - vscode.window.showInformationMessage( - `Profile ${profile} has been saved.` - ); - }) - ); + if (!profile) { + return; + } - context.subscriptions.push( - vscode.commands.registerCommand(ContributedCommands.DeleteProfile, async () => { - let profiles = config.getProfiles(); + await config.removeProfile(profile); + await config.removeProfileSettings(profile); - if (!profiles.length) { - await vscode.window.showInformationMessage( - "There are no profiles saved." - ); - return; - } + await vscode.window.showInformationMessage( + `Profile ${profile} has been deleted.` + ); + }; +} - let profile = await vscode.window.showQuickPick(profiles, { - placeHolder: "Select a profile" - }); +export async function activate(context: vscode.ExtensionContext) { + let settingsHelper = new SettingsHelper(context); + let config = new Config(); - if (!profile) { - return; - } + context.subscriptions.push( + vscode.commands.registerCommand( + ContributedCommands.SelectProfile, + selectProfile(config, settingsHelper) + ) + ); - await config.removeProfile(profile); - await config.removeProfileSettings(profile); + context.subscriptions.push( + vscode.commands.registerCommand( + ContributedCommands.SaveProfile, + saveProfile(config, settingsHelper) + ) + ); - await vscode.window.showInformationMessage( - `Profile ${profile} has been deleted.` - ); - }) + context.subscriptions.push( + vscode.commands.registerCommand( + ContributedCommands.DeleteProfile, + deleteProfile(config) + ) ); }