@@ -17,11 +17,10 @@ public static string FormatDiff(string? left, string? right)
1717 var leftTruncated = TruncateString ( left ) ;
1818 var rightTruncated = TruncateString ( right ) ;
1919
20- var basic = $ " Left: { FormatStringValue ( leftTruncated ) } \n Right: { FormatStringValue ( rightTruncated ) } ";
21-
2220 if ( IsMultiline ( left ) || IsMultiline ( right ) )
23- return basic + " \n " + GenerateMultilineDiff ( leftTruncated , rightTruncated ) ;
21+ return FormatMultilineComparison ( leftTruncated , rightTruncated ) ;
2422
23+ var basic = $ " Left: { FormatStringValue ( leftTruncated ) } \n Right: { FormatStringValue ( rightTruncated ) } ";
2524 return basic + "\n " + GenerateInlineDiff ( leftTruncated , rightTruncated ) ;
2625 }
2726
@@ -31,7 +30,22 @@ static string FormatNullComparison(string? left, string? right)
3130 if ( left == null ) return $ " Left: null\n Right: { FormatStringValue ( right ) } ";
3231 return $ " Left: { FormatStringValue ( left ) } \n Right: null";
3332 }
34-
33+
34+ static string FormatMultilineComparison ( string left , string right )
35+ {
36+ var result = new System . Text . StringBuilder ( ) ;
37+ result . AppendLine ( " Left:" ) ;
38+ foreach ( var line in left . Split ( '\n ' ) )
39+ result . AppendLine ( line ) ;
40+
41+ result . AppendLine ( " Right:" ) ;
42+ foreach ( var line in right . Split ( '\n ' ) )
43+ result . AppendLine ( line ) ;
44+
45+ result . Append ( GenerateMultilineDiff ( left , right ) ) ;
46+ return result . ToString ( ) ;
47+ }
48+
3549 static string FormatStringValue ( string ? value )
3650 {
3751 if ( value == null ) return "null" ;
@@ -97,46 +111,38 @@ static void AppendRemainingText(System.Text.StringBuilder builder, string source
97111
98112 static string GenerateMultilineDiff ( string left , string right )
99113 {
100- var differ = new Differ ( ) ;
101- var diffResult = differ . CreateLineDiffs ( left , right , ignoreWhitespace : false ) ;
114+ var diffBuilder = new InlineDiffBuilder ( new Differ ( ) ) ;
115+ var diff = diffBuilder . BuildDiffModel ( left , right , ignoreWhitespace : false ) ;
102116
103117 var result = new List < string > ( ) ;
104- var leftLines = left . Split ( '\n ' ) ;
105- var rightLines = right . Split ( '\n ' ) ;
118+ result . Add ( " Diff:" ) ;
106119
107- foreach ( var block in diffResult . DiffBlocks )
120+ foreach ( var line in diff . Lines )
108121 {
109- AppendDeletedLines ( result , leftLines , block . DeleteStartA , block . DeleteCountA ) ;
110- AppendInsertedLines ( result , rightLines , block . InsertStartB , block . InsertCountB ) ;
122+ switch ( line . Type )
123+ {
124+ case ChangeType . Unchanged :
125+ result . Add ( line . Text ) ;
126+ break ;
127+ case ChangeType . Deleted :
128+ result . Add ( $ "- { line . Text } ") ;
129+ break ;
130+ case ChangeType . Inserted :
131+ result . Add ( $ "+ { line . Text } ") ;
132+ break ;
133+ case ChangeType . Modified :
134+ result . Add ( $ "~ { line . Text } ") ;
135+ break ;
136+ }
111137 }
112138
113- if ( result . Count == 0 )
114- result . Add ( " (No specific line differences found)" ) ;
139+ if ( result . Count == 1 )
140+ result . Add ( "(No specific line differences found)" ) ;
115141
116142 TruncateResultIfNeeded ( result ) ;
117143 return string . Join ( "\n " , result ) ;
118144 }
119145
120- static void AppendDeletedLines ( List < string > output , string [ ] lines , int start , int count )
121- {
122- for ( var i = 0 ; i < count ; i ++ )
123- {
124- var lineIndex = start + i ;
125- if ( lineIndex < lines . Length )
126- output . Add ( $ " - { lines [ lineIndex ] } ") ;
127- }
128- }
129-
130- static void AppendInsertedLines ( List < string > output , string [ ] lines , int start , int count )
131- {
132- for ( var i = 0 ; i < count ; i ++ )
133- {
134- var lineIndex = start + i ;
135- if ( lineIndex < lines . Length )
136- output . Add ( $ " + { lines [ lineIndex ] } ") ;
137- }
138- }
139-
140146 static void TruncateResultIfNeeded ( List < string > result )
141147 {
142148 if ( result . Count <= MaxDiffLines ) return ;
0 commit comments