sublime外掛開發教程(附原始碼)

李博Garvin發表於2014-10-13

1.背景

     
       雖然可能大神門在編輯器方面都比較偏向於vim之類的自由度更高的工具,但是從我個人來講sublime這樣的外掛安裝更方便的工具還是比較得心應手的。之前用sublime寫英語作文,但是沒有一個比較好用的timer,Package_Control裡面的track_timer不能實時顯示時間,所以博主就自己動手,寫了這個外掛,可以實時timer,記錄時間。效果如下圖,



2.使用

 
   使用起來很方便,只要把下載好的sublime-timer資料夾放在下圖這個路徑下即可。


    可以用快捷鍵方便的對timer進行操作:
          "control+alt+t": start timer
          "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
          

三個command class,分別對應著上面提到的三個快捷鍵,這個對應關係可以在另外的keymap檔案中定義,大家可以把整個專案clone下來就看到了。


    (4)釋出

  
      如果你做好了一個個性外掛想讓更多的朋友使用的話可以試試以下兩種途徑。


2.可以給https://github.com/wbond/package_control_channelpull issue(有一個文件,流程比較麻煩)



專案地址:https://github.com/jimenbian/sublime-timer(fork完別忘了給個star)
好了,看到這裡大家應該已經對外掛製作有些瞭解了,動起手來吧!


/********************************

* 本文來自部落格  “李博Garvin“

* 轉載請標明出處:http://blog.csdn.net/buptgshengod

******************************************/



相關文章