Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 20 additions & 7 deletions src/System.CommandLine.Tests/CompletionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,25 @@ public void Default_completions_can_be_cleared_and_replaced()
}

[Fact]
public void Default_completions_can_be_appended_to()
public void Default_completions_are_not_used_when_user_provides_their_own()
{
var argument = new Argument<DayOfWeek>();
argument.Completions.Add(new[] { "mon", "tues", "wed", "thur", "fri", "sat", "sun" });
var command = new Command("the-command")
{
argument
};

var completions = command.Parse("the-command s")
.GetCompletions();

completions.Select(item => item.Label)
.Should()
.BeEquivalentTo("sat", "sun", "tues");
}

[Fact]
public void Default_completions_can_not_be_appended_to()
{
var command = new Command("the-command")
{
Expand All @@ -906,13 +924,8 @@ public void Default_completions_can_be_appended_to()
.Should()
.BeEquivalentTo(
"sat",
nameof(DayOfWeek.Saturday),
"sun",
nameof(DayOfWeek.Sunday),
"tues",
nameof(DayOfWeek.Tuesday),
nameof(DayOfWeek.Thursday),
nameof(DayOfWeek.Wednesday));
"tues");
}

[Fact]
Expand Down
13 changes: 7 additions & 6 deletions src/System.CommandLine/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ internal TryConvertArgument? ConvertArguments
/// <summary>
/// Gets the collection of completion sources for the argument.
/// </summary>
public ICollection<ICompletionSource> Completions =>
_completions ??= new ()
{
CompletionSource.ForType(ValueType)
};
public ICollection<ICompletionSource> Completions => _completions ??= new();

Choose a reason for hiding this comment

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

Huh, so if I read this property, then the getter assigns an empty collection to _completions, and after that, GetCompletions will no longer use the default completions. It seems wrong that the getter would have a side effect like that.


/// <summary>
/// Gets or sets the <see cref="Type" /> that the argument token(s) will be converted to.
Expand Down Expand Up @@ -191,7 +187,12 @@ internal void AddAllowedValues(IReadOnlyList<string> values)
/// <inheritdoc />
public override IEnumerable<CompletionItem> GetCompletions(CompletionContext context)
{
return Completions
var completions = _completions ?? new()
{
CompletionSource.ForType(ValueType)
};

return completions
.SelectMany(source => source.GetCompletions(context))
.Distinct()
.OrderBy(c => c.SortText, StringComparer.OrdinalIgnoreCase);
Expand Down