蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 JSONC 颜色主题配置解析、用户/工作区作用域覆盖与 @category:"themes" 过滤离线校验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-themes.py
explore-themes.py84 行
"""Offline VS Code JSONC color theme config & @category:themes filter verifier for Ch20."""
import json
import re

VERSION = "CPython 3.12.14 offline VS Code color theme & filter verifier"


def check(name, summary, expected, actual):
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def parse_jsonc_snippet(snippet: str) -> dict:
    no_comments = re.sub(r"//.*", "", snippet)
    return json.loads("{" + no_comments.strip().rstrip(",") + "}")


def main():
    cases = []

    # Case 1: JSONC workbench.colorTheme parsing & User vs Workspace scope override
    snippet = '// 定义工作界面使用的颜色主题\n"workbench.colorTheme": "Solarized Dark"\n'
    user_theme = parse_jsonc_snippet(snippet)
    workspace_theme = {"workbench.colorTheme": "One Dark Pro"}
    effective_global = user_theme["workbench.colorTheme"]
    effective_ws = {**user_theme, **workspace_theme}["workbench.colorTheme"]
    cases.append(
        check(
            "color_theme_settings_and_scope",
            "Verify JSONC workbench.colorTheme parsing and User vs Workspace theme scoping",
            {
                "user_theme": "Solarized Dark",
                "workspace_override_theme": "One Dark Pro",
                "picker_shortcut_win": "Ctrl + K Ctrl + T",
                "picker_shortcut_mac": "⌘K ⌘T",
            },
            {
                "user_theme": effective_global,
                "workspace_override_theme": effective_ws,
                "picker_shortcut_win": "Ctrl + K Ctrl + T",
                "picker_shortcut_mac": "⌘K ⌘T",
            },
        )
    )

    # Case 2: Marketplace @category:"themes" filter & "One Dark Pro" lookup
    marketplace = [
        {"id": "zhuangbiaowei.one-dark-pro", "name": "One Dark Pro", "category": "themes", "uiTheme": "vs-dark"},
        {"id": "dracula-theme.theme-dracula", "name": "Dracula Official", "category": "themes", "uiTheme": "vs-dark"},
        {"id": "ms-python.python", "name": "Python", "category": "programming languages", "uiTheme": None},
    ]
    query = '@category:"themes"'
    m = re.search(r'@category:"([^"]+)"', query)
    cat = m.group(1) if m else ""
    themes_only = [item["name"] for item in marketplace if item["category"] == cat]
    one_dark = [item for item in marketplace if "one dark pro" in item["name"].lower()]
    cases.append(
        check(
            "theme_marketplace_category_filter",
            "Verify @category:\"themes\" marketplace filter excludes non-theme extensions and finds One Dark Pro",
            {
                "parsed_category": "themes",
                "filtered_themes": ["One Dark Pro", "Dracula Official"],
                "one_dark_ui_theme": "vs-dark",
            },
            {
                "parsed_category": cat,
                "filtered_themes": themes_only,
                "one_dark_ui_theme": one_dark[0]["uiTheme"],
            },
        )
    )

    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-themes-lab/explore-themes.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-themes.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:VS Code

课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2203.zip

下载课程关联工程包:VS Code

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2223.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习