fix: formatting, logic flow, f-strings

This commit is contained in:
TheSecEng 2020-04-15 20:31:15 -04:00
parent 5c82a50b6c
commit f0c9c8b6a7
No known key found for this signature in database
GPG Key ID: A7C3BA459E8C5C4E
1 changed files with 35 additions and 32 deletions

View File

@ -19,15 +19,17 @@ s = sublime.load_settings('Pretty JSON.sublime-settings')
xml_syntax = 'Packages/XML/XML.tmLanguage' xml_syntax = 'Packages/XML/XML.tmLanguage'
json_syntax = 'Packages/JSON/JSON.tmLanguage' json_syntax = 'Packages/JSON/JSON.tmLanguage'
jq_exists = False jq_exists = bool()
jq_init = False jq_init = bool()
jq_path = str() jq_path = str()
def check_jq(): def check_jq():
global jq_init, jq_exists, jq_path global jq_init, jq_exists, jq_path
if not jq_init: if jq_init:
return
jq_init = True jq_init = True
jq_test = s.get('jq_binary', 'jq') jq_test = s.get('jq_binary', 'jq')
try: try:
@ -103,15 +105,15 @@ class PrettyJsonBaseCommand:
i = 1 i = 1
while i < len(lines): while i < len(lines):
lines[i] = indent_space + lines[i] lines[i] = f'{indent_space}{lines[i]}'
i += 1 i += 1
return '\n'.join(lines) return '\n'.join(lines)
def show_exception(self, region: sublime.Region, msg): def show_exception(self, region: sublime.Region = None, msg=str()):
sublime.status_message(f'[Error]: {msg}') sublime.status_message(f'[Error]: {msg}')
if region is None: if region is None:
sublime.message_dialog(msg) sublime.message_dialog(f'[Error]: {msg}')
return return
self.highlight_error(region=region, message=f'{msg}') self.highlight_error(region=region, message=f'{msg}')
@ -138,7 +140,8 @@ class PrettyJsonBaseCommand:
self.phantom_set.update(self.phantoms) self.phantom_set.update(self.phantoms)
self.view.set_status('json_errors', message) self.view.set_status('json_errors', message)
# Description: Taken from https://github.com/sublimelsp/LSP/blob/master/plugin/diagnostics.py # Description: Taken from
# - https://github.com/sublimelsp/LSP/blob/master/plugin/diagnostics.py
# - Thanks to the LSP Team # - Thanks to the LSP Team
def create_phantom_html(self, content: str, severity: str) -> str: def create_phantom_html(self, content: str, severity: str) -> str:
stylesheet = sublime.load_resource('Packages/Pretty JSON/phantom.css') stylesheet = sublime.load_resource('Packages/Pretty JSON/phantom.css')
@ -164,8 +167,6 @@ class PrettyJsonBaseCommand:
self.phantom_set.update(self.phantoms) self.phantom_set.update(self.phantoms)
def syntax_to_json(self): def syntax_to_json(self):
''' Changes syntax to JSON if its in plain text '''
if 'Plain text' in self.view.settings().get('syntax'):
self.view.set_syntax_file(json_syntax) self.view.set_syntax_file(json_syntax)
@ -267,11 +268,12 @@ class UnPrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
def run(self, edit): def run(self, edit):
PrettyJsonBaseCommand.clear_phantoms(self) PrettyJsonBaseCommand.clear_phantoms(self)
for region in self.view.sel(): regions = self.view.sel()
for region in regions:
selected_entire_file = False selected_entire_file = False
if region.empty() and len(regions) > 1:
# If no selection, use the entire file as the selection continue
if region.empty() and s.get("use_entire_file_if_no_selection", True): elif region.empty() and s.get('use_entire_file_if_no_selection', True):
selection = sublime.Region(0, self.view.size()) selection = sublime.Region(0, self.view.size())
region = sublime.Region(0, self.view.size()) region = sublime.Region(0, self.view.size())
selected_entire_file = True selected_entire_file = True
@ -311,9 +313,11 @@ class JqPrettyJson(sublime_plugin.WindowCommand):
''' returns content of active view or selected region ''' ''' returns content of active view or selected region '''
view = self.window.active_view() view = self.window.active_view()
selection = str() selection = str()
for region in view.sel(): regions = view.sel()
# If no selection, use the entire file as the selection for region in regions:
if region.empty(): if region.empty() and len(regions) > 1:
continue
elif region.empty():
selection = sublime.Region(0, view.size()) selection = sublime.Region(0, view.size())
else: else:
selection = region selection = region
@ -348,12 +352,14 @@ class JsonToXml(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
def run(self, edit): def run(self, edit):
self.view.erase_regions('json_errors') self.view.erase_regions('json_errors')
for region in self.view.sel(): regions = self.view.sel()
for region in regions:
selected_entire_file = False selected_entire_file = False
if region.empty() and len(regions) > 1:
# If no selection, use the entire file as the selection continue
if region.empty() and s.get('use_entire_file_if_no_selection', True): elif region.empty() and s.get('use_entire_file_if_no_selection', True):
selection = sublime.Region(0, self.view.size()) selection = sublime.Region(0, self.view.size())
region = sublime.Region(0, self.view.size())
selected_entire_file = True selected_entire_file = True
else: else:
selection = region selection = region
@ -366,12 +372,10 @@ class JsonToXml(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
xml_string = '<?xml version=\'1.0\' encoding=\'UTF-8\' ?>\n' xml_string = '<?xml version=\'1.0\' encoding=\'UTF-8\' ?>\n'
rtn = ElementTree.tostring(root, 'utf-8') rtn = ElementTree.tostring(root, 'utf-8')
if type(rtn) is bytes: if type(rtn) is bytes:
rtn = rtn.decode('utf-8') rtn = rtn.decode('utf-8')
xml_string += rtn xml_string += rtn
if type(xml_string) is bytes: if type(xml_string) is bytes:
xml_string = xml_string.decode('utf-8') xml_string = xml_string.decode('utf-8')
@ -403,7 +407,6 @@ class JsonToXml(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
return element return element
def syntax_to_xml(self): def syntax_to_xml(self):
''' change syntax to xml '''
self.view.set_syntax_file(xml_syntax) self.view.set_syntax_file(xml_syntax)