Skip to content

Commit

Permalink
Merge pull request #28 from asccc/master
Browse files Browse the repository at this point in the history
Added setting to exclude paths from php-cs-fixer
  • Loading branch information
adael authored Mar 24, 2019
2 parents 3f628c3 + f63e398 commit 373d135
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ Please note that this plugin don't try to find the config file automatically. If

Although you can configure the rules directly on your plugin settings, it's recommended to create the config file, as it's easier to configure every rule than using the 'rules' directive in the plugin settings.

## Exclude files

Since all php files are passed directly to the php-cs-fixer executable, a configured PhpCsFixer\\Finder gets ignored.
In order to exclude files from php-cs-fixer, you can use the "exclude" setting:

{
"exclude": [
".*[\\\\/]vendor[\\\\/].*", // vendor-path (used by Composer)
".*\\.phtml$" // files ending with ".phtml"
]
}

The exclude-filter uses python regular expressions.
For more information see: https://docs.python.org/2/library/re.html


### On Windows:

The plugin tries to find the executable in:
Expand Down
34 changes: 27 additions & 7 deletions SublimePhpCsFixer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sublime, sublime_plugin
import os, tempfile, subprocess
import re


def load_settings():
Expand Down Expand Up @@ -87,7 +88,7 @@ def log_to_console(msg):
print("PHP CS Fixer: {0}".format(msg))


def format_contents(contents):
def format_contents(contents, settings):
"""
Write the contents in a temporary file, format it with php-cs-fixer and return the formatted contents.
Expand All @@ -109,7 +110,7 @@ def format_contents(contents):
file.close()

try:
format_file(tmp_file)
format_file(tmp_file, settings)
with open(tmp_file, 'rb') as file:
content = file.read().decode(encoding)
file.close()
Expand All @@ -120,9 +121,7 @@ def format_contents(contents):
return content


def format_file(tmp_file):
settings = load_settings()

def format_file(tmp_file, settings):
php_path = settings.get('php_path')
path = settings.get('path')

Expand Down Expand Up @@ -209,6 +208,10 @@ def get_project_folder(file):


class SublimePhpCsFixCommand(sublime_plugin.TextCommand):
def __init__(self, view):
sublime_plugin.TextCommand.__init__(self, view)
self.settings = load_settings()

def is_enabled(self):
return self.is_supported_scope(self.view)

Expand All @@ -219,7 +222,7 @@ def run(self, edit):
contents = self.view.substr(region)

if contents:
formatted = format_contents(contents)
formatted = format_contents(contents, self.settings)
if formatted and formatted != contents:
self.view.replace(edit, region, formatted)
log_to_console("Done. View formatted")
Expand All @@ -231,7 +234,24 @@ def run(self, edit):
log_to_console(str(e))

def is_supported_scope(self, view):
return 'embedding.php' in view.scope_name(self.view.sel()[0].begin())
return 'embedding.php' in view.scope_name(view.sel()[0].begin()) and not self.is_excluded(view)

def is_excluded(self, view):
if not self.settings.has('exclude'):
return False

exclude = self.settings.get('exclude')
file_name = view.file_name()

if not type(exclude) is list:
exclude = [exclude]

for pattern in exclude:
if re.match(pattern, file_name) is not None:
log_to_console(file_name + ' is excluded via pattern: ' + pattern)
return True

return False


class SublimePhpCsFixListener(sublime_plugin.EventListener):
Expand Down
3 changes: 2 additions & 1 deletion SublimePhpCsFixer.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"rules": "",
"config": "",
"on_save": false,
"on_load": false
"on_load": false,
"exclude": []
}

0 comments on commit 373d135

Please sign in to comment.