code cleanup

This commit is contained in:
TheSecEng 2020-04-19 13:14:51 -04:00
parent 0907e45be7
commit 7aacb2de85
No known key found for this signature in database
GPG Key ID: A7C3BA459E8C5C4E
3 changed files with 18 additions and 15 deletions

View File

@ -19,5 +19,11 @@
// Example: /usr/bin/local/jq // Example: /usr/bin/local/jq
// Example: jq // Example: jq
"jq_binary": "jq", "jq_binary": "jq",
"jq_errors": false "jq_errors": false,
"as_json":[
"JSON",
"Sublime Text Commands",
"Sublime Text Settingss",
"Sublime Text Menu"
]
} }

View File

@ -11,7 +11,7 @@ import sublime_plugin
from .lib import simplejson as json from .lib import simplejson as json
from .lib.simplejson import OrderedDict from .lib.simplejson import OrderedDict
SUBLIME_MAJOR_VERSION = int(sublime.version()) / 1000
PREVIOUS_CONTENT = [str(), str()] PREVIOUS_CONTENT = [str(), str()]
PREVIOUS_QUERY_LEN = int() PREVIOUS_QUERY_LEN = int()
@ -95,13 +95,8 @@ class PrettyJsonBaseCommand:
if s.get("brace_newline", True): if s.get("brace_newline", True):
output_json = PrettyJsonBaseCommand.brace_newline.sub(r'\1\n\2\3', output_json) output_json = PrettyJsonBaseCommand.brace_newline.sub(r'\1\n\2\3', output_json)
return output_json return output_json
def brace_bracket_newline(json_data: str) -> str:
better_json = PrettyJsonBaseCommand.brace_bracket_newline.sub(r'\1\n\2\3', json_data)
return better_json
def reindent(self, text: str, selection: str): def reindent(self, text: str, selection: str):
current_line = self.view.line(selection.begin()) current_line = self.view.line(selection.begin())
@ -221,7 +216,6 @@ class PrettyJsonValidate(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
return result return result
class PrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand): class PrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
''' '''
Description: Pretty Print JSON Description: Pretty Print JSON
@ -328,7 +322,6 @@ class UnPrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
class JqInsertPrettyJsonCommand(sublime_plugin.TextCommand): class JqInsertPrettyJsonCommand(sublime_plugin.TextCommand):
def run(self, edit, string): def run(self, edit, string):
self.view.replace(edit, sublime.Region(0, self.view.size()), string) self.view.replace(edit, sublime.Region(0, self.view.size()), string)
# self.view.sel().clear()
class JqPrettyJsonCommand(sublime_plugin.TextCommand): class JqPrettyJsonCommand(sublime_plugin.TextCommand):

View File

@ -5,16 +5,18 @@ from .PrettyJson import PrettyJsonBaseCommand
s = sublime.load_settings("Pretty JSON.sublime-settings") s = sublime.load_settings("Pretty JSON.sublime-settings")
class PrettyJsonLintListener(sublime_plugin.EventListener, PrettyJsonBaseCommand): class PrettyJsonLintListener(sublime_plugin.EventListener, PrettyJsonBaseCommand):
def on_post_save(self, view): def on_post_save(self, view):
validate = s.get('validate_on_save', True) if not s.get('validate_on_save', True):
return
as_json = s.get('as_json', ['JSON']) as_json = s.get('as_json', ['JSON'])
if validate and any( if any(
syntax in view.settings().get("syntax") for syntax in as_json syntax in view.settings().get("syntax") for syntax in as_json
): ):
self.view = view
PrettyJsonBaseCommand.clear_phantoms(self) PrettyJsonBaseCommand.clear_phantoms(self)
json_content = self.view.substr(sublime.Region(0, self.view.size())) json_content = view.substr(sublime.Region(0, view.size()))
try: try:
self.json_loads(json_content) self.json_loads(json_content)
except Exception as ex: except Exception as ex:
@ -23,9 +25,11 @@ class PrettyJsonLintListener(sublime_plugin.EventListener, PrettyJsonBaseCommand
class PrettyJsonAutoPrettyOnSaveListener(sublime_plugin.EventListener): class PrettyJsonAutoPrettyOnSaveListener(sublime_plugin.EventListener):
def on_pre_save(self, view): def on_pre_save(self, view):
auto_pretty = s.get('pretty_on_save', False) if not s.get('pretty_on_save', False):
return
as_json = s.get('as_json', ['JSON']) as_json = s.get('as_json', ['JSON'])
if auto_pretty and any( if any(
syntax in view.settings().get('syntax') for syntax in as_json syntax in view.settings().get('syntax') for syntax in as_json
): ):
sublime.active_window().run_command('pretty_json') sublime.active_window().run_command('pretty_json')