Skip to content

Commit ae7a764

Browse files
committed
map() modifies the container it iterates over; mapnew() does not
1 parent 918e5d0 commit ae7a764

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/vimscript/expression/evaluate.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -923,8 +923,8 @@ export class EvaluationContext {
923923
const [x] = getArgs(1);
924924
return float(Math.exp(toFloat(x!)));
925925
}
926-
// TODO: extend()
927-
// TODO: filter()
926+
// TODO: extend/extendnew()
927+
// TODO: filter/filternew()
928928
// TODO: flatten()
929929
case 'float2nr': {
930930
const [x] = getArgs(1);
@@ -1176,28 +1176,31 @@ export class EvaluationContext {
11761176
const [x] = getArgs(1);
11771177
return float(Math.log10(toFloat(x!)));
11781178
}
1179-
case 'map': {
1179+
case 'map':
1180+
case 'mapnew': {
11801181
const [seq, fn] = getArgs(2);
11811182
switch (seq?.type) {
11821183
case 'list':
1183-
return list(
1184-
seq.items.map((val, idx) => {
1185-
switch (fn?.type) {
1186-
case 'funcref':
1187-
return this.evaluate(funcrefCall(fn, [int(idx), val]));
1188-
default:
1189-
this.localScopes.push(
1190-
new Map([
1191-
['v:key', new Variable(int(idx))],
1192-
['v:val', new Variable(val)],
1193-
]),
1194-
);
1195-
const retval = this.evaluate(expressionParser.tryParse(toString(fn!)));
1196-
this.localScopes.pop();
1197-
return retval;
1198-
}
1199-
}),
1200-
);
1184+
const newItems = seq.items.map((val, idx) => {
1185+
switch (fn?.type) {
1186+
case 'funcref':
1187+
return this.evaluate(funcrefCall(fn, [int(idx), val]));
1188+
default:
1189+
this.localScopes.push(
1190+
new Map([
1191+
['v:key', new Variable(int(idx))],
1192+
['v:val', new Variable(val)],
1193+
]),
1194+
);
1195+
const retval = this.evaluate(expressionParser.tryParse(toString(fn!)));
1196+
this.localScopes.pop();
1197+
return retval;
1198+
}
1199+
});
1200+
if (call.func === 'map') {
1201+
seq.items = newItems;
1202+
}
1203+
return list(newItems);
12011204
case 'dict_val':
12021205
// TODO
12031206
// case 'blob':

0 commit comments

Comments
 (0)