Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Commit

Permalink
Refactoring logic to be a bit cleaner to read
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronpowell committed Jul 2, 2019
1 parent 4bca7ca commit 92b3311
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ node_modules
.vscode-test/
*.vsix
.ionide
test-output.xml
test-results.xml
.test-profiles
149 changes: 84 additions & 65 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
);
}

0 comments on commit 92b3311

Please # to comment.