diff --git a/Default (Linux).sublime-keymap b/Default (Linux).sublime-keymap index 03f2957..003be3d 100644 --- a/Default (Linux).sublime-keymap +++ b/Default (Linux).sublime-keymap @@ -4,5 +4,9 @@ "ctrl+alt+j" ], "command": "pretty_json" + }, + { + "keys": ["ctrl+alt+shift+j"], + "command": "jq_pretty_json" } ] \ No newline at end of file diff --git a/Default (OSX).sublime-keymap b/Default (OSX).sublime-keymap index df5aa79..3910913 100644 --- a/Default (OSX).sublime-keymap +++ b/Default (OSX).sublime-keymap @@ -4,5 +4,9 @@ "super+ctrl+j" ], "command": "pretty_json" + }, + { + "keys": ["super+ctrl+shift+j"], + "command": "jq_pretty_json" } ] \ No newline at end of file diff --git a/Default (Windows).sublime-keymap b/Default (Windows).sublime-keymap index 03f2957..003be3d 100644 --- a/Default (Windows).sublime-keymap +++ b/Default (Windows).sublime-keymap @@ -4,5 +4,9 @@ "ctrl+alt+j" ], "command": "pretty_json" + }, + { + "keys": ["ctrl+alt+shift+j"], + "command": "jq_pretty_json" } ] \ No newline at end of file diff --git a/Default.sublime-commands b/Default.sublime-commands index e83d70f..1b9c623 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -6,5 +6,9 @@ { "caption": "Pretty JSON: Minify (compress) JSON", "command": "un_pretty_json" + }, + { + "caption": "Pretty JSON: Jq query", + "command": "jq_pretty_json" } ] \ No newline at end of file diff --git a/PrettyJson.py b/PrettyJson.py index b16544b..a34ad2f 100644 --- a/PrettyJson.py +++ b/PrettyJson.py @@ -1,6 +1,7 @@ import sublime import sublime_plugin import decimal +import sys try: # python 3 / Sublime Text 3 @@ -13,6 +14,20 @@ except (ValueError): s = sublime.load_settings("Pretty JSON.sublime-settings") +jq_exits = False +jq_version = None + +import subprocess + +try: + # checking if jq tool is available in PATH so we can use it + s = subprocess.Popen(["jq", "--version"], stderr=subprocess.PIPE, stdout=subprocess.PIPE) + out, err = s.communicate() + jq_version = err.decode("utf-8").replace("jq version ", "").strip() + jq_exits = True +except OSError: + jq_exits = False + class PrettyJsonCommand(sublime_plugin.TextCommand): """ Pretty Print JSON """ @@ -44,7 +59,6 @@ class PrettyJsonCommand(sublime_plugin.TextCommand): self.change_syntax() except Exception: - import sys exc = sys.exc_info()[1] sublime.status_message(str(exc)) @@ -87,10 +101,50 @@ class UnPrettyJsonCommand(PrettyJsonCommand): self.change_syntax() except Exception: - import sys exc = sys.exc_info()[1] sublime.status_message(str(exc)) + +class JqPrettyJson(sublime_plugin.WindowCommand): + """ + Allows work with jq + """ + def run(self): + self.window.show_input_panel("Enter JQ query", ".", on_done=self.done, on_change=None, on_cancel=None) + + def get_content(self): + view = self.window.active_view() + selection = "" + for region in view.sel(): + # If no selection, use the entire file as the selection + if region.empty(): + selection = sublime.Region(0, view.size()) + else: + selection = region + return view.substr(selection) + + def done(self, query): + try: + p = subprocess.Popen(["jq", query], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) + raw_json = self.get_content() + out, err = p.communicate(bytes(raw_json, "utf-8")) + output = out.decode("UTF-8").strip() + + if output: + view = self.window.new_file() + view.run_command("jq_pretty_json_out", {"jq_output": output}) + view.set_syntax_file("Packages/JavaScript/JSON.tmLanguage") + + except OSError: + exc = sys.exc_info()[1] + sublime.status_message(str(exc)) + + +class JqPrettyJsonOut(sublime_plugin.TextCommand): + def run(self, edit, jq_output=''): + self.view.insert(edit, 0, jq_output) + + def plugin_loaded(): global s - s = sublime.load_settings("Pretty JSON.sublime-settings") + s = sublime.load_settings("Pretty JSON.sublime-settings") \ No newline at end of file diff --git a/README.md b/README.md index 96dc780..209525c 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,10 @@ If JSON is not valid it will be displayed in status bar of sublime. Using Command Palette Ctrl+Shift+P find "Pretty JSON: Minify (compress) JSON" this will make selection or full buffer as single line JSON which later you can use in command lines or somewhere else +## jQ usage + +if on your machine "jq" tool is available with ctrl+atl+shift+j you can run against your json. output will be opened in new view + ## Default configuration **use_entire_file_if_no_selection** - true