Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions PSReadLine/Completion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -983,14 +983,25 @@ private void MenuCompleteImpl(Menu menu, CommandCompletion completions)
// TODO: Shift + Backspace does not fail here?
if (menuStack.Count > 1)
{
var newMenu = menuStack.Pop();

newMenu.DrawMenu(menu, menuSelect: true);
previousSelection = -1;
userCompletionText = userCompletionText.Substring(0, userCompletionText.Length - 1);

menu = newMenu;
Menu newMenu = menuStack.Peek();
int pos = FindUserCompletionTextPosition(newMenu.CurrentMenuItem, userCompletionText);
if (pos >= 0)
{
newMenu = menuStack.Pop();
newMenu.DrawMenu(menu, menuSelect: true);

userCompletionText = userCompletionText.Substring(0, userCompletionText.Length - 1);
menu = newMenu;
}
// else {
// We should not pop the stack yet. The updated user completion text contains characters
// that are not included in the selected item of the menu at the top of stack. This may
// happen when the user pressed a 'Tab' before this 'Backspace', which updated the user
// completion text to include the unambiguous common prefix of the available completion
// candidates. In this case, we should stay in the current menu.
// }
Comment on lines +998 to +1004
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh thank you super helpful comment 😄

}
else if (menuStack.Count == 1)
{
Expand Down
97 changes: 97 additions & 0 deletions test/CompletionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,95 @@ public void DirectoryCompletion()
_.Ctrl_c, InputAcceptedNow));
}

[SkippableFact]
public void MenuCompletions_Backspace()
{
TestSetup(KeyMode.Cmd, new KeyHandler("Ctrl+Spacebar", PSConsoleReadLine.MenuComplete));

_console.Clear();
char separator = Path.DirectorySeparatorChar;

Test("cd stro", Keys(
"cd stron", _.Ctrl_Spacebar,
CheckThat(() => AssertScreenIs(3,
TokenClassification.Command, "cd",
TokenClassification.None, $" .{separator}strong",
TokenClassification.Selection, separator,
NextLine,
TokenClassification.Selection, "strong ",
TokenClassification.None, "stronghold strongholp",
NextLine,
NextLine)),
_.h, CheckThat(() => AssertScreenIs(3,
TokenClassification.Command, "cd",
TokenClassification.None, $" .{separator}strongh",
TokenClassification.Selection, $"old{separator}",
NextLine,
TokenClassification.Selection, "stronghold ",
TokenClassification.None, "strongholp",
NextLine,
NextLine)),
// Tab will update the user completion text to include the unambiguous common prefix.
_.Tab, CheckThat(() => AssertScreenIs(3,
TokenClassification.Command, "cd",
TokenClassification.None, $" .{separator}stronghol",
TokenClassification.Selection, $"d{separator}",
NextLine,
TokenClassification.Selection, "stronghold ",
TokenClassification.None, "strongholp",
NextLine,
NextLine)),
_.Backspace, CheckThat(() => AssertScreenIs(3,
TokenClassification.Command, "cd",
TokenClassification.None, $" .{separator}strongho",
TokenClassification.Selection, $"ld{separator}",
NextLine,
TokenClassification.Selection, "stronghold ",
TokenClassification.None, "strongholp",
NextLine,
NextLine)),
_.Backspace, CheckThat(() => AssertScreenIs(3,
TokenClassification.Command, "cd",
TokenClassification.None, $" .{separator}strongh",
TokenClassification.Selection, $"old{separator}",
NextLine,
TokenClassification.Selection, "stronghold ",
TokenClassification.None, "strongholp",
NextLine,
NextLine)),
_.Backspace, CheckThat(() => AssertScreenIs(3,
TokenClassification.Command, "cd",
TokenClassification.None, $" .{separator}strong",
TokenClassification.Selection, separator,
NextLine,
TokenClassification.Selection, "strong ",
TokenClassification.None, "stronghold strongholp",
NextLine,
NextLine)),
_.h, CheckThat(() => AssertScreenIs(3,
TokenClassification.Command, "cd",
TokenClassification.None, $" .{separator}strongh",
TokenClassification.Selection, $"old{separator}",
NextLine,
TokenClassification.Selection, "stronghold ",
TokenClassification.None, "strongholp",
NextLine,
NextLine)),
_.Backspace, CheckThat(() => AssertScreenIs(3,
TokenClassification.Command, "cd",
TokenClassification.None, $" .{separator}strong",
TokenClassification.Selection, separator,
NextLine,
TokenClassification.Selection, "strong ",
TokenClassification.None, "stronghold strongholp",
NextLine,
NextLine)),
_.Backspace,
_.Backspace, CheckThat(() => AssertLineIs("cd stro")),
_.Enter
));
}

internal static CommandCompletion MockedCompleteInput(string input, int cursor, Hashtable options, PowerShell powerShell)
{
var ctor = typeof (CommandCompletion).GetConstructor(
Expand Down Expand Up @@ -1419,6 +1508,14 @@ internal static CommandCompletion MockedCompleteInput(string input, int cursor,
break;
case "none":
break;
case "cd stron":
replacementIndex = 3;
replacementLength = 5;
char separator = Path.DirectorySeparatorChar;
completions.Add(new CompletionResult($".{separator}strong", "strong", CompletionResultType.ProviderContainer, $".{separator}strong"));
completions.Add(new CompletionResult($".{separator}stronghold", "stronghold", CompletionResultType.ProviderContainer, $".{separator}stronghold"));
completions.Add(new CompletionResult($".{separator}strongholp", "strongholp", CompletionResultType.ProviderContainer, $".{separator}strongholp"));
break;

default:
if (input.EndsWith("Get-Mo", StringComparison.OrdinalIgnoreCase))
Expand Down