diff --git a/Default (Linux).sublime-keymap b/Default (Linux).sublime-keymap index 003be3d..f7b2ce2 100644 --- a/Default (Linux).sublime-keymap +++ b/Default (Linux).sublime-keymap @@ -8,5 +8,12 @@ { "keys": ["ctrl+alt+shift+j"], "command": "jq_pretty_json" + }, + { + "keys": ["ctrl+r"], + "command": "pretty_json_goto_symbol", + "context": [ + { "key": "selector", "operator": "equal", "operand": "source.json" } + ] } ] \ No newline at end of file diff --git a/Default (OSX).sublime-keymap b/Default (OSX).sublime-keymap index 3910913..2d42425 100644 --- a/Default (OSX).sublime-keymap +++ b/Default (OSX).sublime-keymap @@ -8,5 +8,12 @@ { "keys": ["super+ctrl+shift+j"], "command": "jq_pretty_json" + }, + { + "keys": ["super+r"], + "command": "pretty_json_goto_symbol", + "context": [ + { "key": "selector", "operator": "equal", "operand": "source.json" } + ] } ] \ No newline at end of file diff --git a/Default (Windows).sublime-keymap b/Default (Windows).sublime-keymap index 72fa9a4..a7e09ca 100644 --- a/Default (Windows).sublime-keymap +++ b/Default (Windows).sublime-keymap @@ -14,5 +14,13 @@ { "keys": ["ctrl+alt+shift+j"], "command": "jq_pretty_json" + }, + { + "keys": ["ctrl+r"], + "command": "pretty_json_goto_symbol", + "context": [ + { "key": "selector", "operator": "equal", "operand": "source.json" } + ] } + ] diff --git a/PrettyJson.py b/PrettyJson.py index eb057a6..21b8362 100644 --- a/PrettyJson.py +++ b/PrettyJson.py @@ -170,23 +170,6 @@ class PrettyJsonValidate(PrettyJsonBaseCommand, sublime_plugin.TextCommand): sublime.message_dialog("Invalid JSON") -class PrettyJsonLintListener(sublime_plugin.EventListener, PrettyJsonBaseCommand): - def on_post_save(self, view): - # will work only in json syntax - if "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 PrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand): """ Pretty Print JSON """ @@ -406,6 +389,58 @@ class JqPrettyJsonOut(sublime_plugin.TextCommand): self.view.insert(edit, 0, jq_output) +class PrettyJsonGotoSymbolCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand): + def run(self, edit): + self.items = [] + self.goto_items = [] + + content = self.view.substr(sublime.Region(0, self.view.size())) + + try: + json_data = self.json_loads(content) + self.generate_items(json_data, '') + sublime.active_window().show_quick_panel(self.items, self.goto) + except Exception: + self.show_exception() + + def generate_items(self, json_data, root_key): + if isinstance(json_data, OrderedDict): + for key in json_data: + new_key_name = root_key + '.' + key + self.items.append('%s' % new_key_name) + self.goto_items.append('"%s"' % key) + self.generate_items(json_data[key], new_key_name) + elif isinstance(json_data, list): + for index, item in enumerate(json_data): + if isinstance(item, str): + self.items.append('%s' % root_key + '.' + item) + self.goto_items.append('"%s"' % item) + + def goto(self, pos): + string_to_search = self.goto_items[pos] + + found = 0 + for index, item in enumerate(self.goto_items): + if index >= pos: + break + if item == string_to_search: + found += 1 + + regions = self.view.find_all(string_to_search, sublime.LITERAL) + for i, r in enumerate(regions): + line = self.view.substr(self.view.full_line(r)) + if ":" in line: + split = line.split(":") + val = split[1].strip() + if string_to_search in val: + del regions[i] + + region = regions[found] + self.view.sel().clear() + self.view.sel().add(region) + self.view.show(region) + + def plugin_loaded(): global s s = sublime.load_settings("Pretty JSON.sublime-settings") diff --git a/PrettyJsonListeners.py b/PrettyJsonListeners.py new file mode 100644 index 0000000..4c7d391 --- /dev/null +++ b/PrettyJsonListeners.py @@ -0,0 +1,25 @@ +import sublime +import sublime_plugin + +try: + # python 3 / Sublime Text 3 + from .PrettyJson import PrettyJsonBaseCommand +except ValueError: + from PrettyJson import PrettyJsonBaseCommand + + +class PrettyJsonLintListener(sublime_plugin.EventListener, PrettyJsonBaseCommand): + def on_post_save(self, view): + # will work only in json syntax + if "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() \ No newline at end of file diff --git a/README.md b/README.md index aeb2267..d43ada2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ [![Build Status](https://travis-ci.org/dzhibas/SublimePrettyJson.svg)](https://travis-ci.org/dzhibas/SublimePrettyJson) -Prettify/Minify/Query/Validate/Lint JSON plugin for Sublime Text 2 & 3 +Prettify/Minify/Query/Goto/Validate/Lint JSON plugin for Sublime Text 2 & 3 ## Installation