sublime插件开发教程(附源码)

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)发布

  
      如果你做好了一个个性插件想让更多的朋友使用的话可以试试以下两种途径。

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

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



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值