Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/php fixer #375

Open
wants to merge 4 commits into
base: feature/php-fixer
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions CodeFormatter.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -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
"excludes": []
// 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":
Expand Down
105 changes: 60 additions & 45 deletions codeformatter/phpformatter.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# @author Avtandil Kikabidze
# @copyright Copyright (c) 2008-2015, Avtandil Kikabidze aka LONGMAN ([email protected])
# @link http://longman.me
# @author Avtandil Kikabidze
# @copyright Copyright (c) 2008-2015, Avtandil Kikabidze aka LONGMAN ([email protected])
# @link http://longman.me
# @license The MIT License (MIT)

import os
import re
import tempfile

import json

import sublime
import subprocess
import os.path
Expand All @@ -21,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)

Expand Down Expand Up @@ -71,6 +76,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']
Expand All @@ -87,49 +96,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)

print(cmd)
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('-')

try:
if self.formatter.platform == 'windows':
Expand All @@ -144,8 +154,13 @@ 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:
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)
Expand Down