File size: 4,951 Bytes
3a14a3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import json
import os
import colorsys

# Configuration
PREFIXES_FILE = 'prefixes.md'
PACKAGE_JSON_FILE = 'package.json'
GRAMMAR_FILE = 'syntaxes/mql5.tmLanguage.json'

def generate_colors(n):
    """Generates n unique colors using HSL."""
    colors = []
    for i in range(n):
        hue = i / n
        # Use high saturation and value/lightness for visibility on both dark/light
        # Slightly varying saturation/lightness to add more distinction
        saturation = 0.6 + (i % 2) * 0.2  # 0.6 or 0.8
        lightness = 0.4 + (i % 3) * 0.1   # 0.4, 0.5, 0.6
        
        rgb = colorsys.hls_to_rgb(hue, lightness, saturation)
        hex_color = '#{:02x}{:02x}{:02x}'.format(
            int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255)
        )
        colors.append(hex_color)
    return colors

def main():
    print(f"Reading {PREFIXES_FILE}...")
    with open(PREFIXES_FILE, 'r') as f:
        # Filter lines that look like prefixes (non-empty)
        prefixes = [line.strip() for line in f if line.strip()]

    print(f"Found {len(prefixes)} prefixes.")
    if len(prefixes) != 557:
        print(f"Warning: Expected 557 prefixes, found {len(prefixes)}. Proceeding anyway.")

    # Sort prefixes by length descending to fix shadowing
    sorted_prefixes = sorted(prefixes, key=len, reverse=True)
    
    unique_colors = generate_colors(len(prefixes))
    
    # 1. Update Grammar File (mql5.tmLanguage.json)
    print(f"Updating {GRAMMAR_FILE}...")
    with open(GRAMMAR_FILE, 'r') as f:
        grammar = json.load(f)

    # Base patterns (comments, strings, numbers)
    # We will rebuild 'patterns'
    new_patterns = [
        {
            "begin": "//",
            "end": "$",
            "name": "comment.line.double-slash.mql5"
        },
        {
            "begin": "/\\*",
            "end": "\\*/",
            "name": "comment.block.mql5"
        },
        {
            "begin": "\"",
            "end": "\"",
            "name": "string.quoted.double.mql5",
            "patterns": [
                {
                    "match": "\\\\.",
                    "name": "constant.character.escape.mql5"
                }
            ]
        },
        {
            "match": "\\b\\d+(\\.\\d+)?\\b",
            "name": "constant.numeric.mql5"
        }
    ]

    # Add prefix patterns
    for prefix in sorted_prefixes:
        # Scope name matches the prefix
        scope_name = f"support.function.prefix.{prefix}.mql5"
        pattern = {
            "match": f"\\b{prefix}[A-Za-z0-9_]*\\b",
            "name": scope_name
        }
        new_patterns.append(pattern)

    grammar['patterns'] = new_patterns
    
    with open(GRAMMAR_FILE, 'w') as f:
        json.dump(grammar, f, indent=4)


    # 2. Update Package.json
    print(f"Updating {PACKAGE_JSON_FILE}...")
    with open(PACKAGE_JSON_FILE, 'r') as f:
        package_json = json.load(f)

    # Clean existing customization
    text_mate_rules = [
        {
            "name": "Comments",
            "scope": [
                "comment.line.double-slash.mql5",
                "comment.block.mql5"
            ],
            "settings": {
                "foreground": "#6A9955"
            }
        },
        {
            "name": "Strings",
            "scope": "string.quoted.double.mql5",
            "settings": {
                "foreground": "#CE9178"
            }
        },
        {
            "name": "Numbers",
            "scope": "constant.numeric.mql5",
            "settings": {
                "foreground": "#B5CEA8"
            }
        }
    ]

    # Add rules for each prefix with unique color
    # NOTE: map original prefixes (unsorted list for stable color assignment if desired, or just iterate sorted)
    # We'll stick to sorted_prefixes to align with grammar, but it doesn't strictly matter for color assignment order
    # as long as scope names match.
    for i, prefix in enumerate(sorted_prefixes):
        scope_name = f"support.function.prefix.{prefix}.mql5"
        rule = {
            "name": f"Prefix {prefix}",
            "scope": scope_name,
            "settings": {
                "foreground": unique_colors[i]
            }
        }
        text_mate_rules.append(rule)

    # Ensure structure exists
    if 'contributes' not in package_json:
        package_json['contributes'] = {}
    if 'configurationDefaults' not in package_json['contributes']:
        package_json['contributes']['configurationDefaults'] = {}
    package_json['contributes']['configurationDefaults']['editor.tokenColorCustomizations'] = {
        "textMateRules": text_mate_rules
    }

    with open(PACKAGE_JSON_FILE, 'w') as f:
        json.dump(package_json, f, indent=4)

    print("Success! Files updated.")

if __name__ == "__main__":
    main()