| 150 | }>; |
| 151 | |
| 152 | export function flattenHunks(hunks: readonly DiffHunk[]): readonly FlattenedDiffLine[] { |
| 153 | const result: FlattenedDiffLine[] = []; |
| 154 | |
| 155 | for (let hunkIndex = 0; hunkIndex < hunks.length; hunkIndex++) { |
| 156 | const hunk = hunks[hunkIndex]; |
| 157 | if (!hunk) continue; |
| 158 | |
| 159 | let oldLine = hunk.oldStart; |
| 160 | let newLine = hunk.newStart; |
| 161 | |
| 162 | for (let lineIndex = 0; lineIndex < hunk.lines.length; lineIndex++) { |
| 163 | const line = hunk.lines[lineIndex]; |
| 164 | if (!line) continue; |
| 165 | |
| 166 | let oldLineNum: number | null = null; |
| 167 | let newLineNum: number | null = null; |
| 168 | |
| 169 | if (line.type === "context") { |
| 170 | oldLineNum = oldLine++; |
| 171 | newLineNum = newLine++; |
| 172 | } else if (line.type === "delete") { |
| 173 | oldLineNum = oldLine++; |
| 174 | } else if (line.type === "add") { |
| 175 | newLineNum = newLine++; |
| 176 | } |
| 177 | |
| 178 | result.push({ |
| 179 | hunkIndex, |
| 180 | lineIndex, |
| 181 | line, |
| 182 | oldLineNum, |
| 183 | newLineNum, |
| 184 | }); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return Object.freeze(result); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Navigate between hunks. |