forked from snowkit/sublime_flow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaxe_parse_completion_list.py
147 lines (104 loc) · 3.19 KB
/
haxe_parse_completion_list.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
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
import sublime, sublime_plugin
import xml.etree.ElementTree as ET
def haxe_has_error(_input):
if not _input:
return False
if _input[0] != '<':
_res = []
for _line in _input.splitlines():
_res.append(sanitize(_line.strip()))
return _res
return False
def haxe_has_args(_list):
root = ET.fromstring(str(_list))
if root.tag == 'type':
return parse_type(root.text.strip())
return None
def haxe_completion_list(_list):
if _list is None:
return None
root = ET.fromstring(str(_list))
#list is completion of properties/methods on an object/Type
if root.tag == 'list':
members = []
for node in root:
_name = node.attrib['n']
_type = node.find('t').text
if _type is None:
if _name[0].islower():
_type = "package"
else:
_type = _name
if is_function(_type):
_a, _ret = parse_args(_type)
if(_ret.find('Unknown') != -1):
_ret = ' '
members.append( ( 'm· ' + _name+'\t'+_ret, _name ) )
else:
members.append( ( 'p· ' + _name+'\t'+_type, _name ) )
if len(members):
return members
else:
return [('No members/properties', '')]
return None
#returns args, return_type from a <type> string
def parse_args(_type):
_tmp = _type
_args = []
_result = 0
_count = 0
while _result != None:
_result = _tmp.find(' -> ')
_arg = _tmp[:_result]
#found a () which means it's a function type
_end = ')'
_par = _arg.find('(')
if(_par == -1):
_par = _arg.find('<')
_end = '> ->'
if _par != -1:
_endpar = _tmp.find(_end)
_arg = _tmp[:_endpar+1]
_tmp = _tmp[_endpar+1:]
else :
_tmp = _tmp[_result+4:]
if _arg:
_args.append(_arg)
_result = _tmp.find(' -> ')
_count += 1
if _count > 10 or _result == -1:
_result = None
return _args, _tmp
#returns a single tuple parsed for legibility as function args, clamped if too long etc
def parse_type(_type):
if _type is None:
return None
if _type == "":
return []
_args = []
if _type.find(':') == -1 and _type.find('->') == -1:
_args = [ _type.strip() ]
else:
_args, _return = parse_args(_type)
if len(_args) == 1:
if _args[0] in ['Void', 'Dynamic']:
return []
_list = []
for item in _args:
node = item.split(':')
_name = node[0]
_typename = "Unknown"
if len(node) > 1:
_typename = node[1]
_list.append(sanitize(_name.strip()) + ':' + sanitize(_typename.strip()))
return ', '.join(_list)
def sanitize(_str):
_str = _str.replace('>','>')
_str = _str.replace('<','<')
return _str
#returns True if the string is completion info for a function
def is_function(_str):
if _str:
return _str.find(' -> ') != -1
else:
return False