* The ranking with a Swift file's `#Preview { … }` calls taken out. * * A preview sits at the top level of a SwiftUI view's file, so its body's * constructions are module-level edges out of the file — and every view with * a preview ranked as "a file that runs something". A preview is Xcode's *
(cg: CodeGraph, rows: T[])
| 286 | * previews leaves the list; the rest are re-ranked on what is left. |
| 287 | */ |
| 288 | function withoutPreviews<T extends { nodeId: string; filePath: string; calls: number; reaches: number; score: number }>(cg: CodeGraph, rows: T[]): T[] { |
| 289 | let adjusted = false; |
| 290 | const out: T[] = []; |
| 291 | for (const row of rows) { |
| 292 | if (!row.filePath.endsWith('.swift')) { |
| 293 | out.push(row); |
| 294 | continue; |
| 295 | } |
| 296 | const text = readIndexedFileText(cg, cg.getProjectRoot(), row.filePath, MAX_PREVIEW_SCAN); |
| 297 | const spans = text && text.includes('#Preview') ? swiftPreviewSpans(text) : []; |
| 298 | if (spans.length === 0) { |
| 299 | out.push(row); |
| 300 | continue; |
| 301 | } |
| 302 | const inPreview = cg |
| 303 | .getOutgoingEdges(row.nodeId) |
| 304 | .filter((e) => (e.kind === 'calls' || e.kind === 'instantiates') && e.line != null && spans.some(([from, to]) => e.line! >= from && e.line! <= to)).length; |
| 305 | if (inPreview === 0) { |
| 306 | out.push(row); |
| 307 | continue; |
| 308 | } |
| 309 | adjusted = true; |
| 310 | const calls = row.calls - inPreview; |
| 311 | if (calls > 0) out.push({ ...row, calls, score: calls * (1 + row.reaches) }); |
| 312 | } |
| 313 | if (adjusted) out.sort((a, b) => b.score - a.score || b.calls - a.calls || (a.filePath < b.filePath ? -1 : a.filePath > b.filePath ? 1 : 0)); |
| 314 | return out; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * The line spans (1-based, inclusive) of a Swift file's top-level |
no test coverage detected