mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
Add indent tracking to formatter
This commit is contained in:
parent
84af660aad
commit
9f959fa186
1 changed files with 24 additions and 2 deletions
|
@ -179,12 +179,19 @@ class BlueprintApp:
|
|||
print(
|
||||
f"{Colors.RED}{Colors.BOLD}Could not find file: {path}{Colors.CLEAR}"
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
formatted_files = 0
|
||||
|
||||
newline_after = [";", "]"]
|
||||
|
||||
opening_tokens = ["{"]
|
||||
|
||||
closing_tokens = ["}"]
|
||||
|
||||
for file in input_files:
|
||||
data = file.read()
|
||||
indent_levels = 0
|
||||
|
||||
try:
|
||||
xml, warnings = self._compile(data)
|
||||
|
||||
|
@ -195,7 +202,22 @@ class BlueprintApp:
|
|||
|
||||
tokenized_str = ""
|
||||
for index, item in enumerate(tokens):
|
||||
if item.type != tokenizer.TokenType.WHITESPACE:
|
||||
tokenized_str += str(item)
|
||||
if str(item) in opening_tokens:
|
||||
indent_levels += 1
|
||||
|
||||
try:
|
||||
if str(tokens[index+1]) in closing_tokens:
|
||||
indent_levels -= 1
|
||||
except:
|
||||
pass
|
||||
|
||||
if str(item) in newline_after + closing_tokens + opening_tokens:
|
||||
tokenized_str += "\n"
|
||||
tokenized_str += indent_levels * " "
|
||||
else:
|
||||
tokenized_str += " "
|
||||
|
||||
if data != tokenized_str:
|
||||
happened = "Would reformat"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue