Skip to content

Commit b43a1ce

Browse files
committed
Implement insert()
1 parent 8ccf974 commit b43a1ce

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/vimscript/expression/evaluate.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ export class EvaluationContext {
831831
throw VimError.fromCode(ErrorCode.InvalidArgument474);
832832
}
833833
if (toInt(start) >= comp!.items.length) {
834-
throw VimError.fromCode(ErrorCode.ListIndexOutOfRange);
834+
throw VimError.fromCode(ErrorCode.ListIndexOutOfRange, toInt(start).toString());
835835
}
836836
while (toInt(start) < 0) {
837837
start = int(toInt(start) + comp!.items.length);
@@ -1010,7 +1010,17 @@ export class EvaluationContext {
10101010
}
10111011
// TODO: indexof()
10121012
// TODO: input()/inputlist()
1013-
// TODO: insert()
1013+
case 'insert': {
1014+
const [l, item, _idx] = getArgs(2, 3);
1015+
const idx = _idx ? toInt(_idx) : 0;
1016+
// TODO: should also work with blob
1017+
const lst = toList(l!);
1018+
if (idx > lst.items.length) {
1019+
throw VimError.fromCode(ErrorCode.ListIndexOutOfRange, idx.toString());
1020+
}
1021+
lst.items.splice(idx, 0, item!);
1022+
return lst;
1023+
}
10141024
case 'invert': {
10151025
const [x] = getArgs(1);
10161026
// eslint-disable-next-line no-bitwise

test/vimscript/expression.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,12 @@ suite('Vimscript expressions', () => {
673673
exprTest('index(["A","C","D","C"], "C", 5)', { value: int(-1) });
674674
});
675675

676+
suite('insert', () => {
677+
exprTest('insert([1,2,3], 4)', { display: '[4, 1, 2, 3]' });
678+
exprTest('insert([1,2,3], 4, 2)', { display: '[1, 2, 4, 3]' });
679+
exprTest('insert(insert(insert([], 1), 2), 3)', { display: '[3, 2, 1]' });
680+
});
681+
676682
suite('invert', () => {
677683
exprTest('invert(123)', { value: int(-124) });
678684
});

0 commit comments

Comments
 (0)