44/* eslint-disable @typescript-eslint/no-unsafe-assignment */
55import { createPatch } from 'diff' ;
66import { Logger } from '../../../utils/logger' ;
7+ import { DiffPair } from '../../interfaces' ;
78
89export class FileDiffUtil {
9- public getFileDiff ( filename : string , originalFileContent : string , modifiedFileContent : string ) : string {
10+ public static getDiffHTML ( diff : string , name : string ) : string {
11+ const diffArray : DiffPair [ ] = JSON . parse ( diff ) as DiffPair [ ] ;
12+ let result = '<div style="height: 120px; text-align: left; overflow-x: auto;">' ;
13+ if ( diffArray . length <= 6 ) {
14+ result += this . getDiffContent ( diff ) + '</div>' ;
15+ } else {
16+ result +=
17+ this . getDiffContent ( diff , 6 ) +
18+ '</div>' +
19+ `<button onclick="toggleDiffModal('${ name } ')" class="expandModalButton"><i class="fa-solid fa-up-right-and-down-left-from-center"></i></button>` ;
20+ result += `<div id="myModal_${ name } " class="diffModal" style="display: none;">
21+ <div class="diffModalContent">
22+ <span onclick="toggleDiffModal('${ name } ')" class="closeButton">×</span>
23+ <h2 class="modalHeader">Summary</h2>
24+ <p class="modalContent">${ this . getDiffContent ( diff , - 1 ) } </p>
25+ </div>
26+ </div>` ;
27+ }
28+ return result ;
29+ }
30+ /*
31+ This function provides the diff html based on the diff array recieved
32+ linelimit is the number of lines we want in the html, this is required as we are showing only few lines in the table not the entire diff
33+ For the entire diff we are using modal, hence the requirement of lineLimit
34+ */
35+ private static getDiffContent ( diff : string , lineLimit = - 1 ) : string {
36+ const diffArray : DiffPair [ ] = JSON . parse ( diff ) as DiffPair [ ] ;
37+ let result = '' ;
38+ let originalLine = 1 ;
39+ let modifiedLine = 1 ;
40+ let linecount = 0 ;
41+ for ( const { old : original , new : modified } of diffArray ) {
42+ if ( original === modified ) {
43+ result += `<div style="color: black;">• Line ${ modifiedLine } : ${ original } </div>` ;
44+ modifiedLine ++ ;
45+ originalLine ++ ;
46+ linecount ++ ;
47+ } else if ( original !== null && modified === null ) {
48+ result += `<div style="color: red;">- Line ${ originalLine } : ${ original } </div>` ;
49+ originalLine ++ ;
50+ linecount ++ ;
51+ } else if ( original === null && modified !== null ) {
52+ result += `<div style="color: green;">+ Line ${ modifiedLine } : ${ modified } </div>` ;
53+ modifiedLine ++ ;
54+ linecount ++ ;
55+ }
56+ if ( linecount >= lineLimit && lineLimit !== - 1 ) {
57+ result += '<div style="color: black;">..........</div>' ;
58+ break ;
59+ }
60+ }
61+ return result ;
62+ }
63+
64+ public getFileDiff ( filename : string , originalFileContent : string , modifiedFileContent : string ) : DiffPair [ ] {
1065 const patch : string = createPatch ( '' , originalFileContent , modifiedFileContent ) ;
1166 try {
1267 // Split the patch into lines
@@ -17,8 +72,8 @@ export class FileDiffUtil {
1772 let newLineNumber = 1 ;
1873 let firstPlusAlreadySkipped = false ;
1974 let firstMinusAlreadySkipped = false ;
75+ const diff : DiffPair [ ] = [ ] ;
2076 // Initialize result as HTML string
21- let result = '' ;
2277
2378 patchLines . forEach ( ( line ) => {
2479 // Parse the hunk header (e.g., @@ -2,3 +2,3 @@)
@@ -36,7 +91,7 @@ export class FileDiffUtil {
3691 oldLineNumber ++ ;
3792 return ;
3893 }
39- result += `<div style="color: red;">- Line ${ oldLineNumber } : ${ this . escapeHtml ( line . slice ( 1 ) ) } </div>` ;
94+ diff . push ( { old : line . slice ( 1 ) , new : null } ) ;
4095 oldLineNumber ++ ;
4196 } else if ( line . startsWith ( '+' ) ) {
4297 // Skip the first line difference
@@ -45,16 +100,17 @@ export class FileDiffUtil {
45100 newLineNumber ++ ;
46101 return ;
47102 }
48- result += `<div style="color: green;">+ Line ${ newLineNumber } : ${ this . escapeHtml ( line . slice ( 1 ) ) } </div>` ;
103+ diff . push ( { old : null , new : line . slice ( 1 ) } ) ;
49104 newLineNumber ++ ;
50105 } else if ( line . startsWith ( ' ' ) ) {
106+ diff . push ( { old : line . slice ( 1 ) , new : line . slice ( 1 ) } ) ;
51107 // Unchanged line, skip it
52108 oldLineNumber ++ ;
53109 newLineNumber ++ ;
54110 }
55111 } ) ;
56- // Return the result string, or an empty string if no differences
57- return result . trim ( ) ? result : '' ;
112+ // Return the diff array
113+ return diff ;
58114 } catch ( error ) {
59115 Logger . logger . error ( 'Error in FileDiffUtil' , error . message ) ;
60116 }
0 commit comments