Skip to content

Commit

Permalink
cleaned up command registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Drakae committed Oct 11, 2024
1 parent 04ab89a commit 4df67bc
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 190 deletions.
20 changes: 10 additions & 10 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@
"category": "STPA Snippets"
},
{
"command": "pasta.stpa.setIDGeneration",
"command": "pasta.stpa.setGenerateIDs",
"title": "Automatic ID Generation",
"category": "STPA ID Enforcement"
},
{
"command": "pasta.stpa.unsetIDGeneration",
"command": "pasta.stpa.unsetGenerateIDs",
"title": "✓ Automatic ID Generation",
"category": "STPA ID Enforcement"
}
Expand Down Expand Up @@ -293,12 +293,12 @@
"when": "editorLangId == 'stpa'"
},
{
"command": "pasta.stpa.setIDGeneration",
"when": "editorLangId == 'stpa' && pasta.idGeneration == false"
"command": "pasta.stpa.setGenerateIDs",
"when": "editorLangId == 'stpa' && pasta.generateIDs == false"
},
{
"command": "pasta.stpa.unsetIDGeneration",
"when": "editorLangId == 'stpa' && pasta.idGeneration == true"
"command": "pasta.stpa.unsetGenerateIDs",
"when": "editorLangId == 'stpa' && pasta.generateIDs == true"
},
{
"command": "pasta.fta.cutSets",
Expand Down Expand Up @@ -351,13 +351,13 @@
"group": "stpa@4"
},
{
"command": "pasta.stpa.setIDGeneration",
"when": "editorLangId == 'stpa' && pasta.idGeneration == false",
"command": "pasta.stpa.setGenerateIDs",
"when": "editorLangId == 'stpa' && pasta.generateIDs == false",
"group": "stpa@1"
},
{
"command": "pasta.stpa.unsetIDGeneration",
"when": "editorLangId == 'stpa' && pasta.idGeneration == true",
"command": "pasta.stpa.unsetGenerateIDs",
"when": "editorLangId == 'stpa' && pasta.generateIDs == true",
"group": "stpa@1"
},
{
Expand Down
271 changes: 91 additions & 180 deletions extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,28 @@ import { StorageService } from "./storage-service";
import { createFile, createOutputChannel, setStorageOption } from "./utils";

let languageClient: LanguageClient;
const validationGroupName = "validation";

/* The names of the groups for the storage options. */
export const validationGroupName = "validation";
export const idGenGroupName = "IDGeneration";

/* The contexts for the validation checks. */
const validationContexts = [
"checkResponsibilitiesForConstraints",
"checkConstraintsForUCAs",
"checkScenariosForUCAs",
"checkSafetyRequirementsForUCAs",
"checkMissingFeedback",
];

/* The contexts for the ID generation. */
const idGenContexts = ["generateIDs"];

/* The command prefixes for toggling and contexts for the different storage groups. */
const contextsAndPrefixForGroups: Map<string, {commandPrefix: string, contexts: string[]}> = new Map([
[validationGroupName, {commandPrefix: ".stpa.checks", contexts: validationContexts}],
[idGenGroupName, {commandPrefix: ".stpa", contexts: idGenContexts}],
]);

/**
* All file endings of the languages that are supported by pasta.
Expand Down Expand Up @@ -153,13 +174,13 @@ function registerPastaCommands(
* Reset the contexts for storage options to the default values.
*/
function resetContextForStorageOptions(): void {
// set context for the validation checks depending on saved valued in storage
// set contexts for PASTA options depending on saved valued in storage
vscode.commands.executeCommand("setContext", "pasta.checkResponsibilitiesForConstraints", true);
vscode.commands.executeCommand("setContext", "pasta.checkConstraintsForUCAs", true);
vscode.commands.executeCommand("setContext", "pasta.checkScenariosForUCAs", true);
vscode.commands.executeCommand("setContext", "pasta.checkSafetyRequirementsForUCAs", true);
vscode.commands.executeCommand("setContext", "pasta.checkMissingFeedback", true);
vscode.commands.executeCommand("setContext", "pasta.idGeneration", true);
vscode.commands.executeCommand("setContext", "pasta.generateIDs", true);
}

/**
Expand Down Expand Up @@ -187,162 +208,9 @@ function registerSTPACommands(
)
);

// set context for the validation checks depending on saved value in storage
const group = storage.getItem(validationGroupName);
vscode.commands.executeCommand(
"setContext",
"pasta.checkResponsibilitiesForConstraints",
group && group["checkResponsibilitiesForConstraints"] !== undefined
? group["checkResponsibilitiesForConstraints"]
: true
);
vscode.commands.executeCommand(
"setContext",
"pasta.checkConstraintsForUCAs",
group && group["checkConstraintsForUCAs"] !== undefined ? group["checkConstraintsForUCAs"] : true
);
vscode.commands.executeCommand(
"setContext",
"pasta.checkScenariosForUCAs",
group && group["checkScenariosForUCAs"] !== undefined ? group["checkScenariosForUCAs"] : true
);
vscode.commands.executeCommand(
"setContext",
"pasta.checkSafetyRequirementsForUCAs",
group && group["checkSafetyRequirementsForUCAs"] !== undefined ? group["checkSafetyRequirementsForUCAs"] : true
);
vscode.commands.executeCommand(
"setContext",
"pasta.checkMissingFeedback",
group && group["checkMissingFeedback"] !== undefined ? group["checkMissingFeedback"] : true
);
// commands for toggling the provided validation checks
context.subscriptions.push(
vscode.commands.registerCommand(
options.extensionPrefix + ".stpa.checks.setCheckResponsibilitiesForConstraints",
async () => {
vscode.commands.executeCommand("setContext", "pasta.checkResponsibilitiesForConstraints", true);
setStorageOption(
validationGroupName,
"checkResponsibilitiesForConstraints",
true,
storage,
languageClient,
manager
);
}
)
);
context.subscriptions.push(
vscode.commands.registerCommand(
options.extensionPrefix + ".stpa.checks.unsetCheckResponsibilitiesForConstraints",
async () => {
vscode.commands.executeCommand("setContext", "pasta.checkResponsibilitiesForConstraints", false);
setStorageOption(
validationGroupName,
"checkResponsibilitiesForConstraints",
false,
storage,
languageClient,
manager
);
}
)
);
context.subscriptions.push(
vscode.commands.registerCommand(
options.extensionPrefix + ".stpa.checks.setCheckConstraintsForUCAs",
async () => {
vscode.commands.executeCommand("setContext", "pasta.checkConstraintsForUCAs", true);
setStorageOption(
validationGroupName,
"checkConstraintsForUCAs",
true,
storage,
languageClient,
manager
);
}
)
);
context.subscriptions.push(
vscode.commands.registerCommand(
options.extensionPrefix + ".stpa.checks.unsetCheckConstraintsForUCAs",
async () => {
vscode.commands.executeCommand("setContext", "pasta.checkConstraintsForUCAs", false);
setStorageOption(
validationGroupName,
"checkConstraintsForUCAs",
false,
storage,
languageClient,
manager
);
}
)
);
context.subscriptions.push(
vscode.commands.registerCommand(options.extensionPrefix + ".stpa.checks.setCheckScenariosForUCAs", async () => {
vscode.commands.executeCommand("setContext", "pasta.checkScenariosForUCAs", true);
setStorageOption(validationGroupName, "checkScenariosForUCAs", true, storage, languageClient, manager);
})
);
context.subscriptions.push(
vscode.commands.registerCommand(
options.extensionPrefix + ".stpa.checks.unsetCheckScenariosForUCAs",
async () => {
vscode.commands.executeCommand("setContext", "pasta.checkScenariosForUCAs", false);
setStorageOption(validationGroupName, "checkScenariosForUCAs", false, storage, languageClient, manager);
}
)
);
context.subscriptions.push(
vscode.commands.registerCommand(
options.extensionPrefix + ".stpa.checks.setCheckSafetyRequirementsForUCAs",
async () => {
vscode.commands.executeCommand("setContext", "pasta.checkSafetyRequirementsForUCAs", true);
setStorageOption(
validationGroupName,
"checkSafetyRequirementsForUCAs",
true,
storage,
languageClient,
manager
);
}
)
);
context.subscriptions.push(
vscode.commands.registerCommand(
options.extensionPrefix + ".stpa.checks.unsetCheckSafetyRequirementsForUCAs",
async () => {
vscode.commands.executeCommand("setContext", "pasta.checkSafetyRequirementsForUCAs", false);
setStorageOption(
validationGroupName,
"checkSafetyRequirementsForUCAs",
false,
storage,
languageClient,
manager
);
}
)
);
context.subscriptions.push(
vscode.commands.registerCommand(options.extensionPrefix + ".stpa.checks.setCheckMissingFeedback", async () => {
vscode.commands.executeCommand("setContext", "pasta.checkMissingFeedback", true);
setStorageOption(validationGroupName, "checkMissingFeedback", true, storage, languageClient, manager);
})
);
context.subscriptions.push(
vscode.commands.registerCommand(
options.extensionPrefix + ".stpa.checks.unsetCheckMissingFeedback",
async () => {
vscode.commands.executeCommand("setContext", "pasta.checkMissingFeedback", false);
setStorageOption(validationGroupName, "checkMissingFeedback", false, storage, languageClient, manager);
}
)
);
setContextsBasedOnStorage(storage);
registerCommandsForStorageUpdate(manager, context, storage, options);

// needed to not activate ID generation on undo/redo
context.subscriptions.push(
vscode.commands.registerCommand(options.extensionPrefix + ".IDs.undo", async () => {
Expand Down Expand Up @@ -405,28 +273,71 @@ function registerSTPACommands(
return formulas;
})
);
}

// command for automating ID generation
const idGenGroup = "IDGeneration";
vscode.commands.executeCommand(
"setContext",
"pasta.idGeneration",
storage.getItem(idGenGroup) && storage.getItem(idGenGroup)["generateIDs"]
? storage.getItem(idGenGroup)["generateIDs"]
: true
);
context.subscriptions.push(
vscode.commands.registerCommand(options.extensionPrefix + ".stpa.setIDGeneration", async () => {
vscode.commands.executeCommand("setContext", "pasta.idGeneration", true);
setStorageOption(idGenGroup, "generateIDs", true, storage, languageClient, manager);
})
);
context.subscriptions.push(
vscode.commands.registerCommand(options.extensionPrefix + ".stpa.unsetIDGeneration", async () => {
vscode.commands.executeCommand("setContext", "pasta.idGeneration", false);
setStorageOption(idGenGroup, "generateIDs", false, storage, languageClient, manager);
})
);
/**
* Register the commands for setting and unsetting the contexts for the storage options.
* The command name must end with ".set" or ".unset" respectively and the name (camel-cased) of the context that should be set or unset.
* For the prefix of the command see {@code contextsAndPrefixForGroups}.
* The context name has to be the same as the key in the storage.
* @param manager The manager that handles the webview panels.
* @param context The context of the extension.
* @param storage The storage service for the extension.
* @param options The options for the commands.
*/
function registerCommandsForStorageUpdate(
manager: StpaLspVscodeExtension,
context: vscode.ExtensionContext,
storage: StorageService,
options: { extensionPrefix: string }
): void {
for (const groupName of contextsAndPrefixForGroups.keys()) {
const prefix = contextsAndPrefixForGroups.get(groupName)?.commandPrefix;
for (const contextOfGroup of contextsAndPrefixForGroups.get(groupName)?.contexts ?? []) {
context.subscriptions.push(
vscode.commands.registerCommand(
options.extensionPrefix +
prefix + ".set" +
contextOfGroup.charAt(0).toUpperCase() +
contextOfGroup.slice(1),
async () => {
vscode.commands.executeCommand("setContext", "pasta." + contextOfGroup, true);
setStorageOption(groupName, contextOfGroup, true, storage, languageClient, manager);
}
)
);
context.subscriptions.push(
vscode.commands.registerCommand(
options.extensionPrefix +
prefix + ".unset" +
contextOfGroup.charAt(0).toUpperCase() +
contextOfGroup.slice(1),
async () => {
vscode.commands.executeCommand("setContext", "pasta." + contextOfGroup, false);
setStorageOption(groupName, contextOfGroup, false, storage, languageClient, manager);
}
)
);
}
}
}

/**
* Sets the context based on the storage for the custom contexts of PASTA.
* The context name has to be the same as the key in the storage.
* @param storage The storage service for the extension.
*/
function setContextsBasedOnStorage(storage: StorageService): void {
for (const groupName of contextsAndPrefixForGroups.keys()) {
const group = storage.getItem(groupName);
for (const contextOfGroup of contextsAndPrefixForGroups.get(groupName)?.contexts ?? []) {
vscode.commands.executeCommand(
"setContext",
"pasta." + contextOfGroup,
group && group[contextOfGroup] !== undefined ? group[contextOfGroup] : true
);
}
}
}

/**
Expand Down

0 comments on commit 4df67bc

Please sign in to comment.