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 1/6] 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)) From cd50ed621645b65a3a944fdd52064128a985011b Mon Sep 17 00:00:00 2001 From: Taylor <03283812@pepaul00302.corp.pep.pvt> Date: Wed, 8 Feb 2012 13:04:45 -0600 Subject: [PATCH 2/6] Cleaned up TODO comments --- PrettyJson.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/PrettyJson.py b/PrettyJson.py index 83b542d..cbb080f 100644 --- a/PrettyJson.py +++ b/PrettyJson.py @@ -8,8 +8,6 @@ class PrettyjsonCommand(sublime_plugin.TextCommand): def run(self, edit): for region in self.view.sel(): # 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: @@ -17,8 +15,6 @@ class PrettyjsonCommand(sublime_plugin.TextCommand): try: 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)) From 9fa1573683c2d93ca0cc8a35de6af6ed02dc7367 Mon Sep 17 00:00:00 2001 From: "H.B. Taylor" Date: Wed, 8 Feb 2012 13:14:52 -0600 Subject: [PATCH 3/6] Added source.json scope to Linux and Windows keymaps --- Default (Linux).sublime-keymap | 12 +++++++++++- Default (Windows).sublime-keymap | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Default (Linux).sublime-keymap b/Default (Linux).sublime-keymap index 15aa8a8..451d647 100644 --- a/Default (Linux).sublime-keymap +++ b/Default (Linux).sublime-keymap @@ -1,3 +1,13 @@ [ - { "keys": ["ctrl+alt+j"], "command": "prettyjson" } + { + "keys": ["ctrl+alt+j"], + "command": "prettyjson", + "context": [ + { + "key": "selector", + "operator": "equal", + "operand": "source.json" + } + ] + } ] \ No newline at end of file diff --git a/Default (Windows).sublime-keymap b/Default (Windows).sublime-keymap index 15aa8a8..451d647 100644 --- a/Default (Windows).sublime-keymap +++ b/Default (Windows).sublime-keymap @@ -1,3 +1,13 @@ [ - { "keys": ["ctrl+alt+j"], "command": "prettyjson" } + { + "keys": ["ctrl+alt+j"], + "command": "prettyjson", + "context": [ + { + "key": "selector", + "operator": "equal", + "operand": "source.json" + } + ] + } ] \ No newline at end of file From ac63699d94662d98971fb8ace57a4b3d609cc503 Mon Sep 17 00:00:00 2001 From: "H.B. Taylor" Date: Wed, 8 Feb 2012 13:19:09 -0600 Subject: [PATCH 4/6] Added Main.sublime-menu to surface the settings files --- Main.sublime-menu | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Main.sublime-menu diff --git a/Main.sublime-menu b/Main.sublime-menu new file mode 100644 index 0000000..0074fbf --- /dev/null +++ b/Main.sublime-menu @@ -0,0 +1,39 @@ +[ + { + "caption": "Preferences", + "mnemonic": "n", + "id": "preferences", + "children": + [ + { + "caption": "Package Settings", + "mnemonic": "P", + "id": "package-settings", + "children": + [ + { + "caption": "Pretty JSON", + "children": + [ + { + "command": "open_file", "args": + { + "file": "${packages}/Pretty JSON/Pretty JSON.sublime-settings" + }, + "caption": "Settings – Default" + }, + { + "command": "open_file", "args": + { + "file": "${packages}/User/Pretty JSON.sublime-settings" + }, + "caption": "Settings – User" + }, + { "caption": "-" } + ] + } + ] + } + ] + } +] From 73721049cb585f80c453df5aa91e530292d517c1 Mon Sep 17 00:00:00 2001 From: "H.B. Taylor" Date: Wed, 8 Feb 2012 13:27:22 -0600 Subject: [PATCH 5/6] Added Default.sublime-commands to add prettyjson to the Commant Palette --- Default.sublime-commands | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Default.sublime-commands diff --git a/Default.sublime-commands b/Default.sublime-commands new file mode 100644 index 0000000..6117e8d --- /dev/null +++ b/Default.sublime-commands @@ -0,0 +1,6 @@ +[ + { + "caption": "Pretty JSON: Reformat (Pretty Print) JSON", + "command": "prettyjson" + } +] \ No newline at end of file From c7e3288175f6b61479b187912bc6047a350560bb Mon Sep 17 00:00:00 2001 From: "H.B. Taylor" Date: Wed, 8 Feb 2012 17:33:41 -0600 Subject: [PATCH 6/6] Added comment to the run() method --- PrettyJson.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/PrettyJson.py b/PrettyJson.py index cbb080f..e97dd6a 100644 --- a/PrettyJson.py +++ b/PrettyJson.py @@ -5,6 +5,8 @@ import json s = sublime.load_settings("Pretty JSON.sublime-settings") class PrettyjsonCommand(sublime_plugin.TextCommand): + """ Pretty Print JSON + """ def run(self, edit): for region in self.view.sel(): # If no selection, use the entire file as the selection