mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
formatter.py: bundle adding to end string into function
This commit is contained in:
parent
f539aeb93d
commit
d53e027610
1 changed files with 43 additions and 23 deletions
|
@ -49,6 +49,17 @@ class Format:
|
||||||
current_line = ""
|
current_line = ""
|
||||||
prev_line_type = None
|
prev_line_type = None
|
||||||
|
|
||||||
|
def commit_current_line(newlines, line_type, new_indent_levels):
|
||||||
|
nonlocal tokenized_str, current_line, prev_line_type
|
||||||
|
current_line += "\n" + (new_indent_levels * " ")
|
||||||
|
tokenized_str += (
|
||||||
|
current_line
|
||||||
|
if newlines == 0
|
||||||
|
else ("\n" * newlines) + (" " * indent_levels) + current_line
|
||||||
|
)
|
||||||
|
current_line = ""
|
||||||
|
prev_line_type = line_type
|
||||||
|
|
||||||
for item in tokens:
|
for item in tokens:
|
||||||
if item.type != tokenizer.TokenType.WHITESPACE:
|
if item.type != tokenizer.TokenType.WHITESPACE:
|
||||||
item_as_string = str(item)
|
item_as_string = str(item)
|
||||||
|
@ -64,7 +75,7 @@ class Format:
|
||||||
and str(last_not_whitespace) not in NO_WHITESPACE_AFTER
|
and str(last_not_whitespace) not in NO_WHITESPACE_AFTER
|
||||||
and item_as_string not in NO_WHITESPACE_BEFORE
|
and item_as_string not in NO_WHITESPACE_BEFORE
|
||||||
):
|
):
|
||||||
current_line = current_line + " "
|
current_line += " "
|
||||||
|
|
||||||
current_line += item_as_string
|
current_line += item_as_string
|
||||||
|
|
||||||
|
@ -73,43 +84,52 @@ 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 += (
|
# num_newlines = 1 if prev_line_type == LineType.CHILD_TYPE else 2
|
||||||
# ("\n" * num_newlines) + (indent_levels * " ") + current_line
|
# prev_line_type = LineType.BLOCK_OPEN
|
||||||
|
# tokenized_str = (
|
||||||
|
# tokenized_str.strip()
|
||||||
|
# + (num_newlines * "\n")
|
||||||
|
# + (indent_levels * " ")
|
||||||
# )
|
# )
|
||||||
|
# current_line += "\n" + (indent_levels * " ")
|
||||||
|
# tokenized_str += current_line
|
||||||
# current_line = ""
|
# current_line = ""
|
||||||
num_newlines = 1 if prev_line_type == LineType.CHILD_TYPE else 2
|
commit_current_line(
|
||||||
prev_line_type = LineType.BLOCK_OPEN
|
1 if prev_line_type == LineType.CHILD_TYPE else 2,
|
||||||
tokenized_str = (
|
LineType.BLOCK_OPEN,
|
||||||
tokenized_str.strip()
|
indent_levels + 1,
|
||||||
+ (num_newlines * "\n")
|
|
||||||
+ (indent_levels * " ")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
indent_levels += 1
|
indent_levels += 1
|
||||||
elif item_as_string in CLOSING_TOKENS:
|
elif item_as_string in CLOSING_TOKENS:
|
||||||
indent_levels -= 1
|
# tokenized_str = (
|
||||||
tokenized_str = (
|
# tokenized_str.strip() + "\n" + (indent_levels * " ")
|
||||||
tokenized_str.strip() + "\n" + (indent_levels * " ")
|
# )
|
||||||
)
|
# current_line += "\n" + (indent_levels * " ")
|
||||||
|
|
||||||
# tokenized_str += current_line
|
# tokenized_str += current_line
|
||||||
# current_line = ""
|
# current_line = ""
|
||||||
|
indent_levels -= 1
|
||||||
|
commit_current_line(
|
||||||
|
0,
|
||||||
|
# LineType.CHILD_TYPE
|
||||||
|
# if current_line.strip().startswith("[")
|
||||||
|
# else LineType.BLOCK_CLOSE,
|
||||||
|
LineType.BLOCK_CLOSE,
|
||||||
|
indent_levels - 1,
|
||||||
|
)
|
||||||
|
# indent_levels -= 1
|
||||||
|
|
||||||
# prev_line_type = (
|
# prev_line_type = (
|
||||||
# LineType.CHILD_TYPE
|
# LineType.CHILD_TYPE
|
||||||
# if current_line.strip().startswith("[")
|
# if current_line.strip().startswith("[")
|
||||||
# else LineType.BLOCK_CLOSE
|
# else LineType.BLOCK_CLOSE
|
||||||
# )
|
# )
|
||||||
|
|
||||||
current_line += "\n" + (indent_levels * " ")
|
else:
|
||||||
tokenized_str += current_line
|
commit_current_line(0, prev_line_type, indent_levels)
|
||||||
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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue