-
Notifications
You must be signed in to change notification settings - Fork 971
feature/bash_fish_power_shells_completions #401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
noameron
wants to merge
6
commits into
Fission-AI:main
Choose a base branch
from
noameron:feature/bash_fish_power_shells_completions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e89f79e
added CLI completions support for: bash, fish and powershell
noameron ad53abc
Add bash/fish/powershell completions
noameron 07cad96
Archive extend-shell-completions
noameron 0bac2e3
Archive extend-shell-completions
noameron 5524026
Merge branch 'main' into feature/bash_fish_power_shells_completions
noameron 9dd9962
Fix canWriteFile control flow and add tests
noameron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| import { CompletionGenerator, CommandDefinition, FlagDefinition } from '../types.js'; | ||
|
|
||
| /** | ||
| * Generates Bash completion scripts for the OpenSpec CLI. | ||
| * Follows Bash completion conventions using complete builtin and COMPREPLY array. | ||
| */ | ||
| export class BashGenerator implements CompletionGenerator { | ||
| readonly shell = 'bash' as const; | ||
|
|
||
| /** | ||
| * Generate a Bash completion script | ||
| * | ||
| * @param commands - Command definitions to generate completions for | ||
| * @returns Bash completion script as a string | ||
| */ | ||
| generate(commands: CommandDefinition[]): string { | ||
| const script: string[] = []; | ||
|
|
||
| // Header comment | ||
| script.push('# Bash completion script for OpenSpec CLI'); | ||
| script.push('# Auto-generated - do not edit manually'); | ||
| script.push(''); | ||
|
|
||
| // Main completion function | ||
| script.push('_openspec_completion() {'); | ||
| script.push(' local cur prev words cword'); | ||
| script.push(' _init_completion || return'); | ||
| script.push(''); | ||
| script.push(' local cmd="${words[1]}"'); | ||
| script.push(' local subcmd="${words[2]}"'); | ||
| script.push(''); | ||
|
|
||
| // Top-level commands | ||
| script.push(' # Top-level commands'); | ||
| script.push(' if [[ $cword -eq 1 ]]; then'); | ||
| script.push(' local commands="' + commands.map(c => c.name).join(' ') + '"'); | ||
| script.push(' COMPREPLY=($(compgen -W "$commands" -- "$cur"))'); | ||
| script.push(' return 0'); | ||
| script.push(' fi'); | ||
| script.push(''); | ||
|
|
||
| // Command-specific completion | ||
| script.push(' # Command-specific completion'); | ||
| script.push(' case "$cmd" in'); | ||
|
|
||
| for (const cmd of commands) { | ||
| script.push(` ${cmd.name})`); | ||
| script.push(...this.generateCommandCase(cmd, ' ')); | ||
| script.push(' ;;'); | ||
| } | ||
|
|
||
| script.push(' esac'); | ||
| script.push(''); | ||
| script.push(' return 0'); | ||
| script.push('}'); | ||
| script.push(''); | ||
|
|
||
| // Helper functions for dynamic completions | ||
| script.push(...this.generateDynamicCompletionHelpers()); | ||
|
|
||
| // Register the completion function | ||
| script.push('complete -F _openspec_completion openspec'); | ||
| script.push(''); | ||
|
|
||
| return script.join('\n'); | ||
| } | ||
|
|
||
| /** | ||
| * Generate completion case logic for a command | ||
| */ | ||
| private generateCommandCase(cmd: CommandDefinition, indent: string): string[] { | ||
| const lines: string[] = []; | ||
|
|
||
| // Handle subcommands | ||
| if (cmd.subcommands && cmd.subcommands.length > 0) { | ||
| lines.push(`${indent}if [[ $cword -eq 2 ]]; then`); | ||
| lines.push(`${indent} local subcommands="` + cmd.subcommands.map(s => s.name).join(' ') + '"'); | ||
| lines.push(`${indent} COMPREPLY=($(compgen -W "$subcommands" -- "$cur"))`); | ||
| lines.push(`${indent} return 0`); | ||
| lines.push(`${indent}fi`); | ||
| lines.push(''); | ||
| lines.push(`${indent}case "$subcmd" in`); | ||
|
|
||
| for (const subcmd of cmd.subcommands) { | ||
| lines.push(`${indent} ${subcmd.name})`); | ||
| lines.push(...this.generateArgumentCompletion(subcmd, indent + ' ')); | ||
| lines.push(`${indent} ;;`); | ||
| } | ||
|
|
||
| lines.push(`${indent}esac`); | ||
| } else { | ||
| // No subcommands, just complete arguments | ||
| lines.push(...this.generateArgumentCompletion(cmd, indent)); | ||
| } | ||
|
|
||
| return lines; | ||
| } | ||
|
|
||
| /** | ||
| * Generate argument completion (flags and positional arguments) | ||
| */ | ||
| private generateArgumentCompletion(cmd: CommandDefinition, indent: string): string[] { | ||
| const lines: string[] = []; | ||
|
|
||
| // Check for flag completion | ||
| if (cmd.flags.length > 0) { | ||
| lines.push(`${indent}if [[ "$cur" == -* ]]; then`); | ||
| const flags = cmd.flags.map(f => { | ||
| const parts: string[] = []; | ||
| if (f.short) parts.push(`-${f.short}`); | ||
| parts.push(`--${f.name}`); | ||
| return parts.join(' '); | ||
| }).join(' '); | ||
| lines.push(`${indent} local flags="${flags}"`); | ||
| lines.push(`${indent} COMPREPLY=($(compgen -W "$flags" -- "$cur"))`); | ||
| lines.push(`${indent} return 0`); | ||
| lines.push(`${indent}fi`); | ||
| lines.push(''); | ||
| } | ||
|
|
||
| // Handle positional completions | ||
| if (cmd.acceptsPositional) { | ||
| lines.push(...this.generatePositionalCompletion(cmd.positionalType, indent)); | ||
| } | ||
|
|
||
| return lines; | ||
| } | ||
|
|
||
| /** | ||
| * Generate positional argument completion based on type | ||
| */ | ||
| private generatePositionalCompletion(positionalType: string | undefined, indent: string): string[] { | ||
| const lines: string[] = []; | ||
|
|
||
| switch (positionalType) { | ||
| case 'change-id': | ||
| lines.push(`${indent}_openspec_complete_changes`); | ||
| break; | ||
| case 'spec-id': | ||
| lines.push(`${indent}_openspec_complete_specs`); | ||
| break; | ||
| case 'change-or-spec-id': | ||
| lines.push(`${indent}_openspec_complete_items`); | ||
| break; | ||
| case 'shell': | ||
| lines.push(`${indent}local shells="zsh bash fish powershell"`); | ||
| lines.push(`${indent}COMPREPLY=($(compgen -W "$shells" -- "$cur"))`); | ||
| break; | ||
| case 'path': | ||
| lines.push(`${indent}COMPREPLY=($(compgen -f -- "$cur"))`); | ||
| break; | ||
| } | ||
|
|
||
| return lines; | ||
| } | ||
|
|
||
| /** | ||
| * Generate dynamic completion helper functions | ||
| */ | ||
| private generateDynamicCompletionHelpers(): string[] { | ||
| const lines: string[] = []; | ||
|
|
||
| lines.push('# Dynamic completion helpers'); | ||
| lines.push(''); | ||
|
|
||
| // Helper for completing change IDs | ||
| lines.push('_openspec_complete_changes() {'); | ||
| lines.push(' local changes'); | ||
| lines.push(' changes=$(openspec __complete changes 2>/dev/null | cut -f1)'); | ||
| lines.push(' COMPREPLY=($(compgen -W "$changes" -- "$cur"))'); | ||
| lines.push('}'); | ||
| lines.push(''); | ||
|
|
||
| // Helper for completing spec IDs | ||
| lines.push('_openspec_complete_specs() {'); | ||
| lines.push(' local specs'); | ||
| lines.push(' specs=$(openspec __complete specs 2>/dev/null | cut -f1)'); | ||
| lines.push(' COMPREPLY=($(compgen -W "$specs" -- "$cur"))'); | ||
| lines.push('}'); | ||
| lines.push(''); | ||
|
|
||
| // Helper for completing both changes and specs | ||
| lines.push('_openspec_complete_items() {'); | ||
| lines.push(' local items'); | ||
| lines.push(' items=$(openspec __complete changes 2>/dev/null | cut -f1; openspec __complete specs 2>/dev/null | cut -f1)'); | ||
| lines.push(' COMPREPLY=($(compgen -W "$items" -- "$cur"))'); | ||
| lines.push('}'); | ||
| lines.push(''); | ||
|
|
||
| return lines; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
# First, let's look at the bash-generator.ts file to understand the complete context fd -t f bash-generator.ts -x wc -l {}Repository: Fission-AI/OpenSpec
Length of output: 118
🏁 Script executed:
Repository: Fission-AI/OpenSpec
Length of output: 7773
🏁 Script executed:
Repository: Fission-AI/OpenSpec
Length of output: 231
🏁 Script executed:
Repository: Fission-AI/OpenSpec
Length of output: 45
🏁 Script executed:
Repository: Fission-AI/OpenSpec
Length of output: 1791
🏁 Script executed:
Repository: Fission-AI/OpenSpec
Length of output: 2577
🏁 Script executed:
# Look at the beginning of bash-generator.ts to see if there's any documentation head -n 30 src/core/completions/generators/bash-generator.tsRepository: Fission-AI/OpenSpec
Length of output: 1115
Document bash-completion requirement in generated script header.
The
_init_completionfunction depends on the bash-completion package. While the BashInstaller already checks for this dependency and warns users during installation if it's missing, the generated script header could document this requirement to make the dependency explicit in the script itself.Consider adding a comment in the generated script header (e.g.,
# Requires: bash-completion package) for clarity.🤖 Prompt for AI Agents