-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmvt-do-completions.py
231 lines (179 loc) · 7.67 KB
/
mvt-do-completions.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import sublime, sublime_plugin
import json
import re
import os
from os.path import dirname, realpath
# Define Path to JSON Cache
__FUNCTIONS_MERCHANT_PATH__ = dirname(realpath(__file__)) + os.sep + '/functions-merchant.json'
class MvtDoCompletions(sublime_plugin.EventListener):
"""
MvtDO File / Function Attribute Completions
| Smartly determine which "Functions" to autocomplete based on chosen "File"
| <mvt:do file="g.Module_Library_DB" value="Product_Load_ID(), Category_Load_ID() ..." />
"""
def __init__(self):
self.functions_merchant_data = self.read_data_file(__FUNCTIONS_MERCHANT_PATH__)
self.quick_panel_data = {}
def on_query_completions(self, view, prefix, locations):
# Only trigger in an <mvt:do> Tag
if not view.match_selector(locations[0], 'text.mvt text.html.basic meta.tag.inline.do.mvt'):
return []
# determine what <mvt:do> attribute you're in
if (view.match_selector(locations[0], 'text.mvt text.html.basic meta.tag.inline.do.mvt attribute-value.file.mvt')):
mvtdo_attribute = 'file'
elif (view.match_selector(locations[0], 'text.mvt text.html.basic meta.tag.inline.do.mvt attribute-value.value.mvt')):
prev_pt = max(0, locations[0] - 1)
is_variable = view.match_selector(prev_pt, 'variable.language')
if (is_variable):
return []
mvtdo_attribute = 'value'
else:
return []
return self.get_completions(view, prefix, locations, mvtdo_attribute)
def on_post_text_command(self, view, command_name, *args):
if (command_name == 'commit_completion' or command_name == 'insert_best_completion'):
for r in view.sel():
in_value_attribute = view.match_selector(r.begin(), 'text.mvt text.html.basic meta.tag.inline.do.mvt attribute-value.value.mvt')
if (in_value_attribute):
prev_pt = max(0, r.begin() - 1)
is_variable = view.match_selector(prev_pt, 'variable.language')
if (is_variable is False):
file_attribute_val = self.get_current_file_attribute_val(view, r.begin(), '')
if (file_attribute_val == ''):
value_attribute_val = self.get_current_value_attribute_val(view, r.begin(), '')
function_name = self.get_function_name(view, value_attribute_val)
if function_name is not False:
file_name = self.get_file_name(view, function_name)
if file_name is not False:
if type(file_name) is list:
self.quick_panel_data = { "view": view, "pt": r.begin(), "file_name": file_name }
view.window().show_quick_panel(file_name, self.choose_file_name, sublime.MONOSPACE_FONT)
elif type(file_name) is str:
self.insert_file_name(view, r.begin(), file_name)
def get_completions(self, view, prefix, locations, mvtdo_attribute):
completion_list = []
if (mvtdo_attribute == 'file'):
completion_list = self.get_file_completions(view, locations[0], prefix)
elif (mvtdo_attribute == 'value'):
file_attribute_val = self.get_current_file_attribute_val(view, locations[0], prefix)
completion_list = self.get_value_completions(view, locations[0], prefix, file_attribute_val)
return (completion_list, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
"""
Custom Methods
"""
def read_data_file(self, path):
with open( path ) as data_file:
data = json.load(data_file)
return data
def get_file_completions(self, view, pt, prefix):
file_completions = [ ( file['distro_path'] + '\tFile', file['distro_path'].replace('$', '\\$') ) for file in self.functions_merchant_data ]
return set(file_completions)
def get_value_completions(self, view, pt, prefix, file_attribute_val):
value_completions = []
for file in self.functions_merchant_data:
if (file_attribute_val == file['distro_path'] or file_attribute_val == ''):
for function in file['functions']:
parameters = self.build_function_parameters(function['parameters'])
value_completions.append( (function['name'] + '\tFunc', function['name'] + parameters) )
return set(value_completions)
def build_function_parameters(self, parameters):
if (len(parameters) == 0):
return '()'
parameters_map = []
count = 0
for parameter in parameters:
count += 1
if (count == len(parameters)):
count = 0
parameters_map.append( '${' + str(count) + ':' + parameter + '}' )
sep = ', '
return '( ' + sep.join(parameters_map) + ' )'
def get_current_file_attribute_val(self, view, pt, prefix):
mvtdo_tag_region = self.get_mvtdo_tag_region(view, pt, prefix)
if (mvtdo_tag_region is False):
return ''
file_attribute_all_locations = view.find_by_selector( 'text.mvt text.html.basic meta.tag.inline.do.mvt attribute-value.file.mvt' )
file_attribute_val = ''
for file_attribute_location in file_attribute_all_locations:
if (mvtdo_tag_region.contains(file_attribute_location)):
file_attribute_val = view.substr(file_attribute_location)
file_attribute_val = file_attribute_val.replace('"', '')
return file_attribute_val
def get_current_value_attribute_val(self, view, pt, prefix):
mvtdo_tag_region = self.get_mvtdo_tag_region(view, pt, prefix)
if (mvtdo_tag_region is False):
return ''
value_attribute_all_locations = view.find_by_selector( 'text.mvt text.html.basic meta.tag.inline.do.mvt attribute-value.value.mvt' )
value_attribute_val = ''
for attribute_value_location in value_attribute_all_locations:
if (mvtdo_tag_region.contains(attribute_value_location)):
value_attribute_val = view.substr(attribute_value_location)
value_attribute_val = value_attribute_val.replace('"', '')
return value_attribute_val
def get_mvtdo_tag_region(self, view, pt, prefix):
# limit the search left/right to 500 characters
_LIMIT = 500
# left side of the string
left_start = pt
left_end = max(0, left_start - _LIMIT)
left_angle_pos = False
i = left_start
while i >= left_end:
c = view.substr(i)
if (c == '<'):
left_angle_pos = i
break
i -= 1
# right side of the string
right_start = pt + len(prefix)
right_end = right_start + _LIMIT
right_angle_pos = False
i = right_start
while i <= right_end:
c = view.substr(i)
if (c == '>'):
right_angle_pos = i
break
i += 1
if (left_angle_pos is False or right_angle_pos is False):
return False
return sublime.Region(left_angle_pos, right_angle_pos)
def get_function_name(self, view, value_attribute_val):
match = re.match(r'([a-z0-9_]+)\s*?\(', value_attribute_val, re.I)
if match:
return match.group(1)
else:
return False
def get_file_name(self, view, function_name):
files = []
for file in self.functions_merchant_data:
for function in file['functions']:
if function_name == function['name']:
files.append(file['distro_path'])
files = set(files)
if (len(files) == 0):
return False
elif (len(files) == 1):
return next(iter(files))
else:
return list(files)
def choose_file_name(self, index):
self.insert_file_name(self.quick_panel_data['view'], self.quick_panel_data['pt'], self.quick_panel_data['file_name'][index])
self.quick_panel_data = {}
def insert_file_name(self, view, pt, file_name):
mvtdo_tag_region = self.get_mvtdo_tag_region(view, pt, '')
if (mvtdo_tag_region is False):
return ''
file_attribute_all_locations = view.find_by_selector( 'text.mvt text.html.basic meta.tag.inline.do.mvt attribute-value.file.mvt' )
for file_attribute_location in file_attribute_all_locations:
if (mvtdo_tag_region.contains(file_attribute_location)):
file_attribute_pt = file_attribute_location.begin() + 1
view.run_command('insert_file_name', {
"args": {
"file_attribute_pt": file_attribute_pt,
"file_name": file_name
}
})
class InsertFileNameCommand(sublime_plugin.TextCommand):
def run(self, edit, args):
self.view.insert(edit, args['file_attribute_pt'], args['file_name'])