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

add options to apply formatting by file extension, file syntax, or both #81

Open
wants to merge 1 commit into
base: master
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
5 changes: 4 additions & 1 deletion JsFormat.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"break_chained_methods": false,

// jsformat options
"format_on_save": false
"format_on_save": false,
"format_by_extension_syntax": "both",
"format_file_extension": ["js", "json"],
"format_file_syntax": ["javascript", "json"]
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ In addition, the following settings are available in JsFormat/JsFormat.sublime-s
* "unescape_strings": false,
* "break_chained_methods": false*
* "format_on_save": false
* "format_by_extension_syntax": "both" (both, extension, syntax)
* "format_file_extension": ["js", "json"]
* "format_file_syntax": ["javascript", "json"]

I had temporary lapse of judgement a while back and merged a pull request that modified jsbeautifier. As a result, the functionality that
was added from that pull request has been lost; ```"ensure_space_before_linestarters"``` is no longer supported.
Expand Down
24 changes: 17 additions & 7 deletions js_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,23 @@ def is_js_buffer(view):
syntaxPath = vSettings.get('syntax')
syntax = ""
ext = ""

if (fName != None): # file exists, pull syntax type from extension
ext = os.path.splitext(fName)[1][1:]
if(syntaxPath != None):
syntax = os.path.splitext(syntaxPath)[0].split('/')[-1].lower()

return ext in ['js', 'json'] or "javascript" in syntax or "json" in syntax
syntaxOrExtension = s.get("format_by_extension_syntax")
syntaxList = s.get("format_file_syntax")
extensionList = s.get("format_file_extension")
result = False

if (syntaxOrExtension in ['both', 'extension']):
if (fName != None): # file exists, pull syntax type from extension
ext = os.path.splitext(fName)[1][1:]
if (ext in extensionList):
result = True
if (syntaxOrExtension in ['both', 'syntax']):
if(syntaxPath != None):
syntax = os.path.splitext(syntaxPath)[0].split('/')[-1].lower()
if (syntax in syntaxList):
result = True;

return result

class PreSaveFormatListner(sublime_plugin.EventListener):
"""Event listener to run JsFormat during the presave event"""
Expand Down