From 31e47da8c1259f0d0c7ca001d3fc7d276fa86269 Mon Sep 17 00:00:00 2001 From: Nikolajus Date: Fri, 15 Jul 2016 01:40:17 +0200 Subject: [PATCH] #65 - naive version of single quote detection, and trying to replace them into double quotes and parse again --- PrettyJson.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/PrettyJson.py b/PrettyJson.py index e70a070..da7a392 100644 --- a/PrettyJson.py +++ b/PrettyJson.py @@ -175,14 +175,26 @@ class PrettyJsonCommand(PrettyJsonBaseCommand): selection = region try: - obj = self.json_loads(self.view.substr(selection)) + selection_text = self.view.substr(selection) + obj = self.json_loads(selection_text) self.view.replace(edit, selection, self.json_dumps(obj)) if selected_entire_file: self.change_syntax() except Exception: - self.show_exception() + 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) + obj = self.json_loads(selection_text_modified) + self.view.replace(edit, selection, self.json_dumps(obj)) + + if selected_entire_file: + self.change_syntax() + else: + self.show_exception() class PrettyJsonAndSortCommand(PrettyJsonCommand):