-
Notifications
You must be signed in to change notification settings - Fork 1
/
flow.py
427 lines (310 loc) · 13.8 KB
/
flow.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# -*- coding: utf-8 -*-
import sys, os, subprocess, shutil, shlex, json, codecs, time
import sublime, sublime_plugin
from .haxe_parse_completion_list import *
import mdpopups
#plugin location
plugin_file = __file__
plugin_filepath = os.path.realpath(plugin_file)
plugin_path = os.path.dirname(plugin_filepath)
try:
STARTUP_INFO = subprocess.STARTUPINFO()
STARTUP_INFO.dwFlags |= subprocess.STARTF_USESHOWWINDOW
STARTUP_INFO.wShowWindow = subprocess.SW_HIDE
except (AttributeError):
STARTUP_INFO = None
_flow_ = None
class FlowProject( sublime_plugin.EventListener ):
def __init__(self):
global _flow_
_flow_ = self
print(_flow_)
self.flow_path = "flow"
self.flow_file = ""
self.target = ""
self.flow_type = ""
self.info_json = None
self.hxml_data = None
self.completion_data = None
self.completion_pending = False
self.system = self.get_system()
self.target = self.system
self.build_debug = False
self.build_verbose = False
self.build_type = 'run'
print("[flow] __init__")
def set_flow_file( self, file_name ):
if not file_name:
print("[flow] set flow file to ?!?" + str(file_name))
print("[flow] nothing will happen")
return
file_name = str(file_name)
print("[flow] set flow file to " + file_name)
sublime.status_message('set flow file to ' + file_name)
self.flow_file = file_name
self.refresh_info()
def set_flow_target_by_index( self, index ):
_targets = self.get_targets()
_target = _targets[index]
self.target = _target[0].lower()
print("[flow] set build target to " + self.target)
self.refresh_info()
def refresh_info(self):
print("[flow] refresh info/hxml on " + self.flow_file)
_fn, _ext = os.path.splitext(self.flow_file)
if "flow" in _ext:
cmd = [
"haxelib", "run", "flow",
"info", self.target,
"--project", self.flow_file
];
if self.build_debug:
cmd.append('--debug')
self.flow_type = "flow"
self.info_json_src = run_process(cmd).decode("utf-8");
# print("[flow] json source: >>\n{}\n<<".format(self.info_json_src));
if self.info_json_src:
self.info_json = json.loads(self.info_json_src)
if not self.info_json:
print("[flow] refresh info/hxml failed! info_json was null")
else:
self.hxml_data = self.info_json['hxml']
# print("[flow] hxml source: " + self.hxml_data);
self.hxml_data = self.hxml_data.replace('\\"','"');
self.hxml_data = self.hxml_data.replace('\\n','\n');
self.hxml_data = self.hxml_data.replace('\\t','\t');
# print("[flow] hxml cleaned: " + self.hxml_data);
else:
print("[flow] refresh info/hxml failed! info_json_src was not returned from haxelib run flow, is your flow up to date?")
elif "hxml" in _ext:
self.flow_type = "hxml"
print ("[flow] fetching hxml contents from " + self.flow_file)
with open(self.flow_file, 'r') as hxml_file:
self.hxml_data = hxml_file.read()
def on_query_completions(self, view, prefix, locations):
pt = view.sel()[0].b
scope = str(view.scope_name(pt))
if "source.haxe" not in scope:
return
_res = self.completion(view, view.file_name())
#explicitly no completion
if _res is None:
# print('[flow] completion res was none')
return ([], sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
else:
if(len(_res)):
return (_res, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
# else:
# print('[flow] completion _res `{}`'.format(_res))
def completion(self, view, fname):
if self.flow_file == "" or self.flow_file is None:
sublime.status_message("No flow file, right click in a flow file! {}".format(str(self.flow_file)))
# print('[flow] completion return [], no flow file')
return []
if not self.hxml_data:
sublime.status_message("no info/hxml for flow file, caching...")
self.refresh_info()
#ignore strings, comments, #if conditionals, for some reason the triggers won't work
ifsel = view.sel()[0]
ifsel.a -= 2; ifsel.b -= 2
scsel = view.sel()[0]
ifdef_score = view.score_selector(ifsel.begin(), "source - (keyword.control.directive.conditional.haxe)")
scope_score = view.score_selector(scsel.begin(), "source - (comment, string.quoted, keyword.control.directive.conditional.haxe)")
# print('[flow] ifdef score `{}`'.format(str(ifdef_score)))
# print('[flow] scope score `{}`'.format(str(scope_score)))
if scope_score <= 0 or ifdef_score <= 0:
# print('[flow] ignore invalid scope for completion')
return None
sel = view.sel()[0]; sel.a -= 1
ch = view.substr(sel)
# print('[flow] completion ch `{}`'.format(ch))
if ch != "." and ch != "(":
# print('[flow] ignore completion by non . or (')
return []
code = view.substr(sublime.Region(0, view.size()))
prior = code[0:view.sel()[0].begin()].encode('utf-8')
offset = len(prior)
cwd = self.get_working_dir()
filename = fname
from sublime_haxe.haxe_completion import _completionist_
self.completion_file = fname
self.completion_view = view
self.completion_data = None
_hxml = self.hxml_data.splitlines()
self.completion_pending = True
self.completion_start_time = time.time()
self.save_file_for_completion(view, fname)
result = _completionist_.complete(cwd, filename, offset, _hxml)
self.restore_file_post_completion()
time_diff = time.time() - self.completion_start_time
print("[flow] completion took {}".format(time_diff))
# print("[flow]\n{}".format(result))
if not result:
return None
_err = haxe_has_error(result)
if _err:
return self.show_errors(view, _err)
_args = haxe_has_args(result)
if _args:
return self.show_args(view, _args)
return haxe_completion_list(result)
def show_errors(self, view, errs):
if not errs:
return None
_pre = '<div class="invalid"> Haxe Errors </div>'
_css = 'div { margin:0.3em; } .flow-error-line { margin-left:1em; margin-right:2em; }'
_res = ''
for _err in errs:
_res += '<div class="flow-error-line">'+_err+'</div>'
mdpopups.show_popup(view, _pre + _res, css=_css, max_width=1280)
def show_args(self, view, args):
if not args:
return []
# print('[flow] args ' + args)
_res = []
_list = args.split(', ')
for _arg in _list:
_parts = _arg.split(':')
_res.append('<span class="entity name">' + _parts[0] + '</span>:<span class="storage type">' + _parts[1] + '</span>')
args = '<div>'+', '.join(_res)+'</div>'
_css = 'p,div { margin:0.3em; }'
mdpopups.show_popup(view, args, css=_css, max_width=1280)
return None
def save_file_for_completion( self, view, fname ):
folder = os.path.dirname(fname)
filename = os.path.basename( fname )
temp_file = os.path.join( folder , "." + filename + ".tmp" )
if os.path.exists( fname ):
shutil.copy2( fname , temp_file )
code = view.substr(sublime.Region(0, view.size()))
f = codecs.open( fname , "wb" , "utf-8" , "ignore" )
f.write( code )
f.close()
print("[flow] saved file for completion")
def restore_file_post_completion( self ):
print("[flow] restore file post completion")
view = self.completion_view
fname = self.completion_file
folder = os.path.dirname( fname )
filename = os.path.basename( fname )
temp_file = os.path.join( folder , "." + filename + ".tmp" )
if os.path.exists( temp_file ) :
# print("do restore!")
shutil.copy2( temp_file , fname )
os.remove( temp_file )
# else:
# os.remove( fname )
def on_post_save_async(self, view):
pt = view.sel()[0].b
scope = str(view.scope_name(pt))
fname = view.file_name()
if ("source.flow" in scope) or ("source.hxml" in scope):
if fname == self.flow_file:
self.refresh_info()
def get_working_dir(self):
cwd = os.path.dirname(self.flow_file)
cwd = os.path.normpath( cwd )
return cwd
def get_status(self):
_result = []
if self.flow_file:
_result.append(['flow file', self.flow_file])
else:
_result.append(['no flow file', 'specify a flow file first'])
return _result
if self.flow_type is not "flow":
return _result
if self.target:
_result.append(['flow target', self.target])
else:
_result.append(['flow target', self.system])
_result.append(['Toggle debug build', "currently : " + str(self.build_debug).lower() ])
_result.append(['Toggle verbose build', "currently : " + str(self.build_verbose).lower() ])
_result.append(['Build type: run / build / compile / launch', "currently : flow " + str(self.build_type).lower() ])
# _result.append(['Package output folder', "generates a zip to output folder"])
# _result.append(['Clean output folder', "delete the output folder"])
# _result.append(['Clean build output', "delete the build folder"])
return _result
def get_system(self):
_result = ""
_system = sys.platform
if _system == "win32" or _system == "cygwin":
_result = "windows"
elif _system == "darwin":
_result = "mac"
else:
_system = "linux"
return _result
def get_targets(self):
_result = []
if not self.flow_file:
return _result
if "flow" not in self.flow_type:
return _result
_result.append(['Mac', 'desktop, native mac app'])
_result.append(['Linux', 'desktop, native linux app'])
_result.append(['Windows', 'desktop, native windows app'])
_result.append(['Android', 'mobile, native android app'])
_result.append(['iOS', 'mobile, native ios project'])
_result.append(['Web', 'web, web based app'])
if self.info_json:
_invalid = self.info_json['targets_invalid']
_result[:] = [_item for _item in _result if not _item[0].lower() in _invalid ]
_result.insert(0, ['unavailable from ' + self.system, ", ".join(_invalid) ])
else :
_result.append(['Error', 'Failed to retrieve flow info from project. View -> Show Console for more info to report!'])
return _result
def run_process( args ):
_proc = None
#this shell_cmd is not used by windows
shell_cmd = ""
for arg in args:
#make sure lines from the hxml file don't trip up the mac/linux shell
shell_cmd += shlex.quote(arg) + " "
if sys.platform == "win32":
_proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, startupinfo=STARTUP_INFO)
elif sys.platform == "darwin":
# Use a login shell on OSX, otherwise the users expected env vars won't be setup
_proc = subprocess.Popen(["/bin/bash", "-l", "-c", shell_cmd], stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, startupinfo=STARTUP_INFO, shell=False,
preexec_fn=os.setsid)
elif sys.platform == "linux":
# Explicitly use /bin/bash on Linux, to keep Linux and OSX as
# similar as possible. A login shell is explicitly not used for
# linux, as it's not required
_proc = subprocess.Popen(["/bin/bash", "-c", shell_cmd], stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, startupinfo=STARTUP_INFO, shell=False,
preexec_fn=os.setsid)
return _proc.communicate()[0]
def panel(_window, options, done, flags=0, sel_index=0, on_highlighted=None):
sublime.set_timeout(lambda: _window.show_quick_panel(options, done, flags, sel_index, on_highlighted), 10)
# http://stackoverflow.com/a/10863489/2503795
class ChainedActionsCommand(sublime_plugin.TextCommand):
def run(self, edit, actions, args):
for i, action in enumerate(actions):
self.view.run_command(action, args[i])
#force reload
def force_reload():
modules_to_load = [
'sublime_flow.commands.flow_set_project_file',
'sublime_flow.commands.flow_set_target_build',
'sublime_flow.commands.flow_show_status',
'sublime_flow.commands.flow_run_build',
'sublime_flow.commands.haxe_generate_import',
'sublime_flow.haxe_parse_completion_list'
]
import imp
for mod in modules_to_load:
if sys.modules.get(mod,None) != None:
try:
# print("reload " + mod)
imp.reload(sys.modules[mod])
except:
pass
#only use this when developing
force_reload()
from .commands.flow_show_status import FlowShowStatus
from .commands.flow_set_target_build import FlowSetTargetBuild
from .commands.flow_set_project_file import FlowSetProjectFile
from .commands.flow_run_build import FlowRunBuild
from .commands.haxe_generate_import import HaxeGenerateImport