zsh-users
 help / color / mirror / code / Atom feed
* completion implementation woes
@ 2016-10-02  0:19 Roman Neuhauser
  2016-10-02  1:31 ` Oliver Kiddle
  0 siblings, 1 reply; 3+ messages in thread
From: Roman Neuhauser @ 2016-10-02  0:19 UTC (permalink / raw)
  To: zsh-users

hello,

i have difficult time with completion for a git subcommand i wrote,
and would appreciate a little help.

the tool has this synopsis (all options have one-letter equivalents,
only the long versions are listed here to keep it simpler):

  git [git-opts] dirs -h | --help
  git [git-opts] dirs activate REPO
  git [git-opts] dirs active [--full | --relative | --short]
  git [git-opts] dirs clone [--no-activate] URL [REPO]
  git [git-opts] dirs init [--no-activate] REPO
  git [git-opts] dirs list [--full | --relative | --short]

the option handling code requires options in places indicated by
the synopsis (`git dirs init stuff --no-activate` is an error).

below is my failed attempt at implementing the completion, with
descriptions of and questions about its behaviors.  the code largely
cargo-cults Completion/Unix/_cvs as seen in zsh-5.2, yet it doesn't
work beyond _git-dirs-_verb (_arguments -> _describe).
_arguments -> _arguments chains behave strangely.

i'm apparently missing something about _arguments: _cvs chains
_arguments calls in the same way, why doesn't mine work?

  #compdef git-dirs
  #description manage multiple git dirs
  # vim: ts=2 sts=2 sw=2 et fdm=marker cms=\ #\ %s

  function _git-dirs # {{{
  {
    local -A opt_args
    local -a state
    _git-dirs-_args \
      - '(help)' \
        '-h[display usage]' \
        '--help[display man page]' \
      - 'command' \
        ":command:_git-dirs-_verb" \
        "*:option or operand:_git-dirs-_verb-arg"
  } # }}}

  function _git-dirs-_verb # {{{
  {
    local -a _commands=(
      activate:'Associate $PWD with REPO'
      active:'Show the currently active repository'
      clone:'Clone URL into .git-dirs/repo.d/REPO'
      init:'Initialize a repository in .git-dirs/repo.d/REPO'
      list:'List repositories in .git-dirs/repo.d'
    )
    _describe -t commands command _commands
  } # }}}

  function _git-dirs-_verb-arg # {{{
  {
    local cmd=$words[2]
    local rv=1

    :; _call_function rv _git-dirs-$cmd \
    || _message -r "unknown command: $cmd"
    :; return $rv
  } # }}}

  function _git-dirs-_args # {{{
  {
    _arguments -S -s : "$@"
  } # }}}

  # $ git dirs activate <TAB>
  # #### no more arguments ####

  function _git-dirs-activate # {{{
  {
    local -a repos=(.git-dirs/repo.d/*(/N:t))
    _git-dirs-_args \
      ":REPO:($repos)"
  } # }}}

  # $ git dirs clone <TAB>
  # #### REPO ####

  # why does it expect REPO?  what happened to URL?

  function _git-dirs-clone # {{{
  {
    _git-dirs-_args \
      {-N,--no-activate}'[Do not activate the repository]' \
      ':URL: ' \
      '::REPO: '
  } # }}}

  # $ git dirs init <TAB> (inserts '-')
  # #### option ####
  --no-activate  -N  -- Do not activate the repository                                                   

  # why does it insert a dash? `git dirs clone <TAB>` has
  # a very similar _arguments call and does something else.

  # $ git dirs init -N<TAB>
  # #### no more arguments ####

  # why all the <NOTHING>s below?  what makes them different
  # from the "no more arguments" cases? (<NOTHING> means no output
  # from the completion system)

  # $ git dirs init -N <TAB>
  # <NOTHING>

  # $ git dirs init --no-activate<TAB> (inserts space)
  # <NOTHING>

  # $ git dirs init --no-activate <TAB>
  # <NOTHING>

  function _git-dirs-init # {{{
  {
    _git-dirs-_args \
      {-N,--no-activate}'[Do not activate the repository]' \
      ':REPO: '
  } # }}}

  # $ git dirs active <TAB>
  # <NOTHING>

  function _git-dirs-active _git-dirs-list # {{{
  {
    _git-dirs-_args \
    - '(full)' \
      {-f,--full}'[Display full paths]' \
    - '(relative)' \
      {-r,--relative}'[Display relative paths]' \
    - '(short)' \
      {-s,--short}'[Display basenames]'
  } # }}}

  _git-dirs "$@"

-- 
roman


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: completion implementation woes
  2016-10-02  0:19 completion implementation woes Roman Neuhauser
@ 2016-10-02  1:31 ` Oliver Kiddle
  2016-10-02 17:43   ` Roman Neuhauser
  0 siblings, 1 reply; 3+ messages in thread
From: Oliver Kiddle @ 2016-10-02  1:31 UTC (permalink / raw)
  To: Roman Neuhauser; +Cc: zsh-users

Roman Neuhauser wrote:
> i have difficult time with completion for a git subcommand i wrote,
> and would appreciate a little help.

>     _git-dirs-_args \
>       - '(help)' \
>         '-h[display usage]' \
>         '--help[display man page]' \
>       - 'command' \
>         ":command:_git-dirs-_verb" \
>         "*:option or operand:_git-dirs-_verb-arg"

You'll find it solves quite a few of your issues if you change this to
the two colon form:

      "*::option or operand:_git-dirs-_verb-arg"

When handling subcommands, it is important to drop initial arguments
from the beginning of $words to avoid confusing later calls to
_arguments. If $words contains ( git dirs init ), _arguments sees a
command and two arguments. That's what _arguments called from _git
wants but not good from _git_dirs-init.

Getting rid of the sets in _git-dirs fixes completion after git
dirs init -N. Somehow it isn't getting past the first _arguments. --help
style options can usually just be given an exclusion list of (- :)

Oliver


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: completion implementation woes
  2016-10-02  1:31 ` Oliver Kiddle
@ 2016-10-02 17:43   ` Roman Neuhauser
  0 siblings, 0 replies; 3+ messages in thread
From: Roman Neuhauser @ 2016-10-02 17:43 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: zsh-users

# okiddle@yahoo.co.uk / 2016-10-02 03:31:54 +0200:
> Roman Neuhauser wrote:
> > i have difficult time with completion for a git subcommand i wrote,
> > and would appreciate a little help.
> 
> >     _git-dirs-_args \
> >       - '(help)' \
> >         '-h[display usage]' \
> >         '--help[display man page]' \
> >       - 'command' \
> >         ":command:_git-dirs-_verb" \
> >         "*:option or operand:_git-dirs-_verb-arg"
> 
> You'll find it solves quite a few of your issues if you change this to
> the two colon form:
> 
>       "*::option or operand:_git-dirs-_verb-arg"
> 
> When handling subcommands, it is important to drop initial arguments
> from the beginning of $words to avoid confusing later calls to
> _arguments. If $words contains ( git dirs init ), _arguments sees a
> command and two arguments. That's what _arguments called from _git
> wants but not good from _git_dirs-init.

  why does '*::' pop $words?

> Getting rid of the sets in _git-dirs fixes completion after git
> dirs init -N. Somehow it isn't getting past the first _arguments. --help
> style options can usually just be given an exclusion list of (- :)

  is there any chance to get the - thing working with chained _arguments
  calls?  this seems to be the least-clutter notation...

  in fact, i'd love to get as close to a grammatic pov as possible,
  and the more faithful the representation the better.  eg. activate
  does not take any operands, so the '*::option or operand:..' rule
  rubs me the really wrong way.

  _git-dirs = "git" gitopt* "dirs" op

  op        = help
            | cmd

  help      = "-h" | "--help"

  cmd       = activate
            | active
            | clone
            | init
            | list

  activate  = "activate" repo

  active    = "active" o_path?
  list      = "list" o_path?

  clone     = "clone" o_N? url repo
  init      = "init" o_N? repo
  
  o_path    = o_f | o_r | o_s

  o_N       = "-N" | "--no-activate"
  o_f       = "-f" | "--full"
  o_r       = "-r" | "--relative"
  o_s       = "-s" | "--short"

-- 
roman


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-10-02 17:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-02  0:19 completion implementation woes Roman Neuhauser
2016-10-02  1:31 ` Oliver Kiddle
2016-10-02 17:43   ` Roman Neuhauser

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).