1
|
|
|
"""Compiler help reduce the complexity of the YAML files.""" |
2
|
|
|
|
3
|
|
|
from ..logger import error |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
def compile_keys(options): |
7
|
|
|
""" |
8
|
|
|
Completion can contain lots of duplicate information. |
9
|
|
|
|
10
|
|
|
For example the trigger is most of the time also the result. |
11
|
|
|
Only in case of a value attribute is that returned. |
12
|
|
|
It also takes hints into consideration for compilation. |
13
|
|
|
""" |
14
|
|
|
compiled_keys = [] |
15
|
|
|
for option in options: |
16
|
|
|
key = option['title'] |
17
|
|
|
display = option['title'] + "\t" + option['hint'] |
18
|
|
|
|
19
|
|
|
if 'value' in option: |
20
|
|
|
result = option['value'] |
21
|
|
|
else: |
22
|
|
|
result = option['title'] |
23
|
|
|
if 'unique' in option: |
24
|
|
|
unique = option['unique'] |
25
|
|
|
else: |
26
|
|
|
unique = False |
27
|
|
|
|
28
|
|
|
compiled_key = (key, display, result, unique) |
29
|
|
|
compiled_keys.append(compiled_key) |
30
|
|
|
|
31
|
|
|
return compiled_keys |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
def compile_values(options): |
35
|
|
|
""" |
36
|
|
|
Bake completions from the provided YAML options. |
37
|
|
|
|
38
|
|
|
Considers stuff like hints and values. |
39
|
|
|
""" |
40
|
|
|
key_to_values = dict() |
41
|
|
|
for option in options: |
42
|
|
|
option_key = option['title'] |
43
|
|
|
compiled_values = list() |
44
|
|
|
if 'values' in option: |
45
|
|
|
option_values = option['values'] |
46
|
|
|
|
47
|
|
|
for option_value in option_values: |
48
|
|
|
compiled_value = _compile_value(option_value) |
49
|
|
|
compiled_values.append(compiled_value) |
50
|
|
|
|
51
|
|
|
key_to_values[option_key] = compiled_values |
52
|
|
|
|
53
|
|
|
return key_to_values |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def _compile_value(value_elements): |
57
|
|
|
""" |
58
|
|
|
Transpile a list of elements into a sublime text completion scheme. |
59
|
|
|
|
60
|
|
|
A completion scheme is a tuple constructed as: |
61
|
|
|
|
62
|
|
|
(key\thint, value) |
63
|
|
|
|
64
|
|
|
key is display on the left side of the completion. |
65
|
|
|
hint is displayed on the right side of the completion and optional. |
66
|
|
|
It has to be seperated from the key using the escaped tabulator \t. |
67
|
|
|
value is the actual rendered content if the key trigger is activated. |
68
|
|
|
.""" |
69
|
|
|
length = len(value_elements) |
70
|
|
|
|
71
|
|
|
# case 1 is if only the key is provided, is generally the default case. |
72
|
|
|
# Meaning is generally explained in the key |
73
|
|
|
if length == 1: |
74
|
|
|
return value_elements[0] + "\tDefault", value_elements[0] |
75
|
|
|
|
76
|
|
|
# case 2 is if only the key and the special hint is given |
77
|
|
|
# means that the key is the value too |
78
|
|
|
elif length == 2: |
79
|
|
|
open_value_key, option_value_hint = value_elements |
80
|
|
|
return open_value_key + "\t" + option_value_hint, open_value_key |
81
|
|
|
|
82
|
|
|
# case 3 is when every option is implemneted |
83
|
|
|
elif length == 3: |
84
|
|
|
open_value_key, option_value_hint, option_value_value = value_elements |
85
|
|
|
return open_value_key + "\t" + option_value_hint, option_value_value |
86
|
|
|
|
87
|
|
|
else: |
88
|
|
|
error("unexpected length of '" + str(length) + |
89
|
|
|
"' with value elements '[" + ", ".join(value_elements) + "]'") |
90
|
|
|
|