Skip to content

Commit

Permalink
[builtin/completion_osh] Support SparseArray COMP_ARGV with compadjust
Browse files Browse the repository at this point in the history
* SparseArray_GetValues is used to obtain comp_argv from the shell
  variable COMP_ARGV.  This ignores all the unset elements and only
  picks up the set elements.  Then the word index would be changed.
  This might not be desired.  Maybe it is better to first convert it
  to BashArray and use the member strs?

* Conversely, when there are unset elements in COMP_ARGV with
  BashArray, doesn't that cause problems in the later AdjustArg?
  • Loading branch information
akinomyoga committed Jan 2, 2025
1 parent ade67dc commit df9b56c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
9 changes: 7 additions & 2 deletions builtin/completion_osh.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from _devbuild.gen.syntax_asdl import loc
from _devbuild.gen.value_asdl import (value, value_e)

from core import bash_impl
from core import completion
from core import error
from core import state
Expand Down Expand Up @@ -454,9 +455,13 @@ def Run(self, cmd_val):
# TODO: How does the user test a completion function programmatically? Set
# COMP_ARGV?
val = self.mem.GetValue('COMP_ARGV')
if val.tag() != value_e.BashArray:
if val.tag() == value_e.BashArray:
comp_argv = cast(value.BashArray, val).strs
elif val.tag() == value_e.SparseArray:
comp_argv = bash_impl.SparseArray_GetValues(
cast(value.SparseArray, val))
else:
raise error.Usage("COMP_ARGV should be an array", loc.Missing)
comp_argv = cast(value.BashArray, val).strs

# These are the ones from COMP_WORDBREAKS that we care about. The rest occur
# "outside" of words.
Expand Down
18 changes: 18 additions & 0 deletions spec/ble-idioms.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,24 @@ compgen -F _set_COMPREPLY
## END


#### SparseArray: compgen -F _set_COMPREPLY
case $SH in bash|zsh|mksh|ash) exit ;; esac

a=(echo 'Hello,' 'Bash' 'world!')
var COMP_ARGV = _a2sp(a)
compadjust cur prev words cword
argv.py "$cur" "$prev" "$cword"
argv.py "${words[@]}"

## STDOUT:
['world!', 'Bash', '3']
['echo', 'Hello,', 'Bash', 'world!']
## END

## N-I bash/zsh/mksh/ash STDOUT:
## END


#### SparseArray (YSH): $[a1 === a2]
case $SH in bash|zsh|mksh|ash) exit ;; esac

Expand Down

0 comments on commit df9b56c

Please sign in to comment.