Skip to content

Commit 4c7669a

Browse files
committed
fix #1563
1 parent a943953 commit 4c7669a

File tree

2 files changed

+65
-35
lines changed

2 files changed

+65
-35
lines changed

src/System.CommandLine.Tests/CompletionTests.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,38 @@ public void Option_GetCompletions_returns_argument_completions_if_configured()
3939
[Fact]
4040
public void Command_GetCompletions_returns_available_option_aliases()
4141
{
42-
IReadOnlyCollection<Symbol> symbols = new[] {
42+
var command = new Command("command")
43+
{
4344
new Option("--one", "option one"),
4445
new Option("--two", "option two"),
4546
new Option("--three", "option three")
4647
};
47-
var command1 = new Command(
48-
"command",
49-
"a command"
50-
);
5148

52-
foreach (var symbol in symbols)
49+
var completions = command.GetCompletions();
50+
51+
completions
52+
.Select(item => item.Label)
53+
.Should()
54+
.BeEquivalentTo("--one", "--two", "--three");
55+
}
56+
57+
[Fact] // https://github.com/dotnet/command-line-api/issues/1563
58+
public void Command_GetCompletions_returns_available_option_aliases_for_global_options()
59+
{
60+
var subcommand = new Command("command")
61+
{
62+
new Option("--one", "option one"),
63+
new Option("--two", "option two")
64+
};
65+
66+
var rootCommand = new RootCommand
5367
{
54-
command1.Add(symbol);
55-
}
68+
subcommand
69+
};
5670

57-
var command = command1;
71+
rootCommand.AddGlobalOption(new Option("--three", "option three"));
5872

59-
var completions = command.GetCompletions();
73+
var completions = subcommand.GetCompletions();
6074

6175
completions
6276
.Select(item => item.Label)

src/System.CommandLine/Command.cs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,6 @@ private protected void AddSymbol(Symbol symbol)
138138
/// <inheritdoc />
139139
public override IEnumerable<CompletionItem> GetCompletions(CompletionContext context)
140140
{
141-
if (Children.Count == 0)
142-
{
143-
return Array.Empty<CompletionItem>();
144-
}
145-
146141
var completions = new List<CompletionItem>();
147142

148143
if (context.WordToComplete is { } textToMatch)
@@ -151,37 +146,58 @@ public override IEnumerable<CompletionItem> GetCompletions(CompletionContext con
151146
{
152147
var child = Children[i];
153148

154-
switch (child)
155-
{
156-
case IdentifierSymbol identifier when !child.IsHidden:
157-
foreach (var alias in identifier.Aliases)
158-
{
159-
if (alias is { } &&
160-
alias.ContainsCaseInsensitive(textToMatch))
161-
{
162-
completions.Add(new CompletionItem(alias, CompletionItemKind.Keyword, detail: child.Description));
163-
}
164-
}
149+
AddCompletionsFor(child);
150+
}
165151

166-
break;
152+
foreach (var parent in Parents.FlattenBreadthFirst(p => p.Parents))
153+
{
154+
if (parent is Command parentCommand)
155+
{
156+
for (var i = 0; i < parentCommand.Options.Count; i++)
157+
{
158+
var option = parentCommand.Options[i];
167159

168-
case Argument argument:
169-
foreach (var completion in argument.GetCompletions(context))
160+
if (option.IsGlobal)
170161
{
171-
if (completion.Label.ContainsCaseInsensitive(textToMatch))
172-
{
173-
completions.Add(completion);
174-
}
162+
AddCompletionsFor(option);
175163
}
176-
177-
break;
164+
}
178165
}
179166
}
180167
}
181168

182169
return completions
183170
.OrderBy(item => item.SortText.IndexOfCaseInsensitive(context.WordToComplete))
184171
.ThenBy(symbol => symbol.Label, StringComparer.OrdinalIgnoreCase);
172+
173+
void AddCompletionsFor(Symbol child)
174+
{
175+
switch (child)
176+
{
177+
case IdentifierSymbol identifier when !child.IsHidden:
178+
foreach (var alias in identifier.Aliases)
179+
{
180+
if (alias is { } &&
181+
alias.ContainsCaseInsensitive(textToMatch))
182+
{
183+
completions.Add(new CompletionItem(alias, CompletionItemKind.Keyword, detail: child.Description));
184+
}
185+
}
186+
187+
break;
188+
189+
case Argument argument:
190+
foreach (var completion in argument.GetCompletions(context))
191+
{
192+
if (completion.Label.ContainsCaseInsensitive(textToMatch))
193+
{
194+
completions.Add(completion);
195+
}
196+
}
197+
198+
break;
199+
}
200+
}
185201
}
186202
}
187203
}

0 commit comments

Comments
 (0)