From 5021a2d37c9acc319ddee452488192c43e01dbbf Mon Sep 17 00:00:00 2001 From: Taylor <03283812@pepaul00302.corp.pep.pvt> Date: Wed, 8 Feb 2012 13:03:59 -0600 Subject: [PATCH] Added Process entire file, and configuration settings --- Default (OSX).sublime-keymap | 13 ++++++++++++- Pretty JSON.sublime-settings | 5 +++++ PrettyJson.py | 17 +++++++++++++---- 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 Pretty JSON.sublime-settings diff --git a/Default (OSX).sublime-keymap b/Default (OSX).sublime-keymap index ab8545f..93a738f 100644 --- a/Default (OSX).sublime-keymap +++ b/Default (OSX).sublime-keymap @@ -1,3 +1,14 @@ [ - { "keys": ["super+ctrl+j"], "command": "prettyjson" } + // Only use this for the "source.json" scope + { + "keys": ["super+ctrl+j"], + "command": "prettyjson", + "context": [ + { + "key": "selector", + "operator": "equal", + "operand": "source.json" + } + ] + } ] \ No newline at end of file diff --git a/Pretty JSON.sublime-settings b/Pretty JSON.sublime-settings new file mode 100644 index 0000000..b5c21a9 --- /dev/null +++ b/Pretty JSON.sublime-settings @@ -0,0 +1,5 @@ +{ + "use_entire_file_if_no_selection" : true, + "indent_size" : 4, + "sort_keys" : true +} \ No newline at end of file diff --git a/PrettyJson.py b/PrettyJson.py index ba29917..83b542d 100644 --- a/PrettyJson.py +++ b/PrettyJson.py @@ -2,14 +2,23 @@ import sublime import sublime_plugin import json +s = sublime.load_settings("Pretty JSON.sublime-settings") class PrettyjsonCommand(sublime_plugin.TextCommand): def run(self, edit): for region in self.view.sel(): - selection = self.view.substr(region) - + # If no selection, use the entire file as the selection + # TODO: Use a setting to determine whether to do the entire file + # if region.empty(): + if region.empty() and s.get("use_entire_file_if_no_selection"): + selection = sublime.Region(0, self.view.size()) + else: + selection = region + try: - obj = json.loads(selection) - self.view.replace(edit, region, json.dumps(obj, indent=4, ensure_ascii=False, sort_keys=True)) + obj = json.loads(self.view.substr(selection)) + # TODO: Use a setting for the sort_keys value + # TODO: Use a setting for the indent value + self.view.replace(edit, selection, json.dumps(obj, indent=s.get("indent_size", 4), ensure_ascii=False, sort_keys=s.get("sort_keys", True))) except Exception, e: sublime.status_message(str(e))