此插件的需求来源于以下类似要求:
SQL语句:
SELECT
U.ID, U.NAME, U.AGE, U.SEX
FROM USER_INFO U
WHERE U.NAME LIKE '%XXX%'
ORDER BY U.AGE
以上SQL需要在java中执行:
buf.append("SELECT ");
buf.append("U.ID, U.NAME, U.AGE, U.SEX ");
buf.append("FROM USER_INFO U ");
buf.append("WHERE U.NAME LIKE '%XXX%' ");
buf.append("ORDER BY U.AGE ");
当然,上述SQL是很简单的,行数可以缩减,也没有多长。但是在实际工作中,经常遇到很长的SQL,在查询器中编辑好的SQL,要一行一行的复制到代码中,并且首尾还要加字符串,那是很费时间的,所以开发一个这样的插件,以减少工作量。
编辑python代码
也可以直接Preferences->Browse Packages,然后在打开的文件夹中新建一个文件夹(e.g. PluginExample),然后在新建的文件夹下新建一个python文件(e.g. plugin_append_str.py)。然后编辑代码:
import sublime, sublime_plugin
class PluginAppendInputStrCommand(sublime_plugin.TextCommand):
def run(self, edit):
def on_done(str):
strs = str.split('*')
if len(strs) < 2:
strs = ["line_begin ", " line_end"]
self.view.run_command("plugin_insert_str", { "strs": strs })
self.view.window().show_input_panel('String: ', 'buf.append("* ");', on_done, None, None)
class PluginInsertStrCommand(sublime_plugin.TextCommand):
def run(self, edit, strs):
lines = self.view.rowcol(self.view.size())
for i in xrange(0, lines[0]+1):
point = self.view.text_point(i, 0)
region = self.view.line(point)
line = self.view.substr(region)
if not line.strip():
continue
self.view.insert(edit, region.begin(), strs[0])
self.view.insert(edit, region.end()+len(strs[0]), strs[1])
代码中class的命名很重要,比如PluginAppendInputStrCommand,要调用此命令的话,命令名称为”plugin_append_input_str”。
配置快捷键
Preferences->Key Bindings - User,打开配置文件,添加快捷键并保存。
示范
新建文件(ctrl+n),贴入SQL或其他字符串,然后不用直接按ctrl+3(根据之上配置)。
添加的字符串默认为buf.append(“* “);,自行修改。