Skip to content

Commit 6feca9b

Browse files
committed
Fix going to a position in the simple editor with one line of code
Off-by-one errors are the worst.
1 parent 5156862 commit 6feca9b

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

ui/frontend/editor/SimpleEditor.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ class CodeByteOffsets {
1515

1616
public lineToOffsets(line: number) {
1717
const precedingBytes = this.bytesBeforeLine(line);
18+
const nextPrecedingBytes = this.bytesBeforeLine(line + 1);
1819

19-
const highlightedLine = this.lines[line];
20-
const highlightedBytes = highlightedLine.length;
21-
22-
return [precedingBytes, precedingBytes + highlightedBytes];
20+
return [precedingBytes, nextPrecedingBytes];
2321
}
2422

2523
public rangeToOffsets(start: Position, end: Position) {
@@ -40,7 +38,7 @@ class CodeByteOffsets {
4038
const precedingLines = this.lines.slice(0, line);
4139

4240
// Add one to account for the newline we split on and removed
43-
return precedingLines.map((l) => l.length + 1).reduce((a, b) => a + b);
41+
return precedingLines.map((l) => l.length + 1).reduce((a, b) => a + b, 0);
4442
}
4543
}
4644

0 commit comments

Comments
 (0)