formatter: Handle Inline comments

This commit is contained in:
gregorni 2023-12-13 02:12:50 +00:00 committed by James Westman
parent 9cfacb9898
commit c5fa33363f
2 changed files with 133 additions and 129 deletions

View file

@ -57,6 +57,7 @@ class Formatter:
watch_parentheses = False watch_parentheses = False
parentheses_balance = 0 parentheses_balance = 0
bracket_tracker = [None] bracket_tracker = [None]
last_whitespace_contains_newline = False
def commit_current_line( def commit_current_line(
line_type=prev_line_type, redo_whitespace=False, newlines_before=1 line_type=prev_line_type, redo_whitespace=False, newlines_before=1
@ -67,7 +68,7 @@ class Formatter:
whitespace_to_add = "\n" + indent_whitespace whitespace_to_add = "\n" + indent_whitespace
if redo_whitespace or newlines_before != 1: if redo_whitespace or newlines_before != 1:
end_str = end_str.strip() + ("\n" * newlines_before) end_str = end_str.strip() + "\n" * newlines_before
if newlines_before > 0: if newlines_before > 0:
end_str += indent_whitespace end_str += indent_whitespace
@ -77,155 +78,158 @@ class Formatter:
prev_line_type = line_type prev_line_type = line_type
for item in tokens: for item in tokens:
if item.type != TokenType.WHITESPACE: str_item = str(item)
str_item = str(item)
whitespace_required = ( if item.type == TokenType.WHITESPACE:
str_item in WHITESPACE_BEFORE last_whitespace_contains_newline = "\n" in str_item
or str(last_not_whitespace) in WHITESPACE_AFTER continue
or (str_item == "(" and end_str.endswith(": bind"))
)
whitespace_blockers = (
str_item in NO_WHITESPACE_BEFORE
or (str(last_not_whitespace) in NO_WHITESPACE_AFTER)
or (str_item == "<" and str(last_not_whitespace) == "typeof")
)
this_or_last_is_ident = ( whitespace_required = (
item.type == TokenType.IDENT str_item in WHITESPACE_BEFORE
or last_not_whitespace.type == TokenType.IDENT or str(last_not_whitespace) in WHITESPACE_AFTER
) or (str_item == "(" and end_str.endswith(": bind"))
current_line_is_empty = len(current_line) == 0 )
is_function = str_item == "(" and not re.match( whitespace_blockers = (
r"^([A-Za-z_\-])+(: bind)?$", current_line str_item in NO_WHITESPACE_BEFORE
) or str(last_not_whitespace) in NO_WHITESPACE_AFTER
or (str_item == "<" and str(last_not_whitespace) == "typeof")
)
any_blockers = ( this_or_last_is_ident = (
whitespace_blockers or current_line_is_empty or is_function item.type == TokenType.IDENT
) or last_not_whitespace.type == TokenType.IDENT
)
current_line_is_empty = len(current_line) == 0
is_function = str_item == "(" and not re.match(
r"^([A-Za-z_\-])+(: bind)?$", current_line
)
if (whitespace_required or this_or_last_is_ident) and not any_blockers: any_blockers = whitespace_blockers or current_line_is_empty or is_function
current_line += " "
current_line += str_item if (whitespace_required or this_or_last_is_ident) and not any_blockers:
current_line += " "
if str_item in ["[", "("]: current_line += str_item
bracket_tracker.append(str_item)
elif str_item in ["]", ")"]:
bracket_tracker.pop()
needs_newline_treatment = ( if str_item in ["[", "("]:
str_item in NEWLINE_AFTER or item.type == TokenType.COMMENT bracket_tracker.append(str_item)
) elif str_item in ["]", ")"]:
if needs_newline_treatment: bracket_tracker.pop()
if str_item in OPENING_TOKENS:
list_or_child_type = str_item == "["
if list_or_child_type:
is_child_type = current_line.startswith("[")
if is_child_type: needs_newline_treatment = (
if str(last_not_whitespace) not in OPENING_TOKENS: str_item in NEWLINE_AFTER or item.type == TokenType.COMMENT
end_str = ( )
end_str.strip() if needs_newline_treatment:
+ "\n\n" if str_item in OPENING_TOKENS:
+ (indent_item * indent_levels) list_or_child_type = str_item == "["
) if list_or_child_type:
last_not_whitespace = item is_child_type = current_line.startswith("[")
continue
indent_levels += 1 if is_child_type:
keep_same_indent = not prev_line_type in [ if str(last_not_whitespace) not in OPENING_TOKENS:
LineType.CHILD_TYPE, end_str = (
LineType.COMMENT, end_str.strip()
LineType.BLOCK_OPEN, + "\n\n"
] + (indent_item * indent_levels)
if keep_same_indent: )
end_str = ( last_not_whitespace = item
end_str.strip() continue
+ "\n\n"
+ (indent_item * (indent_levels - 1))
)
commit_current_line(LineType.BLOCK_OPEN)
elif str_item == "]" and is_child_type: indent_levels += 1
commit_current_line(LineType.CHILD_TYPE, False) keep_same_indent = not prev_line_type in [
is_child_type = False LineType.CHILD_TYPE,
LineType.COMMENT,
LineType.BLOCK_OPEN,
]
if keep_same_indent:
end_str = (
end_str.strip() + "\n\n" + indent_item * (indent_levels - 1)
)
commit_current_line(LineType.BLOCK_OPEN)
elif str_item in CLOSING_TOKENS: elif str_item == "]" and is_child_type:
if str_item == "]" and last_not_whitespace != ",": commit_current_line(LineType.CHILD_TYPE, False)
current_line = current_line[:-1] is_child_type = False
commit_current_line()
current_line = "]"
elif str(last_not_whitespace) in OPENING_TOKENS:
end_str = end_str.strip()
commit_current_line(LineType.BLOCK_CLOSE, True, 0)
indent_levels -= 1 elif str_item in CLOSING_TOKENS:
commit_current_line(LineType.BLOCK_CLOSE, True) if str_item == "]" and last_not_whitespace != ",":
current_line = current_line[:-1]
commit_current_line()
current_line = "]"
elif str(last_not_whitespace) in OPENING_TOKENS:
end_str = end_str.strip()
commit_current_line(LineType.BLOCK_CLOSE, True, 0)
elif str_item == ";": indent_levels -= 1
line_type = LineType.STATEMENT commit_current_line(LineType.BLOCK_CLOSE, True)
if len(current_line) == 1:
elif str_item == ";":
line_type = LineType.STATEMENT
if len(current_line) == 1:
newlines = 0
line_type = LineType.BLOCK_CLOSE
elif prev_line_type == LineType.BLOCK_CLOSE:
newlines = 2
else:
newlines = 1
commit_current_line(line_type, newlines_before=newlines)
elif item.type == TokenType.COMMENT:
require_extra_newline = [
LineType.BLOCK_CLOSE,
LineType.STATEMENT,
LineType.COMMENT,
]
single_line_comment = str_item.startswith("//")
newlines = 1
if single_line_comment:
if not str_item.startswith("// "):
current_line = f"// {current_line[2:]}"
if not last_whitespace_contains_newline:
current_line = " " + current_line
newlines = 0 newlines = 0
line_type = LineType.BLOCK_CLOSE
elif prev_line_type == LineType.BLOCK_CLOSE: elif prev_line_type == LineType.BLOCK_CLOSE:
newlines = 2 newlines = 2
else:
newlines = 1
commit_current_line(line_type, newlines_before=newlines) elif prev_line_type in require_extra_newline:
newlines = 2
elif item.type == TokenType.COMMENT: commit_current_line(LineType.COMMENT, newlines_before=newlines)
require_extra_newline = [
LineType.BLOCK_CLOSE,
LineType.STATEMENT,
LineType.COMMENT,
]
single_line_comment = str_item.startswith("//") else:
if ( commit_current_line()
single_line_comment
and prev_line_type == LineType.BLOCK_CLOSE
) or (
not single_line_comment
and prev_line_type in require_extra_newline
):
newlines = 2
else:
newlines = 1
commit_current_line(LineType.COMMENT, newlines_before=newlines) elif str_item == "(" and (
re.match("^([A-Za-z_\-])+\s*\(", current_line) or watch_parentheses
):
watch_parentheses = True
parentheses_balance += 1
else: elif str_item == ")" and watch_parentheses:
commit_current_line() parentheses_balance -= 1
all_parentheses_closed = parentheses_balance == 0
if all_parentheses_closed:
commit_current_line(
newlines_before=2
if prev_line_type == LineType.BLOCK_CLOSE
else 1
)
watch_parentheses = False
elif str_item == "(" and ( tracker_is_empty = len(bracket_tracker) > 0
re.match("^([A-Za-z_\-])+\s*\(", current_line) or watch_parentheses if tracker_is_empty:
): last_in_tracker = bracket_tracker[-1]
watch_parentheses = True is_list_comma = last_in_tracker == "[" and str_item == ","
parentheses_balance += 1 if is_list_comma:
last_was_list_item = end_str.strip()[-1] not in ["[", ","]
if last_was_list_item:
end_str = end_str.strip()
commit_current_line()
elif str_item == ")" and watch_parentheses: last_not_whitespace = item
parentheses_balance -= 1 last_whitespace_contains_newline = False
all_parentheses_closed = parentheses_balance == 0
if all_parentheses_closed:
commit_current_line(
newlines_before=2
if prev_line_type == LineType.BLOCK_CLOSE
else 1
)
watch_parentheses = False
tracker_is_empty = len(bracket_tracker) > 0
if tracker_is_empty:
last_in_tracker = bracket_tracker[-1]
is_list_comma = last_in_tracker == "[" and str_item == ","
if is_list_comma:
last_was_list_item = end_str.strip()[-1] not in ["[", ","]
if last_was_list_item:
end_str = end_str.strip()
commit_current_line()
last_not_whitespace = item
return end_str.strip() + "\n" return end_str.strip() + "\n"

View file

@ -33,7 +33,7 @@ template $MyTemplate: Label {
// Single line comment. // Single line comment.
value: bind (1.0) as <double>; value: bind (1.0) as <double>;
as: 1; as: 1;
signal => $on_signal() after; signal => $on_signal() after; // Inline comment
type_value: typeof<$MyTemplate>; type_value: typeof<$MyTemplate>;
} }