forked from dzhibas/SublimePrettyJson
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrettyJsonListeners.py
35 lines (26 loc) · 1.17 KB
/
PrettyJsonListeners.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
import sublime
import sublime_plugin
try:
# python 3 / Sublime Text 3
from .PrettyJson import PrettyJsonBaseCommand
except ValueError:
from PrettyJson import PrettyJsonBaseCommand
s = sublime.load_settings("Pretty JSON.sublime-settings")
class PrettyJsonLintListener(sublime_plugin.EventListener, PrettyJsonBaseCommand):
def on_post_save(self, view):
# will work only in json syntax and once validate_on_save setting is true
validate = s.get("validate_on_save", True)
if validate and "JSON" in view.settings().get('syntax'):
self.view = view
self.view.erase_regions('json_errors')
self.view.erase_status('json_errors')
json_content = self.view.substr(sublime.Region(0, view.size()))
try:
self.json_loads(json_content)
except Exception:
self.show_exception()
class PrettyJsonAutoPrettyOnSaveListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
auto_pretty = s.get("pretty_on_save", False)
if auto_pretty and "JSON" in view.settings().get('syntax'):
sublime.active_window().run_command('pretty_json')