parent
466104e77b
commit
a9fab727fd
169
PrettyJson.py
169
PrettyJson.py
|
@ -13,20 +13,20 @@ from .libs.simplejson import OrderedDict
|
|||
|
||||
SUBLIME_MAJOR_VERSION = int(sublime.version()) / 1000
|
||||
|
||||
s = sublime.load_settings("Pretty JSON.sublime-settings")
|
||||
s = sublime.load_settings('Pretty JSON.sublime-settings')
|
||||
|
||||
xml_syntax = "Packages/XML/XML.tmLanguage"
|
||||
json_syntax = "Packages/JSON/JSON.tmLanguage"
|
||||
xml_syntax = 'Packages/XML/XML.tmLanguage'
|
||||
json_syntax = 'Packages/JSON/JSON.tmLanguage'
|
||||
|
||||
jq_exists = False
|
||||
jq_init = False
|
||||
|
||||
""" for OSX we need to manually add brew bin path so jq can be found """
|
||||
if sys.platform != "win32" and "/usr/local/bin" not in os.environ["PATH"]:
|
||||
os.environ["PATH"] += os.pathsep + "/usr/local/bin"
|
||||
''' for OSX we need to manually add brew bin path so jq can be found '''
|
||||
if sys.platform != 'win32' and '/usr/local/bin' not in os.environ['PATH']:
|
||||
os.environ['PATH'] += os.pathsep + '/usr/local/bin'
|
||||
|
||||
""" defer jq presence check until the user tries to use it, include Package "Fix Mac Path" to resolve
|
||||
all homebrew issues (https://github.com/int3h/SublimeFixMacPath) """
|
||||
''' defer jq presence check until the user tries to use it, include Package "Fix Mac Path" to resolve
|
||||
all homebrew issues (https://github.com/int3h/SublimeFixMacPath) '''
|
||||
|
||||
|
||||
def check_jq():
|
||||
|
@ -38,7 +38,7 @@ def check_jq():
|
|||
try:
|
||||
# checking if ./jq tool is available so we can use it
|
||||
s = subprocess.Popen(
|
||||
["jq", "--version"],
|
||||
['jq', '--version'],
|
||||
stdin=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
|
@ -54,7 +54,7 @@ def check_jq():
|
|||
class PrettyJsonBaseCommand:
|
||||
phantom_set = sublime.PhantomSet
|
||||
phantoms = list()
|
||||
json_char_matcher = re.compile(r"char (\d+)")
|
||||
json_char_matcher = re.compile(r'char (\d+)')
|
||||
force_sorting = False
|
||||
|
||||
@staticmethod
|
||||
|
@ -66,20 +66,20 @@ class PrettyJsonBaseCommand:
|
|||
@staticmethod
|
||||
def json_dumps(obj, minified=False):
|
||||
|
||||
sort_keys = s.get("sort_keys", False)
|
||||
sort_keys = s.get('sort_keys', False)
|
||||
if PrettyJsonBaseCommand.force_sorting:
|
||||
sort_keys = True
|
||||
|
||||
line_separator = s.get("line_separator", ",")
|
||||
value_separator = s.get("value_separator", ": ")
|
||||
line_separator = s.get('line_separator', ',')
|
||||
value_separator = s.get('value_separator', ': ')
|
||||
if minified:
|
||||
line_separator = line_separator.strip()
|
||||
value_separator = value_separator.strip()
|
||||
|
||||
output_json = json.dumps(
|
||||
obj,
|
||||
indent=None if minified else s.get("indent", 2),
|
||||
ensure_ascii=s.get("ensure_ascii", False),
|
||||
indent=None if minified else s.get('indent', 2),
|
||||
ensure_ascii=s.get('ensure_ascii', False),
|
||||
sort_keys=sort_keys,
|
||||
separators=(line_separator, value_separator),
|
||||
use_decimal=True,
|
||||
|
@ -87,20 +87,17 @@ class PrettyJsonBaseCommand:
|
|||
if minified:
|
||||
return output_json
|
||||
|
||||
# do we need try and shuffle things around ?
|
||||
post_process = s.get("keep_arrays_single_line", False)
|
||||
|
||||
post_process = s.get('keep_arrays_single_line', False)
|
||||
if post_process:
|
||||
# find all array matches
|
||||
matches = re.findall(r"(\[[^\[\]]+?\])", output_json)
|
||||
matches = re.findall(r'(\[[^\[\]]+?\])', output_json)
|
||||
matches.sort(key=len, reverse=True)
|
||||
join_separator = line_separator.ljust(2)
|
||||
for m in matches:
|
||||
content = m[1:-1]
|
||||
items = [a.strip() for a in content.split(line_separator.strip())]
|
||||
replacement = "[" + join_separator.join(items) + "]"
|
||||
# if line not gets too long, replace with single line
|
||||
if len(replacement) <= s.get("max_arrays_line_length", 120):
|
||||
|
||||
replacement = '[' + join_separator.join(items) + ']'
|
||||
if len(replacement) <= s.get('max_arrays_line_length', 120):
|
||||
output_json = output_json.replace(m, replacement, 1)
|
||||
|
||||
return output_json
|
||||
|
@ -109,43 +106,31 @@ class PrettyJsonBaseCommand:
|
|||
current_line = self.view.line(selection.begin())
|
||||
text_before_sel = sublime.Region(current_line.begin(), selection.begin())
|
||||
|
||||
reindent_mode = s.get("reindent_block", "minimal")
|
||||
|
||||
if reindent_mode == "start":
|
||||
# Reindent to the column where the selection starts
|
||||
reindent_mode = s.get('reindent_block', 'minimal')
|
||||
if reindent_mode == 'start':
|
||||
space_number = text_before_sel.size()
|
||||
indent_space = " " * space_number
|
||||
indent_space = ' ' * space_number
|
||||
else:
|
||||
# Reindent to the number of spaces to the left of the
|
||||
# line where the selection starts
|
||||
indent_space = re.search('^\s*', self.view.substr(text_before_sel)).group(0)
|
||||
|
||||
# Extracts the spaces at the start of the line to use them
|
||||
# as padding
|
||||
indent_space = re.search("^\s*", self.view.substr(text_before_sel)).group(0)
|
||||
lines = text.split('\n')
|
||||
|
||||
lines = text.split("\n")
|
||||
|
||||
# Pad every line except the first one
|
||||
i = 1
|
||||
while i < len(lines):
|
||||
lines[i] = indent_space + lines[i]
|
||||
i += 1
|
||||
|
||||
return "\n".join(lines)
|
||||
return '\n'.join(lines)
|
||||
|
||||
def show_exception(self, region, msg):
|
||||
sublime.status_message("[Error]: {}".format(msg))
|
||||
sublime.status_message('[Error]: {}'.format(msg))
|
||||
if region is None:
|
||||
sublime.message_dialog(msg)
|
||||
return
|
||||
self.highlight_error(region=region, message="{}".format(msg))
|
||||
self.highlight_error(region=region, message='{}'.format(msg))
|
||||
|
||||
def highlight_error(self, region, message):
|
||||
# TODO: Reimplement
|
||||
# - Move this into a global handler
|
||||
# - so that we can generate multiple phantoms
|
||||
# - across multi selections
|
||||
self.phantom_set = sublime.PhantomSet(self.view, "json_errors")
|
||||
self.phantom_set = sublime.PhantomSet(self.view, 'json_errors')
|
||||
|
||||
char_match = self.json_char_matcher.search(message)
|
||||
if char_match:
|
||||
|
@ -159,19 +144,19 @@ class PrettyJsonBaseCommand:
|
|||
self.phantoms.append(
|
||||
sublime.Phantom(
|
||||
region,
|
||||
self.create_phantom_html(message, "error"),
|
||||
self.create_phantom_html(message, 'error'),
|
||||
sublime.LAYOUT_BELOW,
|
||||
self.navigation,
|
||||
)
|
||||
)
|
||||
self.phantom_set.update(self.phantoms)
|
||||
self.view.set_status("json_errors", message)
|
||||
self.view.set_status('json_errors', message)
|
||||
|
||||
# Description: Taken from https://github.com/sublimelsp/LSP/blob/master/plugin/diagnostics.py
|
||||
# - Thanks to the LSP Team
|
||||
def create_phantom_html(self, content: str, severity: str) -> str:
|
||||
stylesheet = sublime.load_resource("Packages/SublimePrettyJson/phantom.css")
|
||||
return """<body id=inline-error>
|
||||
stylesheet = sublime.load_resource('Packages/SublimePrettyJson/phantom.css')
|
||||
return '''<body id=inline-error>
|
||||
<style>{}</style>
|
||||
<div class="{}-arrow"></div>
|
||||
<div class="{} container">
|
||||
|
@ -180,24 +165,23 @@ class PrettyJsonBaseCommand:
|
|||
</div>
|
||||
<div class="content">{}</div>
|
||||
</div>
|
||||
</body>""".format(
|
||||
</body>'''.format(
|
||||
stylesheet, severity, severity, content
|
||||
)
|
||||
|
||||
def navigation(self, href):
|
||||
if href == "hide":
|
||||
self.clear_phantoms()
|
||||
self.clear_phantoms()
|
||||
|
||||
def clear_phantoms(self):
|
||||
if isinstance(self.phantom_set, type):
|
||||
self.phantom_set = sublime.PhantomSet(self.view, "json_errors")
|
||||
self.phantom_set = sublime.PhantomSet(self.view, 'json_errors')
|
||||
|
||||
self.phantoms = list()
|
||||
self.phantom_set.update(self.phantoms)
|
||||
|
||||
def syntax_to_json(self):
|
||||
""" Changes syntax to JSON if its in plain text """
|
||||
if "Plain text" in self.view.settings().get("syntax"):
|
||||
' Changes syntax to JSON if its in plain text '
|
||||
if 'Plain text' in self.view.settings().get('syntax'):
|
||||
self.view.set_syntax_file(json_syntax)
|
||||
|
||||
|
||||
|
@ -208,7 +192,7 @@ class PrettyJsonValidate(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
|
|||
for region in regions:
|
||||
if region.empty() and len(regions) > 1:
|
||||
continue
|
||||
elif region.empty() and s.get("use_entire_file_if_no_selection", True):
|
||||
elif region.empty() and s.get('use_entire_file_if_no_selection', True):
|
||||
selection = sublime.Region(0, self.view.size())
|
||||
region = sublime.Region(0, self.view.size())
|
||||
else:
|
||||
|
@ -216,15 +200,15 @@ class PrettyJsonValidate(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
|
|||
|
||||
try:
|
||||
self.json_loads(self.view.substr(selection))
|
||||
sublime.status_message("JSON is Valid")
|
||||
sublime.status_message('JSON is Valid')
|
||||
except Exception as ex:
|
||||
self.show_exception(region=region, msg=ex)
|
||||
|
||||
|
||||
class PrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
|
||||
"""
|
||||
'''
|
||||
Description: Pretty Print JSON
|
||||
"""
|
||||
'''
|
||||
|
||||
def run(self, edit):
|
||||
PrettyJsonBaseCommand.clear_phantoms(self)
|
||||
|
@ -233,7 +217,7 @@ class PrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
|
|||
selected_entire_file = False
|
||||
if region.empty() and len(regions) > 1:
|
||||
continue
|
||||
elif region.empty() and s.get("use_entire_file_if_no_selection", True):
|
||||
elif region.empty() and s.get('use_entire_file_if_no_selection', True):
|
||||
selection = sublime.Region(0, self.view.size())
|
||||
region = sublime.Region(0, self.view.size())
|
||||
selected_entire_file = True
|
||||
|
@ -245,8 +229,7 @@ class PrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
|
|||
obj = self.json_loads(selection_text)
|
||||
|
||||
json_text = self.json_dumps(obj=obj, minified=False)
|
||||
|
||||
if not selected_entire_file and s.get("reindent_block", False):
|
||||
if not selected_entire_file and s.get('reindent_block', False):
|
||||
json_text = self.reindent(json_text, selection)
|
||||
|
||||
self.view.replace(edit, selection, json_text)
|
||||
|
@ -270,7 +253,7 @@ class PrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
|
|||
obj = self.json_loads(selection_text_modified)
|
||||
json_text = self.json_dumps(obj=obj, minified=False)
|
||||
|
||||
if not selected_entire_file and s.get("reindent_block", False):
|
||||
if not selected_entire_file and s.get('reindent_block', False):
|
||||
json_text = self.reindent(json_text, selection)
|
||||
|
||||
self.view.replace(edit, selection, json_text)
|
||||
|
@ -283,9 +266,9 @@ class PrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
|
|||
|
||||
|
||||
class PrettyJsonAndSortCommand(PrettyJsonCommand, sublime_plugin.TextCommand):
|
||||
"""
|
||||
'''
|
||||
Description: Pretty print json with forced sorting
|
||||
"""
|
||||
'''
|
||||
|
||||
def run(self, edit):
|
||||
PrettyJsonBaseCommand.force_sorting = True
|
||||
|
@ -294,9 +277,9 @@ class PrettyJsonAndSortCommand(PrettyJsonCommand, sublime_plugin.TextCommand):
|
|||
|
||||
|
||||
class UnPrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
|
||||
"""
|
||||
'''
|
||||
Description: Compress/minify JSON - it makes json as one-liner
|
||||
"""
|
||||
'''
|
||||
|
||||
def run(self, edit):
|
||||
PrettyJsonBaseCommand.clear_phantoms(self)
|
||||
|
@ -325,9 +308,9 @@ class UnPrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
|
|||
|
||||
|
||||
class JqPrettyJson(sublime_plugin.WindowCommand):
|
||||
"""
|
||||
'''
|
||||
Description: Allows work with ./jq
|
||||
"""
|
||||
'''
|
||||
|
||||
def run(self):
|
||||
check_jq()
|
||||
|
@ -341,7 +324,7 @@ class JqPrettyJson(sublime_plugin.WindowCommand):
|
|||
)
|
||||
|
||||
def get_content(self):
|
||||
""" returns content of active view or selected region """
|
||||
''' returns content of active view or selected region '''
|
||||
view = self.window.active_view()
|
||||
selection = str()
|
||||
for region in view.sel():
|
||||
|
@ -375,19 +358,17 @@ class JqPrettyJson(sublime_plugin.WindowCommand):
|
|||
|
||||
|
||||
class JsonToXml(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
|
||||
"""
|
||||
'''
|
||||
Description: converts Json to XML
|
||||
"""
|
||||
'''
|
||||
|
||||
def run(self, edit):
|
||||
""" overwriting base class run function to remove intent """
|
||||
self.view.erase_regions("json_errors")
|
||||
self.view.erase_regions('json_errors')
|
||||
for region in self.view.sel():
|
||||
|
||||
selected_entire_file = False
|
||||
|
||||
# If no selection, use the entire file as the selection
|
||||
if region.empty() and s.get("use_entire_file_if_no_selection", True):
|
||||
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:
|
||||
|
@ -395,22 +376,22 @@ class JsonToXml(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
|
|||
|
||||
try:
|
||||
h = json.loads(self.view.substr(selection))
|
||||
root = ElementTree.Element("root")
|
||||
root = ElementTree.Element('root')
|
||||
root = self.traverse(root, h)
|
||||
|
||||
xml_string = "<?xml version='1.0' encoding='UTF-8' ?>\n"
|
||||
xml_string = '<?xml version=\'1.0\' encoding=\'UTF-8\' ?>\n'
|
||||
|
||||
rtn = ElementTree.tostring(root, "utf-8")
|
||||
rtn = ElementTree.tostring(root, 'utf-8')
|
||||
|
||||
if type(rtn) is bytes:
|
||||
rtn = rtn.decode("utf-8")
|
||||
rtn = rtn.decode('utf-8')
|
||||
|
||||
xml_string += rtn
|
||||
|
||||
if type(xml_string) is bytes:
|
||||
xml_string = xml_string.decode("utf-8")
|
||||
xml_string = xml_string.decode('utf-8')
|
||||
|
||||
if not selected_entire_file and s.get("reindent_block", False):
|
||||
if not selected_entire_file and s.get('reindent_block', False):
|
||||
xml_string = self.reindent(xml_string, selection)
|
||||
|
||||
self.view.replace(edit, selection, xml_string)
|
||||
|
@ -422,23 +403,23 @@ class JsonToXml(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
|
|||
self.show_exception(region=region, msg=ex)
|
||||
|
||||
def traverse(self, element, json_dict):
|
||||
""" recursive traverse through dict and build xml tree """
|
||||
''' recursive traverse through dict and build xml tree '''
|
||||
if type(json_dict) is dict and json_dict.keys():
|
||||
for i in json_dict.keys():
|
||||
e = ElementTree.Element(i)
|
||||
element.append(self.traverse(e, json_dict[i]))
|
||||
elif type(json_dict) is list:
|
||||
e_items = ElementTree.Element("items")
|
||||
e_items = ElementTree.Element('items')
|
||||
for i in json_dict:
|
||||
e_items.append(self.traverse(ElementTree.Element("item"), i))
|
||||
e_items.append(self.traverse(ElementTree.Element('item'), i))
|
||||
element.append(e_items)
|
||||
else:
|
||||
element.set("value", str(json_dict))
|
||||
element.set('value', str(json_dict))
|
||||
|
||||
return element
|
||||
|
||||
def syntax_to_xml(self):
|
||||
""" change syntax to xml """
|
||||
''' change syntax to xml '''
|
||||
self.view.set_syntax_file(xml_syntax)
|
||||
|
||||
|
||||
|
@ -463,15 +444,15 @@ class PrettyJsonGotoSymbolCommand(PrettyJsonBaseCommand, sublime_plugin.TextComm
|
|||
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("{}".format(new_key_name))
|
||||
self.goto_items.append('"{}"'.format(key))
|
||||
new_key_name = f'{root_key}.{key}'
|
||||
self.items.append(f'"{new_key_name}"')
|
||||
self.goto_items.append(f'"{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("{}.{}".format(root_key, item))
|
||||
self.goto_items.append('"{}"'.format(item))
|
||||
self.items.append(f'{root_key}.{item}')
|
||||
self.goto_items.append(f'"{item}"')
|
||||
|
||||
def goto(self, pos):
|
||||
string_to_search = self.goto_items[pos]
|
||||
|
@ -486,8 +467,8 @@ class PrettyJsonGotoSymbolCommand(PrettyJsonBaseCommand, sublime_plugin.TextComm
|
|||
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(":")
|
||||
if ':' in line:
|
||||
split = line.split(':')
|
||||
val = split[1].strip()
|
||||
if string_to_search in val:
|
||||
del regions[i]
|
||||
|
@ -500,4 +481,4 @@ class PrettyJsonGotoSymbolCommand(PrettyJsonBaseCommand, sublime_plugin.TextComm
|
|||
|
||||
def plugin_loaded():
|
||||
global s
|
||||
s = sublime.load_settings("Pretty JSON.sublime-settings")
|
||||
s = sublime.load_settings('Pretty JSON.sublime-settings')
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
[tool.black]
|
||||
skip_string_normalization: True
|
Loading…
Reference in New Issue