-
Notifications
You must be signed in to change notification settings - Fork 1
/
js_formatter.py
117 lines (88 loc) · 3.5 KB
/
js_formatter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import sublime, sublime_plugin, re, sys, os
directory = os.path.dirname(os.path.realpath(__file__))
libs_path = os.path.join(directory, "libs")
# crazyness to get jsbeautifier.unpackers to actually import
# with sublime's weird hackery of the path and module loading
if libs_path not in sys.path:
sys.path.append(libs_path)
# if you don't explicitly import jsbeautifier.unpackers here things will bomb out,
# even though we don't use it directly.....
import jsbeautifier, jsbeautifier.unpackers
import merge_utils
s = None
def plugin_loaded():
global s
s = sublime.load_settings("JsFormat.sublime-settings")
if sys.version_info < (3, 0):
plugin_loaded()
class PreSaveFormatListner(sublime_plugin.EventListener):
"""Event listener to run JsFormat during the presave event"""
def on_pre_save(self, view):
fName = view.file_name()
vSettings = view.settings()
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()
formatFile = ext in ['js', 'json'] or "javascript" in syntax or "json" in syntax
if(s.get("format_on_save") == True and formatFile):
view.run_command("js_format")
class JsFormatCommand(sublime_plugin.TextCommand):
def run(self, edit):
settings = self.view.settings()
# settings
opts = jsbeautifier.default_options()
opts.indent_char = " " #if settings.get("translate_tabs_to_spaces") else "\t" simply ignore setting
opts.indent_size = int(settings.get("tab_size")) if opts.indent_char == " " else 1
opts.max_preserve_newlines = s.get("max_preserve_newlines") or 3
opts.preserve_newlines = s.get("preserve_newlines") or True
opts.jslint_happy = s.get("jslint_happy") or False
opts.brace_style = s.get("brace_style") or "collapse"
opts.keep_array_indentation = s.get("keep_array_indentation") or False
opts.keep_function_indentation = s.get("keep_function_indentation") or False
opts.indent_with_tabs = s.get("indent_with_tabs") or False
opts.eval_code = s.get("eval_code") or False
opts.unescape_strings = s.get("unescape_strings") or False
opts.break_chained_methods = s.get("break_chained_methods") or False
selection = self.view.sel()[0]
nwsOffset = self.prev_non_whitespace()
# do formatting and replacement
replaceRegion = None
formatSelection = False
# formatting a selection/highlighted area
if(len(selection) > 0):
formatSelection = True
replaceRegion = selection
# formatting the entire file
else:
replaceRegion = sublime.Region(0, self.view.size())
orig = self.view.substr(replaceRegion)
res = jsbeautifier.beautify(orig, opts)
_, err = merge_utils.merge_code(self.view, edit, orig, res)
if err:
sublime.error_message("JsFormat: Merge failure: '%s'" % err)
# re-place cursor
offset = self.get_nws_offset(nwsOffset, self.view.substr(sublime.Region(0, self.view.size())))
rc = self.view.rowcol(offset)
pt = self.view.text_point(rc[0], rc[1])
sel = self.view.sel()
sel.clear()
self.view.sel().add(sublime.Region(pt))
self.view.show_at_center(pt)
def prev_non_whitespace(self):
pos = self.view.sel()[0].a
preTxt = self.view.substr(sublime.Region(0, pos));
return len(re.findall('\S', preTxt))
def get_nws_offset(self, nonWsChars, buff):
nonWsSeen = 0
offset = 0
for i in range(0, len(buff)):
offset += 1
if not(buff[i].isspace()):
nonWsSeen += 1
if(nonWsSeen == nonWsChars):
break
return offset