From 363c5c8037e26cc5c8e33a115f4aaeccfe42dd07 Mon Sep 17 00:00:00 2001 From: Ashutosh Sharma Date: Fri, 10 Aug 2018 16:47:15 +0530 Subject: [PATCH 1/4] added new option for php-cs-fixer Added `passes_option` as a new package option to define php-cs-fixer options and fallback to phpf if `passes_option` is empty or not defined --- codeformatter/phpformatter.py | 88 +++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 39 deletions(-) diff --git a/codeformatter/phpformatter.py b/codeformatter/phpformatter.py index e403986..8500c2a 100644 --- a/codeformatter/phpformatter.py +++ b/codeformatter/phpformatter.py @@ -7,6 +7,8 @@ import re import tempfile +import json + import sublime import subprocess import os.path @@ -71,6 +73,10 @@ def format(self, text): if ('passes' in self.opts and self.opts['passes']): passes = self.opts['passes'] + passes_option = [] + if ('passes_option' in self.opts and self.opts['passes_option']): + passes_option = self.opts['passes_option'] + excludes = [] if ('excludes' in self.opts and self.opts['excludes']): excludes = self.opts['excludes'] @@ -87,47 +93,50 @@ def format(self, text): 'codeformatter', 'lib', 'phpbeautifier', - 'php-cs-fixer.phar' + 'php-cs-fixer.phar' if len(passes_option) > 0 else ('fmt-php55.phar' if php55_compat else 'phpf.phar') ) cmd.append(formatter_path) - # if psr1: - # cmd.append('--psr1') - # - # if psr1_naming: - # cmd.append('--psr1-naming') - # - # if psr2: - # cmd.append('--psr2') - # - # if indent_with_space is True: - # cmd.append('--indent_with_space') - # elif indent_with_space > 0: - # cmd.append('--indent_with_space=' + str(indent_with_space)) - # - # if enable_auto_align: - # cmd.append('--enable_auto_align') - # - # if visibility_order: - # cmd.append('--visibility_order') - # - # if smart_linebreak_after_curly: - # cmd.append('--smart_linebreak_after_curly') - # - # if len(passes) > 0: - # cmd.append('--passes=' + ','.join(passes)) - # - # if len(excludes) > 0: - # cmd.append('--exclude=' + ','.join(excludes)) - - cmd.append('fix') - cmd.append('--quiet') - cmd.append('--no-interaction') - cmd.append('--show-progress=none') - cmd.append('--using-cache=no') - cmd.append('--rules=@PSR2') - cmd.append(tmp_file.name) + if len(passes_option) > 0: + cmd.append('fix') + cmd.append('--quiet') + cmd.append('--no-interaction') + cmd.append('--show-progress=none') + cmd.append('--using-cache=no') + cmd.append('--rules=' + json.dumps(passes_option)) + cmd.append(tmp_file.name) + elif len(passes) > 0: + + if psr1: + cmd.append('--psr1') + + if psr1_naming: + cmd.append('--psr1-naming') + + if psr2: + cmd.append('--psr2') + + if indent_with_space is True: + cmd.append('--indent_with_space') + elif indent_with_space > 0: + cmd.append('--indent_with_space=' + str(indent_with_space)) + + if enable_auto_align: + cmd.append('--enable_auto_align') + + if visibility_order: + cmd.append('--visibility_order') + + if smart_linebreak_after_curly: + cmd.append('--smart_linebreak_after_curly') + + cmd.append('--passes=' + ','.join(passes)) + + if len(excludes) > 0: + cmd.append('--exclude=' + ','.join(excludes)) + + cmd.append('-') print(cmd) @@ -144,8 +153,9 @@ def format(self, text): p = subprocess.Popen( cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout, stderr = p.communicate() - stdout = tmp_file.read() + stdout, stderr = p.communicate() if len(passes_option) > 0 else p.communicate(text) + if len(passes_option) > 0: + stdout = tmp_file.read() except Exception as e: stdout = '' stderr = str(e) From ffd3f4ba3e6b0417f996c9b73c963f3f3248b755 Mon Sep 17 00:00:00 2001 From: Ashutosh Sharma Date: Fri, 10 Aug 2018 16:54:42 +0530 Subject: [PATCH 2/4] added new php-cs-fixer option --- CodeFormatter.sublime-settings | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/CodeFormatter.sublime-settings b/CodeFormatter.sublime-settings index 00182ef..d1e8267 100644 --- a/CodeFormatter.sublime-settings +++ b/CodeFormatter.sublime-settings @@ -15,12 +15,22 @@ "visibility_order": true, // Fixes visibility order for method in classes - PSR-2 4.2 "smart_linebreak_after_curly": true, // Convert multistatement blocks into multiline blocks - // Enable specific transformations. Example: ["ConvertOpenTagWithEcho", "PrettyPrintDocBlocks"] + // Enable specific transformations - only if "passes_options" is empty // You can list all available transformations from command palette: CodeFormatter: Show PHP Transformations + // @deprecated "passes": [], - // Disable specific transformations + // Disable specific transformations - only if "passes_options" is empty + // @deprecated "excludes": [] + + // Define php-cs-fixer option as JSON object, + // Fallbacks to phpf and uses "passes" and "excludes" option if not specified + // Check php-cs-fixer usage section for a list of available options + // @link https://github.com/FriendsOfPhp/PHP-CS-Fixer#readme + "passes_option": { + "@PSR2": true + } }, "codeformatter_js_options": From c3588343623d2270d80f2506160e709c0cfbd2b7 Mon Sep 17 00:00:00 2001 From: Ashutosh Sharma Date: Thu, 16 Aug 2018 16:46:12 +0530 Subject: [PATCH 3/4] Update CodeFormatter.sublime-settings --- CodeFormatter.sublime-settings | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CodeFormatter.sublime-settings b/CodeFormatter.sublime-settings index d1e8267..93ca58f 100644 --- a/CodeFormatter.sublime-settings +++ b/CodeFormatter.sublime-settings @@ -22,7 +22,7 @@ // Disable specific transformations - only if "passes_options" is empty // @deprecated - "excludes": [] + "excludes": [], // Define php-cs-fixer option as JSON object, // Fallbacks to phpf and uses "passes" and "excludes" option if not specified From 6250da192c4b4c304433045cce7148c94cbba351 Mon Sep 17 00:00:00 2001 From: Ashutosh Sharma Date: Fri, 31 Aug 2018 17:07:23 +0530 Subject: [PATCH 4/4] fix for Windows platform added `delete=False` to NamedTemporaryFile() - fixes `Unable to read from ` error on Windows --- codeformatter/phpformatter.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/codeformatter/phpformatter.py b/codeformatter/phpformatter.py index 8500c2a..1f68297 100644 --- a/codeformatter/phpformatter.py +++ b/codeformatter/phpformatter.py @@ -1,6 +1,6 @@ -# @author Avtandil Kikabidze -# @copyright Copyright (c) 2008-2015, Avtandil Kikabidze aka LONGMAN (akalongman@gmail.com) -# @link http://longman.me +# @author Avtandil Kikabidze +# @copyright Copyright (c) 2008-2015, Avtandil Kikabidze aka LONGMAN (akalongman@gmail.com) +# @link http://longman.me # @license The MIT License (MIT) import os @@ -23,7 +23,10 @@ def __init__(self, formatter): def format(self, text): # Create temp file - tmp_file = tempfile.NamedTemporaryFile() + if self.formatter.platform == 'windows': + tmp_file = tempfile.NamedTemporaryFile(delete=False) + else: + tmp_file = tempfile.NamedTemporaryFile() tmp_file.write(text) tmp_file.seek(0) @@ -138,8 +141,6 @@ def format(self, text): cmd.append('-') - print(cmd) - try: if self.formatter.platform == 'windows': startupinfo = subprocess.STARTUPINFO() @@ -155,7 +156,11 @@ def format(self, text): stderr=subprocess.PIPE) stdout, stderr = p.communicate() if len(passes_option) > 0 else p.communicate(text) if len(passes_option) > 0: - stdout = tmp_file.read() + formatted_str = tmp_file.read() + if self.formatter.platform == 'windows': + tmp_file.close() + os.unlink(tmp_file.name) + stdout = formatted_str except Exception as e: stdout = '' stderr = str(e)