mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
Formatter: Handle properties after child
This commit is contained in:
parent
ffff63da41
commit
e37e5180cf
1 changed files with 37 additions and 29 deletions
|
@ -50,31 +50,41 @@ class Format:
|
||||||
def format(data, tab_size=2, insert_space=True):
|
def format(data, tab_size=2, insert_space=True):
|
||||||
indent_levels = 0
|
indent_levels = 0
|
||||||
tokens = tokenizer.tokenize(data)
|
tokens = tokenizer.tokenize(data)
|
||||||
tokenized_str = ""
|
end_str = ""
|
||||||
last_not_whitespace = tokens[0]
|
last_not_whitespace = tokens[0]
|
||||||
current_line = ""
|
current_line = ""
|
||||||
prev_line_type = None
|
prev_line_type = None
|
||||||
is_child_type = False
|
is_child_type = False
|
||||||
indent_item = " " * tab_size if insert_space else "\t"
|
indent_item = " " * tab_size if insert_space else "\t"
|
||||||
|
|
||||||
|
def another_newline(one_indent_less=False):
|
||||||
|
nonlocal end_str
|
||||||
|
end_str = (
|
||||||
|
end_str.strip()
|
||||||
|
+ "\n\n"
|
||||||
|
+ (
|
||||||
|
indent_item
|
||||||
|
* (indent_levels - 1 if one_indent_less else indent_levels)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def commit_current_line(
|
def commit_current_line(
|
||||||
extra_newlines=1, line_type=prev_line_type, indent_decrease=False
|
two_newlines=False, line_type=prev_line_type, indent_decrease=False
|
||||||
):
|
):
|
||||||
nonlocal tokenized_str, current_line, prev_line_type
|
nonlocal end_str, current_line, prev_line_type
|
||||||
|
|
||||||
if indent_decrease:
|
if indent_decrease:
|
||||||
tokenized_str = (
|
end_str = end_str.strip() + "\n" + (indent_levels * indent_item)
|
||||||
tokenized_str.strip() + "\n" + (indent_levels * indent_item)
|
|
||||||
|
if two_newlines:
|
||||||
|
another_newline(
|
||||||
|
not (
|
||||||
|
current_line[-1] == ";"
|
||||||
|
and end_str.strip()[-1] in CLOSING_TOKENS
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if extra_newlines > 1:
|
end_str += current_line + "\n" + (indent_levels * indent_item)
|
||||||
tokenized_str = (
|
|
||||||
tokenized_str.strip()
|
|
||||||
+ ("\n" * (extra_newlines))
|
|
||||||
+ (indent_item * (indent_levels - 1))
|
|
||||||
)
|
|
||||||
|
|
||||||
tokenized_str += current_line + "\n" + (indent_levels * indent_item)
|
|
||||||
|
|
||||||
current_line = ""
|
current_line = ""
|
||||||
prev_line_type = line_type
|
prev_line_type = line_type
|
||||||
|
@ -114,11 +124,7 @@ class Format:
|
||||||
if is_child_type:
|
if is_child_type:
|
||||||
NO_WHITESPACE_BEFORE.append("]")
|
NO_WHITESPACE_BEFORE.append("]")
|
||||||
if str(last_not_whitespace) not in OPENING_TOKENS:
|
if str(last_not_whitespace) not in OPENING_TOKENS:
|
||||||
tokenized_str = (
|
another_newline()
|
||||||
tokenized_str.strip()
|
|
||||||
+ "\n\n"
|
|
||||||
+ (indent_levels * indent_item)
|
|
||||||
)
|
|
||||||
last_not_whitespace = item
|
last_not_whitespace = item
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
|
@ -127,10 +133,10 @@ class Format:
|
||||||
|
|
||||||
indent_levels += 1
|
indent_levels += 1
|
||||||
commit_current_line(
|
commit_current_line(
|
||||||
1
|
not (
|
||||||
if prev_line_type == LineType.CHILD_TYPE
|
prev_line_type == LineType.CHILD_TYPE
|
||||||
or tokenized_str.strip()[-1] == "{"
|
or end_str.strip()[-1] in OPENING_TOKENS
|
||||||
else 2,
|
),
|
||||||
LineType.BLOCK_OPEN,
|
LineType.BLOCK_OPEN,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -145,25 +151,27 @@ class Format:
|
||||||
|
|
||||||
if last_not_whitespace != ",":
|
if last_not_whitespace != ",":
|
||||||
current_line = current_line[:-1]
|
current_line = current_line[:-1]
|
||||||
commit_current_line(
|
commit_current_line()
|
||||||
1,
|
|
||||||
)
|
|
||||||
current_line = "]"
|
current_line = "]"
|
||||||
|
|
||||||
indent_levels -= 1
|
indent_levels -= 1
|
||||||
commit_current_line(
|
commit_current_line(
|
||||||
1,
|
line_type=LineType.CHILD_TYPE
|
||||||
LineType.CHILD_TYPE
|
|
||||||
if is_child_type
|
if is_child_type
|
||||||
else LineType.BLOCK_CLOSE,
|
else LineType.BLOCK_CLOSE,
|
||||||
not is_child_type,
|
indent_decrease=not is_child_type,
|
||||||
)
|
)
|
||||||
|
|
||||||
is_child_type = False
|
is_child_type = False
|
||||||
|
|
||||||
|
elif str_item == ";" and len(end_str) > 0:
|
||||||
|
commit_current_line(
|
||||||
|
two_newlines=end_str.strip()[-1] in CLOSING_TOKENS
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
commit_current_line()
|
commit_current_line()
|
||||||
|
|
||||||
last_not_whitespace = item
|
last_not_whitespace = item
|
||||||
|
|
||||||
return tokenized_str
|
return end_str
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue