formatter.py: put more code into commit_current_line

This commit is contained in:
gregorni 2023-08-23 00:02:22 +02:00
parent aee36ca48b
commit c094e863f7

View file

@ -49,14 +49,23 @@ class Format:
current_line = "" current_line = ""
prev_line_type = None prev_line_type = None
def commit_current_line(extra_newlines, line_type, new_indent_levels): def commit_current_line(
extra_newlines=0, line_type=prev_line_type, indent_decrease=False
):
nonlocal tokenized_str, current_line, prev_line_type nonlocal tokenized_str, current_line, prev_line_type
current_line += "\n" + (new_indent_levels * " ")
if indent_decrease:
tokenized_str = tokenized_str.strip() + "\n" + (indent_levels * " ")
current_line += "\n" + (indent_levels * " ")
tokenized_str += ( tokenized_str += (
current_line current_line
if extra_newlines == 0 if extra_newlines == 0
else ("\n" * extra_newlines) + (" " * indent_levels) + current_line else ("\n" * extra_newlines)
+ (" " * (indent_levels - 1))
+ current_line
) )
current_line = "" current_line = ""
prev_line_type = line_type prev_line_type = line_type
@ -84,43 +93,27 @@ class Format:
or item.type == tokenizer.TokenType.COMMENT or item.type == tokenizer.TokenType.COMMENT
): ):
if item_as_string in OPENING_TOKENS: if item_as_string in OPENING_TOKENS:
# tokenized_str += ( indent_levels += 1
# ("\n" * num_newlines) + (indent_levels * " ") + current_line commit_current_line(
# ) 0 if prev_line_type == LineType.CHILD_TYPE else 1,
# current_line = "" LineType.BLOCK_OPEN,
num_newlines = 1 if prev_line_type == LineType.CHILD_TYPE else 2
prev_line_type = LineType.BLOCK_OPEN
tokenized_str = (
tokenized_str.strip()
+ (num_newlines * "\n")
+ (indent_levels * " ")
) )
indent_levels += 1
elif item_as_string in CLOSING_TOKENS: elif item_as_string in CLOSING_TOKENS:
indent_levels -= 1 indent_levels -= 1
tokenized_str = (
tokenized_str.strip() + "\n" + (indent_levels * " ") commit_current_line(
0,
LineType.CHILD_TYPE
if current_line.strip().startswith("[")
else LineType.BLOCK_CLOSE,
True,
) )
else:
# tokenized_str += current_line commit_current_line()
# current_line = ""
# prev_line_type = (
# LineType.CHILD_TYPE
# if current_line.strip().startswith("[")
# else LineType.BLOCK_CLOSE
# )
current_line += "\n" + (indent_levels * " ")
tokenized_str += current_line
current_line = ""
last_not_whitespace = item last_not_whitespace = item
# else:
# current_line = current_line.strip() + " "
print(tokenized_str) # TODO: Remove this when the MR is ready to be merged print(tokenized_str) # TODO: Remove this when the MR is ready to be merged
return tokenized_str return tokenized_str