Skip to content

GetCompletions provides completions even when ParseResult has errors #1696

Closed

Description

@vlada-shubina

Currently, I have the issues fixing some of tab completion issues for reparsing in dotnet new as I'm not able to evaluate if current input is correct for given command or not while doing tab completion.

  • GetCompletions provides the result even when the string is surely wrong for given command
  • ParseResult.Errors contains error even when the string is a valid input for the command, but not yet complete.

Consider 2 scenarios:

scenario #1

            var myCommand = new Command("new");
            var option = new Option<string>("--test")
            {
                IsRequired = true,
                Arity = new ArgumentArity(1, 1)
            };
            option.FromAmong("val1", "val2");
            myCommand.AddOption(option);

            var parseResult = myCommand.Parse($" new --lang a --test "); //this example has option --lang which is not defined
            var completions = parseResult.GetCompletions();
            Assert.Empty(completions); //fails, completions have "val1", "val2"

Expectation is that completions are not returned as --lang a is already not a valid option.
parseResult.Errors is not empty and have 3 errors:
▶ | [0] | {Required argument missing for option: '--test'.} |
▶ | [1] | {Unrecognized command or argument '--lang'.} |
▶ | [2] | {Unrecognized command or argument 'a'.} |

1 and 2 are expected, but not 0 as this option still may be completed.

scenario #2

            var myCommand = new Command("new");
            var option = new Option<string>("--test")
            {
                IsRequired = true,
                Arity = new ArgumentArity(1, 1)
            };
            option.FromAmong("val1", "val2");
            myCommand.AddOption(option);

            var parseResult = myCommand.Parse($" new  --test ");

parseResult.Errors is not empty, and has a following entry:
▶ | [0] | {Required argument missing for option: '--test'.} |
But the input is a valid input, just not completed. So error is not expected here.

Those scenarios are not allowing to make an decision whether the command line input is correct for a given command, and completions returned should be included to overall result.

Activity

  1. changed the title [-]`GetCompletions` provides a completions even when `ParseResult` has errors[/-] [+]`GetCompletions` provides completions even when `ParseResult` has errors[/+] on Mar 31, 2022
  2. jonsequitur commented on Mar 31, 2022

    @jonsequitur
    Contributor

    This is by design and pretty common. I would say the errors are correct, but do not indicate that the text is invalid for the purpose of completion. Completions are almost always calculated against invalid input in any language.

    Tricks exist such as inserting virtual tokens in order to make a better guess at what the correct completion would be. Consider this broken C# code:

    public ThisIsMeantToBeAClass
    {
        public string DoSomethng => "".sub
    }

    You can get correct completions on "".sub even though there's no class declaration.

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