Skip to content

Commit

Permalink
Fix rollback behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Feb 6, 2013
1 parent 8af8a02 commit 23feec9
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions Diff/ViewModel/DiffViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,41 +285,36 @@ private void Rollback()

using (ITextEdit edit = snapshot.TextBuffer.CreateEdit())
{
if (_hunkRangeInfo.IsAddition)
Span newSpan;
if (_hunkRangeInfo.IsDeletion)
{
ITextSnapshotLine startLine = snapshot.GetLineFromLineNumber(_hunkRangeInfo.NewHunkRange.StartingLineNumber);
ITextSnapshotLine endLine = snapshot.GetLineFromLineNumber(_hunkRangeInfo.NewHunkRange.StartingLineNumber + _hunkRangeInfo.NewHunkRange.NumberOfLines - 1);
edit.Delete(Span.FromBounds(startLine.Start.Position, endLine.EndIncludingLineBreak.Position));
newSpan = new Span(startLine.Start.Position, 0);
}
else if (_hunkRangeInfo.NewHunkRange.NumberOfLines == 1)
else
{
var line = snapshot.GetLineFromLineNumber(_hunkRangeInfo.NewHunkRange.StartingLineNumber);

var text = line.ExtentIncludingLineBreak.GetText();
ITextSnapshotLine startLine = snapshot.GetLineFromLineNumber(_hunkRangeInfo.NewHunkRange.StartingLineNumber);
ITextSnapshotLine endLine = snapshot.GetLineFromLineNumber(_hunkRangeInfo.NewHunkRange.StartingLineNumber + _hunkRangeInfo.NewHunkRange.NumberOfLines - 1);
newSpan = Span.FromBounds(startLine.Start.Position, endLine.EndIncludingLineBreak.Position);
}

if(text.EndsWith("\r\n"))
{
edit.Replace(line.ExtentIncludingLineBreak, _hunkRangeInfo.OriginalText[0] + "\r\n");
}
else
{
edit.Replace(line.ExtentIncludingLineBreak, _hunkRangeInfo.OriginalText[0]);
}
if (_hunkRangeInfo.IsAddition)
{
ITextSnapshotLine startLine = snapshot.GetLineFromLineNumber(_hunkRangeInfo.NewHunkRange.StartingLineNumber);
ITextSnapshotLine endLine = snapshot.GetLineFromLineNumber(_hunkRangeInfo.NewHunkRange.StartingLineNumber + _hunkRangeInfo.NewHunkRange.NumberOfLines - 1);
edit.Delete(Span.FromBounds(startLine.Start.Position, endLine.EndIncludingLineBreak.Position));
}
else
{
for (var n = 0; n <= _hunkRangeInfo.NewHunkRange.NumberOfLines; n++)
{
var line = snapshot.GetLineFromLineNumber(_hunkRangeInfo.NewHunkRange.StartingLineNumber + n);
edit.Delete(line.Start.Position, line.Length);
}
string lineBreak = snapshot.GetLineFromLineNumber(0).GetLineBreakText();
if (string.IsNullOrEmpty(lineBreak))
lineBreak = Environment.NewLine;

var startLine = snapshot.GetLineFromLineNumber(_hunkRangeInfo.NewHunkRange.StartingLineNumber);
string originalText = string.Join(lineBreak, _hunkRangeInfo.OriginalText);
if (_hunkRangeInfo.NewHunkRange.StartingLineNumber + _hunkRangeInfo.NewHunkRange.NumberOfLines != snapshot.LineCount)
originalText += lineBreak;

foreach (var line in _hunkRangeInfo.OriginalText)
{
edit.Insert(startLine.Start.Position, line + "\r\n");
}
edit.Replace(newSpan, originalText);
}

edit.Apply();
Expand All @@ -331,4 +326,4 @@ private void ShowPopUp()
ShowPopup = true;
}
}
}
}

0 comments on commit 23feec9

Please sign in to comment.