Skip to content
Open
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
45 changes: 44 additions & 1 deletion src/System.CommandLine.Tests/CompletionContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,49 @@ public void When_position_is_unspecified_in_array_command_line_and_final_token_m
textToMatch.Should().Be("a");
}

[Theory]
[InlineData(" ")]
[InlineData("\t")]
[InlineData("\r")]
[InlineData("\n")]
[InlineData("\r\n")]
[InlineData("\u00a0")]
public void WordToComplete_uses_the_parsers_whitespace_boundaries(string separator)
{
var command = new Command("the-command")
{
new Argument<string>("arg"),
new Option<bool>("--two")
};
var commandLine = $"the-command{separator}one{separator}--two";
var parseResult = command.Parse(commandLine);
parseResult.Errors.Should().BeEmpty();

var context = (TextCompletionContext)parseResult.GetCompletionContext();
var position = "the-command".Length + separator.Length + 1;

context.AtCursorPosition(position).WordToComplete.Should().Be("one");
}

[Theory]
[InlineData(" ")]
[InlineData("\t")]
[InlineData("\r")]
[InlineData("\n")]
[InlineData("\r\n")]
[InlineData("\u00a0")]
public void WordToComplete_is_empty_after_trailing_parser_whitespace(string separator)
{
var command = new Command("the-command")
{
new Argument<string>("arg")
};
var parseResult = command.Parse($"the-command{separator}one{separator}");
parseResult.Errors.Should().BeEmpty();

parseResult.GetCompletionContext().WordToComplete.Should().BeEmpty();
}

[Theory]
[InlineData("the-command $one --two", "one")]
[InlineData("the-command one$ --two", "one")]
Expand Down Expand Up @@ -224,4 +267,4 @@ public void When_position_is_specified_in_string_command_line_then_it_returns_ar
textToMatch.Should().Be(expected);
}
}
}
}
18 changes: 18 additions & 0 deletions src/System.CommandLine.Tests/CompletionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ public void Option_GetCompletions_returns_argument_completions_if_configured()
.BeEquivalentTo("one", "two", "three");
}

[Theory]
[InlineData(" ")]
[InlineData("\t")]
[InlineData("\r")]
[InlineData("\n")]
[InlineData("\r\n")]
[InlineData("\u00a0")]
public void GetCompletions_matches_option_values_after_parser_whitespace(string separator)
{
var option = new Option<string>("--fruit");
option.CompletionSources.Add("apple", "banana");
var command = new Command("the-command") { option };

var completions = command.Parse($"the-command --fruit{separator}ap").GetCompletions();

completions.Select(item => item.Label).Should().BeEquivalentTo("apple");
}

[Fact]
public void Command_GetCompletions_returns_available_option_aliases()
{
Expand Down
16 changes: 10 additions & 6 deletions src/System.CommandLine/Completions/CompletionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,17 @@ protected static string GetWordToComplete(
{
int positionValue = position!.Value;

int startIndex = positionValue > 0 ? rawInput!.LastIndexOf(' ', positionValue - 1) : -1;
startIndex = startIndex == -1 ? 0 : startIndex + 1;
var beforeCursor = rawInput.AsSpan(0, positionValue);
int startIndex = beforeCursor.Length;
while (startIndex > 0 && !char.IsWhiteSpace(beforeCursor[startIndex - 1]))
{
startIndex--;
}

int endIndex = rawInput!.IndexOf(' ', positionValue);
if (endIndex == -1)
int endIndex = positionValue;
while (endIndex < rawInput!.Length && !char.IsWhiteSpace(rawInput[endIndex]))
{
endIndex = rawInput.Length;
endIndex++;
}

return rawInput.Substring(startIndex, endIndex - startIndex);
Expand All @@ -104,4 +108,4 @@ protected static string GetWordToComplete(
return "";
}
}
}
}