-
Notifications
You must be signed in to change notification settings - Fork 9
/
JsonTree.py
64 lines (54 loc) · 2.26 KB
/
JsonTree.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sublime, sublime_plugin, json
from collections import OrderedDict
__version__ = '0.1.2'
class JsonTreeCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.keys = []
self.trimkeys = []
content = self.view.substr(sublime.Region(0, self.view.size()))
try:
#json_data = json.loads(content)
json_data = json.JSONDecoder(object_pairs_hook=OrderedDict).decode(content)
self.fill_keys(json_data)
sublime.active_window().show_quick_panel(self.keys, self.goto)
except Exception as ex:
print(ex)
def fill_keys(self, json_data, indent=0):
if isinstance(json_data, OrderedDict):
for key in json_data:
item = json_data[key]
post = '"..."'
if isinstance(item, dict):
post = '{...}'
elif isinstance(item, list):
post = '[...]'
self.keys.append('%s%s: %s' % (' ' * indent, key, post))
self.trimkeys.append('"%s"' % key)
self.fill_keys(json_data[key], indent+3)
elif isinstance(json_data, list):
for index, item in enumerate(json_data):
if isinstance(item, str):
self.keys.append('%s%d. %s' % (' ' * indent, index, item))
self.trimkeys.append('"%s"' % item)
elif isinstance(item, dict):
self.keys.append('%s%d. {...}' % (' ' * indent, index))
self.trimkeys.append('')
self.fill_keys(item, indent+3)
def goto(self, arrpos):
# The position gives us the line number
strgo = self.trimkeys[arrpos]
# Check for matches in the array before 48
found = 0
for index, item in enumerate(self.trimkeys):
if index >= arrpos:
break
if item == strgo:
found += 1
# Now we need to find that string N times in
# the text, and go to its line number
# and highlight it, why not?
regions = self.view.find_all(strgo, sublime.LITERAL)
region = regions[found]
self.view.sel().clear()
self.view.sel().add(region)
self.view.show(region)