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 (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/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 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 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": "-" } + ] + } + ] + } + ] + } +] 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..e97dd6a 100644 --- a/PrettyJson.py +++ b/PrettyJson.py @@ -2,14 +2,21 @@ import sublime import sublime_plugin 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(): - selection = self.view.substr(region) - + # If no selection, use the entire file as the selection + 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)) + 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))