Fix undo bug

There was a bug in undo code where if you undid some stuff, made an edit,
then started a grouped edit, the undo state was incorrect.  A simple
repro was something like (in Windows mode)

    a b c d Ctrl+z Ctrl+z Ctrl+z Shift+LeftArrow Ctrl+x
This commit is contained in:
Jason Shirk
2014-06-12 19:16:13 -07:00
parent a697c222f5
commit fceb3a241a
2 changed files with 17 additions and 9 deletions
+2 -4
View File
@@ -32,7 +32,7 @@ namespace PSConsoleUtilities
private readonly StringBuilder _statusBuffer;
private string _statusLinePrompt;
private List<EditItem> _edits;
private readonly Stack<int> _pushedEditGroupCount;
private int _editGroupStart;
private int _undoEditIndex;
private int _mark;
private bool _inputAccepted;
@@ -359,8 +359,6 @@ namespace PSConsoleUtilities
_savedCurrentLine = new HistoryItem();
_queuedKeys = new Queue<ConsoleKeyInfo>();
_pushedEditGroupCount = new Stack<int>();
string hostName = null;
try
{
@@ -394,7 +392,7 @@ namespace PSConsoleUtilities
_buffer.Clear();
_edits = new List<EditItem>();
_undoEditIndex = 0;
_pushedEditGroupCount.Clear();
_editGroupStart = -1;
_current = 0;
_mark = 0;
_emphasisStart = -1;
+15 -5
View File
@@ -14,6 +14,11 @@ namespace PSConsoleUtilities
if (removeCount > 0)
{
_edits.RemoveRange(_undoEditIndex, removeCount);
if (_editGroupStart >= 0)
{
// Adjust the edit group start if we are started a group.
_editGroupStart -= removeCount;
}
}
_edits.Add(editItem);
_undoEditIndex = _edits.Count;
@@ -21,16 +26,21 @@ namespace PSConsoleUtilities
private void StartEditGroup()
{
_pushedEditGroupCount.Push(_edits.Count);
if (_editGroupStart != -1)
{
// Nesting not supported.
throw new InvalidOperationException();
}
_editGroupStart = _edits.Count;
}
private void EndEditGroup()
{
var groupEditStart = _pushedEditGroupCount.Pop();
var groupEditCount = _edits.Count - groupEditStart;
var groupedEditItems = _edits.GetRange(groupEditStart, groupEditCount);
_edits.RemoveRange(groupEditStart, groupEditCount);
var groupEditCount = _edits.Count - _editGroupStart;
var groupedEditItems = _edits.GetRange(_editGroupStart, groupEditCount);
_edits.RemoveRange(_editGroupStart, groupEditCount);
SaveEditItem(GroupedEdit.Create(groupedEditItems));
_editGroupStart = -1;
}
/// <summary>