Last active
December 12, 2015 04:58
-
-
Save markwatson/4717764 to your computer and use it in GitHub Desktop.
A sublime text plugin that pretty formats all the select JSON blobs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime, sublime_plugin | |
import json | |
import re | |
class PrettyJsonCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
re_json_p = re.compile(r'^\s*(\w+)\s*\(\s*(.*)\s*\)\s*$') | |
try: | |
for region in self.view.sel(): | |
if not region.empty(): | |
# Get the selected text | |
s = self.view.substr(region) | |
# Strip out JSONP | |
func = None | |
m = re_json_p.match(s) | |
if m: | |
func = m.group(1) | |
s = m.group(2) | |
# Transform it | |
s = json.dumps(json.loads(s), sort_keys=True, indent=4) | |
# Add back the JSONP | |
if func is not None: | |
s = func + '(' + s + ')' | |
# Replace the selection with transformed text | |
self.view.replace(edit, region, s) | |
except Exception, e: | |
sublime.error_message("Error making JSON pretty: " + str(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment