fix: Wrong completion context for chained commands in zsh
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 82/100
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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)
- Langage dominant
- Dart
- Étoiles
- 54
- Forks
- 6
- Merge moyen
- 24 min
- PR mergées (30 j)
- 1
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Aucun guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de VeryGoodOpenSource/cli_completion
-
feature p3
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 30/100
VeryGoodOpenSource/cli_completion#91 · 2 commentaires · 1 réaction ·
-
Better shell completionOuvertefeature p3
Difficulté 3/5 1-2 jours Accessibilité débutants 35/100
VeryGoodOpenSource/cli_completion#90 · 1 commentaire · 1 réaction ·
-
feat: CLI-user configurable auto installPeut-être pris @vgvbot l’a pris il y a 53 jours. Ouvertefeature p2
VeryGoodOpenSource/cli_completion#55 · 1 commentaire · 1 réaction · 1 personne assignée ·
-
feature p2
Difficulté 4/5 3-5 jours Accessibilité débutants 35/100
VeryGoodOpenSource/cli_completion#47 · 2 commentaires · 2 réactions ·
Toutes les issues de VeryGoodOpenSource/cli_completion
Issues similaires
-
direct-code
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
aaronified/fluenough#500 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
Les mainteneurs répondent en général sous 1 jour
-
Export `SignalEquality` classOuverte
Difficulté 1/5 Moins d'une heure Accessibilité débutants 84/100
rodydavis/signals.dart#508 ·
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
lugassawan/swe-workbench#799 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 67/100
Les mainteneurs répondent en général sous 5 jours