diff --git a/Default (Linux).sublime-keymap b/Default (Linux).sublime-keymap index e655c91..6c83c6e 100644 --- a/Default (Linux).sublime-keymap +++ b/Default (Linux).sublime-keymap @@ -12,4 +12,4 @@ // { "key": "selector", "operator": "equal", "operand": "source.json" } // ] // } -] \ No newline at end of file +] diff --git a/Default.sublime-commands b/Default.sublime-commands index 91dd67f..2a90553 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -28,12 +28,12 @@ "command": "pretty_json_validate" }, { - "caption": "Preferences: Pretty JSON Settings", - "command": "edit_settings", "args": { "base_file": "${packages}/Pretty JSON/Pretty JSON.sublime-settings", "default": "{\n\t$0\n}\n" - } + }, + "caption": "Preferences: Pretty JSON Settings", + "command": "edit_settings" } ] \ No newline at end of file diff --git a/Main.sublime-menu b/Main.sublime-menu index 3751f65..475f4ae 100644 --- a/Main.sublime-menu +++ b/Main.sublime-menu @@ -1,25 +1,29 @@ [ -{ - "id": "preferences", - "children": [ { - "id": "package-settings", "children": [ - { - "caption": "Pretty JSON", - "children": [ { - "caption": "Settings", - "command": "edit_settings", - "args": - { - "base_file": "${packages}/Pretty JSON/Pretty JSON.sublime-settings", - "default": "{\n\t$0\n}\n", - } - }, - { - "caption": "-" - }] - }] - }] -}] \ No newline at end of file + "children": [ + { + "caption": "Pretty JSON", + "children": [ + { + "args": + { + "base_file": "${packages}/Pretty JSON/Pretty JSON.sublime-settings", + "default": "{\n\t$0\n}\n" + }, + "caption": "Settings", + "command": "edit_settings" + }, + { + "caption": "-" + } + ] + } + ], + "id": "package-settings" + } + ], + "id": "preferences" + } +] \ No newline at end of file diff --git a/Pretty JSON.sublime-settings b/Pretty JSON.sublime-settings index efe77e8..ae4e31d 100644 --- a/Pretty JSON.sublime-settings +++ b/Pretty JSON.sublime-settings @@ -18,10 +18,10 @@ // Example: /usr/bin/local/jq "jq_binary": "jq", "jq_errors": false, - "as_json":[ + "as_json": [ "Packages/JSON/JSON.sublime-syntax", "Packages/PackageDev/Package/Sublime Text Commands/Sublime Text Commands.sublime-syntax", "Packages/PackageDev/Package/Sublime Text Settings/Sublime Text Settings.sublime-syntax", "Packages/PackageDev/Package/Sublime Text Menu/Sublime Text Menu.sublime-syntax" - ] -} \ No newline at end of file + ] +} diff --git a/PrettyJson.py b/PrettyJson.py index b52cd34..ab3b98a 100644 --- a/PrettyJson.py +++ b/PrettyJson.py @@ -55,11 +55,11 @@ class PrettyJsonBaseCommand: ) @staticmethod - def json_dumps(obj, minified: bool = False) -> str: + def json_dumps(obj, minified: bool = False, force_sorting: bool = False) -> str: settings = sublime.load_settings("Pretty JSON.sublime-settings") sort_keys = settings.get("sort_keys", False) - if PrettyJsonBaseCommand.force_sorting: + if force_sorting: sort_keys = True line_separator = settings.get("line_separator", ",") @@ -87,7 +87,20 @@ class PrettyJsonBaseCommand: content = m[1:-1].strip() items = [a.strip() for a in content.split(os.linesep)] items = [item[:-1] if item[-1] == "," else item for item in items] - replacement = f"[{join_separator.join(items)}]" + replacement = "[" + for index, item in enumerate(items): + if item in ('{', '}') or item.endswith("{") or item.startswith("}"): + replacement = replacement + item + if item == '}': + if index != len(items)-1 and items[index+1] != "}": + replacement = replacement + ',' + else: + replacement = replacement + item + if index != len(items)-1: + if items[index+1] != '}': + replacement = replacement + ',' + replacement = replacement + ']' + if len(replacement) <= settings.get("max_arrays_line_length", 120): output_json = output_json.replace(m, replacement, 1) @@ -257,8 +270,7 @@ class PrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand): selection_text = self.view.substr(region) try: obj = self.json_loads(selection_text) - - json_text = self.json_dumps(obj=obj, minified=False) + json_text = self.json_dumps(obj=obj, minified=False, force_sorting=self.force_sorting) if not entire_file and settings.get("reindent_block", False): json_text = self.reindent(json_text, region) diff --git a/README.md b/README.md index 0b4c04c..38a854a 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,11 @@ you can add a setting like this to your .sublime-keymap file { "keys": [ "ctrl+alt+m" ], "command": "un_pretty_json" } ``` +#### List of commands that can be mapped to shortcuts +- `pretty_json` +- `un_pretty_json` +- `pretty_json_goto_symbol` + ### Convert JSON to XML Using Command Palette Ctrl+Shift+P search for diff --git a/phantom.css b/phantom.css index 5fbf927..e40ba08 100644 --- a/phantom.css +++ b/phantom.css @@ -33,4 +33,4 @@ html.dark div.toolbar { } html.light div.toolbar { background-color: #ffffff18; -} \ No newline at end of file +} diff --git a/pyproject.toml b/pyproject.toml deleted file mode 100644 index d41a7e6..0000000 --- a/pyproject.toml +++ /dev/null @@ -1,2 +0,0 @@ -[tool.black] -skip_string_normalization: True \ No newline at end of file