From 12851c48256371fa8474d6955f3feef2108d8e7c Mon Sep 17 00:00:00 2001 From: Nicolai Syvertsen Date: Mon, 19 Aug 2013 22:37:32 +0200 Subject: [PATCH 1/4] Add windows compatiblity The command line script is called php_beautifier.bat on Windows. --- php_beautifier.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/php_beautifier.py b/php_beautifier.py index 3a8f1eb..b8a999e 100644 --- a/php_beautifier.py +++ b/php_beautifier.py @@ -24,6 +24,7 @@ def run(self, edit): # Start doing stuff cmd = "php_beautifier" + cmd_windows = cmd + ".bat" indent = "-s4" filters = "ArrayNested() NewLines(before=switch:while:for:foreach:T_CLASS:return:break) Pear(add-header=false)" @@ -34,7 +35,7 @@ def run(self, edit): startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW startupinfo.wShowWindow = subprocess.SW_HIDE - p = subprocess.Popen([cmd, indent, "-l", filters, "-f", "-", "-o", "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=startupinfo) + p = subprocess.Popen([cmd_windows, indent, "-l", filters, "-f", "-", "-o", "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=startupinfo) else: p = subprocess.Popen([cmd, indent, "-l", filters, "-f", "-", "-o", "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate(AllFileText) From 691d71e897cc35214b51364dcd2f220812ecbee2 Mon Sep 17 00:00:00 2001 From: galindus_olidop Date: Sun, 2 Feb 2014 16:16:00 +0100 Subject: [PATCH 2/4] Fixes #11. Added more info in status messages and moved cmd_win inside os if. --- php_beautifier.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/php_beautifier.py b/php_beautifier.py index b8a999e..410a2b6 100644 --- a/php_beautifier.py +++ b/php_beautifier.py @@ -13,18 +13,17 @@ def run(self, edit): return if self.view.is_dirty(): - return sublime.status_message("Please save the file.") + return self.status("Please save the file.") FILE = self.view.file_name() if not FILE or not os.path.exists(FILE): - return self.status("File does not exist.") + return self.status("File {0} does not exist.".format(FILE)) if not FILE[-3:] == 'php': - return self.status("File does not have php extension.") + return self.status("File {0} does not have php extension.".format(FILE)) # Start doing stuff cmd = "php_beautifier" - cmd_windows = cmd + ".bat" indent = "-s4" filters = "ArrayNested() NewLines(before=switch:while:for:foreach:T_CLASS:return:break) Pear(add-header=false)" @@ -32,10 +31,11 @@ def run(self, edit): AllFileText = self.view.substr(allFile).encode('utf-8') if os.name == 'nt': + cmd_win = cmd + ".bat" startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW startupinfo.wShowWindow = subprocess.SW_HIDE - p = subprocess.Popen([cmd_windows, indent, "-l", filters, "-f", "-", "-o", "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=startupinfo) + p = subprocess.Popen([cmd_win, indent, "-l", filters, "-f", "-", "-o", "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=startupinfo) else: p = subprocess.Popen([cmd, indent, "-l", filters, "-f", "-", "-o", "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate(AllFileText) @@ -58,3 +58,6 @@ def show_error_panel(self, stderr): def fixup(self, string): return re.sub(r'\r\n|\r', '\n', string.decode('utf-8')) + + def status(self, string): + return sublime.status_message(string) \ No newline at end of file From b5d1de6fc6213b2e56ce8f8ddb94f92042ae1901 Mon Sep 17 00:00:00 2001 From: galindus Date: Sun, 2 Feb 2014 17:51:39 +0100 Subject: [PATCH 3/4] Improved settings. Load settings from settings file. Added menus captions to format code and to edit settings files. Allowed to change/add file extensions to allowed list. Fixes #7. Fixes #8 --- Main.sublime-menu | 47 ++++++++++++++++++++++++++++++++++ PhpBeautifier.sublime-settings | 9 +++++++ php_beautifier.py | 17 ++++++++---- 3 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 Main.sublime-menu create mode 100644 PhpBeautifier.sublime-settings diff --git a/Main.sublime-menu b/Main.sublime-menu new file mode 100644 index 0000000..86e7408 --- /dev/null +++ b/Main.sublime-menu @@ -0,0 +1,47 @@ +[ + { + "caption": "Edit", + "mnemonic": "n", + "id": "edit", + "children": + [ + { + "caption" : "Php: Format Code", + "command": "php_beautifier" + } + ] + }, + { + "caption": "Preferences", + "mnemonic": "n", + "id": "preferences", + "children": + [ + { + "caption": "Package Settings", + "mnemonic": "P", + "id": "package-settings", + "children": + [ + { + "caption": "PhpBeautifier", + "children": + [ + { + "command": "open_file", + "args": {"file": "${packages}/PhpBeautifier/PhpBeautifier.sublime-settings"}, + "caption": "Settings – Default" + }, + { + "command": "open_file", + "args": {"file": "${packages}/User/PhpBeautifier.sublime-settings"}, + "caption": "Settings – User" + }, + { "caption": "-" } + ] + } + ] + } + ] + } +] \ No newline at end of file diff --git a/PhpBeautifier.sublime-settings b/PhpBeautifier.sublime-settings new file mode 100644 index 0000000..dfa0a0d --- /dev/null +++ b/PhpBeautifier.sublime-settings @@ -0,0 +1,9 @@ +{ + "filters" : [ + "ArrayNested()", + "NewLines(before=switch:while:for:foreach:T_CLASS:return:break)", + "Pear(add-header=false)" + ], + "indent" : "-s4", + "extensions" : ["php"] +} \ No newline at end of file diff --git a/php_beautifier.py b/php_beautifier.py index 410a2b6..bfdc21a 100644 --- a/php_beautifier.py +++ b/php_beautifier.py @@ -7,7 +7,9 @@ class PhpBeautifierCommand(sublime_plugin.TextCommand): - def run(self, edit): + def run(self, edit): + # Load settings + settings = sublime.load_settings('PhpBeautifier.sublime-settings') # Test environment if self.view.is_scratch(): return @@ -15,18 +17,23 @@ def run(self, edit): if self.view.is_dirty(): return self.status("Please save the file.") + # check if file exists. FILE = self.view.file_name() if not FILE or not os.path.exists(FILE): return self.status("File {0} does not exist.".format(FILE)) - if not FILE[-3:] == 'php': - return self.status("File {0} does not have php extension.".format(FILE)) + # check if extension is allowed. + fileName, fileExtension = os.path.splitext(FILE) + if fileExtension[1:] not in settings.get('extensions'): + return self.status("File {0}{1} does not have a valid extension. Please edit your settings to allow this extension.".format(fileName, fileExtension)) # Start doing stuff cmd = "php_beautifier" - indent = "-s4" - filters = "ArrayNested() NewLines(before=switch:while:for:foreach:T_CLASS:return:break) Pear(add-header=false)" + # Load indent and filters settings + indent = settings.get('indent'); + filters = ' '.join(settings.get('filters')); + allFile = sublime.Region(0, self.view.size()) AllFileText = self.view.substr(allFile).encode('utf-8') From 5b731539e57faa770139b8bca516a5d9295cc0da Mon Sep 17 00:00:00 2001 From: galindus Date: Sun, 2 Feb 2014 20:34:37 +0100 Subject: [PATCH 4/4] Changed status messages to error dialogs. Added dialog to add extension to list of allowed extensions for phpbeautifier. --- php_beautifier.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/php_beautifier.py b/php_beautifier.py index bfdc21a..66b1c03 100644 --- a/php_beautifier.py +++ b/php_beautifier.py @@ -15,17 +15,18 @@ def run(self, edit): return if self.view.is_dirty(): - return self.status("Please save the file.") + return self.error("Please save the file.") # check if file exists. FILE = self.view.file_name() if not FILE or not os.path.exists(FILE): - return self.status("File {0} does not exist.".format(FILE)) + return self.error("File {0} does not exist.".format(FILE)) # check if extension is allowed. fileName, fileExtension = os.path.splitext(FILE) if fileExtension[1:] not in settings.get('extensions'): - return self.status("File {0}{1} does not have a valid extension. Please edit your settings to allow this extension.".format(fileName, fileExtension)) + if not self.missingFileExtension(fileExtension[1:], settings): + return # Start doing stuff cmd = "php_beautifier" @@ -48,6 +49,7 @@ def run(self, edit): stdout, stderr = p.communicate(AllFileText) if len(stderr) == 0: self.view.replace(edit, allFile, self.fixup(stdout)) + self.status("PhpBeautifier: File processed.") else: self.show_error_panel(self.fixup(stderr)) @@ -67,4 +69,17 @@ def fixup(self, string): return re.sub(r'\r\n|\r', '\n', string.decode('utf-8')) def status(self, string): - return sublime.status_message(string) \ No newline at end of file + return sublime.status_message(string) + + def error(self, string): + return sublime.error_message(string) + + def missingFileExtension(self, fileExtension, settings): + if sublime.ok_cancel_dialog("Extension {0} is not a valid php extension. Would you like to add this extension to your preferences?.".format(fileExtension), "Yes, add {0} extension to my preferences".format(fileExtension)): + extensions = settings.get('extensions') + extensions.append(fileExtension) + settings.set('extensions', extensions) + settings = sublime.save_settings('PhpBeautifier.sublime-settings') + return True + return False +