Add indent tracking to formatter

This commit is contained in:
gregorni 2023-07-16 11:47:47 +02:00
parent 84af660aad
commit 9f959fa186

View file

@ -179,12 +179,19 @@ class BlueprintApp:
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}"
) )
sys.exit(1)
formatted_files = 0 formatted_files = 0
newline_after = [";", "]"]
opening_tokens = ["{"]
closing_tokens = ["}"]
for file in input_files: for file in input_files:
data = file.read() data = file.read()
indent_levels = 0
try: try:
xml, warnings = self._compile(data) xml, warnings = self._compile(data)
@ -195,7 +202,22 @@ class BlueprintApp:
tokenized_str = "" tokenized_str = ""
for index, item in enumerate(tokens): for index, item in enumerate(tokens):
if item.type != tokenizer.TokenType.WHITESPACE:
tokenized_str += str(item) tokenized_str += str(item)
if str(item) in opening_tokens:
indent_levels += 1
try:
if str(tokens[index+1]) in closing_tokens:
indent_levels -= 1
except:
pass
if str(item) in newline_after + closing_tokens + opening_tokens:
tokenized_str += "\n"
tokenized_str += indent_levels * " "
else:
tokenized_str += " "
if data != tokenized_str: if data != tokenized_str:
happened = "Would reformat" happened = "Would reformat"