@@ -72,130 +72,80 @@ export class FileDiffUtil {
7272 }
7373
7474 public getFileDiff ( filename : string , originalFileContent : string , modifiedFileContent : string ) : DiffPair [ ] {
75- const patch : string = createPatch ( '' , originalFileContent , modifiedFileContent ) ;
7675 try {
77- // Split the patch into lines
76+ const originalLines = originalFileContent . split ( '\n' ) ;
77+ const modifiedLines = modifiedFileContent . split ( '\n' ) ;
78+ const patch : string = createPatch ( '' , originalFileContent , modifiedFileContent ) ;
7879 const patchLines = patch . split ( '\n' ) ;
7980
80- // Initialize variables to track line numbers
81- let oldLineNumber = 1 ;
82- let newLineNumber = 1 ;
83- let firstPlusAlreadySkipped = false ;
84- let firstMinusAlreadySkipped = false ;
85- const diff : DiffPair [ ] = [ ] ;
86- // Initialize result as HTML string
81+ let origIdx = 0 ;
82+ let modIdx = 0 ;
83+ const result : DiffPair [ ] = [ ] ;
8784
88- patchLines . forEach ( ( line ) => {
89- // Parse the hunk header (e.g., @@ -2,3 +2,3 @@)
90- const hunkHeader = / ^ @ @ - ( \d + ) , \d + \+ ( \d + ) , \d + @ @ / ;
85+ // Skip the first line of the patch (the file header)
86+ let i = 0 ;
87+ while ( i < patchLines . length ) {
88+ const line = patchLines [ i ] ;
89+ const hunkHeader = / ^ @ @ - ( \d + ) , ? ( \d * ) \+ ( \d + ) , ? ( \d * ) @ @ / ;
9190 const match = hunkHeader . exec ( line ) ;
9291
9392 if ( match ) {
94- oldLineNumber = parseInt ( match [ 1 ] , 10 ) ;
95- newLineNumber = parseInt ( match [ 2 ] , 10 ) ;
96- } else if ( line . startsWith ( '-' ) ) {
97- // Skip the first line difference
98- if ( oldLineNumber === 1 && ! firstMinusAlreadySkipped ) {
99- firstMinusAlreadySkipped = true ;
100- // Skip the first line difference
101- oldLineNumber ++ ;
102- return ;
103- }
104- diff . push ( { old : line . slice ( 1 ) , new : null } ) ;
105- oldLineNumber ++ ;
106- } else if ( line . startsWith ( '+' ) ) {
107- // Skip the first line difference
108- if ( newLineNumber === 1 && ! firstPlusAlreadySkipped ) {
109- firstPlusAlreadySkipped = true ;
110- newLineNumber ++ ;
111- return ;
112- }
113- diff . push ( { old : null , new : line . slice ( 1 ) } ) ;
114- newLineNumber ++ ;
115- } else if ( line . startsWith ( ' ' ) ) {
116- diff . push ( { old : line . slice ( 1 ) , new : line . slice ( 1 ) } ) ;
117- // Unchanged line, skip it
118- oldLineNumber ++ ;
119- newLineNumber ++ ;
120- }
121- } ) ;
122- // Return the diff array
123- return diff ;
124- } catch ( error ) {
125- Logger . error ( `Error in FileDiffUtil: ${ String ( error ) } ` ) ;
126- }
127- }
128-
129- public getFullFileDiff ( filename : string , originalFileContent : string , modifiedFileContent : string ) : DiffPair [ ] {
130- const originalLines = originalFileContent . split ( '\n' ) ;
131- const modifiedLines = modifiedFileContent . split ( '\n' ) ;
132- const patch : string = createPatch ( '' , originalFileContent , modifiedFileContent ) ;
133- const patchLines = patch . split ( '\n' ) ;
93+ // Move to the start of the hunk
94+ const origStart = parseInt ( match [ 1 ] , 10 ) - 1 ;
95+ const modStart = parseInt ( match [ 3 ] , 10 ) - 1 ;
13496
135- let origIdx = 0 ;
136- let modIdx = 0 ;
137- const result : DiffPair [ ] = [ ] ;
138-
139- // Skip the first line of the patch (the file header)
140- let i = 0 ;
141- while ( i < patchLines . length ) {
142- const line = patchLines [ i ] ;
143- const hunkHeader = / ^ @ @ - ( \d + ) , ? ( \d * ) \+ ( \d + ) , ? ( \d * ) @ @ / ;
144- const match = hunkHeader . exec ( line ) ;
145-
146- if ( match ) {
147- // Move to the start of the hunk
148- const origStart = parseInt ( match [ 1 ] , 10 ) - 1 ;
149- const modStart = parseInt ( match [ 3 ] , 10 ) - 1 ;
150-
151- // Emit unchanged lines before the hunk
152- while ( origIdx < origStart && modIdx < modStart ) {
153- result . push ( { old : originalLines [ origIdx ] , new : modifiedLines [ modIdx ] } ) ;
154- origIdx ++ ;
155- modIdx ++ ;
156- }
157-
158- i ++ ;
159- // Now process the hunk lines
160- while ( i < patchLines . length && ! patchLines [ i ] . startsWith ( '@@' ) ) {
161- const hunkLine = patchLines [ i ] ;
162- if ( hunkLine . startsWith ( '-' ) ) {
163- result . push ( { old : originalLines [ origIdx ] , new : null } ) ;
164- origIdx ++ ;
165- } else if ( hunkLine . startsWith ( '+' ) ) {
166- result . push ( { old : null , new : modifiedLines [ modIdx ] } ) ;
167- modIdx ++ ;
168- } else if ( hunkLine . startsWith ( ' ' ) ) {
97+ // Emit unchanged lines before the hunk
98+ while ( origIdx < origStart && modIdx < modStart ) {
16999 result . push ( { old : originalLines [ origIdx ] , new : modifiedLines [ modIdx ] } ) ;
170100 origIdx ++ ;
171101 modIdx ++ ;
172102 }
103+
104+ i ++ ;
105+ // Now process the hunk lines
106+ while ( i < patchLines . length && ! patchLines [ i ] . startsWith ( '@@' ) ) {
107+ const hunkLine = patchLines [ i ] ;
108+ if ( hunkLine . startsWith ( '-' ) ) {
109+ result . push ( { old : originalLines [ origIdx ] , new : null } ) ;
110+ origIdx ++ ;
111+ } else if ( hunkLine . startsWith ( '+' ) ) {
112+ result . push ( { old : null , new : modifiedLines [ modIdx ] } ) ;
113+ modIdx ++ ;
114+ } else if ( hunkLine . startsWith ( ' ' ) ) {
115+ result . push ( { old : originalLines [ origIdx ] , new : modifiedLines [ modIdx ] } ) ;
116+ origIdx ++ ;
117+ modIdx ++ ;
118+ }
119+ i ++ ;
120+ }
121+ } else {
173122 i ++ ;
174123 }
175- } else {
176- i ++ ;
177124 }
178- }
179125
180- // Emit any remaining unchanged lines at the end
181- while ( origIdx < originalLines . length && modIdx < modifiedLines . length ) {
182- result . push ( { old : originalLines [ origIdx ] , new : modifiedLines [ modIdx ] } ) ;
183- origIdx ++ ;
184- modIdx ++ ;
185- }
186- // If there are trailing additions or deletions
187- while ( origIdx < originalLines . length ) {
188- result . push ( { old : originalLines [ origIdx ] , new : null } ) ;
189- origIdx ++ ;
190- }
191- while ( modIdx < modifiedLines . length ) {
192- result . push ( { old : null , new : modifiedLines [ modIdx ] } ) ;
193- modIdx ++ ;
194- }
126+ // Emit any remaining unchanged lines at the end
127+ while ( origIdx < originalLines . length && modIdx < modifiedLines . length ) {
128+ result . push ( { old : originalLines [ origIdx ] , new : modifiedLines [ modIdx ] } ) ;
129+ origIdx ++ ;
130+ modIdx ++ ;
131+ }
132+ // If there are trailing additions or deletions
133+ while ( origIdx < originalLines . length ) {
134+ result . push ( { old : originalLines [ origIdx ] , new : null } ) ;
135+ origIdx ++ ;
136+ }
137+ while ( modIdx < modifiedLines . length ) {
138+ result . push ( { old : null , new : modifiedLines [ modIdx ] } ) ;
139+ modIdx ++ ;
140+ }
195141
196- // Only return if there are any changes
197- const hasChanges = result . some ( ( diff ) => diff . old !== diff . new ) ;
198- return hasChanges ? result : [ ] ;
142+ // Only return if there are any changes
143+ const hasChanges = result . some ( ( diff ) => diff . old !== diff . new ) ;
144+ return hasChanges ? result : [ ] ;
145+ } catch ( error ) {
146+ Logger . error ( `Error in FileDiffUtil: ${ String ( error ) } ` ) ;
147+ return [ ] ;
148+ }
199149 }
200150
201151 public getXMLDiff ( originalFileContent : string , modifiedFileContent : string ) : DiffPair [ ] {
0 commit comments