Skip to content

Commit

Permalink
Use mementos for state management for pinned commands (#4175)
Browse files Browse the repository at this point in the history
* Use mementos for state management for pinned commands

* update activeCommands check
  • Loading branch information
gcampbell-msft authored Nov 20, 2024
1 parent 1f428e4 commit bc0f5ec
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Improvements:
- Fix "Test output isn't visible when failed" and also mark skipped tests as so. [#4116](https://github.com/microsoft/vscode-cmake-tools/issues/4116)
- Ensure that stopping tests actually forces the tests to stop running. [#2095](https://github.com/microsoft/vscode-cmake-tools/issues/2095)
- Retire the Show Options Moved Notification [#4039](https://github.com/microsoft/vscode-cmake-tools/issues/4039)
- Improve the pinned commands experience by defaulting settings and using VS Code state rather than modifying user settings. [#3977](https://github.com/microsoft/vscode-cmake-tools/issues/3977)

Bug Fixes:

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3578,7 +3578,10 @@
"type": "string"
},
"description": "%cmake-tools.configuration.cmake.pinnedCommands.description%",
"default": [],
"default": [
"workbench.action.tasks.configureTaskRunner",
"workbench.action.tasks.runTask"
],
"scope": "resource"
},
"cmake.enableAutomaticKitScan": {
Expand Down
2 changes: 1 addition & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@
},
"cmake-tools.configuration.cmake.launchBehavior.description": "Controls what happens with the launch terminal when you launch a target.",
"cmake-tools.configuration.cmake.automaticReconfigure.description": "Automatically configure CMake project directories when the kit or the configuration preset is changed.",
"cmake-tools.configuration.cmake.pinnedCommands.description":"List of CMake commands to pin.",
"cmake-tools.configuration.cmake.pinnedCommands.description":"List of CMake commands to always pin by default.",
"cmake-tools.configuration.cmake.enableAutomaticKitScan.description": "Enable automatic scanning for kits when a kit isn't selected. This will only take affect when CMake Presets aren't being used.",
"cmake-tools.debugger.pipeName.description": "Name of the pipe (on Windows) or domain socket (on Unix) to use for debugger communication.",
"cmake-tools.debugger.clean.description": "Clean prior to configuring.",
Expand Down
47 changes: 20 additions & 27 deletions src/ui/pinnedCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFo
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
const log = logging.createLogger('pinnedCommands');
const defaultTaskCommands: string[] = ["workbench.action.tasks.configureTaskRunner", "workbench.action.tasks.runTask"];
const mementoKey = "pinDefaultTasks";

interface PinnedCommandsQuickPickItem extends vscode.QuickPickItem {
command: string;
Expand Down Expand Up @@ -97,7 +96,6 @@ class PinnedCommandsTreeDataProvider implements vscode.TreeDataProvider<PinnedCo
private config: vscode.WorkspaceConfiguration | null;
private pinnedCommandsKey: string = "cmake.pinnedCommands";
private isInitialized = false;
private pinDefaultTasks = true;
private readonly _settingsSub ;
private extensionContext: vscode.ExtensionContext;

Expand All @@ -106,7 +104,6 @@ class PinnedCommandsTreeDataProvider implements vscode.TreeDataProvider<PinnedCo
this._settingsSub = configReader.onChange('pinnedCommands', () => this.doConfigureSettingsChange());
this.config = vscode.workspace.getConfiguration();
this.extensionContext = extensionContext;
this.pinDefaultTasks = this.extensionContext.globalState.get(mementoKey) === undefined; // the user has not unpinned any of the tasks commands yet.
onExtensionActiveCommandsChanged(this.doConfigureSettingsChange, this);
}

Expand All @@ -118,29 +115,29 @@ class PinnedCommandsTreeDataProvider implements vscode.TreeDataProvider<PinnedCo
this.config = vscode.workspace.getConfiguration();
this.pinnedCommands = []; //reset to empty list.
const localization = getExtensionLocalizedStrings();
if (this.config.has(this.pinnedCommandsKey)) {
const settingsPinnedCommands = this.config.get(this.pinnedCommandsKey) as string[];
const activeCommands = new Set<string>(PinnedCommands.getPinnableCommands());
for (const commandName of settingsPinnedCommands) {
const label = localization[`cmake-tools.command.${commandName}.title`];
const activeCommands = new Set<string>(PinnedCommands.getPinnableCommands());

const tryPushCommands = (commands: string[]) => {
commands.forEach((x) => {
const label = localization[`cmake-tools.command.${x}.title`];
if (this.findNode(label) === -1) {
// only show commands that are contained in the active commands for the extension.
this.pinnedCommands.push(new PinnedCommandNode(label, commandName, activeCommands.has(commandName)));
this.pinnedCommands.push(new PinnedCommandNode(label, x, activeCommands.has(x)));
}
}
});
};

// Pin the commands that are requested from the users settings.
if (this.config.has(this.pinnedCommandsKey)) {
const settingsPinnedCommands = this.config.get(this.pinnedCommandsKey) as string[];
tryPushCommands(settingsPinnedCommands);
}

if (this.pinDefaultTasks) {
if (this.pinnedCommands.filter(x => defaultTaskCommands.includes(x.commandName)).length !== defaultTaskCommands.length) {
defaultTaskCommands.forEach((x) => {
const label = localization[`cmake-tools.command.${x}.title`];
if (this.findNode(label) === -1) {
this.pinnedCommands.push(new PinnedCommandNode(label, x, true));
}
});
await this.updateSettings();
}
// Pin commands that were pinned in the last session.
const lastSessionPinnedCommands = this.extensionContext.workspaceState.get(this.pinnedCommandsKey) as string[];
if (lastSessionPinnedCommands) {
tryPushCommands(lastSessionPinnedCommands);
}

this.isInitialized = true;
}

Expand Down Expand Up @@ -174,10 +171,6 @@ class PinnedCommandsTreeDataProvider implements vscode.TreeDataProvider<PinnedCo
this.pinnedCommands.splice(index, 1);
await this.refresh();
}
if (this.pinDefaultTasks && defaultTaskCommands.includes(node.commandName)) {
await this.extensionContext.globalState.update(mementoKey, false);
this.pinDefaultTasks = false;
}
await this.updateSettings();
}

Expand All @@ -191,8 +184,8 @@ class PinnedCommandsTreeDataProvider implements vscode.TreeDataProvider<PinnedCo

async updateSettings() {
if (this.config) {
const newValue: string[] = this.pinnedCommands.map(x => x.commandName);
await this.config.update(this.pinnedCommandsKey, newValue, true); // update global
const pinnedCommands: string[] = this.pinnedCommands.map(x => x.commandName);
await this.extensionContext.workspaceState.update(this.pinnedCommandsKey, pinnedCommands);
}
}

Expand Down

0 comments on commit bc0f5ec

Please sign in to comment.