Skip to content
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Mac files
.DS_Store

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
8 changes: 8 additions & 0 deletions Context.sublime-menu
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{ "caption": "-" },
{
"caption": "PHP CodeBeautifier",
"command": "php_cbf"
},
{ "caption": "-" }
]
2 changes: 1 addition & 1 deletion Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"id": "package-settings",
"children": [
{
"caption": "PHP Code Beautifier",
"caption": "PHP CodeBeautifier",
"children": [
{
"command": "open_file",
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ Nearly all configuration options from phpcbf are bound to options in the setting
The three most important ones are

1. path - Sets the path to the phpcbf executable
2. level - Sets the code standard to follow. Default: psr-2
3. patch - Sets whenether patch command should be used.
2. level - Sets the code standard to follow. Default: PSR-2

Now configuration can be used by project.
82 changes: 64 additions & 18 deletions phpcbf.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,73 @@
import sublime, sublime_plugin, subprocess, os

class Pref:

project_file = None

keys = [
"path",
"level",
"encoding",
"on_save",
"on_load",
"syntaxes",
"suffix",
"sniffs",
"tab_width"
]

def load(self):
self.settings = sublime.load_settings('phpcbf.sublime-settings')

if sublime.active_window() is not None and sublime.active_window().active_view() is not None:
project_settings = sublime.active_window().active_view().settings()
if project_settings.has("phpcbf"):
project_settings.clear_on_change('phpcbf')
self.project_settings = project_settings.get('phpcbf')
project_settings.add_on_change('phpcbf', pref.load)
else:
self.project_settings = {}
else:
self.project_settings = {}

for key in self.keys:
self.settings.clear_on_change(key)
setattr(self, key, self.get_setting(key))
self.settings.add_on_change(key, pref.load)

def get_setting(self, key):
if key in self.project_settings:
return self.project_settings.get(key)
else:
return self.settings.get(key)

def set_setting(self, key, value):
if key in self.project_settings:
self.project_settings[key] = value
else:
self.settings.set(key, value)

pref = Pref()

def plugin_loaded():
pref.load()


class PhpCbfCommand(sublime_plugin.TextCommand):

def run(self, edit):
settings = sublime.load_settings("phpcbf.sublime-settings")
syntaxes = settings.get('syntaxes')
syntaxes = pref.syntaxes
cur_syntax = self.view.settings().get('syntax')
if cur_syntax not in syntaxes:
return
path = settings.get('path', "")
level = settings.get('level', 'psr2')
patch = settings.get('patch', False)
suffix = settings.get('suffix', '')
sniffs = settings.get('sniffs', '')
tab_width = settings.get('tab_width', False)
if not patch:
patch = "--no-patch"
else:
patch = ""
path = pref.path
level = pref.level
suffix = pref.suffix
sniffs = pref.sniffs
tab_width = pref.tab_width
encoding = self.view.encoding()
if encoding == 'Undefined':
encoding = settings.get('encoding', 'UTF-8')
encoding = pref.encoding
if not path:
path = "phpcbf"
if suffix:
Expand All @@ -35,7 +82,7 @@ def run(self, edit):
tab_width = ' --tab_width='+tab_width
file_name = self.view.file_name()
if file_name:
call = path+" "+patch+suffix+sniff_string+tab_width+' --standard='+level+" --encoding="+encoding+" "+file_name
call = path+" "+suffix+sniff_string+tab_width+' --standard='+level+" --encoding="+encoding+" "+file_name
try:
output = subprocess.check_output(call, shell=True,universal_newlines=True)
except subprocess.CalledProcessError as e:
Expand All @@ -45,14 +92,13 @@ def run(self, edit):
sublime.status_message("All fixable errors have been fixed")
else:
sublime.status_message("Please save the file first")

class PhpCbfListener(sublime_plugin.EventListener):
def on_post_save(self, view):
settings = sublime.load_settings("phpcbf.sublime-settings")
if(settings.get('on_save', True)):
if(pref.on_save):
view.run_command('php_cbf')
def on_load(self, view):
settings = sublime.load_settings("phpcbf.sublime-settings")
if(settings.get('on_load', True)):
if(pref.on_load):
view.run_command('php_cbf')


Expand Down
6 changes: 3 additions & 3 deletions phpcbf.sublime-settings
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"path": "",
"level": "psr2",
"patch": "true",
"level": "PSR2",
"patch": false,
"encoding": "UTF-8",
"on_save": true,
"on_load": false,
Expand All @@ -12,4 +12,4 @@
"suffix": "",
"sniffs": [],
"tab_width": ""
}
}