关于sublime text 3写一个插件

前言

我之前一直想写一个记录自己笔记的软件,可以给因为我都记录在桌面的便签上很乱,以至于便签上满满的全是字母,很难看,但是我有不想写图形化界面,所以最终我选择了写一个sublime 插件,功能就是记录和查询我找到的一些api,以及linux一些命令,和一些代码片段(sublime text 3带有代码片段功能,不过书写格式较为复制,我只喜欢复制粘贴然后改几个变量,有记录和查询功能就可以了)。

开始编程

首先要了解一些基本的知识,为了后面写插件理解

  1. sublime 自带一个python,这个pythonsublime里面集成的,因为sublime插件都是python脚本,你可以通过 ctrl +` 来进入这种交互,在sublime中叫 console(控制台)
  2. 基于前一点,我们可以知道,如果写的插件想要调用自己安装的第三方库是不可能的,因为编程使用的pythonsublime自带的python完全是两个环境,就连版本可能都不一样
  3. 调试插件不能再使用你系统中安装的 python ,必须使用 sublime自带的python来调试,其实这些都是基于第一点,怎么调试后面会讲到

打开sublime--------tools-----developer-----new plugin

import sublime
import sublime_plugin


class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.insert(edit, 0, "Hello, World!")

这段代码不能使用 ctrl+b执行,首先要保存,保存在默认目录里面就可以,名字随便起
那怎么执行这个插件呢?
两种办法:

  1. 打开 console(ctrl+`),在底部输入 sublime.active_window().run_command('example')回车出现,Hello,World!

  2. ctrl+shirft+p输入 key bindings回车,进入快捷键配置,配置一个快捷键

{ "keys": ["ctrl+t"], "command": "example" }

其实类的名字除去Command就是这里面命令的名字,跟文件名字没有关系
保存后,ctrl+t 就执行这个插件了

其实这个插件的作用就是在你编辑的位置插入 Hello world!
关于具体里面的api 可以参考官方文档
http://www.sublimetext.com/docs/3/api_reference.html

插件示例

import sublime
import sublime_plugin
import sys
import os
import threading
import time

class InputCommand(sublime_plugin.TextCommand):
    def run(self, edit):

        # 初始化
        path = os.path.join(os.environ['HOME'],'notes')
        if not os.path.exists(path):
            os.makedirs(path)
        self.path = path
        self.code_file_path =  os.path.join(self.path,'code.txt')
        self.api_file_path =  os.path.join(self.path,'api.txt')
        self.linux_file_path =  os.path.join(self.path,'linux.txt')
        if not os.path.exists(self.code_file_path):
            f = open(self.code_file_path,'w')
            f.close()
        if not os.path.exists(self.api_file_path):
            f = open(self.api_file_path,'w')
            f.close()
        if not os.path.exists(self.linux_file_path):
            f = open(self.linux_file_path,'w')
            f.close()

        list_data = [
        "new code snippets",
        "new api snippets",
        "new linux command",
        "search code snippets",
        "search api snippets",
        "search linux command",
        ]

        self.fun_list = [
        'new_code',
        'new_api',
        'new_linux',
        'search_code',
        'search_api',
        'search_linux'
        ]

        sublime.active_window().show_quick_panel(list_data, self.on_run)

    def on_run(self,index):
        if index != -1:
            threading.Thread(target=eval('self.'+self.fun_list[index])).start()

    def new_code(self):
        sublime.active_window().open_file(self.code_file_path)
        # sublime.Window.open_file("hello",sublime.TRANSIENT)

    def new_api(self):
        def on_start(args):
            self.save(args+"\n",self.api_file_path)
        sublime.active_window().show_input_panel('输入api(中文说明和命令间用#分割):','',on_start, None,None)

    def new_linux(self):
        def on_start(args):
            self.save(args+"\n",self.linux_file_path)
        sublime.active_window().show_input_panel('输入linux命令(中文说明和命令间用#分割):','',on_start, None,None)

    def search_code(self):
        data_list = self.read(self.code_file_path).split('####\n')
        self.data = data_list
        sublime.active_window().show_quick_panel([[i.split('\n')[0][1:],i[i.find("\n")+1:]] for i in data_list if i], self.on_select)
    
    def search_api(self):
        data_list = self.read(self.api_file_path).split('\n')
        self.data = [i.split('#')[1] for i in data_list if i]
        sublime.active_window().show_quick_panel([i.split('#') for i in data_list if i], self.on_select)
        
    def search_linux(self):
        data_list = self.read(self.linux_file_path).split('\n')
        self.data = [i.split('#')[1] for i in data_list if i]
        sublime.active_window().show_quick_panel([i.split('#') for i in data_list if i], self.on_select)
        

    def read(self,filepath):
        f = open(filepath,'r')
        data = f.read()
        f.close()
        return data

    def save(self,args,filepath):
        with open(filepath,'a') as f:
            f.write(args)
        sublime.Window.status_message(sublime.active_window(),"已保存")

    def on_select(self,index):
        sublime.set_clipboard(self.data[index])
        sublime.Window.status_message(sublime.active_window(),"已复制到剪切板")
 

        # self.view.insert(edit, 0, "fuck this World!")

这个插件就是我前面说的我自己记录笔记的插件,其实就是简单的调用一些sublime的api,以及对文本进行读和写

posted @ 2019-09-30 10:29  Hello_wshuo  阅读(49)  评论(0编辑  收藏  举报