fix: Wrong completion context for chained commands in zsh
还没有人认领这个 Issue。
评估
调研方向
Start with lib/src/installer/shell_completion_configuration.dart and CompletionState.fromEnvironment, which the issue identifies as passing and reading the completion context. Check the existing completion tests and how the words passed after completion -- relate to COMP_CWORD. Done means commands after |, && or ; suggest the current command’s options, while existing bash completion still works.
由索引模型根据 Issue 内容生成。
描述
Description
Thank you for this cool lib! I'm facing a minor bug when used with zsh.
In zsh, completing a command that isn't at the start of the command line (after |, && or ;) uses the wrong context. The zsh script passes COMP_LINE="$BUFFER", which is zsh's whole edit buffer, and CompletionState.fromEnvironment reads the arguments from COMP_LINE. So everything typed before the current command is treated as CLI arguments.
Steps To Reproduce
Take a CLI my_cli built on CompletionCommandRunner, with commands foo and bar that have different options, and install its completion in zsh with my_cli install-completion-files.
my_cli foo | my_cli bar <TAB>suggestsfoo's options. The request is parsed asfoo | my_cli bar '', sofoois taken as the command being completed.cd somewhere && my_cli bar <TAB>suggests nothing, so zsh falls back to file names.
Without a shell, this is the request zsh sends for step 1:
ZSH_NAME=zsh COMP_LINE='my_cli foo | my_cli bar ' COMP_POINT=24 COMP_CWORD=2 \
my_cli completion -- my_cli bar ''
It prints foo's options.
Expected Behavior
A command after |, && or ; completes the same way as at the start of the line. Both my_cli foo | my_cli bar <TAB> and cd somewhere && my_cli bar <TAB> suggest bar's options, just like my_cli bar <TAB>. Bash already behaves this way.
Additional Context
Cause
- The zsh template in
lib/src/installer/shell_completion_configuration.dartrunsCOMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" … completion -- "${words[@]}".$BUFFERis the whole edit buffer, while$wordsand$CURRENTcover only the current command. CompletionState.fromEnvironmenttakes the arguments fromCOMP_LINE(line.trimLeft().split(' ').skip(1)) and ignores the words passed aftercompletion --.- Bash isn't affected, because bash itself limits
COMP_LINEto the current command. With bash 3.2,echo x | mycmd a <TAB>setsCOMP_LINEtomycmd a.
Suggested fix
Build the arguments from the words the scripts already pass after completion --, up to COMP_CWORD, instead of from COMP_LINE. Both scripts pass only the current command's words there, so this fixes zsh without changing the scripts or making users reinstall them.
Workaround
Until this is fixed, a CLI can narrow COMP_LINE itself before the request is handled, in its CompletionCommandRunner subclass:
@override
Future<int?> runCommand(ArgResults topLevelResults) {
final command = topLevelResults.command;
if (command?.name == HandleCompletionRequestCommand.commandName) {
final words = command!.rest; // the words after `completion --`
final environment = Platform.environment;
final cword = int.tryParse(environment['COMP_CWORD'] ?? '');
if (cword != null && cword >= 0 && cword < words.length) {
final line = words.take(cword + 1).join(' ');
environmentOverride = {...environment, 'COMP_LINE': line, 'COMP_POINT': '${line.length}'};
}
}
return super.runCommand(topLevelResults);
}
Environment
- cli_completion 0.6.0
- Dart 3.13.3
- zsh 5.9, macOS (arm64)
- 主要语言
- Dart
- 星标
- 54
- 派生
- 6
- 平均合并
- 24 分钟
- 30 天内合并 PR
- 1
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 没有贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
VeryGoodOpenSource/cli_completion 的其他 Issue
-
feature p3
难度 5/5 一周以上 新手友好度 30/100
VeryGoodOpenSource/cli_completion#91 · 2 条评论 · 1 个 reaction ·
-
feature p3
难度 3/5 1-2 天 新手友好度 35/100
VeryGoodOpenSource/cli_completion#90 · 1 条评论 · 1 个 reaction ·
-
feat: CLI-user configurable auto install可能已有人在做 @vgvbot 于 55 天前认领。 未关闭feature p2
VeryGoodOpenSource/cli_completion#55 · 1 条评论 · 1 个 reaction · 已指派 1 人 ·
-
feature p2
难度 4/5 3-5 天 新手友好度 35/100
VeryGoodOpenSource/cli_completion#47 · 2 条评论 · 2 个 reaction ·
查看 VeryGoodOpenSource/cli_completion 的全部 Issue
相似的 Issue
-
coverage 1.16.0: format_coverage fails on the `{}` that `dart test --coverage` writes for fully skipped suites可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
-
fyi-ecosystem platform-ios team-ios
难度 1/5 1 小时以内 新手友好度 70/100
flutter/flutter#194348 · 1 条评论 ·
维护者通常 1 天内回复
-
documentation good first issue
难度 1/5 1 小时以内 新手友好度 88/100
djazairdev/wilayas#22 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 80/100
rajput-hemant/calculator#199 ·
-
难度 2/5 1-3 小时 新手友好度 62/100
leancodepl/app-rating#35 ·