英文:
how to add a additional completion to the already defined git subcommand completion?
问题
我正在尝试扩展自定义的 git 子命令完成:
我试图扩展 git commit -m 或 git commit --message 的完成。
$HOME/.zsh/completions/_git-foo(此路径已添加到 fpath:fpath+=~/.zsh/completions)
#compdef git-foo
_git-foo() {
  _git-commit
  local -a commands
  commands=(
    'hello-git:我们的第一个自动完成函数。'
    'version:显示所使用的 gem 版本。'
  )
  if (( CURRENT == 3 )); then
    if [[ $words[2] == "--message" || $words[2] == "--message=" || $words[2] == "-m" ]]; then
      _describe -t commands 'commands' commands
    fi
  fi
  return 0
}
_git-foo
但主要目标是在不破坏 git commit 的原始完成功能的情况下将此附加完成添加到已定义的 _git-commit 完成中。
我尝试将 #compdef 指令更改为 #compdef git-commit,并将文件名从 _git-foo 更改为 _git-commit,但它没有起作用。
我遇到了以下错误:
_git-commit:1: maximum nested function level reached; increase FUNCNEST?
_git-commit:1: maximum nested function level reached; increase FUNCNEST?
_git-commit:1: maximum nested function level reached; increase FUNCNEST?
为自定义子命令创建自定义完成可以正常工作,但如何扩展已定义的 git 完成而不破坏原始完成功能呢?
英文:
I was experimenting with custom git subcommand completion:
I am trying to extend the completion for git commit -m or git commit --message.
$HOME/.zsh/completions/_git-foo (this path is added to fpath fpath+=~/.zsh/completions)
#compdef git-foo
_git-foo() {
  _git-commit
  local -a commands
  commands=(
    'hello-git:our first autocompletion function.'
    'version:show the used gem version.'
  )
  if (( CURRENT == 3 )); then
    if [[ $words[2] == "--message" || $words[2] == "--message=" || $words[2] == "-m" ]]; then
      _describe -t commands 'commands' commands
    fi
  fi
  return 0
}
_git-foo
but the main objective is to add this additional completion to the already defined _git-commit completion without breaking the original completion function for git commit
I have tried to change the #compdef directive to #compdef git-commit and file name from _git-foo to _git-commit but it didn't work.
I was getting the following error:
_git-commit:1: maximum nested function level reached; increase FUNCNEST?
_git-commit:1: maximum nested function level reached; increase FUNCNEST?
_git-commit:1: maximum nested function level reached; increase FUNCNEST?
Creating your custom completion for a custom subcommand works fine, but how to extend the already defined git completions without breaking the original one?
答案1
得分: 1
请注意,我还不能实现你的目标。最近我也在尝试自定义zsh自动补全,希望我找到的内容能对你有所帮助。
无论如何,我会很感兴趣听听你的反馈,因为我也在摸索中 ![]()
最大嵌套函数级别错误
你正试图定义一个名为_git_commit的函数,它调用了自身,从而创建了一个递归。为了防止这样的错误导致无限循环,zsh会在嵌套函数调用达到配置的数量(FUNCNEST)时停止。
可能的解决方案
你可以保存对git原生使用的_git_commit完成函数的引用,并根据需要重新定义它。然后使用你保存的引用来调用回原始的完成函数:
_git_commit_orig="$functions[_git_commit]"
_git_commit() {
  [...]
  _git_commit_orig "$@"
}
正如Attila所述:
你不需要再次调用
compdef,因为它已经绑定到那个函数,你只是改变了函数定义。
希望这对你有所帮助。
英文:
Note that I'm not yet able to acheive what you're aiming to do. I am also fiddling with zsh custom autocompletion lately, hopefully what I found can help you out
In any case I'll be interested in your feedback as I'm discovering the ropes as well ![]()
Maximum nested function level error
Based on this answer from user 4015-alt:
You are attempting to define a function _git_commit which calls itself, thus creating a recursion. To prevent such mistakes from creating infinite loop, zsh will stop when the amount of nested function call reaches configured amount: FUNCNEST
Potential solution
Based on this answer from user Attila
You could save a reference to the original _git_commit completion function used natively by git, and redefine it as you need
Then use the reference you saved to call back the original completion function:
_git_commit_orig="$functions[_git_commit]"
_git_commit() {
  [...]
  _git_commit_orig "$@"
}
As mentioned by Attila:
> You wouldn't need to call compdef again, since it is already bound to that function, and you just changed the function definition.
Hope this helps
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论