"""Offline VS Code cross-platform shortcut normalizer & keybindings.json conflict detector for Ch19."""
import json
VERSION = "CPython 3.12.14 offline VS Code keybindings & conflict verifier"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def win_to_mac(combo: str) -> str:
return (
combo.replace("Ctrl", "Cmd")
.replace("Alt", "Option")
)
def main():
cases = []
# Case 1: Cross-platform modifier mapping across 12 shortcut categories
category_counts = {
"通用操作": 10,
"文件与编辑器": 12,
"代码编辑": 26,
"多光标操作": 8,
"调试": 6,
"搜索和导航": 7,
"版本控制": 3,
"终端操作": 4,
"命令面板": 2,
"显示": 8,
"扩展操作": 2,
"其他": 2,
}
samples = {
"palette": ("Ctrl + Shift + P", "Cmd + Shift + P"),
"format": ("Shift + Alt + F", "Shift + Option + F"),
"keybindings": ("Ctrl + K, Ctrl + S", "Cmd + K, Cmd + S"),
}
converted = {k: win_to_mac(v[0]) == v[1] for k, v in samples.items()}
cases.append(
check(
"cross_platform_keybinding_normalizer",
"Verify 12 VS Code shortcut categories (90 rows) and Windows/Linux-to-macOS Ctrl/Alt mapping",
{
"category_count": 12,
"total_rows": 90,
"all_sample_mappings_match": True,
},
{
"category_count": len(category_counts),
"total_rows": sum(category_counts.values()),
"all_sample_mappings_match": all(converted.values()),
},
)
)
# Case 2: keybindings.json context-aware conflict detector
keybindings = [
{"key": "ctrl+enter", "command": "editor.action.insertLineAfter", "when": "editorTextFocus"},
{"key": "ctrl+enter", "command": "scm.acceptInput", "when": "scmRepository"},
{"key": "ctrl+shift+p", "command": "workbench.action.showCommands", "when": ""},
{"key": "ctrl+shift+p", "command": "custom.duplicateCommand", "when": ""},
]
seen = {}
conflicts = []
for entry in keybindings:
sig = (entry["key"].lower(), entry.get("when", ""))
if sig in seen:
conflicts.append(entry["key"])
else:
seen[sig] = entry["command"]
cases.append(
check(
"keybindings_json_conflict_detector",
"Verify keybindings.json distinguishes same-key different-when bindings from true conflicts",
{
"total_entries": 4,
"context_isolated_ctrl_enter_ok": True,
"detected_conflicts": ["ctrl+shift+p"],
},
{
"total_entries": len(keybindings),
"context_isolated_ctrl_enter_ok": "ctrl+enter" not in conflicts,
"detected_conflicts": conflicts,
},
)
)
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-shortcut-keys-lab/explore-shortcut-keys.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-shortcut-keys.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2203.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2218.zip