Skip to content

Commit

Permalink
bash,aot: Emit trailing spaces precisely #35
Browse files Browse the repository at this point in the history
  • Loading branch information
adaszko committed Nov 23, 2023
1 parent ee13638 commit 6f9734b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
36 changes: 18 additions & 18 deletions e2e/bash/test_bash_aot.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_bash_uses_correct_transition_with_duplicated_literals(complgen_binary_p
'''

with completion_script_path(complgen_binary_path, GRAMMAR) as completions_file_path:
assert get_sorted_bash_completions(completions_file_path, r'''COMP_WORDS=(cmd remote); COMP_CWORD=2; _cmd; if [[ ${#COMPREPLY[@]} -gt 0 ]]; then printf '%s\n' "${COMPREPLY[@]}"; fi''') == sorted(['--help', 'rm'])
assert get_sorted_bash_completions(completions_file_path, r'''COMP_WORDS=(cmd remote); COMP_CWORD=2; _cmd; if [[ ${#COMPREPLY[@]} -gt 0 ]]; then printf '%s\n' "${COMPREPLY[@]}"; fi''') == sorted(['--help ', 'rm '])
assert get_sorted_bash_completions(completions_file_path, r'''COMP_WORDS=(cmd rm); COMP_CWORD=2; _cmd; if [[ ${#COMPREPLY[@]} -gt 0 ]]; then printf '%s\n' "${COMPREPLY[@]}"; fi''') == sorted([])


Expand Down Expand Up @@ -96,7 +96,7 @@ def test_matches_prefix(complgen_binary_path: Path):
'''
with completion_script_path(complgen_binary_path, GRAMMAR) as path:
input = r'''COMP_WORDS=(cargo +stable-aarch64-apple-darwin); COMP_CWORD=2; _cargo; printf '%s\n' "${COMPREPLY[@]}"'''
assert get_sorted_bash_completions(path, input) == sorted(['foo'])
assert get_sorted_bash_completions(path, input) == sorted(['foo '])


def test_completes_prefix(complgen_binary_path: Path):
Expand All @@ -106,7 +106,7 @@ def test_completes_prefix(complgen_binary_path: Path):
'''
with completion_script_path(complgen_binary_path, GRAMMAR) as path:
input = r'''COMP_WORDS=(cargo +); COMP_CWORD=1; _cargo; printf '%s\n' "${COMPREPLY[@]}"'''
assert get_sorted_bash_completions(path, input) == sorted(['+stable-aarch64-apple-darwin', '+stable-x86_64-apple-darwin'])
assert get_sorted_bash_completions(path, input) == sorted(['+stable-aarch64-apple-darwin ', '+stable-x86_64-apple-darwin '])


def test_completes_strace_expr(complgen_binary_path: Path):
Expand All @@ -125,7 +125,7 @@ def test_subword_descriptions(complgen_binary_path: Path):
GRAMMAR = r'''cmd --option=(arg1 "descr1" | arg2 "descr2");'''
with completion_script_path(complgen_binary_path, GRAMMAR) as path:
input = r'''COMP_WORDS=(cmd --option=); COMP_CWORD=1; _cmd; printf '%s\n' "${COMPREPLY[@]}"'''
assert get_sorted_bash_completions(path, input) == sorted(['arg1', 'arg2'])
assert get_sorted_bash_completions(path, input) == sorted(['arg1 ', 'arg2 '])


def test_completes_subword_external_command(complgen_binary_path: Path):
Expand All @@ -150,17 +150,17 @@ def test_mygrep_example(complgen_binary_path: Path, usage_directory_path: Path):
GRAMMAR = (usage_directory_path / "mygrep.usage").read_text()
with completion_script_path(complgen_binary_path, GRAMMAR) as path:
input = r'''COMP_WORDS=(mygrep); COMP_CWORD=1; _mygrep; printf '%s\n' "${COMPREPLY[@]}"'''
assert get_sorted_bash_completions(path, input) == sorted(['--', '-E', '--extended-regexp', '-F', '--fixed-strings', '-G', '--basic-regexp',
'-P', '--perl-regexp', '-e', '--regexp', '-f', '--file', '-i',
'--ignore-case', '--no-ignore-case', '-w', '--word-regexp', '-x',
'--line-regexp', '-z', '--null-data', '-s', '--no-messages', '-v',
'--invert-match', '-V', '--version', '--help', '-m', '--max-count=',
'--max-count', '-b', '--byte-offset', '-n', '--line-number',
'--line-buffered', '-H', '--with-filename', '-h', '--no-filename', '--label',
'-o', '--only-matching', '-q', '--quiet', '--silent', '--binary-files', '-a',
'--text', '-d', '--directories', '-D', '--devices', '-r', '--recursive',
'-R', '--dereference-recursive', '--include', '--exclude', '--exclude-from', '--exclude-dir', '-L', '--files-without-match', '-l',
'--files-with-matches', '-c', '--count', '-T', '--initial-tab', '-Z',
'--null', '-B', '--before-context', '-A', '--after-context', '-C',
'--context', '-', '--group-separator=', '--no-group-separator', '--color',
'--colour', '--color=', '--colour=', '-U', '--binary'])
assert get_sorted_bash_completions(path, input) == sorted(['-- ', '-E ', '--extended-regexp ', '-F ', '--fixed-strings ', '-G ', '--basic-regexp ',
'-P ', '--perl-regexp ', '-e ', '--regexp ', '-f ', '--file ', '-i ',
'--ignore-case ', '--no-ignore-case ', '-w ', '--word-regexp ', '-x ',
'--line-regexp ', '-z ', '--null-data ', '-s ', '--no-messages ', '-v ',
'--invert-match ', '-V ', '--version ', '--help ', '-m ', '--max-count=',
'--max-count ', '-b ', '--byte-offset ', '-n ', '--line-number ',
'--line-buffered ', '-H ', '--with-filename ', '-h ', '--no-filename ', '--label ',
'-o ', '--only-matching ', '-q ', '--quiet ', '--silent ', '--binary-files ', '-a ',
'--text ', '-d ', '--directories ', '-D ', '--devices ', '-r ', '--recursive ',
'-R ', '--dereference-recursive ', '--include ', '--exclude ', '--exclude-from ', '--exclude-dir ', '-L ', '--files-without-match ', '-l ',
'--files-with-matches ', '-c ', '--count ', '-T ', '--initial-tab ', '-Z ',
'--null ', '-B ', '--before-context ', '-A ', '--after-context ', '-C ',
'--context ', '-', '--group-separator=', '--no-group-separator ', '--color ',
'--colour ', '--color=', '--colour=', '-U ', '--binary '])
9 changes: 7 additions & 2 deletions src/aot/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ fn write_subword_fn<W: Write>(buffer: &mut W, command: &str, id: usize, dfa: &DF
if [[ $literal = "${{completed_prefix}}"* ]]; then
local completion="$matched_prefix$literal"
completion=${{completion#"$superfluous_prefix"}}
echo $completion
local to_state=${{state_transitions[$literal_id]}}
if [[ -v "literal_transitions[$to_state]" || -v "match_anything_transitions[$to_state]" ]]; then
echo $completion
else
echo "$completion "
fi
fi
done
fi
Expand Down Expand Up @@ -348,7 +353,7 @@ pub fn write_completion_script<W: Write>(buffer: &mut W, command: &str, dfa: &DF
local literal="${{literals[$literal_id]}}"
if [[ $literal = "${{prefix}}"* ]]; then
local completion=${{literal#"$superfluous_prefix"}}
COMPREPLY+=("$completion")
COMPREPLY+=("$completion ")
fi
done
fi
Expand Down
6 changes: 3 additions & 3 deletions src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ fn make_specializations_map(statements: &[Statement]) -> Result<UstrMap<Speciali

specializations.entry(ustr("USER")).or_insert_with(||
Specialization {
bash: Some(ustr(r#"compgen -A user"#)),
bash: Some(ustr(r#"compgen -A user | while read line; do echo "$line "; done"#)),
fish: Some(ustr(r#"__fish_complete_users"#)),
zsh: Some(ustr(r#"_users"#)),
generic: None,
Expand All @@ -666,7 +666,7 @@ fn make_specializations_map(statements: &[Statement]) -> Result<UstrMap<Speciali

specializations.entry(ustr("GROUP")).or_insert_with(||
Specialization {
bash: Some(ustr(r#"compgen -A group"#)),
bash: Some(ustr(r#"compgen -A group | while read line; do echo "$line "; done"#)),
fish: Some(ustr(r#"__fish_complete_groups"#)),
zsh: Some(ustr(r#"_groups"#)),
generic: None,
Expand All @@ -675,7 +675,7 @@ fn make_specializations_map(statements: &[Statement]) -> Result<UstrMap<Speciali

specializations.entry(ustr("HOST")).or_insert_with(||
Specialization {
bash: Some(ustr(r#"compgen -A hostname"#)),
bash: Some(ustr(r#"compgen -A hostname | while read line; do echo "$line "; done"#)),
fish: Some(ustr(r#"__fish_complete_hostnames"#)),
zsh: Some(ustr(r#"_hosts"#)),
generic: None,
Expand Down

0 comments on commit 6f9734b

Please sign in to comment.