Sublime 自定義主選單 插入程式碼片段
阿新 • • 發佈:2019-01-11
存放在:…\Sublime Text\Data\Packages\User\Default 下
Sublime 自帶的 snippet 只是 XML 的文字,如果想同時使用自定義變數沒辦法了。(起碼我沒辦法)
來自己寫個插入作者資訊的命令放在主選單、熱鍵
選單檔案:Main.sublime-menu
[ { "command": "save", "caption": "儲存 (S)" }, { "command": "build", "caption": "編譯 (B)" , "mnemonic": "B" ,"id":"build"}, { "caption": "自定義(J)", "mnemonic": "J", "id": "CustomizeMenus", "children": [ { "command": "add_author_info", "caption": "插入作者", "id":"insertAuthor"}, { "command": "add_current_date", "caption": "插入日期", "id":"insertDate"}, { "command": "add_current_time", "caption": "插入時間", "id":"insertTime"}, { "caption": "計算器", "command": "exec", "args": { "cmd": ["calc.exe","引數1","引數2"] } }, { "caption": "-" }, { "command": "open_url", "args": {"url": "http://blog.csdn.net/jx520"}, "caption": "湖邊的小屋遺址" } ] } ]
存放在 …\Sublime Text\Data\Packages 下即可,我就在這裡建立了個資料夾,放自己寫的外掛。
檔名不限,但要注意類名和呼叫命令的關係
import datetime import sublime_plugin class add_author_info(sublime_plugin.TextCommand): def run(self, edit): self.view.run_command("insert_snippet", { "contents": """ /*########################################################################### * @作者 : ${1:JerryJin} * @簽名 : ${2:大家好,我是笨笨,笨笨的笨,笨笨的笨,謝謝!} * @日期 : %s * @授權 : ${3:表客氣,當自己家 ^_^} * @說明 : ${4:文件的說明介紹} ###########################################################################*/ ${5}""" %datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") } ) ## 插入日期 # class AddCurrentDateCommand(sublime_plugin.TextCommand): # class add_current_dateCommand(sublime_plugin.TextCommand): # 個人覺得能與命令呼叫時保持一致是滿不錯的選擇 class add_current_date(sublime_plugin.TextCommand): def run(self, edit): self.view.run_command("insert_snippet", { "contents": "%s" % datetime.datetime.now().strftime("%Y-%m-%d ") } ) ## 插入時間 class add_current_time(sublime_plugin.TextCommand): def run(self, edit): self.view.run_command("insert_snippet", { "contents": "%s" % datetime.datetime.now().strftime("%H:%M:%S") } )