(src: string, baseLine: number)
| 17 | * never produce a match — then answers each call with a binary search. |
| 18 | */ |
| 19 | export function makeLineAt(src: string, baseLine: number): (idx: number) => number { |
| 20 | let nl: number[] | null = null; |
| 21 | return (idx: number) => { |
| 22 | if (!nl) { |
| 23 | nl = []; |
| 24 | for (let i = src.indexOf('\n'); i !== -1; i = src.indexOf('\n', i + 1)) nl.push(i); |
| 25 | } |
| 26 | // Count newlines strictly before idx. |
| 27 | let lo = 0; |
| 28 | let hi = nl.length; |
| 29 | while (lo < hi) { |
| 30 | const mid = (lo + hi) >> 1; |
| 31 | if (nl[mid]! < idx) lo = mid + 1; |
| 32 | else hi = mid; |
| 33 | } |
| 34 | return baseLine + lo; |
| 35 | }; |
| 36 | } |
| 37 | |
| 38 | /** Innermost function/method node whose line range contains `line`. */ |
| 39 | export function enclosingFn(nodesInFile: readonly Node[], line: number): Node | null { |
no test coverage detected