Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
address code review feedback: rename Completions to CompletionSources
  • Loading branch information
adamsitnik committed Dec 15, 2022
commit c2a7102940314cac8faa0e55968a021bd6076f1b
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
System.CommandLine
public abstract class Argument : Symbol, System.CommandLine.Binding.IValueDescriptor
public ArgumentArity Arity { get; set; }
public System.Collections.Generic.List<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> Completions { get; }
public System.Collections.Generic.List<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> CompletionSources { get; }
public System.Boolean HasDefaultValue { get; }
public System.String HelpName { get; set; }
public System.Collections.Generic.List<System.Action<System.CommandLine.Parsing.ArgumentResult>> Validators { get; }
Expand All @@ -24,8 +24,6 @@ System.CommandLine
public Argument<T> AcceptLegalFileNamesOnly()
public Argument<T> AcceptLegalFilePathsOnly()
public Argument<T> AcceptOnlyFromAmong(System.String[] values)
public Argument<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> completionsDelegate)
public Argument<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> completionsDelegate)
public System.Void SetDefaultValue(T value)
public System.Void SetDefaultValueFactory(Func<T> defaultValueFactory)
public System.Void SetDefaultValueFactory(Func<System.CommandLine.Parsing.ArgumentResult,T> defaultValueFactory)
Expand Down Expand Up @@ -192,7 +190,7 @@ System.CommandLine
public System.Boolean AllowMultipleArgumentsPerToken { get; set; }
public System.String ArgumentHelpName { get; set; }
public ArgumentArity Arity { get; set; }
public System.Collections.Generic.List<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> Completions { get; }
public System.Collections.Generic.List<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> CompletionSources { get; }
public System.Boolean IsRequired { get; set; }
public System.Collections.Generic.List<System.Action<System.CommandLine.Parsing.OptionResult>> Validators { get; }
public System.Type ValueType { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public void Setup()
_nullConsole = new NullConsole();

Option<string> fruitOption = new("--fruit");
fruitOption.Completions.Add("apple", "banana", "cherry");
fruitOption.CompletionSources.Add("apple", "banana", "cherry");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This naming now looks wrong. The things being added here are really completion items, not completion sources.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonsequitur I've tried renaming the Add extension method to AddCompletionItems but it break the C# duck typing for collection initialization:

image

Error	CS1503	Argument 1: cannot convert from 'string' to 'System.Func<System.CommandLine.Completions.CompletionContext, System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>'	System.CommandLine.Tests (net462), System.CommandLine.Tests (net7.0)	D:\projects\command-line-api\src\System.CommandLine.Tests\CompletionTests.cs	125	Active


Option<string> vegetableOption = new("--vegetable");
vegetableOption.Completions.Add("asparagus", "broccoli", "carrot");
vegetableOption.CompletionSources.Add("asparagus", "broccoli", "carrot");

var eatCommand = new Command("eat")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private IEnumerable<Option> GenerateOptionsArray(int count)
public void Setup_FromSymbol()
{
_testSymbol = new Option<string>("--hello");
_testSymbol.Completions.Add(GenerateSuggestionsArray(TestSuggestionsCount));
_testSymbol.CompletionSources.Add(GenerateSuggestionsArray(TestSuggestionsCount));
}

[Benchmark]
Expand Down
2 changes: 0 additions & 2 deletions src/System.CommandLine.Tests/ArgumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,6 @@ public void Argument_of_T_fluent_APIs_return_Argument_of_T()
{
Argument<string> argument = new Argument<string>("--path")
.AcceptOnlyFromAmong("text")
.AddCompletions(ctx => Array.Empty<string>())
.AddCompletions(ctx => Array.Empty<CompletionItem>())
.AcceptLegalFileNamesOnly()
.AcceptLegalFilePathsOnly();

Expand Down
20 changes: 10 additions & 10 deletions src/System.CommandLine.Tests/CompletionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public CompletionTests(ITestOutputHelper output)
public void Option_GetCompletions_returns_argument_completions_if_configured()
{
var option = new Option<string>("--hello");
option.Completions.Add("one", "two", "three");
option.CompletionSources.Add("one", "two", "three");

var completions = option.GetCompletions();

Expand Down Expand Up @@ -121,7 +121,7 @@ public void Command_GetCompletions_returns_available_subcommands_and_option_alia
new Argument<string[]>
{
Arity = ArgumentArity.OneOrMore,
Completions = { "command-argument" }
CompletionSources = { "command-argument" }
}
};

Expand Down Expand Up @@ -212,7 +212,7 @@ public void Command_GetCompletions_can_access_ParseResult()
var originOption = new Option<string>("--origin");
var cloneOption = new Option<string>("--clone");

cloneOption.Completions.Add(ctx =>
cloneOption.CompletionSources.Add(ctx =>
{
var opt1Value = ctx.ParseResult.GetValue(originOption);
return !string.IsNullOrWhiteSpace(opt1Value) ? new[] { opt1Value } : Array.Empty<string>();
Expand Down Expand Up @@ -581,7 +581,7 @@ public void Option_GetCompletions_can_be_based_on_the_proximate_option_and_parti
public void Completions_can_be_provided_in_the_absence_of_validation()
{
Option<string> option = new ("-t");
option.Completions.Add("vegetable", "mineral", "animal");
option.CompletionSources.Add("vegetable", "mineral", "animal");

var command = new Command("the-command")
{
Expand Down Expand Up @@ -610,7 +610,7 @@ public void Command_argument_completions_can_be_provided_using_a_delegate()
{
new Argument<string>
{
Completions = { _ => new[] { "vegetable", "mineral", "animal" } }
CompletionSources = { _ => new[] { "vegetable", "mineral", "animal" } }
}
}
};
Expand All @@ -626,7 +626,7 @@ public void Command_argument_completions_can_be_provided_using_a_delegate()
public void Option_argument_completions_can_be_provided_using_a_delegate()
{
var option = new Option<string>("-x");
option.Completions.Add(_ => new[] { "vegetable", "mineral", "animal" });
option.CompletionSources.Add(_ => new[] { "vegetable", "mineral", "animal" });

var command = new Command("the-command")
{
Expand Down Expand Up @@ -857,7 +857,7 @@ public void It_can_provide_completions_within_quotes(string commandLine, int pos
};

var argument = new Argument<string>();
argument.Completions.Add(expectedSuggestions);
argument.CompletionSources.Add(expectedSuggestions);

var r = new Command("#r")
{
Expand All @@ -878,8 +878,8 @@ public void It_can_provide_completions_within_quotes(string commandLine, int pos
public void Default_completions_can_be_cleared_and_replaced()
{
var argument = new Argument<DayOfWeek>();
argument.Completions.Clear();
argument.Completions.Add(new[] { "mon", "tues", "wed", "thur", "fri", "sat", "sun" });
argument.CompletionSources.Clear();
argument.CompletionSources.Add(new[] { "mon", "tues", "wed", "thur", "fri", "sat", "sun" });
var command = new Command("the-command")
{
argument
Expand All @@ -900,7 +900,7 @@ public void Default_completions_can_be_appended_to()
{
new Argument<DayOfWeek>
{
Completions = { "mon", "tues", "wed", "thur", "fri", "sat", "sun" }
CompletionSources = { "mon", "tues", "wed", "thur", "fri", "sat", "sun" }
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/System.CommandLine.Tests/SuggestDirectiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public class SuggestDirectiveTests
public SuggestDirectiveTests()
{
_fruitOption = new Option<string>("--fruit");
_fruitOption.Completions.Add("apple", "banana", "cherry");
_fruitOption.CompletionSources.Add("apple", "banana", "cherry");

_vegetableOption = new Option<string>("--vegetable");
_vegetableOption.Completions.Add(_ => new[] { "asparagus", "broccoli", "carrot" });
_vegetableOption.CompletionSources.Add(_ => new[] { "asparagus", "broccoli", "carrot" });

_eatCommand = new Command("eat")
{
Expand Down
8 changes: 4 additions & 4 deletions src/System.CommandLine/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract class Argument : Symbol, IValueDescriptor
{
private ArgumentArity _arity;
private TryConvertArgument? _convertArguments;
private List<Func<CompletionContext, IEnumerable<CompletionItem>>>? _completions = null;
private List<Func<CompletionContext, IEnumerable<CompletionItem>>>? _completionSources = null;
private List<Action<ArgumentResult>>? _validators = null;

/// <summary>
Expand Down Expand Up @@ -73,8 +73,8 @@ internal TryConvertArgument? ConvertArguments
/// <summary>
/// Gets the list of completion sources for the argument.
/// </summary>
public List<Func<CompletionContext, IEnumerable<CompletionItem>>> Completions =>
_completions ??= new ()
public List<Func<CompletionContext, IEnumerable<CompletionItem>>> CompletionSources =>
_completionSources ??= new ()
{
CompletionSource.ForType(ValueType)
};
Expand Down Expand Up @@ -140,7 +140,7 @@ internal void AddAllowedValues(IReadOnlyList<string> values)
/// <inheritdoc />
public override IEnumerable<CompletionItem> GetCompletions(CompletionContext context)
{
return Completions
return CompletionSources
.SelectMany(source => source.Invoke(context))
.Distinct()
.OrderBy(c => c.SortText, StringComparer.OrdinalIgnoreCase);
Expand Down
26 changes: 2 additions & 24 deletions src/System.CommandLine/Argument{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,28 +172,6 @@ public void SetDefaultValueFactory(Func<ArgumentResult, T> defaultValueFactory)
return _defaultValueFactory.Invoke(argumentResult);
}

/// <summary>
/// Adds completions for the argument.
/// </summary>
/// <param name="completionsDelegate">A function that will be called to provide completions.</param>
/// <returns>The option being extended.</returns>
public Argument<T> AddCompletions(Func<CompletionContext, IEnumerable<string>> completionsDelegate)
{
Completions.Add(completionsDelegate);
return this;
}

/// <summary>
/// Adds completions for the argument.
/// </summary>
/// <param name="completionsDelegate">A function that will be called to provide completions.</param>
/// <returns>The configured argument.</returns>
public Argument<T> AddCompletions(Func<CompletionContext, IEnumerable<CompletionItem>> completionsDelegate)
{
Completions.Add(completionsDelegate);
return this;
}

/// <summary>
/// Configures the argument to accept only the specified values, and to suggest them as command line completions.
/// </summary>
Expand All @@ -203,8 +181,8 @@ public Argument<T> AcceptOnlyFromAmong(params string[] values)
{
AllowedValues?.Clear();
AddAllowedValues(values);
Completions.Clear();
Completions.Add(values);
CompletionSources.Clear();
CompletionSources.Add(values);

return this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public override string Name
/// <summary>
/// Gets the list of completion sources for the option.
/// </summary>
public List<Func<CompletionContext, IEnumerable<CompletionItem>>> Completions => Argument.Completions;
public List<Func<CompletionContext, IEnumerable<CompletionItem>>> CompletionSources => Argument.CompletionSources;

/// <summary>
/// Indicates whether a given alias exists on the option, regardless of its prefix.
Expand Down
2 changes: 0 additions & 2 deletions src/System.CommandLine/Option{T}.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.CommandLine.Binding;
using System.CommandLine.Completions;
using System.CommandLine.Parsing;

namespace System.CommandLine
Expand Down