Merge branch 'wip/velsinki/formatting-improve-comments' into 'main'

Improve comment formatting

See merge request GNOME/blueprint-compiler!246
This commit is contained in:
Matthijs Velsink 2025-06-12 22:11:01 +00:00
commit ecd3305bcd
6 changed files with 22 additions and 18 deletions

View file

@ -375,7 +375,7 @@ def decompile_translatable(
comments = "" comments = ""
else: else:
comments = comments.replace("/*", " ").replace("*/", " ") comments = comments.replace("/*", " ").replace("*/", " ")
comments = f"/* Translators: {comments} */" comments = f"\n/* Translators: {comments} */"
if context is not None: if context is not None:
return comments, f"C_({escape_quote(context)}, {escape_quote(string)})" return comments, f"C_({escape_quote(context)}, {escape_quote(string)})"

View file

@ -179,24 +179,26 @@ def format(data, tab_size=2, insert_space=True):
) )
single_line_comment = str_item.startswith("//") single_line_comment = str_item.startswith("//")
newlines = 1 if single_line_comment and not str_item.startswith("// "):
if single_line_comment:
if not str_item.startswith("// "):
current_line = f"// {current_line[2:]}" current_line = f"// {current_line[2:]}"
if not last_whitespace_contains_newline: inline_comment = not last_whitespace_contains_newline
line_type = LineType.COMMENT
newlines = 1
if inline_comment:
current_line = " " + current_line current_line = " " + current_line
line_type = prev_line_type
newlines = 0 newlines = 0
elif prev_line_type == LineType.BLOCK_CLOSE: elif single_line_comment:
if prev_line_type == LineType.BLOCK_CLOSE:
newlines = 2 newlines = 2
elif prev_line_type in require_extra_newline: elif prev_line_type in require_extra_newline:
newlines = 2 newlines = 2
current_line = "\n".join( current_line = "\n".join(
[line.rstrip() for line in current_line.split("\n")] [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 else: # pragma: no cover
raise CompilerBugError() raise CompilerBugError()

View file

@ -167,8 +167,11 @@ def decompile_mark(
comments=None, comments=None,
context=None, context=None,
): ):
comments, translatable = decompile_translatable(
cdata, translatable, context, comments
)
if comments is not None: if comments is not None:
ctx.print(f"/* Translators: {comments} */") ctx.print(comments)
text = f"mark ({value}" text = f"mark ({value}"
@ -177,10 +180,7 @@ def decompile_mark(
elif cdata: elif cdata:
text += f", bottom" text += f", bottom"
if truthy(translatable): if cdata:
comments, translatable = decompile_translatable(
cdata, translatable, context, comments
)
text += f", {translatable}" text += f", {translatable}"
text += ")," text += "),"

View file

@ -46,7 +46,7 @@ menu menu {
item ("test") item ("test")
item { item {
label: "test"; label: "test"; /* Different inline comment style */
} }
item ("test") item ("test")
@ -59,7 +59,7 @@ Adw.MessageDialog {
} }
Adw.Breakpoint { Adw.Breakpoint {
condition ("width < 100") condition ("width < 100") // Another inline comment
setters { setters {
label2.label: _("Hello, world!"); label2.label: _("Hello, world!");

View file

@ -4,6 +4,7 @@ Scale {
marks [ marks [
mark (-1, bottom), mark (-1, bottom),
mark (0, top, _("Hello, world!")), mark (0, top, _("Hello, world!")),
mark (1, bottom, "1"),
mark (2), mark (2),
] ]
} }

View file

@ -10,6 +10,7 @@ corresponding .blp file and regenerate this file with blueprint-compiler.
<marks> <marks>
<mark value="-1" position="bottom"></mark> <mark value="-1" position="bottom"></mark>
<mark value="0" position="top" translatable="yes">Hello, world!</mark> <mark value="0" position="top" translatable="yes">Hello, world!</mark>
<mark value="1" position="bottom">1</mark>
<mark value="2"></mark> <mark value="2"></mark>
</marks> </marks>
</object> </object>