From 700ed37cb3f900f2559c2c43e6c8a7c683e73593 Mon Sep 17 00:00:00 2001 From: asccc Date: Mon, 18 Mar 2019 21:30:53 +0100 Subject: [PATCH 1/5] added "exclude" setting --- README.md | 12 ++++++++++ SublimePhpCsFixer.py | 36 ++++++++++++++++++++++++------ SublimePhpCsFixer.sublime-settings | 4 ++++ 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 25e6d45..aa81d4f 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,18 @@ 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": [ + ".*\\.phtml$" + ] + } + + ### On Windows: The plugin tries to find the executable in: diff --git a/SublimePhpCsFixer.py b/SublimePhpCsFixer.py index 2a98c74..0c4c62a 100644 --- a/SublimePhpCsFixer.py +++ b/SublimePhpCsFixer.py @@ -1,5 +1,6 @@ import sublime, sublime_plugin import os, tempfile, subprocess +import re def load_settings(): @@ -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. @@ -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() @@ -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') @@ -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) @@ -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") @@ -231,7 +234,26 @@ 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): + log_to_console(self.settings) + + 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): diff --git a/SublimePhpCsFixer.sublime-settings b/SublimePhpCsFixer.sublime-settings index 3ac63e1..97fe471 100644 --- a/SublimePhpCsFixer.sublime-settings +++ b/SublimePhpCsFixer.sublime-settings @@ -5,4 +5,8 @@ "config": "", "on_save": false, "on_load": false + "exclude": [ + ".*[\\/](vendor|svn)[\\/].*", + "\\..*$", + ] } From 9b3c1f672eff383c269e8c184a5e103010bc1a7c Mon Sep 17 00:00:00 2001 From: asccc Date: Mon, 18 Mar 2019 21:38:08 +0100 Subject: [PATCH 2/5] add information about regular expressions --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index aa81d4f..8b648f3 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,8 @@ In order to exclude files from php-cs-fixer, you can use the "exclude" setting: ] } +The exclude-filter uses python regular expressions, for more information see: https://docs.python.org/2/library/re.html + ### On Windows: From b6e8c2f6ea1ff435ea129278ca590c04839e30a4 Mon Sep 17 00:00:00 2001 From: asccc Date: Mon, 18 Mar 2019 21:50:34 +0100 Subject: [PATCH 3/5] removed exclude default settings, added vendor-exclude example to README --- README.md | 8 +++++--- SublimePhpCsFixer.sublime-settings | 5 +---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8b648f3..fbca8ff 100644 --- a/README.md +++ b/README.md @@ -62,16 +62,18 @@ Although you can configure the rules directly on your plugin settings, it's reco ## Exclude files -Since all PHP files are passed directly to the php-cs-fixer executable, a configured PhpCsFixer\\Finder gets ignored. +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": [ - ".*\\.phtml$" + ".*[\\\\/]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 +The exclude-filter uses python regular expressions. +For more information see: https://docs.python.org/2/library/re.html ### On Windows: diff --git a/SublimePhpCsFixer.sublime-settings b/SublimePhpCsFixer.sublime-settings index 97fe471..0d761e8 100644 --- a/SublimePhpCsFixer.sublime-settings +++ b/SublimePhpCsFixer.sublime-settings @@ -5,8 +5,5 @@ "config": "", "on_save": false, "on_load": false - "exclude": [ - ".*[\\/](vendor|svn)[\\/].*", - "\\..*$", - ] + "exclude": [] } From 73816e8ba490e9eacba15d898f95f8c24968e202 Mon Sep 17 00:00:00 2001 From: asccc Date: Mon, 18 Mar 2019 21:51:59 +0100 Subject: [PATCH 4/5] removed debug-code --- SublimePhpCsFixer.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/SublimePhpCsFixer.py b/SublimePhpCsFixer.py index 0c4c62a..c221508 100644 --- a/SublimePhpCsFixer.py +++ b/SublimePhpCsFixer.py @@ -236,9 +236,7 @@ def run(self, edit): def is_supported_scope(self, view): return 'embedding.php' in view.scope_name(view.sel()[0].begin()) and not self.is_excluded(view) - def is_excluded(self, view): - log_to_console(self.settings) - + def is_excluded(self, view): if not self.settings.has('exclude'): return False From f63e398d4d17e0dfaffee7729ad1f1e16bcf9816 Mon Sep 17 00:00:00 2001 From: Carlos Gant Date: Sun, 24 Mar 2019 20:46:16 +0100 Subject: [PATCH 5/5] added missing trailing comma before exclude --- SublimePhpCsFixer.sublime-settings | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SublimePhpCsFixer.sublime-settings b/SublimePhpCsFixer.sublime-settings index 0d761e8..97adf96 100644 --- a/SublimePhpCsFixer.sublime-settings +++ b/SublimePhpCsFixer.sublime-settings @@ -4,6 +4,6 @@ "rules": "", "config": "", "on_save": false, - "on_load": false + "on_load": false, "exclude": [] }