Skip to content

Commit

Permalink
Add snapshot translation for HunkRangeInfo, used when updating diff p…
Browse files Browse the repository at this point in the history
…ositions after an edit
  • Loading branch information
sharwell committed Feb 6, 2013
1 parent 5e7fbcc commit b9bf835
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
41 changes: 41 additions & 0 deletions Diff/HunkRangeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public HunkRangeInfo(ITextSnapshot snapshot, Edit edit, RawText originalText, Ra
_originalText = originalText.GetString(edit.GetBeginA(), edit.GetEndA(), true).Split('\n').Select(i => i.TrimEnd('\r')).ToList();
}

private HunkRangeInfo(ITextSnapshot snapshot, Edit edit, List<string> originalText)
{
_snapshot = snapshot;
_edit = edit;
_originalText = originalText;
}

public ITextSnapshot Snapshot
{
get
Expand Down Expand Up @@ -83,5 +90,39 @@ public bool IsDeletion
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);
}
}
}
}
10 changes: 6 additions & 4 deletions Diff/ViewModel/DiffViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ private void UpdateDimensions()
if (_reverted)
return;

ITextSnapshotLine startLine = _textView.TextSnapshot.GetLineFromLineNumber(_hunkRangeInfo.NewHunkRange.StartingLineNumber);
ITextSnapshotLine endLine = _textView.TextSnapshot.GetLineFromLineNumber(_hunkRangeInfo.NewHunkRange.StartingLineNumber + _hunkRangeInfo.NewHunkRange.NumberOfLines - 1);
HunkRangeInfo hunkRangeInfo = _hunkRangeInfo.TranslateTo(_textView.TextBuffer.CurrentSnapshot);

ITextSnapshotLine startLine = _textView.TextSnapshot.GetLineFromLineNumber(hunkRangeInfo.NewHunkRange.StartingLineNumber);
ITextSnapshotLine endLine = _textView.TextSnapshot.GetLineFromLineNumber(hunkRangeInfo.NewHunkRange.StartingLineNumber + hunkRangeInfo.NewHunkRange.NumberOfLines - 1);
if (startLine != null && endLine != null)
{
IWpfTextViewLine startLineView = _textView.GetTextViewLineContainingBufferPosition(startLine.Start);
Expand Down Expand Up @@ -171,7 +173,7 @@ private void UpdateDimensions()

if (stopBottom <= startTop)
{
if (_hunkRangeInfo.IsDeletion)
if (hunkRangeInfo.IsDeletion)
{
double center = (startTop + stopBottom) / 2.0;
Top = center - (_textView.LineHeight / 2.0);
Expand All @@ -180,7 +182,7 @@ private void UpdateDimensions()
}
else
{
// shouldn't be reachable, but definitely hide if this is the case
// could be reachable if translation changes an addition to empty
IsVisible = false;
}

Expand Down

0 comments on commit b9bf835

Please sign in to comment.