jq implementation

This commit is contained in:
Nikolajus 2014-04-02 12:44:23 +02:00 committed by Nikolajus Krauklis
parent b32e528209
commit 801cd85220
6 changed files with 77 additions and 3 deletions

View File

@ -4,5 +4,9 @@
"ctrl+alt+j"
],
"command": "pretty_json"
},
{
"keys": ["ctrl+alt+shift+j"],
"command": "jq_pretty_json"
}
]

View File

@ -4,5 +4,9 @@
"super+ctrl+j"
],
"command": "pretty_json"
},
{
"keys": ["super+ctrl+shift+j"],
"command": "jq_pretty_json"
}
]

View File

@ -4,5 +4,9 @@
"ctrl+alt+j"
],
"command": "pretty_json"
},
{
"keys": ["ctrl+alt+shift+j"],
"command": "jq_pretty_json"
}
]

View File

@ -6,5 +6,9 @@
{
"caption": "Pretty JSON: Minify (compress) JSON",
"command": "un_pretty_json"
},
{
"caption": "Pretty JSON: Jq query",
"command": "jq_pretty_json"
}
]

View File

@ -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")

View File

@ -24,6 +24,10 @@ If JSON is not valid it will be displayed in status bar of sublime.
Using Command Palette <kbd>Ctrl+Shift+P</kbd> 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 <kdb>ctrl+atl+shift+j</kdb> you can run against your json. output will be opened in new view
## Default configuration
**use_entire_file_if_no_selection** - true