Permalink
Browse files

sligh refactoring

  • Loading branch information...
1 parent 42e9f84 commit 03c81319f5c83440104cd382fa2e449e4bcefb7f @kbsali committed Dec 5, 2011
Showing with 51 additions and 44 deletions.
  1. +48 −43 beautifier.py
  2. +3 −1 readme.md
View
@@ -6,6 +6,54 @@
class BeautifierCommand(sublime_plugin.TextCommand):
+ def run(self, edit):
+ settings = sublime.load_settings('Beautifier.sublime-settings')
+ settings_core = self.view.settings()
+ file_name = self.view.file_name()
+ if file_name[-4:] == '.php':
+ self.php(edit, settings, settings_core, file_name)
+ if file_name[-3:] == '.js':
+ self.js(edit, settings, settings_core)
+
+ def php(self, edit, settings, settings_core, file_name):
+ print __name__ + ' Calling php_beautifier'
+
+ opts = self.phpOptions(settings, settings_core)
+ opts.append('\'')
+
+ selection = self.view.sel()[0]
+ replaceRegion = selection if len(selection) > 0 else sublime.Region(0, self.view.size())
+ f = open(file_name + "~", 'w')
+ f.write(self.view.substr(replaceRegion))
+ f.close()
+ cmd = "php_beautifier " + " ".join(opts) + "--input " + file_name + "~ --output -"
+ print __name__ + ' ' + cmd
+ res = commands.getoutput(cmd)
+ resu = unicode(res, "utf-8")
+ os.remove(file_name + "~")
+ self.view.replace(edit, replaceRegion, resu)
+
+ def js(self, edit, settings, settings_core):
+ print __name__ + 'Calling jsbeautifier'
+
+ opts = jsbeautifier.default_options()
+
+ if settings_core.get("translate_tabs_to_spaces", True) == False:
+ opts.indent_char = "\t"
+ opts.indent_size = settings_core.get("tab_size", 2)
+
+ opts.preserve_newlines = settings.get("js_preserve_newlines")
+ opts.max_preserve_newlines = settings.get("js_max_preserve_newlines")
+ opts.jslint_happy = settings.get("js_jslint_happy")
+ opts.brace_style = settings.get("js_brace_style")
+
+ selection = self.view.sel()[0]
+ replaceRegion = selection if len(selection) > 0 else sublime.Region(0, self.view.size())
+ res = jsbeautifier.beautify(self.view.substr(replaceRegion), opts)
+ prePos = self.view.sel()[0]
+ self.view.replace(edit, replaceRegion, res)
+ self.view.show_at_center(prePos.begin())
+
def phpOptions(self, settings, settings_core):
opts = []
if settings_core.get("translate_tabs_to_spaces", True):
@@ -22,46 +70,3 @@ def phpOptions(self, settings, settings_core):
if settings.get("php_NewLines", True):
opts.append('NewLines(before=switch:while:T_COMMENT:for:foreach:T_CLASS:return:break,after=T_COMMENT:protected:private)')
return opts
-
- def run(self, edit):
- settings = sublime.load_settings('Beautifier.sublime-settings')
- settings_core = self.view.settings()
- FILE = self.view.file_name()
- if FILE[-4:] == '.php':
- print __name__ + ' Calling php_beautifier'
-
- opts = self.phpOptions(settings, settings_core)
- opts.append('\'')
-
- selection = self.view.sel()[0]
- replaceRegion = selection if len(selection) > 0 else sublime.Region(0, self.view.size())
- f = open(FILE + "~", 'w')
- f.write(self.view.substr(replaceRegion))
- f.close()
- cmd = "php_beautifier " + " ".join(opts) + "--input " + FILE + "~ --output -"
- print __name__ + ' ' + cmd
- res = commands.getoutput(cmd)
- resu = unicode(res, "utf-8")
- os.remove(FILE + "~")
- self.view.replace(edit, replaceRegion, resu)
-
- if FILE[-3:] == '.js':
- print __name__ + 'Calling jsbeautifier'
-
- opts = jsbeautifier.default_options()
-
- if settings_core.get("translate_tabs_to_spaces", True) == False:
- opts.indent_char = "\t"
- opts.indent_size = settings_core.get("tab_size", 2)
-
- opts.preserve_newlines = settings.get("js_preserve_newlines")
- opts.max_preserve_newlines = settings.get("js_max_preserve_newlines")
- opts.jslint_happy = settings.get("js_jslint_happy")
- opts.brace_style = settings.get("js_brace_style")
-
- selection = self.view.sel()[0]
- replaceRegion = selection if len(selection) > 0 else sublime.Region(0, self.view.size())
- res = jsbeautifier.beautify(self.view.substr(replaceRegion), opts)
- prePos = self.view.sel()[0]
- self.view.replace(edit, replaceRegion, res)
- self.view.show_at_center(prePos.begin())
View
@@ -4,6 +4,8 @@ Both were interesting to me, but their key bindings being the same, they were th
To install it I would recommend using the great Package Control plugin (https://github.com/wbond/sublime_package_control) and add this repository to your personal list of repositories. Maybe one day it will make it to the "official" package control channel! :)
+**NOTE** : only tested on Ubuntu
+
## Installation
Install php-pear and php-cli with your package manager :
@@ -22,5 +24,5 @@ Clone or download the files and copy them to your `Packages` folder. You can acc
Check the Beautifier.sublime-settings for available options
## TODO
- * Implement php_before_* / php_after_* options
+ * Implement `php_before_*` / `php_after_*` options
* Find an alternative to php_beautifier pear install

0 comments on commit 03c8131

Please sign in to comment.