Created
July 24, 2014 10:34
-
-
Save NotBadPad/cc86e1646125c6df4277 to your computer and use it in GitHub Desktop.
code-template-manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding: UTF-8 -*- | |
import sublime, sublime_plugin | |
import os | |
import os.path | |
#写入模板,如果是新模板按照模板名创建一个文件,如果是旧模板则覆盖写原文件 | |
class CodeTplAddCommand(sublime_plugin.TextCommand): | |
def run(self,edit): | |
#获取当前选中内容 | |
selstr = '' | |
sels = self.view.sel() | |
for sel in sels: | |
value = self.view.substr(sel) | |
if value == '': | |
continue | |
selstr = selstr + value + '\n\n' | |
if selstr == '': | |
sublime.message_dialog("Select content is empty.") | |
return | |
def on_done(name): | |
if name=='': | |
sublime.message_dialog("Template name is empty.") | |
return | |
#写入文件 | |
path = os.path.join(sublime.packages_path(),"MyTest","template",name+".ctpl") | |
f = open(path, 'w') | |
f.write(selstr) | |
def on_change(name): | |
return | |
def on_cancel(): | |
return | |
#输入模板名 | |
self.view.window().show_input_panel('Plese input the code template name','template',on_done,on_change,on_cancel) | |
#将对应模板插入当前位置 | |
class CodeTplInsertCommand(sublime_plugin.TextCommand): | |
def run(self,edit): | |
filenames = [] | |
rootdir= os.path.join(sublime.packages_path(),"MyTest","template") | |
for p,d,files in os.walk(rootdir): | |
filenames = files | |
def on_sel_doen(selvalue): | |
filename = filenames[selvalue] | |
content = '' | |
if filename=='': | |
sublime.message_dialog("Template is not exist.") | |
path = os.path.join(sublime.packages_path(),"MyTest","template",filename) | |
try: | |
f = open(path, 'r') | |
content = f.read() | |
except Exception, e: | |
sublime.message_dialog("Open template file fail:"+str(e)) | |
finally: | |
f.close() | |
if content=='': | |
sublime.message_dialog("Template file is empty") | |
return | |
#把内容插入当前位置 | |
sels = self.view.sel() | |
for sel in sels: | |
self.view.insert(edit,sel.begin(),content) | |
print sel.begin() | |
#打开选择列表 | |
self.view.window().show_quick_panel(filenames,on_sel_doen) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment