-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_snippet.py
43 lines (33 loc) · 1.17 KB
/
build_snippet.py
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
from transform_snippet import transform_snippet
def build_snippet_definition(d):
block = []
for k, v in d["snippet"]["snippet_definition"].items():
if isinstance(v, str):
block.append(f"{k}={v}")
elif k == "options":
if "r" in v:
block.append("regTrig=true")
if "A" in v:
block.append('snippetType="autosnippet"')
if "priority" in d.keys():
block.append(f'priority={d["priority"]}')
return "\n\t{" + ", ".join(block) + "}"
def build_snippet_content_block(d):
nodes = d["snippet"]["snippet_content"]
return "\t{\n\t\t" + ",\n\t\t".join([repr(node) for node in nodes]) + "\n\t}"
def build_conditions_block(d):
block = []
if "context" in d.keys():
block.append(d["context"][1:-3])
if block:
return "\n\t{ condition = " + ", ".join(block) + " }"
else:
return block
def build_snippet(text):
d = transform_snippet(text)
blocks = [
build_snippet_definition(d),
build_snippet_content_block(d),
build_conditions_block(d),
]
return "s(" + ",\n".join([block for block in blocks if len(block) > 0]) + "\n),"