forked from menees/Libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffViewLine.cs
More file actions
224 lines (165 loc) · 4.99 KB
/
DiffViewLine.cs
File metadata and controls
224 lines (165 loc) · 4.99 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
namespace Menees.Diffs.Windows.Forms
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
#endregion
[DebuggerDisplay("Number = {Number}, EditType = {EditType}, Text = {Text}, FromA = {FromA}")]
internal sealed class DiffViewLine
{
#region Public Fields
public static readonly DiffViewLine Empty = new DiffViewLine();
#endregion
#region Private Data Members
private readonly int? number;
private readonly string text;
private readonly EditType editType;
private EditScript changeEditScript;
#endregion
#region Constructors
public DiffViewLine(string text, int? number, EditType editType, bool fromA)
{
this.text = text;
this.number = number;
this.editType = editType;
this.FromA = fromA;
}
private DiffViewLine()
{
this.text = string.Empty;
}
#endregion
#region Public Properties
public DiffViewLine Counterpart { get; internal set; }
[SuppressMessage("", "SA1101", Justification = "The EditType reference is to the type not to this.EditType.")]
public bool Edited => this.editType != EditType.None;
public EditType EditType => this.editType;
public bool FromA { get; }
public int? Number => this.number;
public string Text => this.text;
#endregion
#region Public Methods
public EditScript GetChangeEditScript(ChangeDiffOptions options)
{
if (this.changeEditScript == null && this.editType == EditType.Change && this.Counterpart != null)
{
if (this.FromA)
{
MyersDiff<char> diff = new MyersDiff<char>(
GetCharactersToDiff(this.text, options, out int trimCountA),
GetCharactersToDiff(this.Counterpart.text, options, out int trimCountB),
false); // We don't want Change edits; just Deletes and Inserts.
this.changeEditScript = diff.Execute();
// If we trimmed/ignored leading whitespace, we have to offset each Edit to account for that.
foreach (Edit edit in this.changeEditScript)
{
edit.Offset(trimCountA, trimCountB);
}
}
else if (this.Counterpart.FromA && this.Counterpart.Counterpart == this)
{
// Defer to the A line because its edit script changes A into B.
this.changeEditScript = this.Counterpart.GetChangeEditScript(options);
}
}
return this.changeEditScript;
}
#endregion
#region Private Methods
private static CharList GetCharactersToDiff(string lineText, ChangeDiffOptions options, out int leadingTrimCount)
{
leadingTrimCount = 0;
// Check binary prefix first because the prefix length is a fixed number of characters.
if (options.HasFlag(ChangeDiffOptions.IgnoreBinaryPrefix) && lineText.Length >= BinaryDiffLines.PrefixLength)
{
lineText = lineText.Substring(BinaryDiffLines.PrefixLength);
leadingTrimCount += BinaryDiffLines.PrefixLength;
}
// Check whitespace next because this will shorten the string.
if (options.HasFlag(ChangeDiffOptions.IgnoreWhitespace))
{
string trimmedLine = lineText.Trim();
if (string.IsNullOrEmpty(trimmedLine))
{
leadingTrimCount += lineText.Length;
}
else
{
leadingTrimCount += lineText.IndexOf(trimmedLine[0]);
}
lineText = trimmedLine;
}
// Check case last because the line is now as short as it's going to get.
if (options.HasFlag(ChangeDiffOptions.IgnoreCase))
{
lineText = lineText.ToUpper();
}
// Use CharList so we don't have to make separate char[] for the string.
return new CharList(lineText);
}
#endregion
#region Private Types
private sealed class CharList : IList<char>
{
#region Private Data Members
private readonly string text;
#endregion
#region Constructors
public CharList(string text)
{
this.text = text ?? string.Empty;
}
#endregion
#region Public Properties
public int Count => this.text.Length;
public bool IsReadOnly => true;
public char this[int index]
{
get
{
return this.text[index];
}
set
{
throw new NotSupportedException();
}
}
#endregion
#region Public Methods
public void Add(char item)
{
throw new NotSupportedException();
}
public void Clear()
{
throw new NotSupportedException();
}
public bool Contains(char item) => this.text.IndexOf(item) >= 0;
public void CopyTo(char[] array, int arrayIndex)
{
this.text.CopyTo(0, array, arrayIndex, this.text.Length);
}
public IEnumerator<char> GetEnumerator() => this.text.GetEnumerator();
public int IndexOf(char item) => this.text.IndexOf(item);
public void Insert(int index, char item)
{
throw new NotSupportedException();
}
public bool Remove(char item)
{
throw new NotSupportedException();
}
public void RemoveAt(int index)
{
throw new NotSupportedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => this.GetEnumerator();
#endregion
}
#endregion
}
}