#68 ~ adding separate menu item to force sorting on demand

This commit is contained in:
Nikolajus 2016-07-14 17:02:25 +02:00
parent ae2e94bda3
commit 6d08a51286
2 changed files with 25 additions and 2 deletions

View File

@ -3,6 +3,10 @@
"caption": "Pretty JSON: Format (Pretty Print) JSON", "caption": "Pretty JSON: Format (Pretty Print) JSON",
"command": "pretty_json" "command": "pretty_json"
}, },
{
"caption": "Pretty JSON: Format and Sort JSON",
"command": "pretty_json_and_sort"
},
{ {
"caption": "Pretty JSON: Minify (compress) JSON", "caption": "Pretty JSON: Minify (compress) JSON",
"command": "un_pretty_json" "command": "un_pretty_json"

View File

@ -54,6 +54,7 @@ s = sublime.load_settings("Pretty JSON.sublime-settings")
class PrettyJsonBaseCommand(sublime_plugin.TextCommand): class PrettyJsonBaseCommand(sublime_plugin.TextCommand):
json_error_matcher = re.compile(r"line (\d+) column (\d+) \(char (\d+)\)") json_error_matcher = re.compile(r"line (\d+) column (\d+) \(char (\d+)\)")
force_sorting = False
@staticmethod @staticmethod
def json_loads(selection): def json_loads(selection):
@ -63,10 +64,15 @@ class PrettyJsonBaseCommand(sublime_plugin.TextCommand):
@staticmethod @staticmethod
def json_dumps(obj): def json_dumps(obj):
sort_keys = s.get("sort_keys", False)
if PrettyJsonBaseCommand.force_sorting:
sort_keys = True
return json.dumps(obj, return json.dumps(obj,
indent=s.get("indent", 2), indent=s.get("indent", 2),
ensure_ascii=s.get("ensure_ascii", False), ensure_ascii=s.get("ensure_ascii", False),
sort_keys=s.get("sort_keys", False), sort_keys=sort_keys,
separators=(s.get("line_separator", ","), s.get("value_separator", ": ")), separators=(s.get("line_separator", ","), s.get("value_separator", ": ")),
use_decimal=True) use_decimal=True)
@ -77,9 +83,13 @@ class PrettyJsonBaseCommand(sublime_plugin.TextCommand):
value_separator = s.get("value_separator", ": ") value_separator = s.get("value_separator", ": ")
""":type : str""" """:type : str"""
sort_keys = s.get("sort_keys", False)
if PrettyJsonBaseCommand.force_sorting:
sort_keys = True
return json.dumps(obj, return json.dumps(obj,
ensure_ascii=s.get("ensure_ascii", False), ensure_ascii=s.get("ensure_ascii", False),
sort_keys=s.get("sort_keys", False), sort_keys=sort_keys,
separators=(line_separator.strip(), value_separator.strip()), separators=(line_separator.strip(), value_separator.strip()),
use_decimal=True) use_decimal=True)
@ -150,6 +160,15 @@ class PrettyJsonCommand(PrettyJsonBaseCommand):
self.show_exception() self.show_exception()
class PrettyJsonAndSortCommand(PrettyJsonCommand):
""" Pretty print json with forced sorting """
def run(self, edit):
PrettyJsonBaseCommand.force_sorting = True
PrettyJsonCommand.run(self, edit)
PrettyJsonBaseCommand.force_sorting = False
class UnPrettyJsonCommand(PrettyJsonBaseCommand): class UnPrettyJsonCommand(PrettyJsonBaseCommand):
""" Compress/minify JSON - it makes json as one-liner """ """ Compress/minify JSON - it makes json as one-liner """