Merge remote-tracking branch 'KuttKatrea/reindent-block'

This commit is contained in:
TheSecEng 2020-04-14 17:38:00 -04:00
commit 2edda28695
No known key found for this signature in database
GPG Key ID: A7C3BA459E8C5C4E
3 changed files with 97 additions and 20 deletions

View File

@ -8,5 +8,6 @@
"keep_arrays_single_line": false,
"max_arrays_line_length": 120,
"pretty_on_save": false,
"validate_on_save": true
"validate_on_save": true,
"reindent_block": false
}

View File

@ -53,7 +53,6 @@ def check_jq():
s = sublime.load_settings("Pretty JSON.sublime-settings")
class PrettyJsonBaseCommand:
json_error_matcher = re.compile(r"line (\d+)")
force_sorting = False
@ -120,6 +119,37 @@ class PrettyJsonBaseCommand:
use_decimal=True,
)
def reindent(self, text, selection):
current_line = self.view.line(selection.begin())
text_before_sel = sublime.Region(
current_line.begin(), selection.begin())
reindent_mode = s.get('reindent_block', 'minimal')
if reindent_mode == 'start':
# Reindent to the column where the selection starts
space_number = text_before_sel.size()
indent_space = " " * space_number
else:
# Reindent to the number of spaces to the left of the
# line where the selection starts
# Extracts the spaces at the start of the line to use them
# as padding
indent_space = re.search(
'^\s*', self.view.substr(text_before_sel)).group(0)
lines = text.split('\n')
# Pad every line except the first one
i = 1
while (i < len(lines)):
lines[i] = indent_space + lines[i]
i += 1
return "\n".join(lines)
def highlight_error(self, message):
self.view.erase_regions("json_errors")
@ -190,7 +220,6 @@ class PrettyJsonValidate(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
class PrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
""" Pretty Print JSON """
def run(self, edit):
@ -209,26 +238,33 @@ class PrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
try:
selection_text = self.view.substr(selection)
obj = self.json_loads(selection_text)
self.view.replace(edit, selection, self.json_dumps(obj))
json_text = self.json_dumps(obj)
if not selected_entire_file and s.get("reindent_block", False):
json_text = self.reindent(json_text, selection)
self.view.replace(edit, selection, json_text)
if selected_entire_file:
self.syntax_to_json()
except Exception as ex:
try:
amount_of_single_quotes = re.findall(
r"(\'[^\']+\'?)", selection_text
)
amount_of_double_quotes = re.findall(
r"(\"[^\"]+\"?)", selection_text
)
amount_of_single_quotes = re.findall(r"(\'[^\']+\'?)", selection_text)
amount_of_double_quotes = re.findall(r"(\"[^\"]+\"?)", selection_text)
if len(amount_of_single_quotes) >= len(amount_of_double_quotes):
selection_text_modified = re.sub(
r"(?:\'([^\']+)\'?)", r'"\1"', selection_text
)
selection_text_modified = re.sub(r"(?:\'([^\']+)\'?)", r'"\1"', selection_text)
obj = self.json_loads(selection_text_modified)
self.view.replace(edit, selection, self.json_dumps(obj))
json_text = self.json_dumps(obj)
if not selected_entire_file and s.get("reindent_block", False):
json_text = self.reindent(json_text, selection)
self.view.replace(edit, selection, json_text)
if selected_entire_file:
self.syntax_to_json()
@ -238,6 +274,7 @@ class PrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
self.show_exception(msg=ex)
class PrettyJsonAndSortCommand(PrettyJsonCommand, sublime_plugin.TextCommand):
""" Pretty print json with forced sorting """
@ -379,6 +416,10 @@ class JsonToXml(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
if type(xml_string) is bytes:
xml_string = xml_string.decode("utf-8")
if not selected_entire_file and s.get("reindent_block", False):
xml_string = self.reindent(xml_string, selection)
self.view.replace(edit, selection, xml_string)
if selected_entire_file:

View File

@ -60,7 +60,8 @@ http://stedolan.github.io/jq/
**use_entire_file_if_no_selection** - true
**indent** - 2
`int used for how many spaces to use for indent, replace it with value "\t" and tabs will be used instead`
int used for how many spaces to use for indent, replace it with value "\t" and tabs will be used instead
**sort_keys** - false
@ -69,19 +70,53 @@ http://stedolan.github.io/jq/
**line_separator** - ","
**value_separator** - ": "
`value separator in config, so if you need to get rid of extra space you can remove it with this param`
value separator in config, so if you need to get rid of extra space you can remove it with this param
**keep_arrays_single_line** - false
`if we need to re-structure arrays and make them single-line`
if we need to re-structure arrays and make them single-line
**max_arrays_line_length** - 120
`if array for example '["a", "b", 123213, ....]' length will reach max it will be kept multi-line (for beauty)`
if array for example '["a", "b", 123213, ....]' length will reach max it will be kept multi-line (for beauty)
**pretty_on_save** - false
`do we need to automatically Pretty JSON on save`
do we need to automatically Pretty JSON on save
**validate_on_save** - true
`do we need validate JSON files on each save`
do we need validate JSON files on each save
**reindent_block** - false
if we are formatting a selection, if we need to reindent the
resulting block to follow the flow of the source document
the posible values are 'minimal' and 'start'
using `minimal`, the resulting json lines are indented as much
spaces as the line where the selection starts. e.g
yaml_container:
yaml_key: { "json": "value" }
gets formatted as:
yaml_container:
yaml_key: {
"json": "value"
}
using `start`, the resulting json lines are indented a number
of spaces equal to the column number of the start of the selection
with `start` the previous example gets formatted as:
yaml_container:
yaml_key: {
"json": "value"
}
## Using tabs for indentation