zsh-users
 help / color / mirror / code / Atom feed
* _values with controllable duplicates suppression?
@ 2021-03-18 23:40 Phil Pennock
  2021-03-19  2:40 ` Oliver Kiddle
  0 siblings, 1 reply; 2+ messages in thread
From: Phil Pennock @ 2021-03-18 23:40 UTC (permalink / raw)
  To: zsh-users

My completion-fu is failing me once more and I would like some guidance
on how to handle supplying completions, where currently I use
`_values -w`.

I have a custom `git grab` which has various heuristics and ultimately
lets me do `git grab -o orgname repo1 repo2 repo3`.  I have
tab-completion for this and it mostly works, and has for a long time.

The completion caches the results of using the GitHub API for a given
org/user, using appropriate keying into an associative array var, so
that if I hit tab multiple times there's only one remote API call until
a half hour passes and the cached data is invalidated.

I will be creating entries in the current directory, so have:

  already=(*(D-/))

and then call, when the cache is already populated:

  candidates=(${(o)=_github_repos_byowner[$cache_key]})
  _values -w "uncloned github repositories for $identifier" "${(@)candidates:|already}"

and ultimately the flow for when the cache needs to be populated ends up
with the same _values call.

The top level is:

  _arguments -C -s [options stuff] '*:REPO:_github_repos_for'

I really like the way that `_values -w` means that if I have already put
a given repo onto the command-line, it's not offered again.

My problem is that sometimes there's an organization with the same name
as one of the repos inside that organization.  Eg, "foo/foo" as well as
"foo/bar", "foo/baz", so if I do:

  git grab -o foo <TAB>

then the "foo" result is suppressed because `_values -w` sees that `foo`
is already on the command-line, and I'm only offered `bar` and `baz`.

Is there any way (context shift, something else?) to track from
_arguments which word is the start of the `*:REPO:...` completion point
and constrain _values (or equivalent) to only suppress duplicate words
which appear from that point onwards?

Thanks,
-Phil


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

* Re: _values with controllable duplicates suppression?
  2021-03-18 23:40 _values with controllable duplicates suppression? Phil Pennock
@ 2021-03-19  2:40 ` Oliver Kiddle
  0 siblings, 0 replies; 2+ messages in thread
From: Oliver Kiddle @ 2021-03-19  2:40 UTC (permalink / raw)
  To: Phil Pennock; +Cc: zsh-users

Phil Pennock wrote:
> I really like the way that `_values -w` means that if I have already put
> a given repo onto the command-line, it's not offered again.

I wouldn't use _values just for that feature. Try passing -F words (or
-F line if using _arguments) to compadd.

> My problem is that sometimes there's an organization with the same name
> as one of the repos inside that organization.  Eg, "foo/foo" as well as
> "foo/bar", "foo/baz", so if I do:
>
>   git grab -o foo <TAB>
>
> then the "foo" result is suppressed because `_values -w` sees that `foo`
> is already on the command-line, and I'm only offered `bar` and `baz`.

It is ignoring the contents of the $words array so you can manipulate
that (plus $CURRENT) to only cover the final arguments. Actually,
it is probably using $words[2,-1]. You can use two colons after the
asterisk in the _arguments spec to strip down the words array:

   '*::repo:_github_repos_for'

You may be better off with '*:::repo:= _github_repos_for' actually.
Depends how many normal arguments there are. That sets $words to
only those covered by this spec and then the = makes it add a fake
one for the command-position.

Oliver


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

end of thread, other threads:[~2021-03-19  2:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-18 23:40 _values with controllable duplicates suppression? Phil Pennock
2021-03-19  2:40 ` Oliver Kiddle

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).