-
-
Notifications
You must be signed in to change notification settings - Fork 285
增加 Zsh Tab 补全 #395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
swim233
wants to merge
6
commits into
RubyMetric:dev
Choose a base branch
from
swim233:feat/zsh-completion
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
增加 Zsh Tab 补全 #395
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| #!/usr/bin/env zsh | ||
| # --------------------------------------------------------------- | ||
| # SPDX-License-Identifier: GPL-3.0-or-later | ||
| # --------------------------------------------------------------- | ||
| # Test File : simulate-zsh-completion.zsh | ||
| # Test Authors : @swim233 | ||
| # Contributors : TheSw1m <swim853279614@163.com> | ||
| # | | ||
| # Created On : <2026-09-08> | ||
| # Last Modified : <2026-09-09> | ||
| # | ||
| # 使用同名函数覆盖 Zsh 补全组件,模拟并测试 _chsrc 的状态机。 | ||
| # 测试不会读取或修改用户的 Zsh 配置。 | ||
| # | ||
| # $ zsh -f test/simulate-zsh-completion.zsh | ||
| # --------------------------------------------------------------- | ||
|
|
||
| emulate -L zsh | ||
| setopt errexit nounset pipefail extendedglob | ||
|
|
||
| typeset test_dir=${0:A:h} | ||
| fpath=("${test_dir}/../tool/completion" ${fpath}) | ||
| autoload -Uz _chsrc | ||
|
|
||
| typeset -ga captured | ||
|
|
||
| # 模拟 _describe,将符合 PREFIX 的候选项记录到 captured | ||
| _describe() { | ||
| local array_name=${argv[-1]} | ||
| local -a specs | ||
| specs=("${(@P)array_name}") | ||
| local spec candidate | ||
| for spec in ${specs}; do | ||
| candidate=${spec%%:*} | ||
| if [[ -z ${PREFIX} || ${candidate[1,${#PREFIX}]} == ${PREFIX} ]]; then | ||
| captured+=("${candidate}") | ||
| fi | ||
| done | ||
| } | ||
|
|
||
| # 模拟 compadd,仅记录 -- 分隔符之后且符合 PREFIX 的候选项 | ||
| compadd() { | ||
| local after_separator=0 arg | ||
| for arg in ${argv}; do | ||
| if (( after_separator )); then | ||
| if [[ -z ${PREFIX} || ${arg[1,${#PREFIX}]} == ${PREFIX} ]]; then | ||
| captured+=("${arg}") | ||
| fi | ||
| elif [[ ${arg} == -- ]]; then | ||
| after_separator=1 | ||
| fi | ||
| done | ||
| } | ||
|
|
||
| # 模拟 compset -P,从 PREFIX 中移除已经输入的固定前缀 | ||
| compset() { | ||
| if [[ $1 == -P ]]; then | ||
| PREFIX=${PREFIX#$2} | ||
| fi | ||
| } | ||
|
|
||
| # 测试无需展示补全提示信息 | ||
| _message() { return 0 } | ||
|
|
||
| # 构造 Zsh 补全上下文并执行 _chsrc | ||
| capture() { | ||
| captured=() | ||
| words=("${(@)argv}") | ||
| CURRENT=${#words} | ||
| PREFIX=${words[CURRENT]} | ||
| _chsrc | ||
| } | ||
|
|
||
| assert_has() { | ||
| local expected=$1 | ||
| # (I) 返回最后一个匹配元素的下标,未匹配时返回 0; | ||
| # (e) 使用字符串精确匹配,避免将候选项当作模式解析。 | ||
| if (( ! ${captured[(Ie)${expected}]} )); then | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里的 |
||
| print -u2 -- "Expected completion '$expected' in: ${captured[*]}" | ||
| return 1 | ||
| fi | ||
| } | ||
|
|
||
| assert_lacks() { | ||
| local unexpected=$1 | ||
| # 与 assert_has 相同,使用 (Ie) 取得精确匹配元素的下标。 | ||
| if (( ${captured[(Ie)${unexpected}]} )); then | ||
| print -u2 -- "Unexpected completion '$unexpected' in: ${captured[*]}" | ||
| return 1 | ||
| fi | ||
| } | ||
|
|
||
| capture chsrc '' | ||
| assert_has version | ||
| assert_has measure | ||
| assert_has reset | ||
| assert_has -scope= | ||
| assert_lacks mea | ||
| assert_lacks rest | ||
| assert_lacks -local | ||
| assert_lacks -scope | ||
| assert_lacks -english | ||
| assert_lacks -no-colour | ||
| assert_lacks -h | ||
| assert_lacks -help | ||
| assert_lacks --help | ||
| assert_lacks -v | ||
| assert_lacks -version | ||
| assert_lacks --version | ||
|
|
||
| capture chsrc -scope=p | ||
| assert_has project | ||
| assert_lacks default | ||
|
|
||
| typeset -A dispatch_expect=( | ||
| help -dry | ||
| h -dry | ||
| -h -dry | ||
| -help -dry | ||
| --help -dry | ||
| issue -dry | ||
| issues -dry | ||
| isue -dry | ||
| i -dry | ||
| version -dry | ||
| ver -dry | ||
| -v -dry | ||
| -version -dry | ||
| --version -dry | ||
| list mirror | ||
| ls mirror | ||
| l mirror | ||
| measure ruby | ||
| mea ruby | ||
| m ruby | ||
| cesu ruby | ||
| ce ruby | ||
| c ruby | ||
| get ruby | ||
| g ruby | ||
| set ruby | ||
| s ruby | ||
| reset ruby | ||
| rest ruby | ||
| r ruby | ||
| ) | ||
|
|
||
| typeset command expected | ||
| for command expected in "${(@kv)dispatch_expect}"; do | ||
| capture chsrc "${command}" '' | ||
| assert_has "${expected}" | ||
| done | ||
|
|
||
| capture chsrc set '' | ||
| assert_has omarchy | ||
| assert_has uv-pypi-index | ||
| assert_has uv-python-build | ||
| assert_has -scope= | ||
| assert_lacks pypi | ||
| assert_lacks nodejs-binary | ||
|
|
||
| capture chsrc s -dry '' | ||
| assert_has ruby | ||
|
|
||
| capture chsrc set -local '' | ||
| assert_has ruby | ||
|
|
||
| capture chsrc set -english '' | ||
| assert_has ruby | ||
|
|
||
| capture chsrc set -no-colour '' | ||
| assert_has ruby | ||
|
|
||
| capture chsrc set -scope project '' | ||
| assert_has ruby | ||
|
|
||
| capture chsrc set -scope '' | ||
| assert_has default | ||
| assert_has project | ||
| assert_has user | ||
| assert_has system | ||
| assert_lacks ruby | ||
|
|
||
| capture chsrc set -scope= | ||
| assert_has default | ||
| assert_has project | ||
| assert_has user | ||
| assert_has system | ||
|
|
||
| capture chsrc set -scope=p | ||
| assert_has project | ||
| assert_lacks default | ||
| assert_lacks system | ||
|
|
||
| capture chsrc set -scope=x | ||
| assert_lacks default | ||
| assert_lacks project | ||
|
|
||
| capture chsrc set -lo | ||
| assert_lacks -local | ||
|
|
||
| capture chsrc -en -he | ||
| assert_lacks -help | ||
| assert_lacks help | ||
|
|
||
| capture chsrc list '' | ||
| assert_has mirror | ||
| assert_has dish | ||
| assert_has lang | ||
| assert_has ware | ||
| assert_lacks mirrors | ||
| assert_lacks dishes | ||
| assert_lacks language | ||
| assert_lacks software | ||
| assert_has ruby | ||
|
|
||
| capture chsrc set ruby '' | ||
| assert_has first | ||
| assert_has upstream | ||
| assert_lacks -dry | ||
|
|
||
| capture chsrc set ruby f | ||
| assert_has first | ||
| assert_lacks upstream | ||
|
|
||
| capture chsrc set ruby first '' | ||
| assert_lacks first | ||
| assert_lacks upstream | ||
|
|
||
| capture chsrc set ruby first u | ||
| assert_lacks upstream | ||
|
|
||
| capture chsrc set ruby -scope= | ||
| assert_lacks default | ||
|
|
||
| print 'Zsh completion state-machine tests passed.' | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI的时候用的是另一个目录覆盖了这个变量,可以注释一下二者的区别