Skip to content

Commit

Permalink
Merge pull request #263 from waf/handle-shift-delete-at-beginning-of-…
Browse files Browse the repository at this point in the history
…prompt

Handle shift-delete of empty line at beginning of prompt
  • Loading branch information
waf authored Jul 16, 2023
2 parents 1f40010 + ba22a69 commit fec0637
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/PrettyPrompt/Documents/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private int CalculateLineBoundaryIndexNearCaret(int caret, int direction, bool s
}
else
{
//smart Home implementation (repeating Home presses switch between 'non-white-space start of line' and 'start of line')
if (caret == 0 && !smartHome) return 0;

int lineStart = 0;
var beforeCaretIndex = (caret - 1).Clamp(0, Length - 1);
Expand All @@ -307,6 +307,7 @@ private int CalculateLineBoundaryIndexNearCaret(int caret, int direction, bool s

if (!smartHome) return lineStart;

//smart Home implementation (repeating Home presses switch between 'non-white-space start of line' and 'start of line')
int lineStartNonWhiteSpace = lineStart;
for (int i = lineStart; i < Length; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/PrettyPrompt.Tests/DocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace PrettyPrompt.Tests;
public class DocumentTests
{
[Fact]
public void WordBoundariesTests_WithoutWhitspaces()
public void WordBoundariesTests_WithoutWhitespaces()
{
//empty
var document = new Document("", caret: 0);
Expand Down Expand Up @@ -117,7 +117,7 @@ from c2 in testChars
}

[Fact]
public void WordBoundariesTests_WithWhitspaces()
public void WordBoundariesTests_WithWhitespaces()
{
//Different editors have different behaviour regarding to whitespaces.
//Have tried Visual Studio vs Visual Studio Code vs Windows Terminal.
Expand Down
21 changes: 21 additions & 0 deletions tests/PrettyPrompt.Tests/PromptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,27 @@ public async Task ReadLine_DeleteLine()
await TestLineCutting(cutKeyPress: $"{Shift}{Delete}");
}

/// <summary>
/// Triggers bug from https://github.com/waf/PrettyPrompt/issues/254.
/// </summary>
[Fact]
public async Task ReadLine_DeleteLineFromMultipleEmptyLines_DeletesLine()
{
var console = ConsoleStub.NewConsole();
var input = new List<FormattableString>
{
$"{Shift}{Enter}",
$"{Shift}{Enter}",
$"{UpArrow}{UpArrow}",
$"{Shift}{Delete}",
$"{Enter}"
};
console.StubInput(input.ToArray());
var prompt = new Prompt(console: console);
var result = await prompt.ReadLineAsync();
Assert.Equal(NewLine, result.Text);
}

private static async Task TestLineCutting(FormattableString cutKeyPress)
{
var console = ConsoleStub.NewConsole();
Expand Down

0 comments on commit fec0637

Please sign in to comment.