Skip to content

Commit be2d4d0

Browse files
committed
fix #9: use an explicit newline character
1 parent 867aa01 commit be2d4d0

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

code.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@
600600
let raw = lines[line];
601601
let runs = [];
602602
let i = 0;
603-
let n = raw.length;
603+
let n = raw.length + 1; // Add 1 for the extra character at the end
604604
let column = 0;
605605

606606
while (i < n) {
@@ -624,6 +624,16 @@
624624
break;
625625
}
626626

627+
// Draw each newline into its own run
628+
if (c1 !== c1 /* end of line */) {
629+
if (i > startIndex) break;
630+
isSingleChunk = true;
631+
column++;
632+
i++;
633+
whitespace = 0x0A /* newline */;
634+
break;
635+
}
636+
627637
// Draw each non-ASCII character into its own run (e.g. emoji)
628638
if (c1 < 0x20 || c1 > 0x7E) {
629639
if (i > startIndex) break;
@@ -701,14 +711,15 @@
701711
}
702712

703713
runs.push({
704-
isWhitespace: !!whitespace,
714+
whitespace,
705715
startIndex, endIndex: i,
706716
startColumn, endColumn: column,
707717
isSingleChunk,
708718
text:
709719
!whitespace ? raw.slice(startIndex, i) :
710720
whitespace === 0x20 /* space */ ? '·'.repeat(i - startIndex) :
711-
'→' /* tab */,
721+
whitespace === 0x0A /* newline */ ? line + 1 === lines.length ? '∅' : '↵' :
722+
'→' /* tab */,
712723
});
713724
}
714725

@@ -789,10 +800,15 @@
789800
let runCount = runs.length;
790801
let endOfLineIndex = 0;
791802
let endOfLineColumn = 0;
803+
let beforeNewlineIndex = 0;
804+
let hasTrailingNewline = false;
792805

793806
if (runCount > 0) {
794-
endOfLineIndex = runs[runCount - 1].endIndex;
795-
endOfLineColumn = runs[runCount - 1].endColumn;
807+
let lastRun = runs[runCount - 1];
808+
endOfLineIndex = lastRun.endIndex;
809+
endOfLineColumn = lastRun.endColumn;
810+
beforeNewlineIndex = lastRun.startIndex;
811+
hasTrailingNewline = lastRun.whitespace === 0x0A /* newline */;
796812

797813
// Binary search to find the first run
798814
firstRun = 0;
@@ -889,7 +905,10 @@
889905
function rangeOfMapping(map) {
890906
if (mappings[map + mappingsOffset] !== row) return null;
891907
let startIndex = mappings[map + mappingsOffset + 1];
892-
let endIndex = startIndex > endOfLineIndex ? startIndex : endOfLineIndex;
908+
let endIndex =
909+
startIndex > endOfLineIndex ? startIndex :
910+
hasTrailingNewline && startIndex < beforeNewlineIndex ? beforeNewlineIndex :
911+
endOfLineIndex;
893912
let isLastMappingInLine = false;
894913

895914
// Ignore subsequent duplicate mappings
@@ -1130,7 +1149,7 @@
11301149
// Draw the runs
11311150
let currentColumn = firstColumn;
11321151
for (let run = firstRun; run <= lastRun; run++) {
1133-
let { isWhitespace, text, startColumn, endColumn, isSingleChunk } = runs[run];
1152+
let { whitespace, text, startColumn, endColumn, isSingleChunk } = runs[run];
11341153

11351154
// Limit the run to the visible columns (but only for ASCII runs)
11361155
if (!isSingleChunk) {
@@ -1145,7 +1164,7 @@
11451164
}
11461165

11471166
// Draw whitespace in a separate batch
1148-
(isWhitespace ? whitespaceBatch : textBatch).push(text, dx + startColumn * columnWidth, dy);
1167+
(whitespace ? whitespaceBatch : textBatch).push(text, dx + startColumn * columnWidth, dy);
11491168
currentColumn = endColumn;
11501169
}
11511170
}

0 commit comments

Comments
 (0)