Skip to content

Commit 69155f9

Browse files
committed
chore: unify add finding to region implementations
1 parent 13ed0e0 commit 69155f9

File tree

1 file changed

+31
-68
lines changed

1 file changed

+31
-68
lines changed

src/codeMarker.ts

Lines changed: 31 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,7 +2015,7 @@ export class CodeMarker implements vscode.TreeDataProvider<TreeEntry> {
20152015
});
20162016

20172017
vscode.commands.registerCommand("weAudit.addRegionToAnEntryWithLabel", () => {
2018-
this.addRegionToAnEntryWithLabel();
2018+
void this.addRegionToAnEntryWithLabel();
20192019
});
20202020

20212021
vscode.commands.registerCommand("weAudit.deleteLocation", (entry: FullLocationEntry) => {
@@ -3173,9 +3173,11 @@ export class CodeMarker implements vscode.TreeDataProvider<TreeEntry> {
31733173
}
31743174

31753175
/**
3176-
* Add the selected code region to an existing entry
3176+
* Shared helper that adds the current editor selection(s) to an existing entry.
3177+
* Optionally prompts for a label that is applied to each new location.
3178+
* @param getLabel function that resolves to the label to assign, or undefined to skip labeling
31773179
*/
3178-
async addRegionToAnEntry(): Promise<void> {
3180+
private async addRegionToEntryWithOptionalLabel(getLabel?: () => Promise<string | undefined>): Promise<void> {
31793181
const editor = vscode.window.activeTextEditor;
31803182
if (editor === undefined) {
31813183
return;
@@ -3215,12 +3217,23 @@ export class CodeMarker implements vscode.TreeDataProvider<TreeEntry> {
32153217
return;
32163218
}
32173219

3220+
let label: string | undefined;
3221+
if (getLabel) {
3222+
label = await getLabel();
3223+
if (label === undefined) {
3224+
return;
3225+
}
3226+
}
3227+
32183228
const entry = pickItem.entry;
3219-
// Add each selection as a separate region
3229+
// Add each selection as a separate region, optionally tagging with the provided label
32203230
for (const location of locations) {
3231+
if (label !== undefined) {
3232+
location.label = label;
3233+
}
32213234
entry.locations.push(location);
32223235
}
3223-
void this.updateSavedData(entry.author);
3236+
this.updateSavedData(entry.author);
32243237
this.decorateWithUri(editor.document.uri);
32253238
this.refresh(editor.document.uri);
32263239
// reveal the entry in the tree view if the treeview is visible,
@@ -3231,73 +3244,23 @@ export class CodeMarker implements vscode.TreeDataProvider<TreeEntry> {
32313244
}
32323245
}
32333246

3247+
/**
3248+
* Add the selected code region to an existing entry
3249+
*/
3250+
async addRegionToAnEntry(): Promise<void> {
3251+
await this.addRegionToEntryWithOptionalLabel();
3252+
}
3253+
32343254
/**
32353255
* Add the selected code region to an existing entry, prompting for a label
32363256
*/
32373257
async addRegionToAnEntryWithLabel(): Promise<void> {
3238-
const editor = vscode.window.activeTextEditor;
3239-
if (editor === undefined) {
3240-
return;
3241-
}
3242-
const locations = this.getActiveSelectionLocation();
3243-
if (locations === undefined || locations.length === 0) {
3244-
return;
3245-
}
3246-
3247-
// create a quick pick to select the entry to add the region to
3248-
const items = this.treeEntries
3249-
.filter((entry) => {
3250-
if (entry.locations.length === 0 || entry.locations[0].rootPath !== locations[0].rootPath) {
3251-
return false;
3252-
}
3253-
return true;
3254-
})
3255-
.map((entry) => {
3256-
return {
3257-
label: entry.label,
3258-
entry: entry,
3259-
};
3260-
});
3261-
3262-
// if we have no findings so far, create a new one
3263-
if (items.length === 0) {
3264-
this.addFinding();
3265-
return;
3266-
}
3267-
3268-
const pickItem = await vscode.window.showQuickPick(items, {
3269-
ignoreFocusOut: true,
3270-
title: "Select the finding to add the region to",
3271-
});
3272-
3273-
if (pickItem === undefined) {
3274-
return;
3275-
}
3276-
3277-
const label = await vscode.window.showInputBox({
3278-
title: "Enter a label for this location",
3279-
ignoreFocusOut: true,
3280-
});
3281-
3282-
if (label === undefined) {
3283-
return;
3284-
}
3285-
3286-
const entry = pickItem.entry;
3287-
// Add each selection as a separate region with the label
3288-
for (const location of locations) {
3289-
location.label = label;
3290-
entry.locations.push(location);
3291-
}
3292-
this.updateSavedData(entry.author);
3293-
this.decorateWithUri(editor.document.uri);
3294-
this.refresh(editor.document.uri);
3295-
// reveal the entry in the tree view if the treeview is visible,
3296-
// for some reason, it won't expand even if though it is created
3297-
// with an expanded state
3298-
if (treeView.visible) {
3299-
treeView.reveal(entry, { expand: 1, select: false });
3300-
}
3258+
await this.addRegionToEntryWithOptionalLabel(async () =>
3259+
vscode.window.showInputBox({
3260+
title: "Enter a label for this location",
3261+
ignoreFocusOut: true,
3262+
}),
3263+
);
33013264
}
33023265

33033266
/**

0 commit comments

Comments
 (0)