Issue #50 fixed ~ adding validation command, in case of error view jumps to error

This commit is contained in:
Nikolajus 2015-05-29 17:50:39 +02:00
parent 4925960ced
commit 5e455c6a0f
2 changed files with 24 additions and 0 deletions

View File

@ -14,5 +14,9 @@
{ {
"caption": "Pretty JSON: JSON query with ./jq", "caption": "Pretty JSON: JSON query with ./jq",
"command": "jq_pretty_json" "command": "jq_pretty_json"
},
{
"caption": "Pretty JSON: Validate",
"command": "pretty_json_validate"
} }
] ]

View File

@ -77,6 +77,7 @@ class PrettyJsonBaseCommand(sublime_plugin.TextCommand):
regions = [self.view.full_line(self.view.text_point(line, 0)), ] regions = [self.view.full_line(self.view.text_point(line, 0)), ]
self.view.add_regions('json_errors', regions, 'invalid', 'dot', self.view.add_regions('json_errors', regions, 'invalid', 'dot',
sublime.DRAW_OUTLINED) sublime.DRAW_OUTLINED)
self.view.show(regions[0])
self.view.set_status('json_errors', message) self.view.set_status('json_errors', message)
def show_exception(self): def show_exception(self):
@ -90,6 +91,25 @@ class PrettyJsonBaseCommand(sublime_plugin.TextCommand):
self.view.set_syntax_file("Packages/JavaScript/JSON.tmLanguage") self.view.set_syntax_file("Packages/JavaScript/JSON.tmLanguage")
class PrettyJsonValidate(PrettyJsonBaseCommand):
def run(self, edit):
self.view.erase_regions('json_errors')
for region in self.view.sel():
# If no selection, use the entire file as the selection
if region.empty() and s.get("use_entire_file_if_no_selection", True):
selection = sublime.Region(0, self.view.size())
selected_entire_file = True
else:
selection = region
try:
obj = self.json_loads(self.view.substr(selection))
sublime.message_dialog("JSON is Valid")
except Exception:
self.show_exception()
sublime.message_dialog("Invalid JSON")
class PrettyJsonCommand(PrettyJsonBaseCommand): class PrettyJsonCommand(PrettyJsonBaseCommand):
""" Pretty Print JSON """ """ Pretty Print JSON """