Added Process entire file, and configuration settings

This commit is contained in:
Taylor 2012-02-08 13:03:59 -06:00
parent c55fdfb5e3
commit 5021a2d37c
3 changed files with 30 additions and 5 deletions

View File

@ -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"
}
]
}
]

View File

@ -0,0 +1,5 @@
{
"use_entire_file_if_no_selection" : true,
"indent_size" : 4,
"sort_keys" : true
}

View File

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