Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix of crash when repeating deleting selected text and Ctrl+Z. #139

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/PrettyPrompt/Documents/UndoRedoHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace PrettyPrompt.Documents;
/// Stores undo/redo history for a single document.
/// </summary>
/// <remarks>
/// Implementation is naive -- it just stores full snapshots in a linked and undo/redo navigates
/// Implementation is naive -- it just stores full snapshots in a list and undo/redo navigates
/// through it. If tracking snapshots of the input ends up causing high memory, we can rework it.
/// </remarks>
internal sealed class UndoRedoHistory
Expand Down Expand Up @@ -45,7 +45,7 @@ internal void Track(ReadOnlyStringBuilder text, int caret, SelectionSpan? select
{
//edit after undos -> we will throw following redos away
var itemsToRemove = history.Count - currentIndex - 1;
history.RemoveRange(1, itemsToRemove);
history.RemoveRange(index: history.Count - itemsToRemove, itemsToRemove);
}

history.Add(new Record(text.ToString(), caret, selection));
Expand Down
22 changes: 22 additions & 0 deletions tests/PrettyPrompt.Tests/UndoRedoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,26 @@ public async Task ReadLine_UndoRedoAndCaretPosition()
Assert.Equal("a|d", result.Text);
}
}

/// <summary>
/// Reproduces bug from https://github.com/waf/PrettyPrompt/issues/133
/// </summary>
[Fact]
public async Task ReadLine_Repeated_CtrlZ_Backspace()
{
var console = ConsoleStub.NewConsole();
console.StubInput(
$"abcd",
$"{Shift}{LeftArrow}{Shift}{LeftArrow}", //select 'cd'
$"{Backspace}",
$"{Control}{Z}",
$"{Backspace}",
$"{Control}{Z}",
$"{Enter}"
);
var prompt = new Prompt(console: console);
var result = await prompt.ReadLineAsync();
Assert.True(result.IsSuccess);
Assert.Equal("abcd", result.Text);
}
}