forked from leitwolf/QuickXDev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickx.py
More file actions
295 lines (267 loc) · 9.58 KB
/
quickx.py
File metadata and controls
295 lines (267 loc) · 9.58 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author: lonewolf
# Date: 2013-10-26 11:23:48
#
import sublime
import sublime_plugin
import functools
import os
import datetime
import json
import re
import subprocess
import sys
import time
try:
import helper
import rebuild
import definition
except ImportError:
from . import helper
from . import rebuild
from . import definition
TEMP_PATH=""
DEFINITION_LIST=[]
USER_DEFINITION_LIST=[]
luaTemplate="""--
-- Author: ${author}
-- Date: ${date}
--
"""
# init plugin,load definitions
def init():
global TEMP_PATH
TEMP_PATH=sublime.packages_path()+"/User/QuickXDev.cache"
global DEFINITION_LIST
DEFINITION_LIST=json.loads(definition.data)
global USER_DEFINITION_LIST
path=os.path.join(TEMP_PATH,"user_definition.json")
if os.path.exists(path):
USER_DEFINITION_LIST=json.loads(helper.readFile(path))
def checkRoot():
# quick_cocos2dx_root
settings = helper.loadSettings("QuickXDev")
quick_cocos2dx_root = settings.get("quick_cocos2dx_root", "")
if len(quick_cocos2dx_root)==0:
sublime.error_message("quick_cocos2dx_root no set")
return False
return quick_cocos2dx_root
class LuaNewFileCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
self.window.run_command("hide_panel")
title = "untitle"
on_done = functools.partial(self.on_done, dirs[0])
v = self.window.show_input_panel(
"File Name:", title + ".lua", on_done, None, None)
v.sel().clear()
v.sel().add(sublime.Region(0, len(title)))
def on_done(self, path, name):
filePath = os.path.join(path, name)
if os.path.exists(filePath):
sublime.error_message("Unable to create file, file exists.")
else:
code = luaTemplate
# add attribute
settings = helper.loadSettings("QuickXDev")
format = settings.get("date_format", "%Y-%m-%d %H:%M:%S")
date = datetime.datetime.now().strftime(format)
code = code.replace("${date}", date)
author=settings.get("author", "Your Name")
code = code.replace("${author}", author)
# save
helper.writeFile(filePath, code)
v=sublime.active_window().open_file(filePath)
# cursor
v.run_command("insert_snippet",{"contents":code})
sublime.status_message("Lua file create success!")
def is_enabled(self, dirs):
return len(dirs) == 1
class QuickxRunWithPlayerCommand(sublime_plugin.WindowCommand):
def __init__(self,window):
super(QuickxRunWithPlayerCommand,self).__init__(window)
self.process=None
def run(self, dirs):
# root
quick_cocos2dx_root = checkRoot()
if not quick_cocos2dx_root:
return
# player path for platform
playerPath=""
if sublime.platform()=="osx":
playerPath=quick_cocos2dx_root+"/player/bin/mac/quick-x-player.app/Contents/MacOS/quick-x-player"
elif sublime.platform()=="windows":
playerPath=quick_cocos2dx_root+"/player/bin/win32/quick-x-player.exe"
if playerPath=="" or not os.path.exists(playerPath):
sublime.error_message("player no exists")
return
args=[playerPath]
# param
path=dirs[0]
configPath=path+"/config.lua"
args.append("-workdir")
args.append(os.path.split(path)[0])
args.append("-file")
args.append("scripts/main.lua")
args.append("-load-framework")
if os.path.exists(configPath):
f=open(configPath,"r")
width=640
height=960
while True:
line=f.readline()
if line:
# debug
m=re.match("^DEBUG\s*=\s*(\d+)",line)
if m:
debug=m.group(1)
if debug=="0":
args.append("-disable-write-debug-log")
args.append("-disable-console")
elif debug=="1":
args.append("-disable-write-debug-log")
args.append("-console")
else:
args.append("-write-debug-log")
args.append("-console")
# resolution
m=re.match("^CONFIG_SCREEN_WIDTH\s*=\s*(\d+)",line)
if m:
width=m.group(1)
m=re.match("^CONFIG_SCREEN_HEIGHT\s*=\s*(\d+)",line)
if m:
height=m.group(1)
else:
break
f.close()
args.append("-size")
args.append(width+"x"+height)
for i in range(0,len(args)):
args[i]=args[i].encode(sys.getfilesystemencoding())
if self.process:
self.process.kill()
if sublime.platform()=="osx":
self.process=subprocess.Popen(args)
elif sublime.platform()=="windows":
self.process=subprocess.Popen(args,shell=True)
def is_enabled(self, dirs):
if len(dirs)!=1:
return False
mainLuaPath=dirs[0]+"/main.lua"
if not os.path.exists(mainLuaPath):
return False
return True
def is_visible(self, dirs):
if len(dirs)!=1:
return False
mainLuaPath=dirs[0]+"/main.lua"
if not os.path.exists(mainLuaPath):
return False
return True
class QuickxGotoDefinitionCommand(sublime_plugin.TextCommand):
def run(self, edit):
# select text
sel=self.view.substr(self.view.sel()[0])
if len(sel)==0:
return
quick_cocos2dx_root = checkRoot()
if not quick_cocos2dx_root:
return
# find all match file
matchList=[]
showList=[]
for item in DEFINITION_LIST:
for key in item[0]:
if key==sel:
matchList.append(item)
showList.append(item[1])
for item in USER_DEFINITION_LIST:
for key in item[0]:
if key==sel:
matchList.append(item)
showList.append(item[1])
if len(matchList)==0:
sublime.status_message("Can not find definition '%s'"%(sel))
elif len(matchList)==1:
filepath=os.path.join(quick_cocos2dx_root,matchList[0][2])
if os.path.exists(filepath):
self.view.window().open_file(filepath+":"+str(matchList[0][3]),sublime.ENCODED_POSITION)
else:
sublime.status_message("%s not exists"%(filepath))
else:
# multi match
self.matchList=matchList
self.quick_cocos2dx_root=quick_cocos2dx_root
on_done = functools.partial(self.on_done)
self.view.window().show_quick_panel(showList,on_done)
def on_done(self,index):
if index==-1:
return
item=self.matchList[index]
filepath=os.path.join(self.quick_cocos2dx_root,item[2])
filepath=os.path.abspath(filepath)
if os.path.exists(filepath):
self.view.window().open_file(filepath+":"+str(item[3]),sublime.ENCODED_POSITION)
else:
sublime.status_message("%s not exists"%(filepath))
def is_enabled(self):
return helper.checkFileExt(self.view.file_name(),"lua")
def is_visible(self):
return helper.checkFileExt(self.view.file_name(),"lua")
class QuickxRebuildUserDefinitionCommand(sublime_plugin.WindowCommand):
def __init__(self,window):
super(QuickxRebuildUserDefinitionCommand,self).__init__(window)
self.lastTime=0
def run(self, dirs):
curTime=time.time()
if curTime-self.lastTime<3:
sublime.status_message("Rebuild frequently!")
return
self.lastTime=curTime
global USER_DEFINITION_LIST
USER_DEFINITION_LIST=rebuild.rebuild(dirs[0],TEMP_PATH)
path=os.path.join(TEMP_PATH, "user_definition.json")
data=json.dumps(USER_DEFINITION_LIST)
if not os.path.exists(TEMP_PATH):
os.makedirs(TEMP_PATH)
helper.writeFile(path,data)
sublime.status_message("Rebuild user definition complete!")
def is_enabled(self, dirs):
return len(dirs)==1
def is_visible(self, dirs):
return len(dirs)==1
class QuickxRebuildUserDefinitionListener(sublime_plugin.EventListener):
def __init__(self):
self.lastTime=0
def on_post_save(self, view):
filename=view.file_name()
if not filename:
return
if not helper.checkFileExt(filename,"lua"):
return
curTime=time.time()
if curTime-self.lastTime<2:
return
self.lastTime=curTime
a=rebuild.rebuildSingle(filename,TEMP_PATH)
arr=a[0]
path=a[1]
# remove prev
global USER_DEFINITION_LIST
for item in USER_DEFINITION_LIST:
if item[2]==path:
USER_DEFINITION_LIST.remove(item)
USER_DEFINITION_LIST.extend(arr)
path=os.path.join(TEMP_PATH, "user_definition.json")
data=json.dumps(USER_DEFINITION_LIST)
if not os.path.exists(TEMP_PATH):
os.makedirs(TEMP_PATH)
helper.writeFile(path,data)
sublime.status_message("Current file definition rebuild complete!")
# st3
def plugin_loaded():
sublime.set_timeout(init, 200)
# st2
if not helper.isST3():
init()