接上篇
CCI的出现和初衷虽然是极好的,但是使用过程中还是会有不尽人意的地方,比如复制黏贴卡顿/代码提示卡顿,或者不习惯用Eclipse等种种原因,还是有童鞋喜欢更轻量级一点的编辑器,比如Sublime Text
Sublime Text(简称ST)
ST是一款文本编辑器,它十分轻量级,以至于连IDE都称不上,而它最厉害的地方就是其丰富的插件库,使用插件的组合,可以迅速打造出一套属于自己的高效开发环境
插件开发
基于个人习惯原因,我们会有很多稀奇古怪的需求,这类需求可能是很小众的,大路的插件并不会予以支持,所以我们就得自己动手 下面的例子,就是我开发过程中碰到的需求,参见CCI版本的Cocos Code IDE的Lua模板加强
代码我上传到Github了
新建插件LuaTemplate
首先找到插件目录,菜单中
- OSX:Sublime Text->Preferences->Browse Packages…
- Windows:Preferences->Browse Packages…
新建一个文件夹,命名为LuaTemplate 选择Tools->New Plugin… 新建一个.py文件,保存到LuaTemplate下
1 2 3 4 5 |
import sublime, sublime_plugin class ExampleCommand(sublime_plugin.TextCommand): def run(self, edit): self.view.insert(edit, 0, "Hello, World!") |
“ExampleCommand”就是我们执行命令的名字,以大驼峰式命名,实际的命令为”example” 按Ctrl+`打开ST控制台,输入
1 |
view.run_command("example") |
此时已打开文件的文件头,就会被输入”Hello World!”的字样 这里是ST Plugin API
编写插件逻辑
我们还是以newclass newlayer newfunction这几个来做例子
PS:博主不会python,现学的,语法写的烂请轻喷
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 |
import sublime, sublime_plugin import time import subprocess import os import codecs import re process=None file_head_template="""--- -- -- ${date} -- @author ${author} -- """ new_class_template=file_head_template+""" local ${short_file_name} = class("${short_file_name}") function ${short_file_name}:ctor() end return ${short_file_name} """ new_layer_template=file_head_template+""" local ${short_file_name} = class("${short_file_name}",BaseLayer) function ${short_file_name}:ctor() ${short_file_name}.super.ctor(self) end function ${short_file_name}:onEnter() end function ${short_file_name}:onExit() end return ${short_file_name} """ new_function_template=""" function ${short_file_name}:name() end """ file_separator = "/" if sublime.platform() == "windows": file_separator = "\" def getConfig(config,key): if os.path.exists(config): f=codecs.open(config,"r","utf-8") while True: line=f.readline() if line: sps=line.split("=") if sps[0].strip() == key: return sps[1].strip().replace(""","") else: break def generateCode(self,template): file_name = self.view.file_name() sp_start = file_name.rfind(file_separator) + 1 sp_end = file_name.rfind(".") settings=sublime.load_settings("LuaTemplate.sublime-settings") author = settings.get("author","Your name") #短文件名 short_file_name = file_name[sp_start:sp_end] code = template code = code.replace("${date}",time.strftime("%Y-%m-%d %X", time.localtime())) code = code.replace("${short_file_name}",short_file_name) code = code.replace("${author}",author) return code # ltnewclass 新建一个类的模板 class LtnewclassCommand(sublime_plugin.TextCommand): def run(self, edit): code = generateCode(self,new_class_template) (row,col) = self.view.rowcol(self.view.sel()[0].begin()) self.view.insert(edit,self.view.text_point(row,col),code) #ltnewlayer 新建一个层的模板 class LtnewlayerCommand(sublime_plugin.TextCommand): def run(self, edit): code = generateCode(self,new_layer_template) (row,col) = self.view.rowcol(self.view.sel()[0].begin()) self.view.insert(edit,self.view.text_point(row,col),code) #ltnewfunction 新建一个函数的模板 class LtnewfunctionCommand(sublime_plugin.TextCommand): def run(self, edit): code = generateCode(self,new_function_template) (row,col) = self.view.rowcol(self.view.sel()[0].begin()) self.view.insert(edit,self.view.text_point(row,col),code) #ltrunwithplayer 跑起来 class LtrunwithplayerCommand(sublime_plugin.TextCommand): def run(self, edit): settings=sublime.load_settings("LuaTemplate.sublime-settings") quickroot = settings.get("quick_v3_root","") if sublime.platform() == "windows": playerPath = quickroot+"/quick/player/win32/player3.exe" if sublime.platform() == "osx": playerPath = quickroot+"/player3.app/Contents/MacOS/player3" file_name = self.view.file_name() workdir=file_name[0:file_name.rfind("src")] srcDirName=workdir+"/src" args=[playerPath] args.append("-workdir") args.append(workdir) args.append("-file") args.append(srcDirName+"/main.lua") global process if process: try: process.terminate() except Exception: pass if sublime.platform()=="osx": process=subprocess.Popen(args) elif sublime.platform()=="windows": process=subprocess.Popen(args) #ltrunwithsimulator 跑起来2 class LtrunwithsimulatorCommand(sublime_plugin.TextCommand): def run(self, edit): file_name = self.view.file_name() workdir=file_name[0:file_name.rfind("src")] if sublime.platform() == "windows": simulatorPath = workdir+"/runtime/win32/PrebuiltRuntimeLua.exe" if sublime.platform() == "osx": simulatorPath = workdir+"/runtime/mac/PrebuiltRuntimeLua.app/Contents/MacOS/PrebuiltRuntimeLua Mac" args=[simulatorPath] args.append("-workdir") args.append(workdir) global process if process: try: process.terminate() except Exception: pass if sublime.platform()=="osx": process=subprocess.Popen(args) elif sublime.platform()=="windows": process=subprocess.Popen(args) |
配置文件
新建LuaTemplate.sublime-settings
1 2 3 4 |
{ "author" : "yaoyl", "quick_v3_root" : "/Users/superyyl/Documents/quick-3.3" } |
入口快捷方式
新建Default.sublime-commands
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[ { "caption": "Lua Template: New Class", "command": "ltnewclass" }, { "caption": "Lua Template: New Layer", "command": "ltnewlayer" }, { "caption": "Lua Template: New Function", "command": "ltnewfunction" }, { "caption": "Lua Template: Run With Player", "command": "ltrunwithplayer" }, { "caption": "Lua Template: Run With Simulator", "command": "ltrunwithsimulator" } ] |
这个是通过cmd+shift+p(windows:ctrl+shift+p)里输入命令来执行python代码的
还有菜单,侧边栏菜单,右键菜单等快捷入口,就不一一说了
跑一个
新建一个LoginLayer.lua文件,cmd+shift+p,然后输入new class,选择Lua Template: New Class
1 2 3 4 5 6 7 8 9 10 11 12 |
--- -- -- 2015-01-29 16:57:56 -- @author yaoyl -- local LoginLayer = class("LoginLayer") function LoginLayer:ctor() end return LoginLayer |
选择Lua Template: Run With Player也能顺利调用模拟器