( startLine: number, lines: readonly string[], callLines: readonly number[] )
| 143 | * @param callLines every line in the body that makes an outgoing edge |
| 144 | */ |
| 145 | export function buildCodeBlock( |
| 146 | startLine: number, |
| 147 | lines: readonly string[], |
| 148 | callLines: readonly number[] |
| 149 | ): CodeBlock { |
| 150 | const endLine = startLine + lines.length - 1; |
| 151 | const slice = (from: number, to: number): SourceWindow => ({ |
| 152 | start: from, |
| 153 | lines: lines.slice(from - startLine, to - startLine + 1), |
| 154 | }); |
| 155 | |
| 156 | if (lines.length <= FULL_BODY_LINES) { |
| 157 | return { |
| 158 | windows: lines.length > 0 ? [slice(startLine, endLine)] : [], |
| 159 | gapsAfter: [], |
| 160 | tailGap: 0, |
| 161 | whole: true, |
| 162 | }; |
| 163 | } |
| 164 | |
| 165 | const headEnd = Math.min(endLine, startLine + HEAD_LINES - 1); |
| 166 | const ranges: Array<[number, number]> = [[startLine, headEnd]]; |
| 167 | const sites = [...new Set(callLines)] |
| 168 | .filter((line) => line > headEnd && line <= endLine) |
| 169 | .sort((a, b) => a - b); |
| 170 | for (const line of sites) { |
| 171 | ranges.push([ |
| 172 | Math.max(startLine, line - WINDOW_CONTEXT), |
| 173 | Math.min(endLine, line + WINDOW_CONTEXT), |
| 174 | ]); |
| 175 | } |
| 176 | |
| 177 | const merged: Array<[number, number]> = []; |
| 178 | for (const range of ranges) { |
| 179 | const last = merged[merged.length - 1]; |
| 180 | if (last && range[0] <= last[1] + WINDOW_MERGE_GAP) last[1] = Math.max(last[1], range[1]); |
| 181 | else merged.push([...range] as [number, number]); |
| 182 | } |
| 183 | |
| 184 | const kept = merged.slice(0, MAX_WINDOWS); |
| 185 | const windows = kept.map(([from, to]) => slice(from, to)); |
| 186 | const gapsAfter = kept.slice(0, -1).map((range, i) => (kept[i + 1] as [number, number])[0] - range[1] - 1); |
| 187 | const lastEnd = kept[kept.length - 1]?.[1] ?? endLine; |
| 188 | |
| 189 | return { windows, gapsAfter, tailGap: Math.max(0, endLine - lastEnd), whole: false }; |
| 190 | } |
| 191 | |
| 192 | /* ------------------------------------------------------------------ refs -- */ |
| 193 |
no test coverage detected