parent
f0c9c8b6a7
commit
ced7e12107
|
@ -1 +1,3 @@
|
||||||
/tests export-ignore
|
/tests export-ignore
|
||||||
|
/.github export-ignore
|
||||||
|
pyproject.toml export-ignore
|
|
@ -0,0 +1,20 @@
|
||||||
|
name: Pretty Json Tests
|
||||||
|
|
||||||
|
on: [push, pull_request]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
python-version: [3.8]
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Set up Python ${{ matrix.python-version }}
|
||||||
|
uses: actions/setup-python@v1
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.python-version }}
|
||||||
|
- name: Run Tests
|
||||||
|
run: |
|
||||||
|
cd tests
|
||||||
|
python tests.py
|
|
@ -1,6 +1,7 @@
|
||||||
{
|
{
|
||||||
"use_entire_file_if_no_selection": true,
|
"use_entire_file_if_no_selection": true,
|
||||||
"indent": 4,
|
"indent": 4,
|
||||||
|
|
||||||
"sort_keys": false,
|
"sort_keys": false,
|
||||||
"ensure_ascii": false,
|
"ensure_ascii": false,
|
||||||
"line_separator": ",",
|
"line_separator": ",",
|
||||||
|
@ -9,7 +10,11 @@
|
||||||
"max_arrays_line_length": 120,
|
"max_arrays_line_length": 120,
|
||||||
"pretty_on_save": false,
|
"pretty_on_save": false,
|
||||||
"validate_on_save": true,
|
"validate_on_save": true,
|
||||||
"reindent_block": false,
|
"brace_newline": true,
|
||||||
|
"bracket_newline": true,
|
||||||
|
// Default: False
|
||||||
|
// Valid Options: False, start, minimal
|
||||||
|
"reindent_block": "minimal",
|
||||||
// Name or Path to jq binary
|
// Name or Path to jq binary
|
||||||
// Example: /usr/bin/local/jq
|
// Example: /usr/bin/local/jq
|
||||||
// Example: jq
|
// Example: jq
|
||||||
|
|
|
@ -43,8 +43,10 @@ def check_jq():
|
||||||
class PrettyJsonBaseCommand:
|
class PrettyJsonBaseCommand:
|
||||||
phantom_set = sublime.PhantomSet
|
phantom_set = sublime.PhantomSet
|
||||||
phantoms = list()
|
phantoms = list()
|
||||||
json_char_matcher = re.compile(r'char (\d+)')
|
|
||||||
force_sorting = False
|
force_sorting = False
|
||||||
|
json_char_matcher = re.compile(r'char (\d+)')
|
||||||
|
brace_newline = re.compile(r'^((\s*)".*?":)\s*([{])', re.MULTILINE)
|
||||||
|
bracket_newline = re.compile(r'^((\s*)".*?":)\s*([\[])', re.MULTILINE)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def json_loads(selection: str) -> dict:
|
def json_loads(selection: str) -> dict:
|
||||||
|
@ -87,19 +89,31 @@ class PrettyJsonBaseCommand:
|
||||||
replacement = '[' + join_separator.join(items) + ']'
|
replacement = '[' + join_separator.join(items) + ']'
|
||||||
if len(replacement) <= s.get('max_arrays_line_length', 120):
|
if len(replacement) <= s.get('max_arrays_line_length', 120):
|
||||||
output_json = output_json.replace(m, replacement, 1)
|
output_json = output_json.replace(m, replacement, 1)
|
||||||
|
if s.get("brace_newline", True):
|
||||||
|
output_json = PrettyJsonBaseCommand.brace_newline.sub(r'\1\n\2\3', output_json)
|
||||||
|
|
||||||
|
if (
|
||||||
|
s.get("bracket_newline", True)
|
||||||
|
and s.get('keep_arrays_single_line', False) is False
|
||||||
|
):
|
||||||
|
output_json = PrettyJsonBaseCommand.bracket_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())
|
||||||
text_before_sel = sublime.Region(current_line.begin(), selection.begin())
|
text_before_sel = sublime.Region(current_line.begin(), selection.begin())
|
||||||
|
|
||||||
reindent_mode = s.get('reindent_block', 'minimal')
|
reindent_mode = s.get('reindent_block', False)
|
||||||
if reindent_mode == 'start':
|
if reindent_mode == 'start':
|
||||||
space_number = text_before_sel.size()
|
space_number = text_before_sel.size()
|
||||||
indent_space = ' ' * space_number
|
indent_space = ' ' * space_number
|
||||||
else:
|
elif reindent_mode == 'minimal':
|
||||||
indent_space = re.search('^\s*', self.view.substr(text_before_sel)).group(0)
|
indent_space = re.search(r'^\s*', self.view.substr(text_before_sel)).group(0)
|
||||||
|
|
||||||
lines = text.split('\n')
|
lines = text.split('\n')
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
/* Description: Taken from LSP
|
||||||
|
- https://github.com/sublimelsp/LSP/blob/master/plugin/diagnostics.py
|
||||||
|
*/
|
||||||
div.error-arrow {
|
div.error-arrow {
|
||||||
border-top: 0.4rem solid transparent;
|
border-top: 0.4rem solid transparent;
|
||||||
border-left: 0.5rem solid color(var(--redish) blend(var(--background) 30%));
|
border-left: 0.5rem solid color(var(--redish) blend(var(--background) 30%));
|
||||||
|
|
|
@ -29,9 +29,9 @@ class TestIssues(unittest.TestCase):
|
||||||
# issue 15
|
# issue 15
|
||||||
def test_float_issue_15(self):
|
def test_float_issue_15(self):
|
||||||
tmp_str = '{"real":0.99}'
|
tmp_str = '{"real":0.99}'
|
||||||
expected_output = """{
|
expected_output = '''{
|
||||||
"real": 0.99
|
"real": 0.99
|
||||||
}"""
|
}'''
|
||||||
obj = json.loads(tmp_str, object_pairs_hook=OrderedDict, parse_float=decimal.Decimal)
|
obj = json.loads(tmp_str, object_pairs_hook=OrderedDict, parse_float=decimal.Decimal)
|
||||||
tmp_str = json.dumps(obj, indent=2, ensure_ascii=False, sort_keys=False,
|
tmp_str = json.dumps(obj, indent=2, ensure_ascii=False, sort_keys=False,
|
||||||
separators=(',', ': '),
|
separators=(',', ': '),
|
||||||
|
@ -52,7 +52,7 @@ class TestIssues(unittest.TestCase):
|
||||||
def test_float_issue_16_2(self):
|
def test_float_issue_16_2(self):
|
||||||
tmp_str = '{"test1":0.99, "test2":"1.99", "test3":1.00000000001, "test4":1.99, "test5":1,' \
|
tmp_str = '{"test1":0.99, "test2":"1.99", "test3":1.00000000001, "test4":1.99, "test5":1,' \
|
||||||
' "test6":4.589999999999999999, "test7":1.0}'
|
' "test6":4.589999999999999999, "test7":1.0}'
|
||||||
expected_output = """{
|
expected_output = '''{
|
||||||
"test1": 0.99,
|
"test1": 0.99,
|
||||||
"test2": "1.99",
|
"test2": "1.99",
|
||||||
"test3": 1.00000000001,
|
"test3": 1.00000000001,
|
||||||
|
@ -60,7 +60,7 @@ class TestIssues(unittest.TestCase):
|
||||||
"test5": 1,
|
"test5": 1,
|
||||||
"test6": 4.589999999999999999,
|
"test6": 4.589999999999999999,
|
||||||
"test7": 1.0
|
"test7": 1.0
|
||||||
}"""
|
}'''
|
||||||
obj = json.loads(tmp_str, object_pairs_hook=OrderedDict, parse_float=decimal.Decimal)
|
obj = json.loads(tmp_str, object_pairs_hook=OrderedDict, parse_float=decimal.Decimal)
|
||||||
tmp_str = json.dumps(obj, indent=2, ensure_ascii=False, sort_keys=False,
|
tmp_str = json.dumps(obj, indent=2, ensure_ascii=False, sort_keys=False,
|
||||||
separators=(',', ': '),
|
separators=(',', ': '),
|
||||||
|
@ -68,9 +68,9 @@ class TestIssues(unittest.TestCase):
|
||||||
self.assertEqual(tmp_str, expected_output)
|
self.assertEqual(tmp_str, expected_output)
|
||||||
|
|
||||||
def test_compress_feature(self):
|
def test_compress_feature(self):
|
||||||
tmp_str = """{
|
tmp_str = '''{
|
||||||
"real": 0.99
|
"real": 0.99
|
||||||
}"""
|
}'''
|
||||||
expected_output = '{"real":0.99}'
|
expected_output = '{"real":0.99}'
|
||||||
obj = json.loads(tmp_str, object_pairs_hook=OrderedDict, parse_float=decimal.Decimal)
|
obj = json.loads(tmp_str, object_pairs_hook=OrderedDict, parse_float=decimal.Decimal)
|
||||||
tmp_str = json.dumps(obj, ensure_ascii=False, sort_keys=False,
|
tmp_str = json.dumps(obj, ensure_ascii=False, sort_keys=False,
|
||||||
|
|
Loading…
Reference in New Issue