(h: Highlight, theme: Theme, ranges: Range[])
| 766 | } |
| 767 | |
| 768 | function applyBackground(h: Highlight, theme: Theme, ranges: Range[]): void { |
| 769 | if (!h.marker) return |
| 770 | const lineBg = lineBackground(h.marker, theme) |
| 771 | const wordBg = wordBackground(h.marker, theme) |
| 772 | |
| 773 | let rangeIdx = 0 |
| 774 | let byteOff = 0 |
| 775 | for (let li = 0; li < h.lines.length; li++) { |
| 776 | const newLine: Block[] = [] |
| 777 | for (const [style, text] of h.lines[li]!) { |
| 778 | const textStart = byteOff |
| 779 | const textEnd = byteOff + text.length |
| 780 | |
| 781 | while (rangeIdx < ranges.length && ranges[rangeIdx]!.end <= textStart) { |
| 782 | rangeIdx++ |
| 783 | } |
| 784 | if (rangeIdx >= ranges.length) { |
| 785 | newLine.push([{ ...style, background: lineBg }, text]) |
| 786 | byteOff = textEnd |
| 787 | continue |
| 788 | } |
| 789 | |
| 790 | let remaining = text |
| 791 | let pos = textStart |
| 792 | while (remaining.length > 0 && rangeIdx < ranges.length) { |
| 793 | const r = ranges[rangeIdx]! |
| 794 | const inRange = pos >= r.start && pos < r.end |
| 795 | let next: number |
| 796 | if (inRange) { |
| 797 | next = Math.min(r.end, textEnd) |
| 798 | } else if (r.start > pos && r.start < textEnd) { |
| 799 | next = r.start |
| 800 | } else { |
| 801 | next = textEnd |
| 802 | } |
| 803 | const segLen = next - pos |
| 804 | const seg = remaining.slice(0, segLen) |
| 805 | newLine.push([{ ...style, background: inRange ? wordBg : lineBg }, seg]) |
| 806 | remaining = remaining.slice(segLen) |
| 807 | pos = next |
| 808 | if (pos >= r.end) rangeIdx++ |
| 809 | } |
| 810 | if (remaining.length > 0) { |
| 811 | newLine.push([{ ...style, background: lineBg }, remaining]) |
| 812 | } |
| 813 | byteOff = textEnd |
| 814 | } |
| 815 | h.lines[li] = newLine |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | function intoLines( |
| 820 | h: Highlight, |
no test coverage detected