sublime外掛開發教程(附原始碼)
1.背景
雖然可能大神門在編輯器方面都比較偏向於vim之類的自由度更高的工具,但是從我個人來講sublime這樣的外掛安裝更方便的工具還是比較得心應手的。之前用sublime寫英語作文,但是沒有一個比較好用的timer,Package_Control裡面的track_timer不能實時顯示時間,所以博主就自己動手,寫了這個外掛,可以實時timer,記錄時間。效果如下圖,
![](https://i.iter01.com/images/7b0b535aeef8e54e20b8c6f0c3b3b417061523a40155c3f24a1571ab18573139.png)
2.使用
使用起來很方便,只要把下載好的sublime-timer資料夾放在下圖這個路徑下即可。
![](https://i.iter01.com/images/2e944eadd936f2a5bcb8d6999725574419f84ae2974aa69d3287153f4cbf9309.png)
可以用快捷鍵方便的對timer進行操作:
"control+alt+t": start timer
"control+alt+p": pause or stop timer
"control+alt+z": make zero
"control+alt+p": pause or stop timer
"control+alt+z": make zero
3.製作過程
(1)環境
開發sublime外掛用到的是python語言,因為要用到sublime內建的sublime和sublime_plugin庫,所以debug和除錯都應該在sublime裡面。
下面的連結是sublime的庫得引數資訊:http://www.sublimetext.com/docs/2/api_reference.html
(2)自帶example
如果不習慣看開發文件,可以參考下以下example的開發(下面參考自http://www.welefen.com/how-to-develop-sublime-text-plugin.html)。
1、通過Tools -> New Plugin...來開啟一個初始化的外掛編輯檔案,它將有如下的內容:
import sublime, sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.insert(edit, 0, "Hello, World!")
2、通過Preferences -> Browse Packages...開啟Packages資料夾,在該資料夾下建立個子資料夾,名字為你想開發的外掛名字,如:KeymapManager。回到外掛開發的初始化編輯器頁面,通過ctrl+s (Windows/Linux) orcmd+s (OS X)儲存這個檔案,並放到你建立的子資料夾下,檔名如:KeymapManager.py
3、通過ctrl+`快捷鍵開啟SublimeText的控制檯,執行如下的命令:
view.run_command('example')
如果你在當前檔案最前面看到插入了Hello, Word!,那表明外掛執行成功了。4、ExampleCommand名字改為你想要的外掛名字,如: KeymapmanagerCommand,然後就可以開發該外掛對應的功能了。
5、通過官方的API文件查詢你需要的介面,文件見:http://www.sublimetext.com/docs/2/api_reference.html
(3)sublime-timer
這個就是我開發的sublime-timer,比example會複雜一些。大家可以參照以下程式碼:
import sublime, sublime_plugin
import threading
import time
i=0
class timer(threading.Thread): #The timer class is derived from the class threading.Thread
def __init__(self, num, interval):
threading.Thread.__init__(self)
self.thread_num = num
self.interval = interval
self.thread_stop = False
def run(self): #Overwrite run() method, put what you want the thread do here
global i
while not self.thread_stop:
sublime.set_timeout(write_time,1)
i+=1
time.sleep(self.interval)
def pause(self):
self.thread_stop = True
def zero(self):
global i
i=0
thread1 = timer(1, 1)
class gtimerCommand(sublime_plugin.TextCommand):
def run(self, edit):
global thread1
thread=timer(1,1)
if thread1.isAlive():
live=True
else:
thread.start()
thread1=thread
class gtimerpauseCommand(sublime_plugin.TextCommand):
def run(self, edit):
global thread1
thread1.pause()
class gtimerzeroCommand(sublime_plugin.TextCommand):
def run(self, edit):
global thread1
thread1.zero()
def write_time():
sublime.status_message(time_manage(i))
def time_manage(time_number):
time_str='time:'+str(time_number/60)+'min '+str(time_number%60)+'s'
return time_str
(4)釋出
如果你做好了一個個性外掛想讓更多的朋友使用的話可以試試以下兩種途徑。
1.可以給https://github.com/SublimeText發email
2.可以給https://github.com/wbond/package_control_channelpull issue(有一個文件,流程比較麻煩)
專案地址:https://github.com/jimenbian/sublime-timer(fork完別忘了給個star)
好了,看到這裡大家應該已經對外掛製作有些瞭解了,動起手來吧!
/********************************
* 本文來自部落格 “李博Garvin“
* 轉載請標明出處:http://blog.csdn.net/buptgshengod
******************************************/
相關文章
- sublime安裝外掛詳細教程
- eslint外掛開發教程EsLint
- Maven外掛開發教程Maven
- Chrome外掛開發教程Chrome
- Sublime外掛推薦
- Sublime裝vue外掛Vue
- vscode外掛開發實踐與demo原始碼VSCode原始碼
- Sublime安裝PlantUML外掛
- Flutter外掛iOS端開發教程FlutteriOS
- immutability-helper 外掛的基本使用(附原始碼)原始碼
- 我使用的 Sublime Text 外掛
- Sublime 安裝外掛不生效
- vue 外掛開發教程與釋出Vue
- MacOS Sublime Text 3 安裝使用 Sublime-phpcs 外掛MacPHP
- HeyUI元件庫釋出vscode外掛,PS教程: 如何開發vscode外掛?UI元件VSCode
- sublime text3 好用的外掛
- sublime Text3 前端常用外掛前端
- sublime text3外掛安裝
- InDesign外掛--常規功能開發--隨機填充--js指令碼開發--ID外掛隨機JS指令碼
- Flutter外掛SharedPreferences原始碼分析Flutter原始碼
- InDesign外掛--常規功能開發--對齊到頁面--js指令碼開發--ID外掛JS指令碼
- 開發Rhino外掛
- Flutter外掛開發Flutter
- Mybatis外掛開發MyBatis
- chrome 外掛開發Chrome
- flutter 外掛開發Flutter
- VscodeIDEA開發外掛VSCodeIdea
- Webstorm 外掛開發WebORM
- Skywalking 外掛開發
- sublime中文版 附使用教程
- 剛安裝的sublime如何安裝外掛
- Sublime text3 無法安裝外掛
- Sublime Text 3 的微信小程式外掛!微信小程式
- YonBuilder移動開發-移動原生外掛開發環境配置教程UI移動開發開發環境
- apisix~lua外掛開發與外掛註冊API
- ZBlog外掛開發檔案結構(外掛)
- VIM 程式碼片段外掛 Ultisnips 使用教程
- fastadmin的【外掛管理】外掛使用教程AST
- sublime text前端程式碼開發神器前端