#56 symbol search / goto / jump to source cmd+r

This commit is contained in:
Nikolajus 2016-07-15 17:25:55 +02:00
parent a8648980af
commit 62bdb1d281
6 changed files with 100 additions and 18 deletions

View File

@ -8,5 +8,12 @@
{ {
"keys": ["ctrl+alt+shift+j"], "keys": ["ctrl+alt+shift+j"],
"command": "jq_pretty_json" "command": "jq_pretty_json"
},
{
"keys": ["ctrl+r"],
"command": "pretty_json_goto_symbol",
"context": [
{ "key": "selector", "operator": "equal", "operand": "source.json" }
]
} }
] ]

View File

@ -8,5 +8,12 @@
{ {
"keys": ["super+ctrl+shift+j"], "keys": ["super+ctrl+shift+j"],
"command": "jq_pretty_json" "command": "jq_pretty_json"
},
{
"keys": ["super+r"],
"command": "pretty_json_goto_symbol",
"context": [
{ "key": "selector", "operator": "equal", "operand": "source.json" }
]
} }
] ]

View File

@ -14,5 +14,13 @@
{ {
"keys": ["ctrl+alt+shift+j"], "keys": ["ctrl+alt+shift+j"],
"command": "jq_pretty_json" "command": "jq_pretty_json"
},
{
"keys": ["ctrl+r"],
"command": "pretty_json_goto_symbol",
"context": [
{ "key": "selector", "operator": "equal", "operand": "source.json" }
]
} }
] ]

View File

@ -170,23 +170,6 @@ class PrettyJsonValidate(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
sublime.message_dialog("Invalid JSON") 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): class PrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
""" Pretty Print JSON """ """ Pretty Print JSON """
@ -406,6 +389,58 @@ class JqPrettyJsonOut(sublime_plugin.TextCommand):
self.view.insert(edit, 0, jq_output) 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(): def plugin_loaded():
global s global s
s = sublime.load_settings("Pretty JSON.sublime-settings") s = sublime.load_settings("Pretty JSON.sublime-settings")

25
PrettyJsonListeners.py Normal file
View File

@ -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()

View File

@ -1,6 +1,6 @@
[![Build Status](https://travis-ci.org/dzhibas/SublimePrettyJson.svg)](https://travis-ci.org/dzhibas/SublimePrettyJson) [![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 ## Installation