"""Offline VS Code Command Palette mode router & fuzzy matcher for Ch12."""
import json
VERSION = "CPython 3.12.14 offline VS Code Command Palette verifier"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def route_palette_input(raw_query: str) -> dict:
if raw_query.startswith(">"):
return {"mode": "commands", "term": raw_query[1:].strip()}
return {"mode": "quick_open_files", "term": raw_query.strip()}
def fuzzy_match(term: str, candidate: str) -> bool:
it = iter(candidate.lower())
return all(ch in it for ch in term.lower() if not ch.isspace())
def main():
cases = []
# Case 1: '>' command mode vs Quick Open file mode routing
r_cmd = route_palette_input(">move terminal")
r_file = route_palette_input("index.html")
cases.append(
check(
"command_palette_prefix_modes",
"Verify Command Palette '>' prefix routes to command mode and removing '>' routes to file search",
{
"cmd_mode": "commands",
"cmd_term": "move terminal",
"file_mode": "quick_open_files",
"file_term": "index.html",
},
{
"cmd_mode": r_cmd["mode"],
"cmd_term": r_cmd["term"],
"file_mode": r_file["mode"],
"file_term": r_file["term"],
},
)
)
# Case 2: Fuzzy filtering over built-in/extension commands (with shortcut metadata) and workspace files
commands = [
{"title": "Terminal: Move Terminal into New Window", "shortcut": ""},
{"title": "File: Save", "shortcut": "Ctrl+S"},
{"title": "Preferences: Color Theme", "shortcut": "Ctrl+K Ctrl+T"},
{"title": "Python: Select Interpreter", "shortcut": ""},
]
files = ["src/index.html", "src/app.js", "package.json", "README.md"]
cmd_hits = [c["title"] for c in commands if fuzzy_match(r_cmd["term"], c["title"])]
file_hits = [f for f in files if fuzzy_match("idxhl", f)]
with_shortcuts = [c["title"] for c in commands if c["shortcut"]]
cases.append(
check(
"fuzzy_command_and_file_ranking",
"Verify fuzzy matching for 'move terminal' command and 'idxhl' workspace file plus shortcut hints",
{
"cmd_hits": ["Terminal: Move Terminal into New Window"],
"file_hits": ["src/index.html"],
"commands_with_shortcut_count": 2,
},
{
"cmd_hits": cmd_hits,
"file_hits": file_hits,
"commands_with_shortcut_count": len(with_shortcuts),
},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/vscode-command-palette-lab/explore-command-palette.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-command-palette.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2203.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2205.zip