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,9 +78,12 @@ 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)
if item.type == TokenType.WHITESPACE:
last_whitespace_contains_newline = "\n" in str_item
continue
whitespace_required = ( whitespace_required = (
str_item in WHITESPACE_BEFORE str_item in WHITESPACE_BEFORE
or str(last_not_whitespace) in WHITESPACE_AFTER or str(last_not_whitespace) in WHITESPACE_AFTER
@ -87,7 +91,7 @@ class Formatter:
) )
whitespace_blockers = ( whitespace_blockers = (
str_item in NO_WHITESPACE_BEFORE str_item in NO_WHITESPACE_BEFORE
or (str(last_not_whitespace) in NO_WHITESPACE_AFTER) or str(last_not_whitespace) in NO_WHITESPACE_AFTER
or (str_item == "<" and str(last_not_whitespace) == "typeof") or (str_item == "<" and str(last_not_whitespace) == "typeof")
) )
@ -100,9 +104,7 @@ class Formatter:
r"^([A-Za-z_\-])+(: bind)?$", current_line r"^([A-Za-z_\-])+(: bind)?$", current_line
) )
any_blockers = ( any_blockers = whitespace_blockers or current_line_is_empty or is_function
whitespace_blockers or current_line_is_empty or is_function
)
if (whitespace_required or this_or_last_is_ident) and not any_blockers: if (whitespace_required or this_or_last_is_ident) and not any_blockers:
current_line += " " current_line += " "
@ -141,9 +143,7 @@ class Formatter:
] ]
if keep_same_indent: if keep_same_indent:
end_str = ( end_str = (
end_str.strip() end_str.strip() + "\n\n" + indent_item * (indent_levels - 1)
+ "\n\n"
+ (indent_item * (indent_levels - 1))
) )
commit_current_line(LineType.BLOCK_OPEN) commit_current_line(LineType.BLOCK_OPEN)
@ -183,16 +183,19 @@ class Formatter:
] ]
single_line_comment = str_item.startswith("//") single_line_comment = str_item.startswith("//")
if (
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 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
elif prev_line_type == LineType.BLOCK_CLOSE:
newlines = 2
elif prev_line_type in require_extra_newline:
newlines = 2
commit_current_line(LineType.COMMENT, newlines_before=newlines) commit_current_line(LineType.COMMENT, newlines_before=newlines)
@ -227,5 +230,6 @@ class Formatter:
commit_current_line() commit_current_line()
last_not_whitespace = item last_not_whitespace = item
last_whitespace_contains_newline = False
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>;
} }