Skip to content

Completion word boundaries ignore tabs and other parser whitespace #2903

Backlog

Description

@pradeep-ramola

Description

CommandLineParser.SplitCommandLine recognizes char.IsWhiteSpace separators, but CompletionContext.GetWordToComplete searches only for the ASCII space character. With tabs or other accepted whitespace, the returned word includes multiple tokens. This can make public ParseResult.GetCompletions() throw InvalidOperationException: Sequence contains no matching element when it tries to find that joined word in the parsed token list.

Reproduction

using System;
using System.CommandLine;
using System.Linq;

var option = new Option<string>("--fruit");
option.CompletionSources.Add("apple", "banana");
var command = new Command("the-command") { option };

var result = command.Parse("the-command --fruit\tap");
Console.WriteLine(result.GetCompletionContext().WordToComplete);
Console.WriteLine(string.Join(", ", result.GetCompletions().Select(item => item.Label)));

Actual: the word is --fruit\tap and enumerating completions throws. Replacing the tab with an ordinary space produces the expected ap word and apple suggestion.

A trailing tab also leaves the entire preceding tab-separated input in WordToComplete rather than producing an empty word. Cursor positions inside a word have the same boundary mismatch. CR, LF, CRLF, and U+00A0 are also affected, despite being recognized during command-line splitting.

Expected behavior

Use the same whitespace predicate when finding completion-word boundaries as when splitting the raw command line. Preserve existing ordinary-space and cursor-position behavior. No parser, shell script, completion ranking, or public API change is needed.

Investigation and validation

I have a focused candidate and plan to submit it with 18 regression cases covering a word at the cursor, trailing whitespace, and actual option-value completions. Against unchanged source: 15 fail and three ordinary-space controls pass. With the candidate, the full net472 suite passes (956 passed, four existing skips). The full net10.0 target suite also passes (960 passed, seven existing skips) using .NET 12 preview runtime roll-forward, not a native .NET 10 runtime.

Checked existing completion issues and PRs. #2902 touches the same source file to extract LastNonDirectiveToken, but leaves this word-boundary calculation unchanged.

Environment: Windows; main at b73d4e8a38cc615fedd1f0c8b16950fe5da073cc; SDK 12.0.100-alpha.1.26571.102. Prepared with AI assistance and reproduced locally.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions