internal static SymbolResult SymbolToComplete( this ParseResult parseResult, int?position = null) { var commandResult = parseResult.CommandResult; var currentSymbol = AllSymbolResultsForCompletion() .LastOrDefault(); return(currentSymbol); IEnumerable <SymbolResult> AllSymbolResultsForCompletion() { foreach (var item in commandResult.AllSymbolResults()) { if (item is CommandResult command) { yield return(command); } else if (item is OptionResult option) { var willAcceptAnArgument = !option.IsImplicit && (!option.IsArgumentLimitReached || parseResult.TextToMatch(position).Length > 0); if (willAcceptAnArgument) { yield return(option); } } } } }
public static IEnumerable <string> GetSuggestions( this ParseResult parseResult, int?position = null) { var textToMatch = parseResult.TextToMatch(position); var currentSymbolResult = parseResult.SymbolToComplete(position); var currentSymbol = currentSymbolResult.Symbol; var currentSymbolSuggestions = currentSymbol is ISuggestionSource currentSuggestionSource ? currentSuggestionSource.GetSuggestions(textToMatch) : Array.Empty <string>(); IEnumerable <string> siblingSuggestions; var parentSymbol = currentSymbolResult.Parent?.Symbol; if (parentSymbol == null || !currentSymbolResult.IsArgumentLimitReached) { siblingSuggestions = Array.Empty <string>(); } else { siblingSuggestions = parentSymbol .GetSuggestions(textToMatch) .Except(parentSymbol .Children .OfType <ICommand>() .Select(c => c.Name)); } if (currentSymbolResult is CommandResult commandResult) { currentSymbolSuggestions = currentSymbolSuggestions .Except(OptionsWithArgumentLimitReached(currentSymbolResult)); if (currentSymbolResult.Parent is CommandResult parent) { siblingSuggestions = siblingSuggestions.Except(OptionsWithArgumentLimitReached(parent)); } } return(currentSymbolSuggestions.Concat(siblingSuggestions)); string[] OptionsWithArgumentLimitReached(SymbolResult symbolResult) { var optionsWithArgLimitReached = symbolResult .Children .Where(c => c.IsArgumentLimitReached); var exclude = optionsWithArgLimitReached .SelectMany(c => c.Symbol.RawAliases) .Concat(commandResult.Symbol.RawAliases) .ToArray(); return(exclude); } }
public static IEnumerable <string?> GetSuggestions( this ParseResult parseResult, int?position = null) { var textToMatch = parseResult.TextToMatch(position); var currentSymbolResult = parseResult.SymbolToComplete(position); var currentSymbol = currentSymbolResult.Symbol; var currentSymbolSuggestions = currentSymbol is ISuggestionSource currentSuggestionSource ? currentSuggestionSource.GetSuggestions(textToMatch) : Array.Empty <string>(); IEnumerable <string?> siblingSuggestions; var parentSymbol = currentSymbolResult.Parent?.Symbol; if (parentSymbol is null || !currentSymbolResult.IsArgumentLimitReached) { siblingSuggestions = Array.Empty <string?>(); }