From 3c1a5ba717194364c1edd0c645d58f441c903d3f Mon Sep 17 00:00:00 2001 From: Pradeep Ramola Date: Tue, 1 Dec 2026 16:23:09 -0500 Subject: [PATCH] Respect parser whitespace in completions Match completion word boundaries with the whitespace predicate used by command-line splitting. Cover cursor boundaries, trailing whitespace, and option-value completions without changing tokenization or ranking. Fix #2903 --- .../CompletionContextTests.cs | 45 ++++++++++++++++++- .../CompletionTests.cs | 18 ++++++++ .../Completions/CompletionContext.cs | 16 ++++--- 3 files changed, 72 insertions(+), 7 deletions(-) diff --git a/src/System.CommandLine.Tests/CompletionContextTests.cs b/src/System.CommandLine.Tests/CompletionContextTests.cs index 09d805d6e1..ff5e2e7887 100644 --- a/src/System.CommandLine.Tests/CompletionContextTests.cs +++ b/src/System.CommandLine.Tests/CompletionContextTests.cs @@ -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("arg"), + new Option("--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("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")] @@ -224,4 +267,4 @@ public void When_position_is_specified_in_string_command_line_then_it_returns_ar textToMatch.Should().Be(expected); } } -} \ No newline at end of file +} diff --git a/src/System.CommandLine.Tests/CompletionTests.cs b/src/System.CommandLine.Tests/CompletionTests.cs index 4d902ff07a..16a5af07aa 100644 --- a/src/System.CommandLine.Tests/CompletionTests.cs +++ b/src/System.CommandLine.Tests/CompletionTests.cs @@ -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("--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() { diff --git a/src/System.CommandLine/Completions/CompletionContext.cs b/src/System.CommandLine/Completions/CompletionContext.cs index a7130c1701..61d24b25b7 100644 --- a/src/System.CommandLine/Completions/CompletionContext.cs +++ b/src/System.CommandLine/Completions/CompletionContext.cs @@ -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); @@ -104,4 +108,4 @@ protected static string GetWordToComplete( return ""; } } -} \ No newline at end of file +}