Skip to content

Commit 1a6ccd6

Browse files
committed
v1.67.0 pickStringRemember: save the picked items of a group in a separate remember key, fix #108
1 parent 3ba34e9 commit 1a6ccd6

File tree

4 files changed

+141
-23
lines changed

4 files changed

+141
-23
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Change Log
22

3-
## [1.6x.x] 2025-02-xx
3+
## [1.67.0] 2025-02-26
4+
### Added
5+
- `pickStringRemember`: save the picked items of a group in a separate remember key.
46
### Fixed
57
- `pickStringRemember`: `dependsOn` for groups
68

README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,7 @@ The command has the following configuration attributes:
13021302
* `name`: [_string_] (Optional) Used in multi pick list. They are the variables used in the `dependsOn` expressions.
13031303
It must be a [valid JavaScript variable name](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#variables).
13041304
* `dependsOn`: [_string_] (Optional) Used in multi pick list. It must be a [valid JavaScript expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators) that has a boolean ([ `true` | `false` ]) result. The variables allowed in the expression are the `name`s of items or groups. Here defined on a group it controls if the group validation is performed and if the value of the group items is part of the result when it is picked. See [`dependsOn`](#`dependson`). (default: `true`)
1305+
* `key`, `separator`, `joinByKey` : (Optional) If you want to remember the picked items of a group in a different remember store item define these properties in the group. They have the same meaning as for the top `args` property. They are not inherited from the top `args` property (see Example 13).
13051306
* `addLabelToTop` : [_string_] (Optional) Any [variables](#variables) are resolved. The pickItem with the identical label will be put on top. If needed add an extra remember item (see Example 7).
13061307
* `key` : (Optional) Used to store and retrieve a particular pick. (default: `pickString` )
13071308
The value can later be retrieved with the [`remember`](#remember) command or [`${remember}`](#variable-remember) variable.
@@ -2165,6 +2166,105 @@ If `name1` and `name3` are selected the remember storage contains
21652166

21662167
and `${input:set-v1-v2}` returns the value for key `v2`: `other1 other3`
21672168

2169+
**Example 13**
2170+
2171+
Next feature is by Axel Le Bourhis ([issue 108](https://github.com/rioj7/command-variable/issues/108))
2172+
2173+
Sometimes you also want to use the items selected in a group. Or have the group selected items saved in individual remember keys and use 1 multipick instead of multiple `pickStringRemember` calls for each group.
2174+
2175+
You specify for the particular groups a `key`. If you want a particular `separator` you can.
2176+
You can add the property `joinByKey` to a group if the option values are objects with key-value pairs.
2177+
2178+
```json
2179+
{
2180+
"version": "2.0.0",
2181+
"tasks": [
2182+
{
2183+
"label": "Grovery List",
2184+
"type": "shell",
2185+
"command": "echo",
2186+
"args": ["${input:groceryList}"]
2187+
}
2188+
],
2189+
"inputs": [
2190+
{
2191+
"id": "groceryList",
2192+
"type": "command",
2193+
"command": "extension.commandvariable.pickStringRemember",
2194+
"args": {
2195+
"description": "Select groceries",
2196+
"key": "groceries",
2197+
"multiPick": true,
2198+
"optionGroups": [
2199+
{
2200+
"label": "Fruit",
2201+
"minCount": 1,
2202+
"maxCount": 3,
2203+
"options": ["apple", "orange", "banana", "lychee"],
2204+
"key": "category-fruit",
2205+
"separator": "##"
2206+
},
2207+
{
2208+
"label": "Vegetable",
2209+
"minCount": 1,
2210+
"maxCount": 3,
2211+
"options": ["kousenband", "artichoke", "sopropo", "lettuce"],
2212+
"key": "category-veg",
2213+
"separator": "%%"
2214+
}
2215+
]
2216+
}
2217+
},
2218+
{ "id": "category-fruit", "type": "command", "command": "extension.commandvariable.remember", "args": { "key": "category-fruit" } },
2219+
{ "id": "category-veg", "type": "command", "command": "extension.commandvariable.remember", "args": { "key": "category-veg" } }
2220+
]
2221+
}
2222+
```
2223+
2224+
You can use this also for a single pick list. Groups that do not have an item selected are stored in the remember store as an empty string.
2225+
2226+
```json
2227+
{
2228+
"version": "2.0.0",
2229+
"tasks": [
2230+
{
2231+
"label": "Food Item",
2232+
"type": "shell",
2233+
"command": "echo",
2234+
"args": ["${input:fooditem}"]
2235+
}
2236+
],
2237+
"inputs": [
2238+
{
2239+
"id": "fooditem",
2240+
"type": "command",
2241+
"command": "extension.commandvariable.pickStringRemember",
2242+
"args": {
2243+
"description": "Select a food item",
2244+
"key": "food-item",
2245+
"optionGroups": [
2246+
{
2247+
"label": "Fruit",
2248+
"options": ["apple", "orange", "banana", "lychee"],
2249+
"name": "fruit",
2250+
"key": "category-fruit",
2251+
"dependsOn": "fruit + veg === 1"
2252+
},
2253+
{
2254+
"label": "Vegetable",
2255+
"options": ["kousenband", "artichoke", "sopropo", "lettuce"],
2256+
"name": "veg",
2257+
"key": "category-veg"
2258+
}
2259+
]
2260+
}
2261+
},
2262+
{ "id": "category-fruit", "type": "command", "command": "extension.commandvariable.remember", "args": { "key": "category-fruit" } },
2263+
{ "id": "category-veg", "type": "command", "command": "extension.commandvariable.remember", "args": { "key": "category-veg" } }
2264+
]
2265+
}
2266+
```
2267+
21682268
## promptStringRemember
21692269

21702270
`extension.commandvariable.promptStringRemember` has the same configuration attributes as the [Input variable promptString](https://code.visualstudio.com/docs/editor/variables-reference#_input-variables).

extension-common.js

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ class PickStringGroup extends PickStringMulti {
245245
this.minCount = minCount;
246246
this.maxCount = maxCount;
247247
this.items = [];
248+
this.key = undefined;
248249
}
249250
addItem(qpItem) {
250251
let label = utils.getProperty(qpItem, 'label');
@@ -332,6 +333,10 @@ async function pickStringRemember(args, processPick) {
332333
name = name ? name : `group-${groups.length + 1}`;
333334
groupLabel = groupLabel ? groupLabel : name;
334335
let group = new PickStringGroup(name, groupLabel, minCount, maxCount, dependsOn);
336+
for (const p of ['key', 'separator', 'joinByKey']) {
337+
let v = utils.getProperty(optionGroup, p);
338+
if (v !== undefined) { group[p] = v; }
339+
}
335340
groups.push(group);
336341
for (const option of utils.getProperty(optionGroup, 'options', ['item1', 'item2'])) {
337342
let qpItem = undefined;
@@ -411,30 +416,41 @@ async function pickStringRemember(args, processPick) {
411416
if (newStored !== prevStored) {
412417
multiPickStorage.update(multiPickLabelKey, newStored);
413418
}
414-
let separator = utils.getProperty(args, 'separator', ' ');
415-
let picked = result.filter(e => groups.some(g => g.allowedLabel(pickContext, e.label))).map(e => e.value);
416-
if (picked.length > 0 && utils.getProperty(args, 'joinByKey')) {
417-
let keys = new Set();
418-
for (const p of picked) {
419-
if (!utils.isObject(p)) { continue; }
420-
for (const vkey in p) {
421-
if (p.hasOwnProperty(vkey)) {
422-
keys.add(vkey);
419+
const resultFor = (picked, args) => {
420+
let result;
421+
let separator = utils.getProperty(args, 'separator', ' ');
422+
picked = picked.map(e => e.value);
423+
if (picked.length > 0 && utils.getProperty(args, 'joinByKey')) {
424+
let keys = new Set();
425+
for (const p of picked) {
426+
if (!utils.isObject(p)) { continue; }
427+
for (const vkey in p) {
428+
if (p.hasOwnProperty(vkey)) {
429+
keys.add(vkey);
430+
}
423431
}
424432
}
425-
}
426-
result = { value: {} };
427-
for (const vkey of keys) {
428-
result.value[vkey] = picked.filter(p => utils.isObject(p) && (utils.getProperty(p, vkey) !== undefined)).map(p => p[vkey]).join(separator);
429-
}
430-
} else {
431-
result = { value: picked.map(value => {
432-
if (utils.isObject(value)) {
433-
value = storeStringRemember2(args, value);
433+
result = { value: {} };
434+
for (const vkey of keys) {
435+
result.value[vkey] = picked.filter(p => utils.isObject(p) && (utils.getProperty(p, vkey) !== undefined)).map(p => p[vkey]).join(separator);
434436
}
435-
return value;
436-
}).join(separator)};
437-
}
437+
} else {
438+
result = { value: picked.map(value => {
439+
if (utils.isObject(value)) {
440+
value = storeStringRemember2(args, value);
441+
}
442+
return value;
443+
}).join(separator)};
444+
}
445+
return result;
446+
};
447+
let resultAllowed = result.filter(e => groups.some(g => g.allowedLabel(pickContext, e.label)));
448+
groups.filter(g => g.key).forEach(g => {
449+
let picked = resultAllowed.filter(e => g.allowedLabel(pickContext, e.label));
450+
let result = resultFor(picked, g);
451+
storeStringRemember2(g, result.value);
452+
});
453+
result = resultFor(resultAllowed, args);
438454
}
439455
let rememberTransformed = utils.getProperty(args, 'rememberTransformed', false);
440456
// @ts-ignore

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Calculate command variables for launch.json and tasks.json",
55
"publisher": "rioj7",
66
"license": "MIT",
7-
"version": "1.66.0",
7+
"version": "1.67.0",
88
"engines": {"vscode": "^1.55.0"},
99
"categories": ["Other"],
1010
"keywords": ["command","variable","launch","tasks","date","multi-root ready","pick","file","key","value","uuid","clipboard","number","remember"],

0 commit comments

Comments
 (0)