Skip to content

Commit 5ced132

Browse files
committed
add and insert now work with blobs
1 parent b43a1ce commit 5ced132

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/vimscript/expression/evaluate.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,10 +693,16 @@ export class EvaluationContext {
693693
return float(Math.acos(toFloat(x!)));
694694
}
695695
case 'add': {
696-
const [l, expr] = getArgs(2);
697-
// TODO: should also work with blob
696+
const [l, item] = getArgs(2);
697+
if (l!.type === 'blob') {
698+
const newBytes = new Uint8Array(l!.data.byteLength + 1);
699+
newBytes.set(new Uint8Array(l!.data));
700+
newBytes[newBytes.length - 1] = toInt(item!);
701+
l!.data = newBytes.buffer;
702+
return blob(newBytes);
703+
}
698704
const lst = toList(l!);
699-
lst.items.push(expr!);
705+
lst.items.push(item!);
700706
return lst;
701707
}
702708
case 'asin': {
@@ -1013,7 +1019,18 @@ export class EvaluationContext {
10131019
case 'insert': {
10141020
const [l, item, _idx] = getArgs(2, 3);
10151021
const idx = _idx ? toInt(_idx) : 0;
1016-
// TODO: should also work with blob
1022+
if (l!.type === 'blob') {
1023+
if (idx > l!.data.byteLength) {
1024+
throw VimError.fromCode(ErrorCode.InvalidArgument475, idx.toString());
1025+
}
1026+
const bytes = new Uint8Array(l!.data);
1027+
const newBytes = new Uint8Array(bytes.length + 1);
1028+
newBytes.set(bytes.subarray(0, idx), 0);
1029+
newBytes[idx] = toInt(item!);
1030+
newBytes.set(bytes.subarray(idx), idx + 1);
1031+
l!.data = newBytes.buffer;
1032+
return blob(newBytes);
1033+
}
10171034
const lst = toList(l!);
10181035
if (idx > lst.items.length) {
10191036
throw VimError.fromCode(ErrorCode.ListIndexOutOfRange, idx.toString());

test/vimscript/expression.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,11 @@ suite('Vimscript expressions', () => {
570570
exprTest('assert_report("whatever")', FAIL);
571571
});
572572

573-
suite('count', () => {
573+
suite('add', () => {
574574
exprTest('add([1,2,3], 4)', { display: '[1, 2, 3, 4]' });
575575
exprTest('add(add(add([], 1), 2), 3)', { display: '[1, 2, 3]' });
576+
577+
exprTest('add(0zABCD, 0xEF)', { display: '0zABCDEF' });
576578
});
577579

578580
suite('count', () => {
@@ -677,6 +679,9 @@ suite('Vimscript expressions', () => {
677679
exprTest('insert([1,2,3], 4)', { display: '[4, 1, 2, 3]' });
678680
exprTest('insert([1,2,3], 4, 2)', { display: '[1, 2, 4, 3]' });
679681
exprTest('insert(insert(insert([], 1), 2), 3)', { display: '[3, 2, 1]' });
682+
683+
exprTest('insert(0zABCD, 0xEF)', { display: '0zEFABCD' });
684+
exprTest('insert(0zABCD, 0xEF, 1)', { display: '0zABEFCD' });
680685
});
681686

682687
suite('invert', () => {

0 commit comments

Comments
 (0)