Apply some of @jwestman's suggestions

This commit is contained in:
gregorni 2023-09-17 10:10:25 +02:00
parent 455924e22f
commit f777c531e4
4 changed files with 66 additions and 69 deletions

View file

@ -1,6 +1,6 @@
# decompiler.py # formatter.py
# #
# Copyright 2021 James Westman <james@jwestman.net> # Copyright 2023 James Westman <james@jwestman.net>
# #
# This file is free software; you can redistribute it and/or modify it # This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as # under the terms of the GNU Lesser General Public License as
@ -27,7 +27,7 @@ CLOSING_TOKENS = ["}", "]"]
NEWLINE_AFTER = [";"] + OPENING_TOKENS + CLOSING_TOKENS NEWLINE_AFTER = [";"] + OPENING_TOKENS + CLOSING_TOKENS
NO_WHITESPACE_BEFORE = [",", ":", "::", ";", ")", ".", ">"] NO_WHITESPACE_BEFORE = [",", ":", "::", ";", ")", ".", ">", "]"]
NO_WHITESPACE_AFTER = ["C_", "_", "("] NO_WHITESPACE_AFTER = ["C_", "_", "("]
# NO_WHITESPACE_BEFORE takes precedence over WHITESPACE_AFTER # NO_WHITESPACE_BEFORE takes precedence over WHITESPACE_AFTER
@ -127,9 +127,8 @@ class Format:
): ):
if str_item in OPENING_TOKENS: if str_item in OPENING_TOKENS:
if str_item == "[": if str_item == "[":
is_child_type = (current_line + "[").startswith("[") is_child_type = current_line.startswith("[")
if is_child_type: if is_child_type:
NO_WHITESPACE_BEFORE.append("]")
if str(last_not_whitespace) not in OPENING_TOKENS: if str(last_not_whitespace) not in OPENING_TOKENS:
another_newline() another_newline()
last_not_whitespace = item last_not_whitespace = item
@ -147,14 +146,17 @@ class Format:
LineType.BLOCK_OPEN, 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: elif str_item in CLOSING_TOKENS:
if str_item == "]": 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 != ",": if last_not_whitespace != ",":
current_line = current_line[:-1] current_line = current_line[:-1]
@ -163,17 +165,13 @@ class Format:
indent_levels -= 1 indent_levels -= 1
commit_current_line( commit_current_line(
line_type=LineType.CHILD_TYPE line_type=LineType.BLOCK_CLOSE,
if is_child_type indent_decrease=True,
else LineType.BLOCK_CLOSE,
indent_decrease=not is_child_type,
) )
is_child_type = False
elif str_item == ";" and len(end_str) > 0: elif str_item == ";" and len(end_str) > 0:
commit_current_line( commit_current_line(
two_newlines=end_str.strip()[-1] in CLOSING_TOKENS two_newlines=prev_line_type == LineType.BLOCK_CLOSE
) )
else: else:

View file

@ -292,17 +292,14 @@ class LanguageServer:
return return
try: try:
XmlOutput().emit(open_file.ast)
except:
printerr(traceback.format_exc())
self._send_error(id, ErrorCode.RequestFailed, "Could not compile document")
return
formatted_blp = Format.format( formatted_blp = Format.format(
open_file.text, open_file.text,
params["options"]["tabSize"], params["options"]["tabSize"],
params["options"]["insertSpaces"], params["options"]["insertSpaces"],
) )
except PrintableError:
self._send_error(id, ErrorCode.RequestFailed, "Could not format document")
return
lst = [] lst = []
for tag, i1, i2, j1, j2 in SequenceMatcher( for tag, i1, i2, j1, j2 in SequenceMatcher(

View file

@ -186,12 +186,12 @@ class BlueprintApp:
for path in opts.inputs: for path in opts.inputs:
if os.path.isfile(path): if os.path.isfile(path):
input_files.append(open(path, "r+")) input_files.append(path)
elif os.path.isdir(path): elif os.path.isdir(path):
for root, subfolders, files in os.walk(path): for root, subfolders, files in os.walk(path):
for file in files: for file in files:
if file.endswith(".blp"): if file.endswith(".blp"):
input_files.append(open(os.path.join(root, file), "r+")) input_files.append(os.path.join(root, file))
else: else:
print( print(
f"{Colors.RED}{Colors.BOLD}Could not find file: {path}{Colors.CLEAR}" f"{Colors.RED}{Colors.BOLD}Could not find file: {path}{Colors.CLEAR}"
@ -200,6 +200,7 @@ class BlueprintApp:
formatted_files = 0 formatted_files = 0
for file in input_files: for file in input_files:
with open(file, "r+") as file:
data = file.read() data = file.read()
try: try:
@ -236,7 +237,8 @@ class BlueprintApp:
diff_lines.append("\\ No newline at end of file\n") diff_lines.append("\\ No newline at end of file\n")
print( print(
f"{''.join(diff_lines)}\n{Colors.BOLD}{happened} {file.name}{Colors.CLEAR}\n" # f"{''.join(diff_lines)}\n{Colors.BOLD}{happened} {file.name}{Colors.CLEAR}\n"
formatted_str
) )
formatted_files += 1 formatted_files += 1

View file

@ -1,6 +1,6 @@
# test_formatter.py # test_formatter.py
# #
# Copyright 2021 James Westman <james@jwestman.net> # Copyright 2023 James Westman <james@jwestman.net>
# #
# This file is free software; you can redistribute it and/or modify it # This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as # under the terms of the GNU Lesser General Public License as