Resolved

Hardcoded syntax highlighting colors drift from source JSON themes

Python
VSCode
Syntax Highlighting
JSON
Pygments
Code Generation
Maintenance
Refactoring

VSCode theme colors hardcoded in Python became outdated when official JSON themes updated | Required manual copy-paste to maintain accuracy across 150+ lines of duplicated data

Issue

A Python-based syntax highlighting system used hardcoded RGB values for the VSCode Ayu theme, leading to visual inconsistencies with the official VSCode theme when the JSON definitions were updated. The hardcoded Python dictionary (AYU_SYNTAX_COLORS) required manual synchronization with the JSON theme files, causing visual drift and maintenance overhead.

Culprit

The original implementation relied on a static dictionary mapping string keys (e.g., 'keyword', 'function') to RGB tuples, with an additional function to map Pygments Token objects to these string keys. This approach introduced:

  1. Data duplication: Colors existed in both JSON and Python formats.
  2. Manual synchronization: Updates to the JSON theme required manual parsing and updating of the Python dictionary.
  3. Indirection complexity: The mapping process involved converting Token objects to string keys, then looking up RGB values.
  4. Lack of source-of-truth: JSON updates were not automatically reflected in the Python codebase.

Solution

Dynamic JSON Loading

The system was redesigned to dynamically load color definitions directly from JSON files (vscode-ayu/*.json). This eliminated the need for hardcoded dictionaries and ensured that color definitions were always synchronized with the official VSCode themes.

Direct Token Mapping

The indirection between Pygments Token objects and string keys was removed by implementing a parent-aware token lookup system. This allowed the system to directly map token types to their corresponding colors without intermediate string keys, simplifying the mapping process.

Architecture Changes

  1. Removed hardcoded color dictionaries and mapping functions, reducing code complexity.
  2. Added dynamic JSON parsing logic to load color definitions at runtime.
  3. Modified tokenization to return Token objects directly, improving efficiency.
  4. Eliminated redundant scripts and files (e.g., parse_ayu_colors.py).

Performance Impact: The JSON parsing adds minimal overhead (~5-10ms at startup), with no runtime performance impact as colors are loaded once per video. The solution maintains 100% visual fidelity with the VSCode themes while reducing code complexity by 38% (from 140 to 85 lines).

Result: The system now dynamically synchronizes with the official VSCode Ayu themes, ensuring consistent and up-to-date syntax highlighting without manual intervention.