CompletionSources not sorted properly when building HelpName default suggestions
维护者通常 1 天内回复
评估
调研方向
从 src/command-line-api/src/System.CommandLine/Option.cs 第 142 行开始,使用 issue 中的 CompletionItem 示例复现该顺序。跟踪空的和部分填写的 CompletionContext 值如何参与排序,然后验证帮助建议和 Tab 补全遵循 issue 中显示的顺序。
由索引模型根据 Issue 内容生成。
描述
The sorting logic done here when building the default help text from the list of completions is backward:
Since the ThenBy is on the Label, the resulting sorted collection is re-sorted by Label, as a primary order, not as a secondary order like SQL.
And, to hoist the current entry to the top of the list, the OrderBy needs to be OrderByDescending.
Here is an example that shows the bad behavior:
public static class Program
{
static CompletionItem[] sampleCompletions =
[
new ( "5", sortText: "05" ),
new ( "10", sortText: "10" ),
new ( "6", sortText: "06" ),
new ( "20", sortText: "10" ),
new ( "30", sortText: "30" )
];
public static async Task<int> Main ( string[] argv)
{
RootCommand root = new ( "example" );
Option<int> option = new ( "option" );
option.CompletionSources.Add ( static ctx => sampleCompletions );
root.Options.Add ( option );
await root.Parse ( [ "--help" ] ).InvokeAsync ( );
}
}
Output:
```text
Description:
example
Usage:
SnapsInAZfs [options]
Options:
option <10|20|30|5|6>
-?, -h, --help Show help and usage information
--version Show version information
Note the improperly ordered suggestions in the HelpName for option.
Here is why, isolated to what the line referenced at the top of the issue does:
CompletionContext theCompletionContext = CompletionContext.Empty;
IOrderedEnumerable<CompletionItem> improperlyOrdered = sampleCompletions
.OrderBy ( item => item.SortText.IndexOfCaseInsensitive ( theCompletionContext.WordToComplete ) )
.ThenBy ( item => item. Label, StringComparer.OrdinalIgnoreCase );
foreach ( CompletionItem c in improperlyOrdered )
{
Console.WriteLine ( c.Label );
}
Output:
10
20
30
5
6
Here is the fixed sorting code, as it applies to building HelpText (an empty context gets passed in):
IOrderedEnumerable<CompletionItem> properlyOrdered = sampleCompletions
.OrderByDescending ( item => item.Label.IndexOfCaseInsensitive ( theCompletionContext.WordToComplete ) )
.ThenBy ( item => item.SortText, StringComparer.OrdinalIgnoreCase );
foreach ( CompletionItem c in properlyOrdered )
{
Console.WriteLine ( c.Label );
}
Output:
5
6
10
20
30
And here is how it would look during tab completion, if the user had already entered "2" and then presses tab (simulated by giving it the "2" directly):
IOrderedEnumerable<CompletionItem> properlyOrderedWithPartialEntry = sampleCompletions
.OrderByDescending ( item => item.Label.IndexOfCaseInsensitive ( "2" ) )
.ThenBy ( item => item.SortText, StringComparer.OrdinalIgnoreCase );
foreach ( CompletionItem c in properlyOrderedWithPartialEntry )
{
Console.WriteLine ( c.Label );
}
Output:
20
5
6
10
30
The actual code to replace what is there now would be:
This is incorrect. See my follow-up comment that fixes it.
return completions
.OrderByDescending(item => item.Label.IndexOfCaseInsensitive(context.WordToComplete))
.ThenBy(symbol => symbol.SortText, StringComparer.OrdinalIgnoreCase);
Also, it may be worth pointing out that the extension method IndexOfCaseInsensitive does not use the same StringComparer as the code here. It uses invariant culture. This uses ordinal. So the two sorts aren't the same, necessarily, even if the values are identical.
- 主要语言
- C#
- 星标
- 3.7k
- 派生
- 436
- 平均合并
- 6 小时 27 分钟
- 30 天内合并 PR
- 2
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dotnet/command-line-api 的其他 Issue
-
German localization is incomplete可能已有人在做 @b-v-d-e-v 于 22 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 65/100
dotnet/command-line-api#2852 ·
维护者通常 1 天内回复
-
Incomplete French (fr) translation: RequiredOptionWasNotProvided not translated可能已有人在做 @JPBlanc 于 120 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 65/100
dotnet/command-line-api#2822 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
dotnet/command-line-api#2792 · 2 条评论 · 17 个 reaction ·
维护者通常 1 天内回复
-
`Argument.Arity` modify internal state on first get access.可能已有人在做 @zjdangge 于 7 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 62/100
dotnet/command-line-api#2704 ·
维护者通常 1 天内回复
-
GetCompletions should check exit code of invoked application可能已有人在做 @baradgur 于 1291 天前认领。 未关闭Area-Completions bug help wanted
难度 2/5 1-3 小时 新手友好度 72/100
dotnet/command-line-api#2137 · 1 条评论 · 3 个 reaction ·
维护者通常 1 天内回复
查看 dotnet/command-line-api 的全部 Issue
相似的 Issue
-
area-multitenancy area-security p1-recommended priority-high technical-debt
难度 2/5 1 小时以内 新手友好度 75/100
维护者通常 1 天内回复
-
kind/enhancement Provider Upgrade pulumi/pulumi-azure R1 Task
难度 2/5 1-3 小时 新手友好度 65/100
pulumi/pulumi-azure#3669 ·
-
type/bug
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
webclient does not start可能已有人在做 @PawelTroka 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
PawelTroka/Computator.NET#257 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 68/100
eriknihlen/OpenAC#255 ·
维护者通常 1 天内回复