From f539aeb93d3251e709c6db604ce8454d3e21534e Mon Sep 17 00:00:00 2001 From: gregorni Date: Mon, 21 Aug 2023 23:44:58 +0200 Subject: [PATCH] Formatter: Improve inserting whitespace --- blueprintcompiler/formatter.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/blueprintcompiler/formatter.py b/blueprintcompiler/formatter.py index 9ecc602..f1209af 100644 --- a/blueprintcompiler/formatter.py +++ b/blueprintcompiler/formatter.py @@ -45,7 +45,7 @@ class Format: indent_levels = 0 tokens = tokenizer.tokenize(data) tokenized_str = "" - last_not_whitespace = None # To make line 56 not fail + last_not_whitespace = tokens[0] # To make line 56 not fail current_line = "" prev_line_type = None @@ -53,13 +53,18 @@ class Format: if item.type != tokenizer.TokenType.WHITESPACE: item_as_string = str(item) - if item_as_string in WHITESPACE_BEFORE: - current_line = current_line.strip() + " " - elif ( - item_as_string in NO_WHITESPACE_BEFORE - or str(last_not_whitespace) in NO_WHITESPACE_AFTER + if ( + item_as_string in WHITESPACE_BEFORE + and str(last_not_whitespace) not in NO_WHITESPACE_AFTER + ) or ( + ( + str(last_not_whitespace) in WHITESPACE_AFTER + or last_not_whitespace.type == tokenizer.TokenType.IDENT + ) + and str(last_not_whitespace) not in NO_WHITESPACE_AFTER + and item_as_string not in NO_WHITESPACE_BEFORE ): - current_line = current_line.strip() + current_line = current_line + " " current_line += item_as_string @@ -100,12 +105,6 @@ class Format: tokenized_str += current_line current_line = "" - elif ( - item_as_string in WHITESPACE_AFTER - or item.type == tokenizer.TokenType.IDENT - ): - current_line += " " - last_not_whitespace = item # else: