diff --git a/package.json b/package.json index b1e7249..34c83b0 100644 --- a/package.json +++ b/package.json @@ -220,6 +220,10 @@ "command": "weAudit.addRegionToAnEntry", "title": "weAudit: Add Region to a Finding" }, + { + "command": "weAudit.addRegionToAnEntryWithLabel", + "title": "weAudit: Add Region to a Finding with Label" + }, { "command": "weAudit.showFindingsSearchBar", "title": "weAudit: Search and Filter Findings" diff --git a/src/codeMarker.ts b/src/codeMarker.ts index f3cd48c..48cb2e1 100644 --- a/src/codeMarker.ts +++ b/src/codeMarker.ts @@ -2014,6 +2014,10 @@ export class CodeMarker implements vscode.TreeDataProvider { void this.addRegionToAnEntry(); }); + vscode.commands.registerCommand("weAudit.addRegionToAnEntryWithLabel", () => { + void this.addRegionToAnEntryWithLabel(); + }); + vscode.commands.registerCommand("weAudit.deleteLocation", (entry: FullLocationEntry) => { this.deleteLocation(entry); }); @@ -3169,9 +3173,11 @@ export class CodeMarker implements vscode.TreeDataProvider { } /** - * Add the selected code region to an existing entry + * Shared helper that adds the current editor selection(s) to an existing entry. + * Optionally prompts for a label that is applied to each new location. + * @param getLabel function that resolves to the label to assign, or undefined to skip labeling */ - async addRegionToAnEntry(): Promise { + private async addRegionToEntryWithOptionalLabel(getLabel?: () => Promise): Promise { const editor = vscode.window.activeTextEditor; if (editor === undefined) { return; @@ -3211,12 +3217,23 @@ export class CodeMarker implements vscode.TreeDataProvider { return; } + let label: string | undefined; + if (getLabel) { + label = await getLabel(); + if (label === undefined) { + return; + } + } + const entry = pickItem.entry; - // Add each selection as a separate region + // Add each selection as a separate region, optionally tagging with the provided label for (const location of locations) { + if (label !== undefined) { + location.label = label; + } entry.locations.push(location); } - void this.updateSavedData(entry.author); + this.updateSavedData(entry.author); this.decorateWithUri(editor.document.uri); this.refresh(editor.document.uri); // reveal the entry in the tree view if the treeview is visible, @@ -3227,6 +3244,25 @@ export class CodeMarker implements vscode.TreeDataProvider { } } + /** + * Add the selected code region to an existing entry + */ + async addRegionToAnEntry(): Promise { + await this.addRegionToEntryWithOptionalLabel(); + } + + /** + * Add the selected code region to an existing entry, prompting for a label + */ + async addRegionToAnEntryWithLabel(): Promise { + await this.addRegionToEntryWithOptionalLabel(async () => + vscode.window.showInputBox({ + title: "Enter a label for this location", + ignoreFocusOut: true, + }), + ); + } + /** * Loads the saved findings from a file * @param config the configuration to load from