@@ -62,130 +62,80 @@ 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 ;
93- }
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 ;
102- }
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 ++ ;
110- }
111- } ) ;
112- // Return the diff array
113- return diff ;
114- } catch ( error ) {
115- Logger . error ( `Error in FileDiffUtil: ${ String ( error ) } ` ) ;
116- }
117- }
83+ // Move to the start of the hunk
84+ const origStart = parseInt ( match [ 1 ] , 10 ) - 1 ;
85+ const modStart = parseInt ( match [ 3 ] , 10 ) - 1 ;
11886
119- public getFullFileDiff ( filename : string , originalFileContent : string , modifiedFileContent : string ) : DiffPair [ ] {
120- const originalLines = originalFileContent . split ( '\n' ) ;
121- const modifiedLines = modifiedFileContent . split ( '\n' ) ;
122- const patch : string = createPatch ( '' , originalFileContent , modifiedFileContent ) ;
123- const patchLines = patch . split ( '\n' ) ;
124-
125- let origIdx = 0 ;
126- let modIdx = 0 ;
127- const result : DiffPair [ ] = [ ] ;
128-
129- // Skip the first line of the patch (the file header)
130- let i = 0 ;
131- while ( i < patchLines . length ) {
132- const line = patchLines [ i ] ;
133- const hunkHeader = / ^ @ @ - ( \d + ) , ? ( \d * ) \+ ( \d + ) , ? ( \d * ) @ @ / ;
134- const match = hunkHeader . exec ( line ) ;
135-
136- if ( match ) {
137- // Move to the start of the hunk
138- const origStart = parseInt ( match [ 1 ] , 10 ) - 1 ;
139- const modStart = parseInt ( match [ 3 ] , 10 ) - 1 ;
140-
141- // Emit unchanged lines before the hunk
142- while ( origIdx < origStart && modIdx < modStart ) {
143- result . push ( { old : originalLines [ origIdx ] , new : modifiedLines [ modIdx ] } ) ;
144- origIdx ++ ;
145- modIdx ++ ;
146- }
147-
148- i ++ ;
149- // Now process the hunk lines
150- while ( i < patchLines . length && ! patchLines [ i ] . startsWith ( '@@' ) ) {
151- const hunkLine = patchLines [ i ] ;
152- if ( hunkLine . startsWith ( '-' ) ) {
153- result . push ( { old : originalLines [ origIdx ] , new : null } ) ;
154- origIdx ++ ;
155- } else if ( hunkLine . startsWith ( '+' ) ) {
156- result . push ( { old : null , new : modifiedLines [ modIdx ] } ) ;
157- modIdx ++ ;
158- } else if ( hunkLine . startsWith ( ' ' ) ) {
87+ // Emit unchanged lines before the hunk
88+ while ( origIdx < origStart && modIdx < modStart ) {
15989 result . push ( { old : originalLines [ origIdx ] , new : modifiedLines [ modIdx ] } ) ;
16090 origIdx ++ ;
16191 modIdx ++ ;
16292 }
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 ++ ;
110+ }
111+ } else {
163112 i ++ ;
164113 }
165- } else {
166- i ++ ;
167114 }
168- }
169115
170- // Emit any remaining unchanged lines at the end
171- while ( origIdx < originalLines . length && modIdx < modifiedLines . length ) {
172- result . push ( { old : originalLines [ origIdx ] , new : modifiedLines [ modIdx ] } ) ;
173- origIdx ++ ;
174- modIdx ++ ;
175- }
176- // If there are trailing additions or deletions
177- while ( origIdx < originalLines . length ) {
178- result . push ( { old : originalLines [ origIdx ] , new : null } ) ;
179- origIdx ++ ;
180- }
181- while ( modIdx < modifiedLines . length ) {
182- result . push ( { old : null , new : modifiedLines [ modIdx ] } ) ;
183- modIdx ++ ;
184- }
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+ }
185131
186- // Only return if there are any changes
187- const hasChanges = result . some ( ( diff ) => diff . old !== diff . new ) ;
188- return hasChanges ? result : [ ] ;
132+ // Only return if there are any changes
133+ const hasChanges = result . some ( ( diff ) => diff . old !== diff . new ) ;
134+ return hasChanges ? result : [ ] ;
135+ } catch ( error ) {
136+ Logger . error ( `Error in FileDiffUtil: ${ String ( error ) } ` ) ;
137+ return [ ] ;
138+ }
189139 }
190140
191141 escapeHtml ( text : string ) : string {
0 commit comments