From 5e455c6a0f1be6b879eb1b619bf77e2e65f04ffc Mon Sep 17 00:00:00 2001 From: Nikolajus Date: Fri, 29 May 2015 17:50:39 +0200 Subject: [PATCH] Issue #50 fixed ~ adding validation command, in case of error view jumps to error --- Default.sublime-commands | 4 ++++ PrettyJson.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/Default.sublime-commands b/Default.sublime-commands index c43f5b0..68a1462 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -14,5 +14,9 @@ { "caption": "Pretty JSON: JSON query with ./jq", "command": "jq_pretty_json" + }, + { + "caption": "Pretty JSON: Validate", + "command": "pretty_json_validate" } ] \ No newline at end of file diff --git a/PrettyJson.py b/PrettyJson.py index 295e13c..2db85fd 100644 --- a/PrettyJson.py +++ b/PrettyJson.py @@ -77,6 +77,7 @@ class PrettyJsonBaseCommand(sublime_plugin.TextCommand): regions = [self.view.full_line(self.view.text_point(line, 0)), ] self.view.add_regions('json_errors', regions, 'invalid', 'dot', sublime.DRAW_OUTLINED) + self.view.show(regions[0]) self.view.set_status('json_errors', message) def show_exception(self): @@ -90,6 +91,25 @@ class PrettyJsonBaseCommand(sublime_plugin.TextCommand): 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): """ Pretty Print JSON """