-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb_suite_format.py
57 lines (41 loc) · 1.55 KB
/
web_suite_format.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
import sublime
import sublime_plugin
import sys
import os
directory = os.path.dirname(os.path.realpath(__file__))
directory = os.path.join(directory, "libs")
if directory not in sys.path:
sys.path.append(directory)
from jsbeautify_wrapper import JsBeautifyWrapper
settings = sublime.load_settings("websuite.sublime-settings")
def get_type(view):
fName = view.file_name()
vSettings = view.settings()
syntaxPath = vSettings.get('syntax')
syntax = ""
ext = ""
if fName is not None:
# file exists, pull syntax type from extension
ext = os.path.splitext(fName)[1][1:]
if syntaxPath is not None:
syntax = os.path.splitext(syntaxPath)[0].split('/')[-1].lower()
if ext in ['js', 'json'] or "javascript" in syntax or "json" in syntax:
return "js"
if ext in ['css'] or "css" in syntax:
return "css"
def is_supported(view):
return get_type(view) in ["js", "css"]
class WebSuiteEventListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
if settings.get("format_on_save") and is_supported(view):
view.run_command("web_suite_format")
class WebSuiteFormatCommand(sublime_plugin.TextCommand):
def run(self, edit):
if not is_supported(self.view):
return
jsBeautifyWrapper = JsBeautifyWrapper(settings,
get_type(self.view),
self.view)
jsBeautifyWrapper.format(edit)
def is_enabled(self):
return is_supported(self.view)