mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
formatter: Handle Inline comments
This commit is contained in:
parent
9cfacb9898
commit
c5fa33363f
2 changed files with 133 additions and 129 deletions
|
@ -57,6 +57,7 @@ class Formatter:
|
|||
watch_parentheses = False
|
||||
parentheses_balance = 0
|
||||
bracket_tracker = [None]
|
||||
last_whitespace_contains_newline = False
|
||||
|
||||
def commit_current_line(
|
||||
line_type=prev_line_type, redo_whitespace=False, newlines_before=1
|
||||
|
@ -67,7 +68,7 @@ class Formatter:
|
|||
whitespace_to_add = "\n" + indent_whitespace
|
||||
|
||||
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:
|
||||
end_str += indent_whitespace
|
||||
|
||||
|
@ -77,9 +78,12 @@ class Formatter:
|
|||
prev_line_type = line_type
|
||||
|
||||
for item in tokens:
|
||||
if item.type != TokenType.WHITESPACE:
|
||||
str_item = str(item)
|
||||
|
||||
if item.type == TokenType.WHITESPACE:
|
||||
last_whitespace_contains_newline = "\n" in str_item
|
||||
continue
|
||||
|
||||
whitespace_required = (
|
||||
str_item in WHITESPACE_BEFORE
|
||||
or str(last_not_whitespace) in WHITESPACE_AFTER
|
||||
|
@ -87,7 +91,7 @@ class Formatter:
|
|||
)
|
||||
whitespace_blockers = (
|
||||
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")
|
||||
)
|
||||
|
||||
|
@ -100,9 +104,7 @@ class Formatter:
|
|||
r"^([A-Za-z_\-])+(: bind)?$", current_line
|
||||
)
|
||||
|
||||
any_blockers = (
|
||||
whitespace_blockers or current_line_is_empty or is_function
|
||||
)
|
||||
any_blockers = whitespace_blockers or current_line_is_empty or is_function
|
||||
|
||||
if (whitespace_required or this_or_last_is_ident) and not any_blockers:
|
||||
current_line += " "
|
||||
|
@ -141,9 +143,7 @@ class Formatter:
|
|||
]
|
||||
if keep_same_indent:
|
||||
end_str = (
|
||||
end_str.strip()
|
||||
+ "\n\n"
|
||||
+ (indent_item * (indent_levels - 1))
|
||||
end_str.strip() + "\n\n" + indent_item * (indent_levels - 1)
|
||||
)
|
||||
commit_current_line(LineType.BLOCK_OPEN)
|
||||
|
||||
|
@ -183,16 +183,19 @@ class Formatter:
|
|||
]
|
||||
|
||||
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
|
||||
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)
|
||||
|
||||
|
@ -227,5 +230,6 @@ class Formatter:
|
|||
commit_current_line()
|
||||
|
||||
last_not_whitespace = item
|
||||
last_whitespace_contains_newline = False
|
||||
|
||||
return end_str.strip() + "\n"
|
||||
|
|
|
@ -33,7 +33,7 @@ template $MyTemplate: Label {
|
|||
// Single line comment.
|
||||
value: bind (1.0) as <double>;
|
||||
as: 1;
|
||||
signal => $on_signal() after;
|
||||
signal => $on_signal() after; // Inline comment
|
||||
type_value: typeof<$MyTemplate>;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue