在非选项(noDashes)输入后修复 zsh _arguments 选项(--whatever)完成

Fix zsh _arguments options (--whatever) completion after non-options (noDashes) input

我想在 my gradle completion script 中输入其他内容后允许完成 --flags,但 _arguments 似乎要求

specs that describe option flags must precede specs that describe non-option ("positional" or "normal") arguments of the analyzed line (from the zsh completion docs)

换句话说:command foo --o[TAB] 什么都不做,但 command --o[TAB] 工作正常。有什么方法可以配置 _arguments 还是我需要使用其他 control functions?

注意: 在我的情况下,单独的完成函数似乎不是一个选项,因为输入不在固定列表中(gradle 任务是任意的,多个可以指定,gradle myfoo mybar --o[TAB] 必须工作)。

所以你希望能够键入 vim foo -[TAB] 之类的内容并让列表自动展开以显示标志和开关,而目前你必须键入 vim -[TAB] 才能获得你的标志和开关以及然后输入 foo,是吗?

希望我能正确理解你的问题。

我当前的 zsh 完成选项可能会对此有所帮助,因为我可以按照我描述的那样做,这似乎是您所要求的?我设置这些已经有一段时间了,所以我不记得每个人都做了什么。我相信您想要的是 setopt COMPLETE_IN_WORDunset LIST_AMBIGUOUS 以及 zstyle ':completion::approximate*:*' prefix-needed false 选项。如果我错了,请纠正我。

我已经将我在 zsh 中使用的内容作为我的完成部分。我已经对其进行了独立测试,它可以在我的 zsh 上按原样运行。

#{{{ Completion Stuff
zmodload -i zsh/complist
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
bindkey -M viins '\C-i' complete-word
# Faster! (?)
zstyle ':completion::complete:*' use-cache 1
# case insensitive completion
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}' \
  'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
zstyle ':completion:*' verbose yes
zstyle ':completion:*:descriptions' format '%B%d%b'
zstyle ':completion:*:messages' format '%d'
zstyle ':completion:*:warnings' format 'No matches for: %d'
zstyle ':completion:*' group-name ''
# generate descriptions with magic.
zstyle ':completion:*' auto-description 'specify: %d'
# Don't prompt for a huge list, page it!
zstyle ':completion:*:default' list-prompt '%S%M matches%s'
# Don't prompt for a huge list, menu it!
zstyle ':completion:*:default' menu 'select=0'
# Have the newer files last so I see them first
zstyle ':completion:*' file-sort modification reverse
# color code completion
zstyle ':completion:*' list-colors "=(#b) #([0-9]#)*=36=31"
unsetopt LIST_AMBIGUOUS
setopt  COMPLETE_IN_WORD
# Separate man page sections.
zstyle ':completion:*:manuals' separate-sections true
#
zstyle ':completion:*' list-separator 'fREW'
# complete with a menu for xwindow ids
zstyle ':completion:*:windows' menu on=0
zstyle ':completion:*:expand:*' tag-order all-expansions
# more errors allowed for large words and fewer for small words
zstyle ':completion:*:approximate:*' max-errors 'reply=(  $((  ($#PREFIX+$#SUFFIX)/3  ))  )'
# Errors format
zstyle ':completion:*:corrections' format '%B%d (errors %e)%b'
# Don't complete stuff already on the line
zstyle ':completion::*:(rm|vi):*' ignore-line true
# Don't complete directory we are already in (../here)
zstyle ':completion:*' ignore-parents parent pwd
zstyle ':completion::approximate*:*' prefix-needed false

我能够用 this commit 解决这个问题,至少在指定 1 个任务后允许选项。

诀窍是用 :->statename 设置 _arguments 内的状态,将上下文重置为下一个单词,并提供匹配 non-command 个单词的通配符匹配器并使用 _arguments 再次。

几乎肯定有一种方法允许在任意数量的单词后指定选项并避免重复,但这是一个可行的开始。