(filePath: string, oldContent: string, newContent: string)
| 146 | } |
| 147 | |
| 148 | function formatMinimalDiff(filePath: string, oldContent: string, newContent: string): string | null { |
| 149 | // Normalize line endings in both inputs |
| 150 | const normalize = (str: string) => str.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); |
| 151 | const normalizedOld = normalize(oldContent); |
| 152 | const normalizedNew = normalize(newContent); |
| 153 | |
| 154 | if (normalizedOld === normalizedNew) { |
| 155 | return null; |
| 156 | } |
| 157 | |
| 158 | // Using context: 2 to show some surrounding lines |
| 159 | const patch = Diff.createPatch(filePath, normalizedOld, normalizedNew, '', '', { context: 2 }); |
| 160 | const patchLines = patch.split('\n'); |
| 161 | const effectiveChangeLines: string[] = []; |
| 162 | |
| 163 | let i = 0; |
| 164 | while (i < patchLines.length) { |
| 165 | const line = patchLines[i]; |
| 166 | |
| 167 | // Skip standard patch headers and hunk metadata |
| 168 | if (line.startsWith('---') || line.startsWith('+++') || line.startsWith('@@')) { |
| 169 | i++; |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | // Check for identical remove/add pairs (which means no effective change for these two lines) |
| 174 | if (line.startsWith('-')) { |
| 175 | let nextDiffLineIndex = i + 1; |
| 176 | // Skip empty lines AND "No newline" markers AND context lines to find the next actual diff line |
| 177 | while (nextDiffLineIndex < patchLines.length && |
| 178 | (patchLines[nextDiffLineIndex].trim() === '' || |
| 179 | patchLines[nextDiffLineIndex].startsWith('\\') || |
| 180 | patchLines[nextDiffLineIndex].startsWith(' '))) { |
| 181 | nextDiffLineIndex++; |
| 182 | } |
| 183 | |
| 184 | if (nextDiffLineIndex < patchLines.length && patchLines[nextDiffLineIndex].startsWith('+')) { |
| 185 | const removedText = line.substring(1).trim(); |
| 186 | const addedText = patchLines[nextDiffLineIndex].substring(1).trim(); |
| 187 | if (removedText === addedText) { |
| 188 | // Advance i past the current line, any skipped empty lines, and the matched added line |
| 189 | i = nextDiffLineIndex + 1; |
| 190 | continue; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // If the line starts with +, -, or space (context), it's a line to be included |
| 196 | if (line.startsWith('+') || line.startsWith('-') || line.startsWith(' ')) { |
| 197 | effectiveChangeLines.push(line); |
| 198 | } |
| 199 | |
| 200 | i++; |
| 201 | } |
| 202 | |
| 203 | if (effectiveChangeLines.length > 0) { |
| 204 | return `\`\`\`diff\n${filePath}\n${effectiveChangeLines.join('\n')}\n\`\`\`\n\n`; |
| 205 | } |
no test coverage detected