From 825f6e3b152d0d90c709735546ca220b21f237bc Mon Sep 17 00:00:00 2001 From: gregorni Date: Fri, 8 Sep 2023 09:56:52 +0200 Subject: [PATCH] Formatter: adapt to unittest --- blueprintcompiler/formatter.py | 23 ++++++++++++++++++----- tests/formatting/out.blp | 4 +++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/blueprintcompiler/formatter.py b/blueprintcompiler/formatter.py index c25c4cc..e509396 100644 --- a/blueprintcompiler/formatter.py +++ b/blueprintcompiler/formatter.py @@ -26,11 +26,17 @@ CLOSING_TOKENS = ["}", "]"] NEWLINE_AFTER = [";"] + OPENING_TOKENS + CLOSING_TOKENS -NO_WHITESPACE_BEFORE = [",", ":", ";", ")", "."] +NO_WHITESPACE_BEFORE = [",", ":", "::", ";", ")", ".", ">"] NO_WHITESPACE_AFTER = ["C_", "_", "("] -WHITESPACE_AFTER = [":", ","] -WHITESPACE_BEFORE = ["{"] +# NO_WHITESPACE_BEFORE takes precedence over WHITESPACE_AFTER +WHITESPACE_AFTER = [ + ":", + ",", + ">", + ")", +] +WHITESPACE_BEFORE = ["{", "$"] class LineType(Enum): @@ -77,7 +83,11 @@ class Format: if item.type != tokenizer.TokenType.WHITESPACE: str_item = str(item) if item.type == tokenizer.TokenType.QUOTED and str_item.startswith('"'): - str_item = ("'" + str_item[1:-1] + "'").replace('\\"', '"') + str_item = ( + "'" + + str_item[1:-1].replace('\\"', '"').replace("'", "\\'") + + "'" + ) if ( str_item in WHITESPACE_BEFORE @@ -117,7 +127,10 @@ class Format: indent_levels += 1 commit_current_line( - 1 if prev_line_type == LineType.CHILD_TYPE else 2, + 1 + if prev_line_type == LineType.CHILD_TYPE + or tokenized_str.strip()[-1] == "{" + else 2, LineType.BLOCK_OPEN, ) diff --git a/tests/formatting/out.blp b/tests/formatting/out.blp index 7f9642f..a46e62d 100644 --- a/tests/formatting/out.blp +++ b/tests/formatting/out.blp @@ -10,7 +10,9 @@ Overlay { Button { notify::icon-name => $on_icon_name_changed (label) swapped; - styles ['destructive'] + styles [ + 'destructive' + ] } visible: bind $isVisible (label.visible, my-menu) as ;