| 
 | 1 | +import sublime, sublime_plugin, subprocess, os  | 
 | 2 | + | 
 | 3 | +class PhpCbfCommand(sublime_plugin.TextCommand):  | 
 | 4 | + | 
 | 5 | +    def run(self, edit):  | 
 | 6 | +        settings = sublime.load_settings("phpcbf.sublime-settings")  | 
 | 7 | +        syntaxes = settings.get('syntaxes')  | 
 | 8 | +        cur_syntax = self.view.settings().get('syntax')  | 
 | 9 | +        if cur_syntax not in syntaxes:  | 
 | 10 | +            return  | 
 | 11 | +        path = settings.get('path', "")  | 
 | 12 | +        level = settings.get('level', 'psr2')  | 
 | 13 | +        patch = settings.get('patch', False)  | 
 | 14 | +        suffix = settings.get('suffix', '')  | 
 | 15 | +        sniffs = settings.get('sniffs', '')  | 
 | 16 | +        tab_width = settings.get('tab_width', False)  | 
 | 17 | +        if not patch:  | 
 | 18 | +            patch = "--no-patch"  | 
 | 19 | +        else:  | 
 | 20 | +            patch = ""  | 
 | 21 | +        encoding = self.view.encoding()  | 
 | 22 | +        if encoding == 'Undefined':  | 
 | 23 | +            encoding = settings.get('encoding', 'UTF-8')  | 
 | 24 | +        if not path:  | 
 | 25 | +            path = "phpcbf"  | 
 | 26 | +        if suffix:  | 
 | 27 | +            suffix = ' --suffix='+suffix  | 
 | 28 | +        sniff_string = ''  | 
 | 29 | +        if sniffs:  | 
 | 30 | +            sniff_string = ' --sniffs='  | 
 | 31 | +            for sniff in sniffs:  | 
 | 32 | +                sniff_string += sniff+','  | 
 | 33 | +            sniff_string.rtrim(',')  | 
 | 34 | +        if tab_width:  | 
 | 35 | +            tab_width = ' --tab_width='+tab_width  | 
 | 36 | +        file_name = self.view.file_name()  | 
 | 37 | +        if file_name:  | 
 | 38 | +            call = path+" "+patch+suffix+sniff_string+tab_width+' --standard='+level+" --encoding="+encoding+" "+file_name  | 
 | 39 | +            try:  | 
 | 40 | +                output = subprocess.check_output(call, shell=True,universal_newlines=True)  | 
 | 41 | +            except subprocess.CalledProcessError as e:  | 
 | 42 | +                print(e.output)  | 
 | 43 | +                sublime.status_message("An error occured while fixing, please check the console")  | 
 | 44 | +            else:  | 
 | 45 | +                sublime.status_message("All fixable errors have been fixed")  | 
 | 46 | +        else:  | 
 | 47 | +            sublime.status_message("Please save the file first")  | 
 | 48 | +class PhpCbfListener(sublime_plugin.EventListener):  | 
 | 49 | +    def on_post_save(self, view):  | 
 | 50 | +        settings = sublime.load_settings("phpcbf.sublime-settings")  | 
 | 51 | +        if(settings.get('on_save', True)):  | 
 | 52 | +            view.run_command('php_cbf')  | 
 | 53 | +    def on_load(self, view):  | 
 | 54 | +        settings = sublime.load_settings("phpcbf.sublime-settings")  | 
 | 55 | +        if(settings.get('on_load', True)):  | 
 | 56 | +            view.run_command('php_cbf')  | 
 | 57 | + | 
 | 58 | + | 
 | 59 | + | 
 | 60 | + | 
 | 61 | +          | 
 | 62 | + | 
 | 63 | + | 
 | 64 | + | 
 | 65 | + | 
0 commit comments