From b9f58aeab58e83f8285b310b9e329ca2f7a06b32 Mon Sep 17 00:00:00 2001 From: Alexey Yerin Date: Sat, 4 Jan 2025 10:20:54 +0300 Subject: [PATCH] Formatter: Add trailing commas in lists --- blueprintcompiler/formatter.py | 4 +++- tests/formatting/lists_in.blp | 21 +++++++++++++++++++++ tests/formatting/lists_out.blp | 31 +++++++++++++++++++++++++++++++ tests/formatting/out.blp | 2 +- tests/samples/string_array.blp | 2 +- tests/samples/style.blp | 2 +- tests/test_formatter.py | 1 + 7 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 tests/formatting/lists_in.blp create mode 100644 tests/formatting/lists_out.blp diff --git a/blueprintcompiler/formatter.py b/blueprintcompiler/formatter.py index 35da5d2..60d87b4 100644 --- a/blueprintcompiler/formatter.py +++ b/blueprintcompiler/formatter.py @@ -146,8 +146,10 @@ def format(data, tab_size=2, insert_space=True): is_child_type = False elif str_item in CLOSING_TOKENS: - if str_item == "]" and last_not_whitespace != ",": + if str_item == "]" and str(last_not_whitespace) != "[": current_line = current_line[:-1] + if str(last_not_whitespace) != ",": + current_line += "," commit_current_line() current_line = "]" elif str(last_not_whitespace) in OPENING_TOKENS: diff --git a/tests/formatting/lists_in.blp b/tests/formatting/lists_in.blp new file mode 100644 index 0000000..66b37a2 --- /dev/null +++ b/tests/formatting/lists_in.blp @@ -0,0 +1,21 @@ +using Gtk 4.0; + +Box { + styles [] +} + +Box { + styles ["a"] +} + +Box { + styles ["a",] +} + +Box { + styles ["a", "b"] +} + +Box { + styles ["a", "b",] +} diff --git a/tests/formatting/lists_out.blp b/tests/formatting/lists_out.blp new file mode 100644 index 0000000..7f1fe4a --- /dev/null +++ b/tests/formatting/lists_out.blp @@ -0,0 +1,31 @@ +using Gtk 4.0; + +Box { + styles [] +} + +Box { + styles [ + "a", + ] +} + +Box { + styles [ + "a", + ] +} + +Box { + styles [ + "a", + "b", + ] +} + +Box { + styles [ + "a", + "b", + ] +} diff --git a/tests/formatting/out.blp b/tests/formatting/out.blp index 9d9a8b4..b84c25f 100644 --- a/tests/formatting/out.blp +++ b/tests/formatting/out.blp @@ -11,7 +11,7 @@ Overlay { notify::icon-name => $on_icon_name_changed(label) swapped; styles [ - "destructive" + "destructive", ] } diff --git a/tests/samples/string_array.blp b/tests/samples/string_array.blp index 2542d17..85a68fe 100644 --- a/tests/samples/string_array.blp +++ b/tests/samples/string_array.blp @@ -5,6 +5,6 @@ AboutDialog about { authors: [ "Jane doe ", - "Jhonny D " + "Jhonny D ", ]; } diff --git a/tests/samples/style.blp b/tests/samples/style.blp index fe20f52..63720de 100644 --- a/tests/samples/style.blp +++ b/tests/samples/style.blp @@ -3,6 +3,6 @@ using Gtk 4.0; Label { styles [ "class-1", - "class-2" + "class-2", ] } diff --git a/tests/test_formatter.py b/tests/test_formatter.py index af4f9f5..a2cb60f 100644 --- a/tests/test_formatter.py +++ b/tests/test_formatter.py @@ -47,3 +47,4 @@ class TestFormatter(unittest.TestCase): self.assert_format_test("correct1.blp", "correct1.blp") self.assert_format_test("string_in.blp", "string_out.blp") self.assert_format_test("comment_in.blp", "comment_out.blp") + self.assert_format_test("lists_in.blp", "lists_out.blp")