diff --git a/blueprintcompiler/formatter.py b/blueprintcompiler/formatter.py index ddd7250..9d44bcd 100644 --- a/blueprintcompiler/formatter.py +++ b/blueprintcompiler/formatter.py @@ -1,6 +1,6 @@ -# decompiler.py +# formatter.py # -# Copyright 2021 James Westman +# Copyright 2023 James Westman # # This file is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as @@ -27,7 +27,7 @@ CLOSING_TOKENS = ["}", "]"] NEWLINE_AFTER = [";"] + OPENING_TOKENS + CLOSING_TOKENS -NO_WHITESPACE_BEFORE = [",", ":", "::", ";", ")", ".", ">"] +NO_WHITESPACE_BEFORE = [",", ":", "::", ";", ")", ".", ">", "]"] NO_WHITESPACE_AFTER = ["C_", "_", "("] # NO_WHITESPACE_BEFORE takes precedence over WHITESPACE_AFTER @@ -127,9 +127,8 @@ class Format: ): if str_item in OPENING_TOKENS: if str_item == "[": - is_child_type = (current_line + "[").startswith("[") + is_child_type = current_line.startswith("[") if is_child_type: - NO_WHITESPACE_BEFORE.append("]") if str(last_not_whitespace) not in OPENING_TOKENS: another_newline() last_not_whitespace = item @@ -147,33 +146,32 @@ class Format: LineType.BLOCK_OPEN, ) + elif str_item == "]" and is_child_type: + commit_current_line( + line_type=LineType.CHILD_TYPE, + indent_decrease=False, + ) + is_child_type = False + elif str_item in CLOSING_TOKENS: if str_item == "]": - if is_child_type: - NO_WHITESPACE_BEFORE.remove("]") - indent_levels += 1 - else: - WHITESPACE_AFTER.append(",") - NEWLINE_AFTER.remove(",") + NEWLINE_AFTER.remove(",") + WHITESPACE_AFTER.append(",") - if last_not_whitespace != ",": - current_line = current_line[:-1] - commit_current_line() - current_line = "]" + if last_not_whitespace != ",": + current_line = current_line[:-1] + commit_current_line() + current_line = "]" indent_levels -= 1 commit_current_line( - line_type=LineType.CHILD_TYPE - if is_child_type - else LineType.BLOCK_CLOSE, - indent_decrease=not is_child_type, + line_type=LineType.BLOCK_CLOSE, + indent_decrease=True, ) - is_child_type = False - elif str_item == ";" and len(end_str) > 0: commit_current_line( - two_newlines=end_str.strip()[-1] in CLOSING_TOKENS + two_newlines=prev_line_type == LineType.BLOCK_CLOSE ) else: diff --git a/blueprintcompiler/lsp.py b/blueprintcompiler/lsp.py index ad8a385..ac3a42a 100644 --- a/blueprintcompiler/lsp.py +++ b/blueprintcompiler/lsp.py @@ -292,18 +292,15 @@ class LanguageServer: return try: - XmlOutput().emit(open_file.ast) - except: - printerr(traceback.format_exc()) - self._send_error(id, ErrorCode.RequestFailed, "Could not compile document") + formatted_blp = Format.format( + open_file.text, + params["options"]["tabSize"], + params["options"]["insertSpaces"], + ) + except PrintableError: + self._send_error(id, ErrorCode.RequestFailed, "Could not format document") return - formatted_blp = Format.format( - open_file.text, - params["options"]["tabSize"], - params["options"]["insertSpaces"], - ) - lst = [] for tag, i1, i2, j1, j2 in SequenceMatcher( None, open_file.text, formatted_blp diff --git a/blueprintcompiler/main.py b/blueprintcompiler/main.py index 76caa4e..e34348a 100644 --- a/blueprintcompiler/main.py +++ b/blueprintcompiler/main.py @@ -186,12 +186,12 @@ class BlueprintApp: for path in opts.inputs: if os.path.isfile(path): - input_files.append(open(path, "r+")) + input_files.append(path) elif os.path.isdir(path): for root, subfolders, files in os.walk(path): for file in files: if file.endswith(".blp"): - input_files.append(open(os.path.join(root, file), "r+")) + input_files.append(os.path.join(root, file)) else: print( f"{Colors.RED}{Colors.BOLD}Could not find file: {path}{Colors.CLEAR}" @@ -200,50 +200,52 @@ class BlueprintApp: formatted_files = 0 for file in input_files: - data = file.read() + with open(file, "r+") as file: + data = file.read() - try: - xml, warnings = self._compile(data) + try: + xml, warnings = self._compile(data) - for warning in warnings: - warning.pretty_print(file.name, data, stream=sys.stderr) + for warning in warnings: + warning.pretty_print(file.name, data, stream=sys.stderr) - formatted_str = Format.format(data, opts.spaces, not opts.tabs) + formatted_str = Format.format(data, opts.spaces, not opts.tabs) - if data != formatted_str: - happened = "Would reformat" + if data != formatted_str: + happened = "Would reformat" - if opts.fix: - file.seek(0) - file.truncate() - file.write(formatted_str) - happened = "Reformatted" + if opts.fix: + file.seek(0) + file.truncate() + file.write(formatted_str) + happened = "Reformatted" - a_lines = data.splitlines(keepends=True) - b_lines = formatted_str.splitlines(keepends=True) - diff_lines = [] + a_lines = data.splitlines(keepends=True) + b_lines = formatted_str.splitlines(keepends=True) + diff_lines = [] - for line in difflib.unified_diff( - a_lines, b_lines, fromfile=file.name, tofile=file.name, n=5 - ): - # Work around https://bugs.python.org/issue2142 - # See: - # https://www.gnu.org/software/diffutils/manual/html_node/Incomplete-Lines.html - if line[-1] == "\n": - diff_lines.append(line) - else: - diff_lines.append(line + "\n") - diff_lines.append("\\ No newline at end of file\n") + for line in difflib.unified_diff( + a_lines, b_lines, fromfile=file.name, tofile=file.name, n=5 + ): + # Work around https://bugs.python.org/issue2142 + # See: + # https://www.gnu.org/software/diffutils/manual/html_node/Incomplete-Lines.html + if line[-1] == "\n": + diff_lines.append(line) + else: + diff_lines.append(line + "\n") + diff_lines.append("\\ No newline at end of file\n") - print( - f"{''.join(diff_lines)}\n{Colors.BOLD}{happened} {file.name}{Colors.CLEAR}\n" - ) + print( + # f"{''.join(diff_lines)}\n{Colors.BOLD}{happened} {file.name}{Colors.CLEAR}\n" + formatted_str + ) - formatted_files += 1 + formatted_files += 1 - except PrintableError as e: - e.pretty_print(file.name, data, stream=sys.stderr) - sys.exit(1) + except PrintableError as e: + e.pretty_print(file.name, data, stream=sys.stderr) + sys.exit(1) left_files = len(input_files) - formatted_files diff --git a/tests/test_formatter.py b/tests/test_formatter.py index 65e865e..8d03805 100644 --- a/tests/test_formatter.py +++ b/tests/test_formatter.py @@ -1,6 +1,6 @@ # test_formatter.py # -# Copyright 2021 James Westman +# Copyright 2023 James Westman # # This file is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as