@@ -62,57 +62,79 @@ export class FileDiffUtil {
6262 }
6363
6464 public getFileDiff ( filename : string , originalFileContent : string , modifiedFileContent : string ) : DiffPair [ ] {
65- const patch : string = createPatch ( '' , originalFileContent , modifiedFileContent ) ;
6665 try {
67- // Split the patch into lines
66+ const originalLines = originalFileContent . split ( '\n' ) ;
67+ const modifiedLines = modifiedFileContent . split ( '\n' ) ;
68+ const patch : string = createPatch ( '' , originalFileContent , modifiedFileContent ) ;
6869 const patchLines = patch . split ( '\n' ) ;
6970
70- // Initialize variables to track line numbers
71- let oldLineNumber = 1 ;
72- let newLineNumber = 1 ;
73- let firstPlusAlreadySkipped = false ;
74- let firstMinusAlreadySkipped = false ;
75- const diff : DiffPair [ ] = [ ] ;
76- // Initialize result as HTML string
71+ let origIdx = 0 ;
72+ let modIdx = 0 ;
73+ const result : DiffPair [ ] = [ ] ;
7774
78- patchLines . forEach ( ( line ) => {
79- // Parse the hunk header (e.g., @@ -2,3 +2,3 @@)
80- const hunkHeader = / ^ @ @ - ( \d + ) , \d + \+ ( \d + ) , \d + @ @ / ;
75+ // Skip the first line of the patch (the file header)
76+ let i = 0 ;
77+ while ( i < patchLines . length ) {
78+ const line = patchLines [ i ] ;
79+ const hunkHeader = / ^ @ @ - ( \d + ) , ? ( \d * ) \+ ( \d + ) , ? ( \d * ) @ @ / ;
8180 const match = hunkHeader . exec ( line ) ;
8281
8382 if ( match ) {
84- oldLineNumber = parseInt ( match [ 1 ] , 10 ) ;
85- newLineNumber = parseInt ( match [ 2 ] , 10 ) ;
86- } else if ( line . startsWith ( '-' ) ) {
87- // Skip the first line difference
88- if ( oldLineNumber === 1 && ! firstMinusAlreadySkipped ) {
89- firstMinusAlreadySkipped = true ;
90- // Skip the first line difference
91- oldLineNumber ++ ;
92- return ;
83+ // Move to the start of the hunk
84+ const origStart = parseInt ( match [ 1 ] , 10 ) - 1 ;
85+ const modStart = parseInt ( match [ 3 ] , 10 ) - 1 ;
86+
87+ // Emit unchanged lines before the hunk
88+ while ( origIdx < origStart && modIdx < modStart ) {
89+ result . push ( { old : originalLines [ origIdx ] , new : modifiedLines [ modIdx ] } ) ;
90+ origIdx ++ ;
91+ modIdx ++ ;
9392 }
94- diff . push ( { old : line . slice ( 1 ) , new : null } ) ;
95- oldLineNumber ++ ;
96- } else if ( line . startsWith ( '+' ) ) {
97- // Skip the first line difference
98- if ( newLineNumber === 1 && ! firstPlusAlreadySkipped ) {
99- firstPlusAlreadySkipped = true ;
100- newLineNumber ++ ;
101- return ;
93+
94+ i ++ ;
95+ // Now process the hunk lines
96+ while ( i < patchLines . length && ! patchLines [ i ] . startsWith ( '@@' ) ) {
97+ const hunkLine = patchLines [ i ] ;
98+ if ( hunkLine . startsWith ( '-' ) ) {
99+ result . push ( { old : originalLines [ origIdx ] , new : null } ) ;
100+ origIdx ++ ;
101+ } else if ( hunkLine . startsWith ( '+' ) ) {
102+ result . push ( { old : null , new : modifiedLines [ modIdx ] } ) ;
103+ modIdx ++ ;
104+ } else if ( hunkLine . startsWith ( ' ' ) ) {
105+ result . push ( { old : originalLines [ origIdx ] , new : modifiedLines [ modIdx ] } ) ;
106+ origIdx ++ ;
107+ modIdx ++ ;
108+ }
109+ i ++ ;
102110 }
103- diff . push ( { old : null , new : line . slice ( 1 ) } ) ;
104- newLineNumber ++ ;
105- } else if ( line . startsWith ( ' ' ) ) {
106- diff . push ( { old : line . slice ( 1 ) , new : line . slice ( 1 ) } ) ;
107- // Unchanged line, skip it
108- oldLineNumber ++ ;
109- newLineNumber ++ ;
111+ } else {
112+ i ++ ;
110113 }
111- } ) ;
112- // Return the diff array
113- return diff ;
114+ }
115+
116+ // Emit any remaining unchanged lines at the end
117+ while ( origIdx < originalLines . length && modIdx < modifiedLines . length ) {
118+ result . push ( { old : originalLines [ origIdx ] , new : modifiedLines [ modIdx ] } ) ;
119+ origIdx ++ ;
120+ modIdx ++ ;
121+ }
122+ // If there are trailing additions or deletions
123+ while ( origIdx < originalLines . length ) {
124+ result . push ( { old : originalLines [ origIdx ] , new : null } ) ;
125+ origIdx ++ ;
126+ }
127+ while ( modIdx < modifiedLines . length ) {
128+ result . push ( { old : null , new : modifiedLines [ modIdx ] } ) ;
129+ modIdx ++ ;
130+ }
131+
132+ // Only return if there are any changes
133+ const hasChanges = result . some ( ( diff ) => diff . old !== diff . new ) ;
134+ return hasChanges ? result : [ ] ;
114135 } catch ( error ) {
115136 Logger . error ( `Error in FileDiffUtil: ${ String ( error ) } ` ) ;
137+ return [ ] ;
116138 }
117139 }
118140
0 commit comments