diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..bdd4ea2 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +/tests export-ignore \ No newline at end of file diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..98fccd6 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.8 \ No newline at end of file diff --git a/Default.sublime-commands b/Default.sublime-commands index 90635aa..518c124 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -12,7 +12,7 @@ "command": "un_pretty_json" }, { - "caption": "Pretty JSON: JSON 2 XML", + "caption": "Pretty JSON: json2xml", "command": "json_to_xml" }, { @@ -24,17 +24,11 @@ "command": "pretty_json_validate" }, { - "caption": "Preferences: Pretty JSON Settings - User", - "command": "open_file", "args": - { - "file": "${packages}/User/Pretty JSON.sublime-settings" - } - }, - { - "caption": "Preferences: Pretty JSON Settings - Default", - "command": "open_file", "args": - { - "file": "${packages}/Pretty JSON/Pretty JSON.sublime-settings" + "caption": "Preferences: Pretty JSON Settings", + "command": "edit_settings", + "args": { + "base_file": "${packages}/Pretty JSON/Pretty JSON.sublime-settings", + "default": "{\n\t$0\n}\n" } } ] \ No newline at end of file diff --git a/Main.sublime-menu b/Main.sublime-menu index b234a57..ac69ad9 100644 --- a/Main.sublime-menu +++ b/Main.sublime-menu @@ -1,38 +1,29 @@ [ +{ + "mnemonic": "n", + "caption": "Preferences", + "id": "preferences", + "children": [ { - "mnemonic": "n", - "caption": "Preferences", - "id": "preferences", + "mnemonic": "P", + "caption": "Package Settings", + "id": "package-settings", "children": [ + { + "caption": "Pretty JSON", + "children": [ { - "mnemonic": "P", - "caption": "Package Settings", - "id": "package-settings", - "children": [ - { - "caption": "Pretty JSON", - "children": [ - { - "caption": "Settings – Default", - "args": { - "file": "${packages}/Pretty JSON/Pretty JSON.sublime-settings" - }, - "command": "open_file" - }, - { - "caption": "Settings – User", - "args": { - "file": "${packages}/User/Pretty JSON.sublime-settings" - }, - "command": "open_file" - }, - { - "caption": "-" - } - ] - } - ] - } - ] - } -] \ No newline at end of file + "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 diff --git a/Pretty JSON.sublime-settings b/Pretty JSON.sublime-settings index 420a0ee..d075273 100644 --- a/Pretty JSON.sublime-settings +++ b/Pretty JSON.sublime-settings @@ -9,5 +9,9 @@ "max_arrays_line_length": 120, "pretty_on_save": false, "validate_on_save": true, - "reindent_block": false + "reindent_block": false, + // Name or Path to jq binary + // Example: /usr/bin/local/jq + // Example: jq + "jq_binary": "jq" } \ No newline at end of file diff --git a/PrettyJson.py b/PrettyJson.py index f8fad39..8308f7f 100644 --- a/PrettyJson.py +++ b/PrettyJson.py @@ -2,14 +2,15 @@ import decimal import os import re import subprocess +import shutil import sys from xml.etree import ElementTree import sublime import sublime_plugin -from .libs import simplejson as json -from .libs.simplejson import OrderedDict +from .lib import simplejson as json +from .lib.simplejson import OrderedDict SUBLIME_MAJOR_VERSION = int(sublime.version()) / 1000 @@ -20,34 +21,20 @@ 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' - -''' 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) ''' +jq_path = str() def check_jq(): - global jq_exists - global jq_init + global jq_init, jq_exists, jq_path if not jq_init: jq_init = True + jq_test = s.get('jq_binary', 'jq') try: - # checking if ./jq tool is available so we can use it - s = subprocess.Popen( - ['jq', '--version'], - stdin=subprocess.PIPE, - stderr=subprocess.PIPE, - stdout=subprocess.PIPE, - ) - out, err = s.communicate() + jq_path = shutil.which(jq_test) jq_exists = True - except OSError: - os_exception = sys.exc_info()[1] - print(str(os_exception)) + except OSError as ex: + print(str(ex)) jq_exists = False @@ -58,14 +45,13 @@ class PrettyJsonBaseCommand: force_sorting = False @staticmethod - def json_loads(selection): + def json_loads(selection: str) -> dict: return json.loads( selection, object_pairs_hook=OrderedDict, parse_float=decimal.Decimal ) @staticmethod - def json_dumps(obj, minified=False): - + def json_dumps(obj, minified: bool = False) -> str: sort_keys = s.get('sort_keys', False) if PrettyJsonBaseCommand.force_sorting: sort_keys = True @@ -102,7 +88,7 @@ class PrettyJsonBaseCommand: return output_json - def reindent(self, text, selection): + def reindent(self, text: str, selection: str): current_line = self.view.line(selection.begin()) text_before_sel = sublime.Region(current_line.begin(), selection.begin()) @@ -122,14 +108,14 @@ class PrettyJsonBaseCommand: return '\n'.join(lines) - def show_exception(self, region, msg): - sublime.status_message('[Error]: {}'.format(msg)) + def show_exception(self, region: sublime.Region, msg): + sublime.status_message(f'[Error]: {msg}') if region is None: sublime.message_dialog(msg) return - self.highlight_error(region=region, message='{}'.format(msg)) + self.highlight_error(region=region, message=f'{msg}') - def highlight_error(self, region, message): + def highlight_error(self, region: sublime.Region, message: str): self.phantom_set = sublime.PhantomSet(self.view, 'json_errors') char_match = self.json_char_matcher.search(message) @@ -155,21 +141,19 @@ class PrettyJsonBaseCommand: # 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 '''
- - -