#47 ~ adding support for folding array into single line if they are not reaching more than 120chars in a line

This commit is contained in:
Nikolajus 2016-07-14 18:26:13 +02:00
parent b7f27df97e
commit 04296b2d13
3 changed files with 29 additions and 7 deletions

View File

@ -4,5 +4,6 @@
"sort_keys": false,
"ensure_ascii": false,
"line_separator": ",",
"value_separator": ": "
"value_separator": ": ",
"keep_arrays_single_line": false
}

View File

@ -69,12 +69,31 @@ class PrettyJsonBaseCommand(sublime_plugin.TextCommand):
if PrettyJsonBaseCommand.force_sorting:
sort_keys = True
return json.dumps(obj,
indent=s.get("indent", 2),
ensure_ascii=s.get("ensure_ascii", False),
sort_keys=sort_keys,
separators=(s.get("line_separator", ","), s.get("value_separator", ": ")),
use_decimal=True)
line_separator = s.get("line_separator", ",")
value_separator = s.get("value_separator", ": ")
output_json = json.dumps(obj,
indent=s.get("indent", 2),
ensure_ascii=s.get("ensure_ascii", False),
sort_keys=sort_keys,
separators=(line_separator, value_separator),
use_decimal=True)
# do we need try and shuffle things around ?
post_process = s.get("keep_arrays_single_line", False)
if post_process:
# find all array matches
matches = re.findall(r"\[([^\[\]]+?)\]", output_json)
join_separator = line_separator.ljust(2)
for m in matches:
items = [a.strip() for a in m.split(line_separator.strip())]
replacement = join_separator.join(items)
# if line not gets too long, replace with single line
if len(replacement) <= 120:
output_json = output_json.replace(m, replacement)
return output_json
@staticmethod
def json_dumps_minified(obj):

View File

@ -69,6 +69,8 @@ http://stedolan.github.io/jq/
**value_separator** - ": "
**keep_arrays_single_line** - false
## Using tabs for indentation
You can change configuration key **indent** to string value "\t" or any other string