美文网首页
自己写sublime插件

自己写sublime插件

作者: 学无止境吧 | 来源:发表于2019-10-12 09:50 被阅读0次

    写lua脚本,没找到比较好用的格式化插件。
    求人不如求己,就自己简单写个sublime插件试试手。
    代码如下:

    #LuaPrettify.py
    import sys
    from os.path import dirname
    from sublime_plugin import TextCommand
    
    sys.path.insert(0, dirname(__file__))
    from src.py.commands import *
    from src.py.event_listeners import *
    from src.py.main import *
    import sublime
    import subprocess
    from sublime import Region
    import re  
    
    #sublime版本
    version = int(sublime.version()) 
    package = "LuaPretty"
    
    #弹出式的窗口
    # sublime.error_message(sublime.version())
    # class LuamixCommand(TextCommand):
    #   def run(self, edit):
    #       sublime.error_message(sublime.version())
    #       
    #   
    
    def lua_line_format(txt):
        next_tab = 0; # 0 不变化  1,比上一行顶4个半角空格 -1 比上一行少4个空格
        current_tab = 0; # 当前tab
        # then 结尾,next_tab = 4
        # 替换单引号 双引号 字符串,替换为空串
        tp_line = txt.strip()
        if tp_line.find('--') >= 0:
            #看是否是字符串中包含
            arr = tp_line.split('--',2)
            tp_line = arr[0]
            tp_line = tp_line.strip()
    
        if tp_line == 'end': # 单独一个end,则前进4个tab
            next_tab = -0
            current_tab = -4
    
        if tp_line == 'else': 
            current_tab = -4
            next_tab = 4
    
        if tp_line.startswith('elseif ') and tp_line.endswith(' then'): 
            current_tab = -4
            next_tab = 4
    
        if tp_line.endswith(' then'):
            current_tab = current_tab
            next_tab = 4
    
        if tp_line.startswith('or') and tp_line.endswith(' then'):
            current_tab = 4
            next_tab = 0
    
        if tp_line.startswith('and') and tp_line.endswith(' then'):
            current_tab = 4
            next_tab = 0
    
        if tp_line.endswith('function ()') or tp_line.endswith('function()') :
            current_tab = 0
            next_tab = 4
    
        if tp_line.startswith('while') and tp_line.endswith(' do'):
            next_tab = 4
            current_tab = 0
    
        if tp_line.startswith('for') and tp_line.endswith(' do'):
            next_tab = 4
            current_tab = 0
    
        if tp_line.startswith('function') and tp_line.endswith(')'):
            next_tab = 4
            current_tab = 0
    
        if tp_line.endswith('= {'):
            current_tab = 0
            next_tab = 4
    
        if tp_line == '}': 
            current_tab = -4
            next_tab = 0
    
        return txt.strip(),next_tab,current_tab
    
    def tab_cnt(txt):
        txt = txt.replace("\t", '    ')
        cnt = 0
        for i in txt:
            if i == ' ':
                cnt = cnt + 1
            else:
                break
        return cnt
    
    def tab_space(cnt):
        return ''.rjust(cnt)
    
    class LuaprettifyCommand(TextCommand):
    
        def run(self, edit):
            view = self.view
            regions = view.sel()
            first_selected_region = view.sel()[0]
            lines = view.lines(first_selected_region)
            space = -1 #前面空着的
            ret_str = ''
            size = len(lines)
            if size > 0:
                s = lines[0]
                e = lines[size-1]
                replace_region = sublime.Region(s.a, e.b)
    
                for line_regin in lines:
                    line_str = view.substr(line_regin)
                    if space == -1:
                        space = tab_cnt(line_str)
                        print(space)
                    new_line,next_tab,current_tab = lua_line_format(line_str)
                    new_line_space = space + current_tab
                    if new_line_space < 0:
                        new_line_space = 0
                    new_line_all = tab_space(new_line_space) + new_line
                    print(new_line_all)
                    ret_str = ret_str + new_line_all+'\n'
                    space = new_line_space+next_tab
                view.replace(edit,replace_region,ret_str)
              
    class LuamixCommand(TextCommand):
        def run(self, edit):
            sublime.error_message('mix')
    
    #打开控制台
    class OpenterminalCommand(TextCommand):
        def run(self, edit):
            view = self.view
            path = view.file_name()
            window = sublime.active_window()
            ev = window.extract_variables()
            folder = ev['folder']
            cmd = "open -n /Applications/Utilities/Terminal.app  --args '"+folder+"'"
            print(cmd)
            os.system(cmd)
    
    #变大写小写
    class UppercaseCommand(TextCommand):
        def run(self, edit):
            view = self.view
            regions = view.sel()
            first_selected_region = view.sel()[0]
            txt = view.substr(first_selected_region)
            an = re.search('[a-z]+', txt)
            if an:
                txt = txt.upper() #str.lower() 
            else:
                txt = txt.lower()
            view.replace(edit,first_selected_region,txt)
    

    相关文章

      网友评论

          本文标题:自己写sublime插件

          本文链接:https://www.haomeiwen.com/subject/zrbhuctx.html