-
-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathInlineDiffBuilder.cs
More file actions
246 lines (207 loc) · 10.1 KB
/
InlineDiffBuilder.cs
File metadata and controls
246 lines (207 loc) · 10.1 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Collections.Generic;
using System.Linq;
using DiffPlex.Chunkers;
using DiffPlex.DiffBuilder.Model;
using DiffPlex.Model;
namespace DiffPlex.DiffBuilder
{
public class InlineDiffBuilder : IInlineDiffBuilder
{
private readonly IDiffer differ;
private delegate ChangeType PieceBuilder(string oldText, string newText, List<DiffPiece> pieces, bool ignoreWhitespace, bool ignoreCase);
/// <summary>
/// Gets the default singleton instance of the inline diff builder.
/// </summary>
public static InlineDiffBuilder Instance { get; } = new InlineDiffBuilder();
public InlineDiffBuilder(IDiffer differ = null)
{
this.differ = differ ?? Differ.Instance;
}
public DiffPaneModel BuildDiffModel(string oldText, string newText)
=> BuildDiffModel(oldText, newText, ignoreWhitespace: true);
public DiffPaneModel BuildDiffModel(string oldText, string newText, bool ignoreWhitespace)
{
var chunker = new LineChunker();
return BuildDiffModel(oldText, newText, ignoreWhitespace, false, chunker);
}
public DiffPaneModel BuildDiffModel(string oldText, string newText, bool ignoreWhitespace, bool ignoreCase, IChunker chunker)
{
if (oldText == null) throw new ArgumentNullException(nameof(oldText));
if (newText == null) throw new ArgumentNullException(nameof(newText));
var model = new DiffPaneModel();
var diffResult = differ.CreateDiffs(oldText, newText, ignoreWhitespace, ignoreCase: ignoreCase, chunker);
BuildDiffPieces(diffResult, model.Lines);
return model;
}
/// <summary>
/// Gets the inline textual diffs.
/// </summary>
/// <param name="oldText">The old text to diff.</param>
/// <param name="newText">The new text.</param>
/// <param name="ignoreWhiteSpace">true if ignore the white space; othewise, false.</param>
/// <param name="ignoreCase">true if case-insensitive; otherwise, false.</param>
/// <param name="chunker">The chunker.</param>
/// <returns>The diffs result.</returns>
public static DiffPaneModel Diff(string oldText, string newText, bool ignoreWhiteSpace = true, bool ignoreCase = false, IChunker chunker = null)
{
return Diff(Differ.Instance, oldText, newText, ignoreWhiteSpace, ignoreCase, chunker);
}
/// <summary>
/// Gets the inline 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">true if ignore the white space; othewise, false.</param>
/// <param name="ignoreCase">true if case-insensitive; otherwise, false.</param>
/// <param name="chunker">The chunker.</param>
/// <returns>The diffs result.</returns>
public static DiffPaneModel Diff(IDiffer differ, string oldText, string newText, bool ignoreWhiteSpace = true, bool ignoreCase = false, IChunker chunker = null)
{
if (oldText == null) throw new ArgumentNullException(nameof(oldText));
if (newText == null) throw new ArgumentNullException(nameof(newText));
var model = new DiffPaneModel();
var diffResult = (differ ?? Differ.Instance).CreateDiffs(oldText, newText, ignoreWhiteSpace, ignoreCase, chunker ?? LineChunker.Instance);
BuildDiffPieces(diffResult, model.Lines);
return model;
}
public static DiffPaneModel Diff(
IDiffer differ,
string oldText, string newText,
List<IChunker> detailsPack,
bool ignoreWhiteSpace = true, bool ignoreCase = false)
{
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);
LinkedList<IChunker> chunkers;
if (detailsPack == null || !detailsPack.Any())
{
chunkers = new LinkedList<IChunker>();
chunkers.AddLast(DiffPlex.Chunkers.LineChunker.Instance);
chunkers.AddLast(DiffPlex.Chunkers.WordChunker.Instance);
chunkers.AddLast(DiffPlex.Chunkers.CharacterChunker.Instance);
}
else
{
chunkers = new LinkedList<IChunker>(detailsPack);
}
var model = new DiffPaneModel();
var cnode = chunkers.First;
var diffResult = differ.CreateDiffs(oldText, newText, ignoreWhiteSpace, ignoreCase, cnode.Value);
BuildDiffPieces(diffResult, model.Lines, NextPieceBuilderInternal(differ, cnode.Next), ignoreWhiteSpace, ignoreCase);
return model;
}
private static PieceBuilder NextPieceBuilderInternal(
IDiffer differ,
LinkedListNode<IChunker> chunkerNode)
{
if (chunkerNode == null)
{
return null;
}
else
{
return (ot, nt, p, iw, ic) =>
{
var r = differ.CreateDiffs(ot, nt, iw, ic, chunkerNode.Value);
return BuildDiffPieces(r, p, NextPieceBuilderInternal(differ, chunkerNode.Next), iw, ic);
};
}
}
private static ChangeType BuildDiffPieces(
DiffResult diffResult,
List<DiffPiece> pieces, PieceBuilder subPieceBuilder,
bool ignoreWhiteSpace, bool ignoreCase)
{
int aPos = 0;
int bPos = 0;
ChangeType changeSummary = ChangeType.Unchanged;
foreach (var diffBlock in diffResult.DiffBlocks)
{
while (bPos < diffBlock.InsertStartB && aPos < diffBlock.DeleteStartA)
{
pieces.Add(new DiffPiece(diffResult.PiecesOld[aPos], ChangeType.Unchanged, aPos + 1));
aPos++;
bPos++;
}
int i = 0;
for (; i < Math.Min(diffBlock.DeleteCountA, diffBlock.InsertCountB); i++)
{
var piece = 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], piece.SubPieces, ignoreWhiteSpace, ignoreCase);
piece.Type = subChangeSummary;
}
pieces.Add(piece);
aPos++;
bPos++;
}
if (diffBlock.DeleteCountA > diffBlock.InsertCountB)
{
for (; i < diffBlock.DeleteCountA; i++)
{
pieces.Add(new DiffPiece(diffResult.PiecesOld[i + diffBlock.DeleteStartA], ChangeType.Deleted, aPos + 1));
aPos++;
}
}
else
{
for (; i < diffBlock.InsertCountB; i++)
{
pieces.Add(new DiffPiece(diffResult.PiecesNew[i + diffBlock.InsertStartB], ChangeType.Inserted, bPos + 1));
bPos++;
}
}
}
while (bPos < diffResult.PiecesNew.Length && aPos < diffResult.PiecesOld.Length)
{
pieces.Add(new DiffPiece(diffResult.PiecesOld[aPos], ChangeType.Unchanged, aPos + 1));
aPos++;
bPos++;
}
// Consider the whole diff as "modified" if we found any change, otherwise we consider it unchanged
if (pieces.Any(x => x.Type == ChangeType.Modified || x.Type == ChangeType.Inserted || x.Type == ChangeType.Deleted))
{
changeSummary = ChangeType.Modified;
}
return changeSummary;
}
private static void BuildDiffPieces(DiffResult diffResult, List<DiffPiece> pieces)
{
int bPos = 0;
foreach (var diffBlock in diffResult.DiffBlocks)
{
for (; bPos < diffBlock.InsertStartB; bPos++)
pieces.Add(new DiffPiece(diffResult.PiecesNew[bPos], ChangeType.Unchanged, bPos + 1));
int i = 0;
for (; i < Math.Min(diffBlock.DeleteCountA, diffBlock.InsertCountB); i++)
pieces.Add(new DiffPiece(diffResult.PiecesOld[i + diffBlock.DeleteStartA], ChangeType.Deleted));
i = 0;
for (; i < Math.Min(diffBlock.DeleteCountA, diffBlock.InsertCountB); i++)
{
pieces.Add(new DiffPiece(diffResult.PiecesNew[i + diffBlock.InsertStartB], ChangeType.Inserted, bPos + 1));
bPos++;
}
if (diffBlock.DeleteCountA > diffBlock.InsertCountB)
{
for (; i < diffBlock.DeleteCountA; i++)
pieces.Add(new DiffPiece(diffResult.PiecesOld[i + diffBlock.DeleteStartA], ChangeType.Deleted));
}
else
{
for (; i < diffBlock.InsertCountB; i++)
{
pieces.Add(new DiffPiece(diffResult.PiecesNew[i + diffBlock.InsertStartB], ChangeType.Inserted, bPos + 1));
bPos++;
}
}
}
for (; bPos < diffResult.PiecesNew.Length; bPos++)
pieces.Add(new DiffPiece(diffResult.PiecesNew[bPos], ChangeType.Unchanged, bPos + 1));
}
}
}