|
1 | | -/* eslint-disable @typescript-eslint/explicit-member-accessibility */ |
2 | | -/* eslint-disable no-console */ |
3 | 1 | /* eslint-disable @typescript-eslint/no-unsafe-call */ |
| 2 | +/* eslint-disable @typescript-eslint/explicit-member-accessibility */ |
| 3 | +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ |
4 | 4 | /* eslint-disable @typescript-eslint/no-unsafe-assignment */ |
5 | 5 | import { createPatch } from 'diff'; |
| 6 | +import { Logger } from '../../../utils/logger'; |
6 | 7 |
|
7 | 8 | export class FileDiffUtil { |
8 | 9 | public getFileDiff(filename: string, originalFileContent: string, modifiedFileContent: string): string { |
9 | | - const patch: string = createPatch(filename, originalFileContent, modifiedFileContent); |
10 | | - |
11 | | - // Split the patch into lines |
12 | | - const patchLines = patch.split('\n'); |
| 10 | + const patch: string = createPatch('', originalFileContent, modifiedFileContent); |
| 11 | + try { |
| 12 | + // Split the patch into lines |
| 13 | + const patchLines = patch.split('\n'); |
13 | 14 |
|
14 | | - // Initialize variables to track line numbers |
15 | | - let oldLineNumber = 1; |
16 | | - let newLineNumber = 1; |
| 15 | + // Initialize variables to track line numbers |
| 16 | + let oldLineNumber = 1; |
| 17 | + let newLineNumber = 1; |
17 | 18 |
|
18 | | - // Initialize result as HTML string |
19 | | - let result = ''; |
| 19 | + // Initialize result as HTML string |
| 20 | + let result = ''; |
20 | 21 |
|
21 | | - patchLines.forEach((line) => { |
22 | | - // Parse the hunk header (e.g., @@ -2,3 +2,3 @@) |
23 | | - const hunkHeader = /^@@ -(\d+),\d+ \+(\d+),\d+ @@/; |
24 | | - const match = hunkHeader.exec(line); |
| 22 | + patchLines.forEach((line) => { |
| 23 | + // Parse the hunk header (e.g., @@ -2,3 +2,3 @@) |
| 24 | + const hunkHeader = /^@@ -(\d+),\d+ \+(\d+),\d+ @@/; |
| 25 | + const match = hunkHeader.exec(line); |
25 | 26 |
|
26 | | - if (match) { |
27 | | - oldLineNumber = parseInt(match[1], 10); |
28 | | - newLineNumber = parseInt(match[2], 10); |
29 | | - result += `<div>${this.escapeHtml(line)}</div>`; |
30 | | - } else if (line.startsWith('-')) { |
31 | | - result += `<div style="color: red;">- Line ${oldLineNumber}: ${this.escapeHtml(line.slice(1))}</div>`; |
32 | | - oldLineNumber++; |
33 | | - } else if (line.startsWith('+')) { |
34 | | - result += `<div style="color: green;">+ Line ${newLineNumber}: ${this.escapeHtml(line.slice(1))}</div>`; |
35 | | - newLineNumber++; |
36 | | - } else if (line.startsWith(' ')) { |
37 | | - // Unchanged line, just increment both line counters |
38 | | - result += `<div>${this.escapeHtml(line)}</div>`; |
39 | | - oldLineNumber++; |
40 | | - newLineNumber++; |
41 | | - } |
42 | | - }); |
43 | | - // Return the result string with color codes |
44 | | - return result; |
| 27 | + if (match) { |
| 28 | + oldLineNumber = parseInt(match[1], 10); |
| 29 | + newLineNumber = parseInt(match[2], 10); |
| 30 | + } else if (line.startsWith('-')) { |
| 31 | + // Skip the first line difference |
| 32 | + if (oldLineNumber === 1) { |
| 33 | + oldLineNumber++; |
| 34 | + return; |
| 35 | + } |
| 36 | + result += `<div style="color: red;">- Line ${oldLineNumber}: ${this.escapeHtml(line.slice(1))}</div>`; |
| 37 | + oldLineNumber++; |
| 38 | + } else if (line.startsWith('+')) { |
| 39 | + // Skip the first line difference |
| 40 | + if (newLineNumber === 1) { |
| 41 | + newLineNumber++; |
| 42 | + return; |
| 43 | + } |
| 44 | + result += `<div style="color: green;">+ Line ${newLineNumber}: ${this.escapeHtml(line.slice(1))}</div>`; |
| 45 | + newLineNumber++; |
| 46 | + } else if (line.startsWith(' ')) { |
| 47 | + // Unchanged line, skip it |
| 48 | + oldLineNumber++; |
| 49 | + newLineNumber++; |
| 50 | + } |
| 51 | + }); |
| 52 | + // Return the result string, or an empty string if no differences |
| 53 | + return result.trim() ? result : ''; |
| 54 | + } catch (error) { |
| 55 | + Logger.logger.error('Error in FileDiffUtil', error.message); |
| 56 | + } |
45 | 57 | } |
46 | 58 |
|
47 | 59 | escapeHtml(text: string): string { |
|
0 commit comments