Skip to content

Commit a501f95

Browse files
committed
byte2line() and line2byte()
1 parent 95ea3bb commit a501f95

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/vimscript/expression/evaluate.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,15 @@ export class EvaluationContext {
880880
}
881881
return assertFailed(msg ? toString(msg) : `Expected True but got ${displayValue(actual!)}`);
882882
}
883+
case 'byte2line': {
884+
const [_byte] = getArgs(1);
885+
const byte = toInt(_byte!);
886+
if (byte <= 0) {
887+
return int(-1);
888+
}
889+
return int(this.vimState!.document.positionAt(byte - 1).line + 1);
890+
}
891+
// TODO: byteidx[comp]
883892
case 'call': {
884893
const [_func, arglist, dict] = getArgs(2, 3);
885894
if (arglist!.type !== 'list') {
@@ -1292,6 +1301,14 @@ export class EvaluationContext {
12921301
const [s, winid] = getArgs(1, 2);
12931302
return int(getpos(toString(s!)).lnum);
12941303
}
1304+
case 'line2byte': {
1305+
const [_line] = getArgs(1);
1306+
const line = toInt(_line!);
1307+
if (line <= 0) {
1308+
return int(-1);
1309+
}
1310+
return int(this.vimState!.document.offsetAt(new Position(line - 1, 0)) + 1);
1311+
}
12951312
case 'localtime': {
12961313
return int(Date.now() / 1000);
12971314
}

test/vimscript/expression.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,8 @@ suite('Vimscript expressions', () => {
594594
exprTest('add(0zABCD, 0xEF)', { display: '0zABCDEF' });
595595
});
596596

597+
// TODO: byte2line()/line2byte()
598+
597599
suite('call', () => {
598600
exprTest('call("abs", [-1])', { value: float(1) });
599601
exprTest('call(function("abs"), [-1])', { value: float(1) });

0 commit comments

Comments
 (0)