如何为已定义的git子命令补全添加额外的补全?

huangapple go评论57阅读模式
英文:

how to add a additional completion to the already defined git subcommand completion?

问题

我正在尝试扩展自定义的 git 子命令完成:

我试图扩展 git commit -mgit 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子命令补全添加额外的补全?

最大嵌套函数级别错误

根据用户4015-alt这个答案中的回答:

你正试图定义一个名为_git_commit的函数,它调用了自身,从而创建了一个递归。为了防止这样的错误导致无限循环,zsh会在嵌套函数调用达到配置的数量(FUNCNEST)时停止。

可能的解决方案

根据用户Attila这个答案中的回答:

你可以保存对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 如何为已定义的git子命令补全添加额外的补全?

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

huangapple
  • 本文由 发表于 2023年1月9日 05:11:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定