From ed867269dd3a004932abcf1903fe68d6b461a49b Mon Sep 17 00:00:00 2001 From: Matthijs Velsink Date: Thu, 12 Jun 2025 19:00:56 +0200 Subject: [PATCH 1/4] formatter: Also allow /*...*/ as inline comments Blueprint allows both `//` and `/*...*/` style comments, but if a project prefers only `/*...*/` comments, it is currently not possible to have these inline. Therefore, treat these comments equal if they occur inline. To make this easier to understand, we refactor the comment handling slightly to first handle single-line comment whitespace, and then handle newlines for both single-line and multi-line style comments. Adjust the test accordingly to make sure this works. --- blueprintcompiler/formatter.py | 18 +++++++++--------- tests/formatting/correct1.blp | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/blueprintcompiler/formatter.py b/blueprintcompiler/formatter.py index f438675..bfff8f4 100644 --- a/blueprintcompiler/formatter.py +++ b/blueprintcompiler/formatter.py @@ -179,17 +179,17 @@ def format(data, tab_size=2, insert_space=True): ) single_line_comment = str_item.startswith("//") + if single_line_comment and not str_item.startswith("// "): + current_line = f"// {current_line[2:]}" + + inline_comment = not last_whitespace_contains_newline newlines = 1 - if single_line_comment: - if not str_item.startswith("// "): - current_line = f"// {current_line[2:]}" - - if not last_whitespace_contains_newline: - current_line = " " + current_line - newlines = 0 - elif prev_line_type == LineType.BLOCK_CLOSE: + if inline_comment: + current_line = " " + current_line + newlines = 0 + elif single_line_comment: + if prev_line_type == LineType.BLOCK_CLOSE: newlines = 2 - elif prev_line_type in require_extra_newline: newlines = 2 diff --git a/tests/formatting/correct1.blp b/tests/formatting/correct1.blp index aedc38a..0101507 100644 --- a/tests/formatting/correct1.blp +++ b/tests/formatting/correct1.blp @@ -46,7 +46,7 @@ menu menu { item ("test") item { - label: "test"; + label: "test"; /* Different inline comment style */ } item ("test") From dc71762c9c28c5797c6eeb89f83697c693577510 Mon Sep 17 00:00:00 2001 From: Matthijs Velsink Date: Thu, 12 Jun 2025 19:22:09 +0200 Subject: [PATCH 2/4] decompiler: Force newline before translator comments The decompiler does not insert newlines and so relies on the formatter to format its output. The previous commit however broke translator comments, as they are now assumed to be inline. Therefore, make sure the decompiler forces a newline before a translator comment. We could special case such a comment in the formatter, but this might be difficult if #202 gets resolved, whereas in the decompiler we already know exactly when a comment is a translator comment. --- blueprintcompiler/decompiler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blueprintcompiler/decompiler.py b/blueprintcompiler/decompiler.py index 850b6d8..072aa89 100644 --- a/blueprintcompiler/decompiler.py +++ b/blueprintcompiler/decompiler.py @@ -375,7 +375,7 @@ def decompile_translatable( comments = "" else: comments = comments.replace("/*", " ").replace("*/", " ") - comments = f"/* Translators: {comments} */" + comments = f"\n/* Translators: {comments} */" if context is not None: return comments, f"C_({escape_quote(context)}, {escape_quote(string)})" From e16e723a813d5a0f8713fb3e8550464bc4bdd2d5 Mon Sep 17 00:00:00 2001 From: Matthijs Velsink Date: Thu, 12 Jun 2025 20:36:54 +0200 Subject: [PATCH 3/4] scale: Decompile non-translatable labels too While working on the previous commit, I noticed Gtk.Scale specially handles translator comments, but more importantly, that it does not put non-translatable labels in its decompiled output. This is wrong, as it's not unlikely that Gtk.Scale marks would use a purely numeric string, which should not be lost upon decompilation. Add a test for this case too. --- blueprintcompiler/language/gtk_scale.py | 10 +++++----- tests/samples/scale_marks.blp | 1 + tests/samples/scale_marks.ui | 1 + 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/blueprintcompiler/language/gtk_scale.py b/blueprintcompiler/language/gtk_scale.py index 1fd5ac3..77166b8 100644 --- a/blueprintcompiler/language/gtk_scale.py +++ b/blueprintcompiler/language/gtk_scale.py @@ -167,8 +167,11 @@ def decompile_mark( comments=None, context=None, ): + comments, translatable = decompile_translatable( + cdata, translatable, context, comments + ) if comments is not None: - ctx.print(f"/* Translators: {comments} */") + ctx.print(comments) text = f"mark ({value}" @@ -177,10 +180,7 @@ def decompile_mark( elif cdata: text += f", bottom" - if truthy(translatable): - comments, translatable = decompile_translatable( - cdata, translatable, context, comments - ) + if cdata: text += f", {translatable}" text += ")," diff --git a/tests/samples/scale_marks.blp b/tests/samples/scale_marks.blp index a766cfa..7b0c573 100644 --- a/tests/samples/scale_marks.blp +++ b/tests/samples/scale_marks.blp @@ -4,6 +4,7 @@ Scale { marks [ mark (-1, bottom), mark (0, top, _("Hello, world!")), + mark (1, bottom, "1"), mark (2), ] } diff --git a/tests/samples/scale_marks.ui b/tests/samples/scale_marks.ui index c08afb4..f50443a 100644 --- a/tests/samples/scale_marks.ui +++ b/tests/samples/scale_marks.ui @@ -10,6 +10,7 @@ corresponding .blp file and regenerate this file with blueprint-compiler. Hello, world! + 1 From edc17dc2df649f767f21e105ae2842c702c89934 Mon Sep 17 00:00:00 2001 From: Matthijs Velsink Date: Thu, 12 Jun 2025 20:40:08 +0200 Subject: [PATCH 4/4] formatter: Make inline comments "transparent" Adding an inline comment somewhere should not affect formatting below it. So, for inline comments, don't make the line type a comment, but instead reuse the current line type so that the comment becomes "transparent". --- blueprintcompiler/formatter.py | 4 +++- tests/formatting/correct1.blp | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/blueprintcompiler/formatter.py b/blueprintcompiler/formatter.py index bfff8f4..aebcace 100644 --- a/blueprintcompiler/formatter.py +++ b/blueprintcompiler/formatter.py @@ -183,9 +183,11 @@ def format(data, tab_size=2, insert_space=True): current_line = f"// {current_line[2:]}" inline_comment = not last_whitespace_contains_newline + line_type = LineType.COMMENT newlines = 1 if inline_comment: current_line = " " + current_line + line_type = prev_line_type newlines = 0 elif single_line_comment: if prev_line_type == LineType.BLOCK_CLOSE: @@ -196,7 +198,7 @@ def format(data, tab_size=2, insert_space=True): current_line = "\n".join( [line.rstrip() for line in current_line.split("\n")] ) - commit_current_line(LineType.COMMENT, newlines_before=newlines) + commit_current_line(line_type, newlines_before=newlines) else: # pragma: no cover raise CompilerBugError() diff --git a/tests/formatting/correct1.blp b/tests/formatting/correct1.blp index 0101507..56286b4 100644 --- a/tests/formatting/correct1.blp +++ b/tests/formatting/correct1.blp @@ -59,7 +59,7 @@ Adw.MessageDialog { } Adw.Breakpoint { - condition ("width < 100") + condition ("width < 100") // Another inline comment setters { label2.label: _("Hello, world!");