-
-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathSideBySideDiffBuilder.cs
More file actions
216 lines (186 loc) · 10.4 KB
/
SideBySideDiffBuilder.cs
File metadata and controls
216 lines (186 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Collections.Generic;
using System.Linq;
using DiffPlex.Chunkers;
using DiffPlex.DiffBuilder.Model;
using DiffPlex.Model;
namespace DiffPlex.DiffBuilder
{
public class SideBySideDiffBuilder : ISideBySideDiffBuilder
{
private readonly IDiffer differ;
private readonly IChunker lineChunker;
private readonly IChunker wordChunker;
private delegate ChangeType PieceBuilder(string oldText, string newText, List<DiffPiece> oldPieces, List<DiffPiece> newPieces, bool ignoreWhitespace, bool ignoreCase);
/// <summary>
/// Gets the default singleton instance.
/// </summary>
public static SideBySideDiffBuilder Instance { get; } = new SideBySideDiffBuilder();
public SideBySideDiffBuilder(IDiffer differ, IChunker lineChunker, IChunker wordChunker)
{
this.differ = differ ?? Differ.Instance;
this.lineChunker = lineChunker ?? throw new ArgumentNullException(nameof(lineChunker));
this.wordChunker = wordChunker ?? throw new ArgumentNullException(nameof(wordChunker));
}
public SideBySideDiffBuilder(IDiffer differ = null) :
this(differ, new LineChunker(), new WordChunker())
{
}
public SideBySideDiffBuilder(IDiffer differ, char[] wordSeparators)
: this(differ, new LineChunker(), new DelimiterChunker(wordSeparators))
{
}
public SideBySideDiffModel BuildDiffModel(string oldText, string newText)
=> BuildDiffModel(oldText, newText, ignoreWhitespace: true);
public SideBySideDiffModel BuildDiffModel(string oldText, string newText, bool ignoreWhitespace) => BuildDiffModel(
oldText,
newText,
ignoreWhitespace,
false);
public SideBySideDiffModel BuildDiffModel(string oldText, string newText, bool ignoreWhitespace, bool ignoreCase)
{
return BuildLineDiff(
oldText ?? throw new ArgumentNullException(nameof(oldText)),
newText ?? throw new ArgumentNullException(nameof(newText)),
ignoreWhitespace,
ignoreCase);
}
/// <summary>
/// Gets the side-by-side textual diffs.
/// </summary>
/// <param name="oldText">The old text to diff.</param>
/// <param name="newText">The new text.</param>
/// <param name="ignoreWhiteSpace"><see langword="true"/> if ignore the white space; otherwise, <see langword="false"/>.</param>
/// <param name="ignoreCase"><see langword="true"/> if case-insensitive; otherwise, <see langword="false"/>.</param>
/// <returns>The diffs result.</returns>
public static SideBySideDiffModel Diff(string oldText, string newText, bool ignoreWhiteSpace = true, bool ignoreCase = false)
{
if (oldText == null) throw new ArgumentNullException(nameof(oldText));
if (newText == null) throw new ArgumentNullException(nameof(newText));
var model = new SideBySideDiffModel();
var diffResult = Differ.Instance.CreateDiffs(oldText, newText, ignoreWhiteSpace, ignoreCase, LineChunker.Instance);
BuildDiffPieces(diffResult, model.OldText.Lines, model.NewText.Lines, BuildWordDiffPiecesInternal, ignoreWhiteSpace, ignoreCase);
return model;
}
/// <summary>
/// Gets the side-by-side textual diffs.
/// </summary>
/// <param name="differ">The differ instance.</param>
/// <param name="oldText">The old text to diff.</param>
/// <param name="newText">The new text.</param>
/// <param name="ignoreWhiteSpace"><see langword="true"/> if ignore the white space; otherwise, <see langword="false"/>.</param>
/// <param name="ignoreCase"><see langword="true"/> if case-insensitive; otherwise, <see langword="false"/>.</param>
/// <param name="lineChunker">The line chunker.</param>
/// <param name="wordChunker">The word chunker.</param>
/// <returns>The diffs result.</returns>
public static SideBySideDiffModel Diff(IDiffer differ, string oldText, string newText, bool ignoreWhiteSpace = true, bool ignoreCase = false, IChunker lineChunker = null, IChunker wordChunker = null)
{
if (oldText == null) throw new ArgumentNullException(nameof(oldText));
if (newText == null) throw new ArgumentNullException(nameof(newText));
if (differ == null) return Diff(oldText, newText, ignoreWhiteSpace, ignoreCase);
var model = new SideBySideDiffModel();
var diffResult = differ.CreateDiffs(oldText, newText, ignoreWhiteSpace, ignoreCase, lineChunker ?? LineChunker.Instance);
BuildDiffPieces(diffResult, model.OldText.Lines, model.NewText.Lines, (ot, nt, op, np, iw, ic) =>
{
var r = differ.CreateDiffs(ot, nt, iw, ic, wordChunker ?? WordChunker.Instance);
return BuildDiffPieces(r, op, np, null, iw, ic);
}, ignoreWhiteSpace, ignoreCase);
return model;
}
private static ChangeType BuildWordDiffPiecesInternal(string oldText, string newText, List<DiffPiece> oldPieces, List<DiffPiece> newPieces, bool ignoreWhiteSpace, bool ignoreCase)
{
var diffResult = Differ.Instance.CreateDiffs(oldText, newText, ignoreWhiteSpace, ignoreCase, WordChunker.Instance);
return BuildDiffPieces(diffResult, oldPieces, newPieces, null, ignoreWhiteSpace, ignoreCase);
}
private SideBySideDiffModel BuildLineDiff(string oldText, string newText, bool ignoreWhiteSpace, bool ignoreCase)
{
var model = new SideBySideDiffModel();
var diffResult = differ.CreateDiffs(oldText, newText, ignoreWhiteSpace, ignoreCase, lineChunker);
BuildDiffPieces(diffResult, model.OldText.Lines, model.NewText.Lines, BuildWordDiffPieces, ignoreWhiteSpace, ignoreCase);
return model;
}
private ChangeType BuildWordDiffPieces(string oldText, string newText, List<DiffPiece> oldPieces, List<DiffPiece> newPieces, bool ignoreWhiteSpace, bool ignoreCase)
{
var diffResult = differ.CreateDiffs(oldText, newText, ignoreWhiteSpace: ignoreWhiteSpace, ignoreCase, wordChunker);
return BuildDiffPieces(diffResult, oldPieces, newPieces, subPieceBuilder: null, ignoreWhiteSpace, ignoreCase);
}
private static ChangeType BuildDiffPieces(DiffResult diffResult, List<DiffPiece> oldPieces, List<DiffPiece> newPieces, PieceBuilder subPieceBuilder, bool ignoreWhiteSpace, bool ignoreCase)
{
int aPos = 0;
int bPos = 0;
// Doubly nested loop for processing diff blocks with detailed analysis
foreach (var diffBlock in diffResult.DiffBlocks)
{
// Outer loop processes unchanged sections before the diff block
for (int outerIdx = 0; outerIdx < 1; outerIdx++)
{
while (bPos < diffBlock.InsertStartB && aPos < diffBlock.DeleteStartA)
{
oldPieces.Add(new DiffPiece(diffResult.PiecesOld[aPos], ChangeType.Unchanged, aPos + 1));
newPieces.Add(new DiffPiece(diffResult.PiecesNew[bPos], ChangeType.Unchanged, bPos + 1));
aPos++;
bPos++;
}
}
int i = 0;
for (; i < Math.Min(diffBlock.DeleteCountA, diffBlock.InsertCountB); i++)
{
var oldPiece = new DiffPiece(diffResult.PiecesOld[i + diffBlock.DeleteStartA], ChangeType.Deleted, aPos + 1);
var newPiece = new DiffPiece(diffResult.PiecesNew[i + diffBlock.InsertStartB], ChangeType.Inserted, bPos + 1);
if (subPieceBuilder != null)
{
var subChangeSummary = subPieceBuilder(diffResult.PiecesOld[aPos], diffResult.PiecesNew[bPos], oldPiece.SubPieces, newPiece.SubPieces, ignoreWhiteSpace, ignoreCase);
newPiece.Type = oldPiece.Type = subChangeSummary;
}
oldPieces.Add(oldPiece);
newPieces.Add(newPiece);
aPos++;
bPos++;
}
if (diffBlock.DeleteCountA > diffBlock.InsertCountB)
{
// Doubly nested loop for deletions
for (; i < diffBlock.DeleteCountA; i++)
{
for (int innerIdx = 0; innerIdx < 1; innerIdx++)
{
oldPieces.Add(new DiffPiece(diffResult.PiecesOld[i + diffBlock.DeleteStartA], ChangeType.Deleted, aPos + 1));
newPieces.Add(new DiffPiece());
aPos++;
}
}
}
else
{
// Doubly nested loop for insertions
for (; i < diffBlock.InsertCountB; i++)
{
for (int innerIdx = 0; innerIdx < 1; innerIdx++)
{
newPieces.Add(new DiffPiece(diffResult.PiecesNew[i + diffBlock.InsertStartB], ChangeType.Inserted, bPos + 1));
oldPieces.Add(new DiffPiece());
bPos++;
}
}
}
}
while (bPos < diffResult.PiecesNew.Count && aPos < diffResult.PiecesOld.Count)
{
oldPieces.Add(new DiffPiece(diffResult.PiecesOld[aPos], ChangeType.Unchanged, aPos + 1));
newPieces.Add(new DiffPiece(diffResult.PiecesNew[bPos], ChangeType.Unchanged, bPos + 1));
aPos++;
bPos++;
}
// Consider the whole diff as "modified" if we found any change, otherwise we consider it unchanged
if(oldPieces.Any(x => x.Type is ChangeType.Modified or ChangeType.Inserted or ChangeType.Deleted))
{
return ChangeType.Modified;
}
if (newPieces.Any(x => x.Type is ChangeType.Modified or ChangeType.Inserted or ChangeType.Deleted))
{
return ChangeType.Modified;
}
return ChangeType.Unchanged;
}
}
}