Skip to content

Add ToggleISEMode command with tests #4117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"onCommand:PowerShell.OpenExamplesFolder",
"onCommand:PowerShell.EnableISEMode",
"onCommand:PowerShell.DisableISEMode",
"onCommand:PowerShell.ToggleISEMode",
"onView:PowerShellCommands"
],
"dependencies": {
Expand Down Expand Up @@ -155,6 +156,11 @@
"title": "Disable ISE Mode (restore to defaults)",
"category": "PowerShell"
},
{
"command": "PowerShell.ToggleISEMode",
"title": "Toggle ISE Mode",
"category": "PowerShell"
},
{
"command": "PowerShell.RefreshCommandsExplorer",
"title": "Refresh Command Explorer",
Expand Down
32 changes: 24 additions & 8 deletions src/features/ISECompatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,29 @@ export class ISECompatibilityFeature implements vscode.Disposable {
{ path: "editor", name: "wordSeparators", value: "`~!@#%^&*()-=+[{]}\\|;:'\",.<>/?" },
{ path: "powershell.buttons", name: "showPanelMovementButtons", value: true }
];
private iseCommandRegistration: vscode.Disposable;
private defaultCommandRegistration: vscode.Disposable;

private _commandRegistrations: vscode.Disposable[] = [];
private _iseModeEnabled: boolean;

constructor() {
this.iseCommandRegistration = vscode.commands.registerCommand(
"PowerShell.EnableISEMode", this.EnableISEMode);
this.defaultCommandRegistration = vscode.commands.registerCommand(
"PowerShell.DisableISEMode", this.DisableISEMode);
// TODO: This test isn't great.
const testSetting = ISECompatibilityFeature.settings[ISECompatibilityFeature.settings.length - 1];
this._iseModeEnabled = vscode.workspace.getConfiguration(testSetting.path).get(testSetting.name) === testSetting.value;
this._commandRegistrations = [
vscode.commands.registerCommand("PowerShell.EnableISEMode", async () => { await this.EnableISEMode(); }),
vscode.commands.registerCommand("PowerShell.DisableISEMode", async () => { await this.DisableISEMode(); }),
vscode.commands.registerCommand("PowerShell.ToggleISEMode", async () => { await this.ToggleISEMode(); })
]
}

public dispose() {
this.iseCommandRegistration.dispose();
this.defaultCommandRegistration.dispose();
for (const command of this._commandRegistrations) {
command.dispose();
}
}

private async EnableISEMode() {
this._iseModeEnabled = true;
for (const iseSetting of ISECompatibilityFeature.settings) {
try {
await vscode.workspace.getConfiguration(iseSetting.path).update(iseSetting.name, iseSetting.value, true);
Expand All @@ -63,11 +70,20 @@ export class ISECompatibilityFeature implements vscode.Disposable {
}

private async DisableISEMode() {
this._iseModeEnabled = false;
for (const iseSetting of ISECompatibilityFeature.settings) {
const currently = vscode.workspace.getConfiguration(iseSetting.path).get<string | boolean>(iseSetting.name);
if (currently === iseSetting.value) {
await vscode.workspace.getConfiguration(iseSetting.path).update(iseSetting.name, undefined, true);
}
}
}

private async ToggleISEMode() {
if (this._iseModeEnabled) {
await this.DisableISEMode();
} else {
await this.EnableISEMode();
}
}
}
32 changes: 29 additions & 3 deletions test/features/ISECompatibility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe("ISE compatibility feature", function () {

async function enableISEMode() { await vscode.commands.executeCommand("PowerShell.EnableISEMode"); }
async function disableISEMode() { await vscode.commands.executeCommand("PowerShell.DisableISEMode"); }
async function toggleISEMode() { await vscode.commands.executeCommand("PowerShell.ToggleISEMode"); }

before(async function () {
// Save user's current theme.
Expand All @@ -24,7 +25,7 @@ describe("ISE compatibility feature", function () {
assert.strictEqual(vscode.workspace.getConfiguration("workbench").get("colorTheme"), currentTheme);
});

describe("EnableISEMode command", async function () {
describe("Enable ISE Mode updates expected settings", async function () {
before(enableISEMode);
after(disableISEMode);
for (const iseSetting of ISECompatibilityFeature.settings) {
Expand All @@ -35,17 +36,42 @@ describe("ISE compatibility feature", function () {
}
});

describe("DisableISEMode command", async function () {
describe("Disable ISE Mode reverts expected settings", async function () {
before(enableISEMode);
before(disableISEMode);
after(disableISEMode);
for (const iseSetting of ISECompatibilityFeature.settings) {
it(`Reverts ${iseSetting.name} correctly`, function () {
const currently = vscode.workspace.getConfiguration(iseSetting.path).get(iseSetting.name);
assert.notStrictEqual(currently, iseSetting.value);
});
}
});

describe("Toggle switches from enabled to disabled", async function () {
before(enableISEMode);
before(toggleISEMode);
after(disableISEMode);
for (const iseSetting of ISECompatibilityFeature.settings) {
it(`Unsets ${iseSetting.name} correctly`, function () {
it(`Reverts ${iseSetting.name} correctly`, function () {
const currently = vscode.workspace.getConfiguration(iseSetting.path).get(iseSetting.name);
assert.notStrictEqual(currently, iseSetting.value);
});
}
});

describe("Toggle switches from disabled to enabled", async function () {
before(disableISEMode);
before(toggleISEMode);
after(disableISEMode);
for (const iseSetting of ISECompatibilityFeature.settings) {
it(`Sets ${iseSetting.name} correctly`, function () {
const currently = vscode.workspace.getConfiguration(iseSetting.path).get(iseSetting.name);
assert.strictEqual(currently, iseSetting.value);
});
}
});

describe("Color theme interactions", async function () {
beforeEach(enableISEMode);

Expand Down