-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_snippet.py
95 lines (69 loc) · 2.16 KB
/
transform_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
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
from lark import Transformer
from parser import ultisnips_parser
from luasnip_nodes import TextNode, InsertNode, ChoiceNode, SnippetNode
class LuaSnipTransformer(Transformer):
def snippet(self, items):
# return "s(" + "\n".join(items) + "\n),"
return ("snippet", dict(items))
def snippet_definition(self, items):
# return "\n\t{" + ", ".join(items) + "},"
return ("snippet_definition", dict(items))
def trigger(self, s):
(s,) = s
if s[0] != "'" or s[0] != '"':
s = f'"{s}"'
return ("trig", s)
def description(self, s):
(s,) = s
return ("dscr", s)
def options(self, s):
(s,) = s
return ("options", list(s))
def tabstop(self, s):
if len(s) == 1:
return InsertNode(s[0])
elif len(s) == 2:
if isinstance(s[1], ChoiceNode):
s[1].index = s[0]
return s[1]
else:
return InsertNode(s[0], s[1])
else:
return SnippetNode(s[0], s[1:])
def conditional(self, s):
nodes = []
for string in s:
nodes.append(TextNode(string))
return ChoiceNode(nodes)
def TEXT(self, text):
return TextNode(text)
def TABSTOP_TEXT(self, s):
# return f't("{s}")'
return f"{s}"
def CHOICE(self, s):
return s
def INT(self, d):
return int(d)
def context(self, s):
(s,) = s
return ("context", s)
def priority(self, d):
return ("priority", int(d[0]))
def snippet_content(self, s):
# return "\t{\n\t\t" + ",\n\t\t".join([str(node) for node in s]) + "\n\t}"
return ("snippet_content", s)
def WORD(self, s):
return s
def ESCAPED_STRING(self, s):
return str(s)
def ultisnips(self, s):
return dict(s)
def transform_snippet(text):
tree = ultisnips_parser(text)
# print(tree)
# print("")
# print(tree.pretty())
# TODO: seen nodes should be cleared betweeen snippet runs
# this must be done in a better way
InsertNode.seen_nodes = []
return LuaSnipTransformer().transform(tree)