├── .gitignore ├── Default (Linux).sublime-keymap ├── Default (OSX).sublime-keymap ├── Default (Windows).sublime-keymap ├── Default.sublime-commands ├── LICENSE ├── Main.sublime-menu ├── README.md ├── phpcbf.py └── phpcbf.sublime-settings /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Django stuff: 51 | *.log 52 | 53 | # Sphinx documentation 54 | docs/_build/ 55 | 56 | # PyBuilder 57 | target/ 58 | -------------------------------------------------------------------------------- /Default (Linux).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | { "keys": ["ctrl+alt+f"], "command": "php_cbf"}, 3 | ] -------------------------------------------------------------------------------- /Default (OSX).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | { "keys": ["ctrl+alt+f"], "command": "php_cbf"}, 3 | ] -------------------------------------------------------------------------------- /Default (Windows).sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | { "keys": ["ctrl+alt+f"], "command": "php_cbf"}, 3 | ] -------------------------------------------------------------------------------- /Default.sublime-commands: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "caption": "PHP CodeBeautifier", 4 | "command": "php_cbf", 5 | }, 6 | ] 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ennosuke 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /Main.sublime-menu: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "id": "tools", 4 | "children": [ 5 | { 6 | "caption": "PHP CodeBeautifier", 7 | "command": "php_cbf", 8 | } 9 | ] 10 | }, 11 | { 12 | "id": "preferences", 13 | "children": [ 14 | { 15 | "id": "package-settings", 16 | "children": [ 17 | { 18 | "caption": "PHP Code Beautifier", 19 | "children": [ 20 | { 21 | "command": "open_file", 22 | "args": { 23 | "file": "${packages}/phpcbf/phpcbf.sublime-settings" 24 | }, 25 | "caption": "Settings - Default" 26 | }, 27 | { 28 | "command": "open_file", 29 | "args": { 30 | "file": "${packages}/User/phpcbf.sublime-settings" 31 | }, 32 | "caption": "Settings - User" 33 | }, 34 | { 35 | "caption": "-" 36 | }, 37 | { 38 | "caption": "Key Bindings – Default", 39 | "command": "open_file", "args": 40 | { 41 | "file": "${packages}/phpcbf/Default (OSX).sublime-keymap", 42 | "platform": "OSX" 43 | } 44 | }, 45 | { 46 | "caption": "Key Bindings – User", 47 | "command": "open_file", "args": 48 | { 49 | "file": "${packages}/User/Default (OSX).sublime-keymap", 50 | "platform": "OSX" 51 | } 52 | }, 53 | { 54 | "caption": "Key Bindings – Default", 55 | "command": "open_file", "args": 56 | { 57 | "file": "${packages}/phpcbf/Default (Linux).sublime-keymap", 58 | "platform": "Linux" 59 | } 60 | }, 61 | { 62 | "caption": "Key Bindings – User", 63 | "command": "open_file", "args": 64 | { 65 | "file": "${packages}/User/Default (Linux).sublime-keymap", 66 | "platform": "Linux" 67 | } 68 | }, 69 | { 70 | "caption": "Key Bindings – Default", 71 | "command": "open_file", "args": 72 | { 73 | "file": "${packages}/phpcbf/Default (Windows).sublime-keymap", 74 | "platform": "Windows" 75 | } 76 | }, 77 | { 78 | "caption": "Key Bindings – User", 79 | "command": "open_file", "args": 80 | { 81 | "file": "${packages}/User/Default (Windows).sublime-keymap", 82 | "platform": "Windows" 83 | } 84 | } 85 | ] 86 | } 87 | ] 88 | } 89 | ] 90 | } 91 | ] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP-Codebeautifier 2 | This is a plugin for Sublime Text 3 that gives the option to run the phpcbf 3 | ## Prerequisite 4 | You need the [PHP Codesniffer](https://github.com/squizlabs/PHP_CodeSniffer) more precise the code beautifier from them. 5 | ## Install 6 | Clone this repo into your Packages folder of Sublime Text. If ```phpcbf``` is in your path you don't need to do much setup. Otherwise create a user config for this plugin and set the path to your executable. 7 | ```Javascript 8 | { 9 | "path": "/path/to/your/phpcbf" 10 | } 11 | ``` 12 | 13 | Please note that the executable has to be in the string. 14 | 15 | ## Usage 16 | The usage is pretty straight forward just press ```CTRL+ALT+F``` or use the button in the Tools menu to execute it. For longer files give it a bit to run. 17 | 18 | ## Configuration 19 | Nearly all configuration options from phpcbf are bound to options in the settings file. Please refer to the [documentiation](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Fixing-Errors-Automatically) for phpcbf to know what they do. 20 | 21 | The three most important ones are 22 | 23 | 1. path - Sets the path to the phpcbf executable 24 | 2. level - Sets the code standard to follow. Default: psr-2 25 | 3. patch - Sets whenether patch command should be used. 26 | -------------------------------------------------------------------------------- /phpcbf.py: -------------------------------------------------------------------------------- 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 | 66 | -------------------------------------------------------------------------------- /phpcbf.sublime-settings: -------------------------------------------------------------------------------- 1 | { 2 | "path": "", 3 | "level": "psr2", 4 | "patch": "true", 5 | "encoding": "UTF-8", 6 | "on_save": true, 7 | "on_load": false, 8 | "syntaxes": [ 9 | "Packages/PHP/PHP.tmLanguage", 10 | "Packages/PHP/PHP.sublime-syntax" 11 | ], 12 | "suffix": "", 13 | "sniffs": [], 14 | "tab_width": "" 15 | } 16 | --------------------------------------------------------------------------------