-
Notifications
You must be signed in to change notification settings - Fork 4
/
DiffViewLines.cs
142 lines (112 loc) · 3.37 KB
/
DiffViewLines.cs
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
namespace Menees.Diffs.Windows.Forms
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Drawing;
#endregion
internal sealed class DiffViewLines : ReadOnlyCollection<DiffViewLine>
{
#region Private Data Members
private readonly int[] diffEndLines;
private readonly int[] diffStartLines;
private int maxLineNumber = 1;
#endregion
#region Constructors
public DiffViewLines(IList<string> stringList, EditScript script, bool useA)
: this()
{
int currentLine = 0;
int totalEdits = script.Count;
this.diffStartLines = new int[totalEdits];
this.diffEndLines = new int[totalEdits];
for (int e = 0; e < totalEdits; e++)
{
Edit edit = script[e];
// Get the starting line for this Edit
int startingLine;
bool dummyLine;
if (useA)
{
dummyLine = edit.EditType == EditType.Insert;
startingLine = edit.StartA;
}
else
{
dummyLine = edit.EditType == EditType.Delete;
startingLine = edit.StartB;
}
// Put in unedited lines up to this point
currentLine += this.AddUneditedLines(stringList, currentLine, startingLine, useA);
// Record where the next diff starts and ends
this.diffStartLines[e] = this.Count;
this.diffEndLines[e] = this.Count + edit.Length - 1;
// Since Inserts occur after the current line
// instead of at it, we have to decrement the index.
for (int i = 0; i < edit.Length; i++)
{
// A - Shows Deletes and Changes
// B - Shows Inserts and Changes
string text = string.Empty;
int? number = null;
if (!dummyLine)
{
number = startingLine + i;
text = stringList[number.Value];
currentLine++;
}
this.AddLine(text, number, edit.EditType, useA);
}
}
// Put in any remaining unedited lines
this.AddUneditedLines(stringList, currentLine, stringList.Count, useA);
}
public DiffViewLines(DiffViewLine lineOne, DiffViewLine lineTwo)
: this()
{
this.AddLine(lineOne);
this.AddLine(lineTwo);
}
private DiffViewLines()
: base(new List<DiffViewLine>())
{
// Called by the other constructors.
this.diffStartLines = CollectionUtility.EmptyArray<int>();
this.diffEndLines = CollectionUtility.EmptyArray<int>();
}
#endregion
#region Public Properties
public int[] DiffEndLines => this.diffEndLines;
public int[] DiffStartLines => this.diffStartLines;
public int MaxLineNumber => this.maxLineNumber;
#endregion
#region Private Methods
private void AddLine(string text, int? number, EditType editType, bool fromA)
{
this.AddLine(new DiffViewLine(text, number, editType, fromA));
}
private void AddLine(DiffViewLine line)
{
if (line.Number.HasValue && line.Number.Value > this.maxLineNumber)
{
this.maxLineNumber = line.Number.Value;
}
this.Items.Add(line);
}
private int AddUneditedLines(IList<string> stringList, int current, int end, bool fromA)
{
for (int i = current; i < end; i++)
{
this.AddLine(stringList[i], i, EditType.None, fromA);
}
// If the edit script isn't using changes (i.e., just inserts and deletes)
// then we can hit cases where iEnd < iCurrent, but we don't want to
// return a negative value for number of added lines.
int result = Math.Max(end - current, 0);
return result;
}
#endregion
}
}