forked from yysun/Git-Source-Control-Provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HunkRangeInfo.cs
142 lines (124 loc) · 4.45 KB
/
HunkRangeInfo.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 GitScc.Diff
{
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.Text;
using NGit.Diff;
public class HunkRangeInfo
{
private readonly ITextSnapshot _snapshot;
private readonly Edit _edit;
private readonly List<string> _originalText;
private readonly bool _canRollback;
public HunkRangeInfo(ITextSnapshot snapshot, Edit edit, RawText originalText, RawText workingText)
{
if (snapshot == null)
throw new ArgumentNullException("snapshot");
if (edit == null)
throw new ArgumentNullException("edit");
if (originalText == null)
throw new ArgumentNullException("originalText");
if (workingText == null)
throw new ArgumentNullException("workingText");
_snapshot = snapshot;
_edit = edit;
_originalText = originalText.GetString(edit.GetBeginA(), edit.GetEndA(), true).Split('\n').Select(i => i.TrimEnd('\r')).ToList();
_canRollback = true;
}
private HunkRangeInfo(ITextSnapshot snapshot, Edit edit, List<string> originalText)
{
_snapshot = snapshot;
_edit = edit;
_originalText = originalText;
_canRollback = false;
}
public ITextSnapshot Snapshot
{
get
{
return _snapshot;
}
}
public HunkRange OriginalHunkRange
{
get
{
return new HunkRange(_edit.GetBeginA(), _edit.GetLengthA());
}
}
public HunkRange NewHunkRange
{
get
{
return new HunkRange(_edit.GetBeginB(), _edit.GetLengthB());
}
}
public List<string> OriginalText
{
get
{
return _originalText;
}
}
public bool CanRollback
{
get
{
if (!_canRollback)
return false;
return IsAddition || IsModification || IsDeletion;
}
}
public bool IsAddition
{
get
{
return _edit.GetType() == Edit.Type.INSERT;
}
}
public bool IsModification
{
get
{
return _edit.GetType() == Edit.Type.REPLACE;
}
}
public bool IsDeletion
{
get
{
return _edit.GetType() == Edit.Type.DELETE;
}
}
public HunkRangeInfo TranslateTo(ITextSnapshot snapshot)
{
if (snapshot == null)
throw new ArgumentNullException("snapshot");
if (snapshot == _snapshot)
return this;
if (IsDeletion)
{
// track a point
ITextSnapshotLine line = _snapshot.GetLineFromLineNumber(_edit.GetBeginB());
ITrackingPoint trackingPoint = _snapshot.CreateTrackingPoint(line.Start, PointTrackingMode.Negative);
SnapshotPoint updated = trackingPoint.GetPoint(snapshot);
int updatedLineNumber = updated.GetContainingLine().LineNumber;
Edit updatedEdit = new Edit(_edit.GetBeginA(), _edit.GetEndA(), updatedLineNumber, updatedLineNumber);
return new HunkRangeInfo(snapshot, updatedEdit, _originalText);
}
else
{
// track a span
ITextSnapshotLine startLine = _snapshot.GetLineFromLineNumber(_edit.GetBeginB());
ITextSnapshotLine endLine = _snapshot.GetLineFromLineNumber(_edit.GetEndB() - 1);
ITrackingSpan trackingSpan = _snapshot.CreateTrackingSpan(new SnapshotSpan(startLine.Start, endLine.EndIncludingLineBreak), SpanTrackingMode.EdgeInclusive);
SnapshotSpan updated = trackingSpan.GetSpan(snapshot);
int updatedStartLineNumber = updated.Start.GetContainingLine().LineNumber;
int updatedEndLineNumber = updated.End.GetContainingLine().LineNumber;
Edit updatedEdit = new Edit(_edit.GetBeginA(), _edit.GetEndA(), updatedStartLineNumber, updatedEndLineNumber);
return new HunkRangeInfo(snapshot, updatedEdit, _originalText);
}
}
}
}