Skip to content

Commit b6a5912

Browse files
committed
Fix MenuComplete to not use nested edit groups
I didn't feel like properly solving how to handle nested edit groups when forgetting edits after an undo, but I forget that MenuComplete was actually using nested edit groups. MenuComplete doesn't really need nested edit groups, it was just convenient. I switched to just remembering the edits to remove and saving a single edit group for the final selection or removing them all if cancelled.
1 parent 2f2db97 commit b6a5912

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

PSReadLine/Completion.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
35
using System.Diagnostics.CodeAnalysis;
46
using System.Linq;
57
using System.Management.Automation;
@@ -415,7 +417,7 @@ private void PossibleCompletionsImpl(CommandCompletion completions, bool menuSel
415417

416418
if (menuSelect)
417419
{
418-
StartEditGroup();
420+
var undoPoint = _edits.Count;
419421

420422
int selectedItem = 0;
421423
bool undo = false;
@@ -502,12 +504,26 @@ private void PossibleCompletionsImpl(CommandCompletion completions, bool menuSel
502504

503505
WriteBlankLines(displayRows, menuAreaTop);
504506

505-
EndEditGroup();
507+
var lastInsert = ((GroupedEdit)_edits[_edits.Count - 1])._groupedEditItems[1];
508+
Debug.Assert(lastInsert is EditItemInsertString, "The only edits possible here are pairs of Delete/Insert");
509+
var firstDelete = ((GroupedEdit)_edits[undoPoint])._groupedEditItems[0];
510+
Debug.Assert(firstDelete is EditItemDelete, "The only edits possible here are pairs of Delete/Insert");
511+
512+
var groupEditCount = _edits.Count - undoPoint;
513+
_edits.RemoveRange(undoPoint, groupEditCount);
514+
_undoEditIndex = undoPoint;
506515

507516
if (undo)
508517
{
509518
// Pretend it never happened.
510-
Undo();
519+
lastInsert.Undo();
520+
firstDelete.Undo();
521+
Render();
522+
}
523+
else
524+
{
525+
// Leave one edit instead of possibly many to undo
526+
SaveEditItem(GroupedEdit.Create(new List<EditItem> { firstDelete, lastInsert }));
511527
}
512528
}
513529
else

PSReadLine/UndoRedo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public override void Redo()
175175

176176
class GroupedEdit : EditItem
177177
{
178-
private List<EditItem> _groupedEditItems;
178+
internal List<EditItem> _groupedEditItems;
179179

180180
public static EditItem Create(List<EditItem> groupedEditItems)
181181
{

0 commit comments

Comments
 (0)