From 7aacb2de85552ff96d67173a170c9a5467edc6f5 Mon Sep 17 00:00:00 2001 From: TheSecEng Date: Sun, 19 Apr 2020 13:14:51 -0400 Subject: [PATCH] code cleanup --- Pretty JSON.sublime-settings | 8 +++++++- PrettyJson.py | 9 +-------- PrettyJsonListeners.py | 16 ++++++++++------ 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Pretty JSON.sublime-settings b/Pretty JSON.sublime-settings index 73f0683..5f783cf 100644 --- a/Pretty JSON.sublime-settings +++ b/Pretty JSON.sublime-settings @@ -19,5 +19,11 @@ // Example: /usr/bin/local/jq // Example: jq "jq_binary": "jq", - "jq_errors": false + "jq_errors": false, + "as_json":[ + "JSON", + "Sublime Text Commands", + "Sublime Text Settingss", + "Sublime Text Menu" + ] } \ No newline at end of file diff --git a/PrettyJson.py b/PrettyJson.py index d220861..ad39e26 100644 --- a/PrettyJson.py +++ b/PrettyJson.py @@ -11,7 +11,7 @@ import sublime_plugin from .lib import simplejson as json from .lib.simplejson import OrderedDict -SUBLIME_MAJOR_VERSION = int(sublime.version()) / 1000 + PREVIOUS_CONTENT = [str(), str()] PREVIOUS_QUERY_LEN = int() @@ -95,13 +95,8 @@ class PrettyJsonBaseCommand: if s.get("brace_newline", True): output_json = PrettyJsonBaseCommand.brace_newline.sub(r'\1\n\2\3', 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): current_line = self.view.line(selection.begin()) @@ -221,7 +216,6 @@ class PrettyJsonValidate(PrettyJsonBaseCommand, sublime_plugin.TextCommand): return result - class PrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand): ''' Description: Pretty Print JSON @@ -328,7 +322,6 @@ class UnPrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand): class JqInsertPrettyJsonCommand(sublime_plugin.TextCommand): def run(self, edit, string): self.view.replace(edit, sublime.Region(0, self.view.size()), string) - # self.view.sel().clear() class JqPrettyJsonCommand(sublime_plugin.TextCommand): diff --git a/PrettyJsonListeners.py b/PrettyJsonListeners.py index 43026b8..007d19d 100644 --- a/PrettyJsonListeners.py +++ b/PrettyJsonListeners.py @@ -5,16 +5,18 @@ from .PrettyJson import PrettyJsonBaseCommand s = sublime.load_settings("Pretty JSON.sublime-settings") + class PrettyJsonLintListener(sublime_plugin.EventListener, PrettyJsonBaseCommand): 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']) - if validate and any( + if any( syntax in view.settings().get("syntax") for syntax in as_json ): - self.view = view 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: self.json_loads(json_content) except Exception as ex: @@ -23,9 +25,11 @@ class PrettyJsonLintListener(sublime_plugin.EventListener, PrettyJsonBaseCommand class PrettyJsonAutoPrettyOnSaveListener(sublime_plugin.EventListener): 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']) - if auto_pretty and any( + if any( syntax in view.settings().get('syntax') for syntax in as_json ): sublime.active_window().run_command('pretty_json')