Compare commits

...

6 commits

Author SHA1 Message Date
Matthijs Velsink
f8849d9084 Merge branch 'wip/velsinki/formatting-improve-comments' into 'main'
Improve comment formatting and Gtk.Scale fix

See merge request GNOME/blueprint-compiler!246
2025-06-15 09:52:22 +00:00
James Westman
6e010148b2 Remove donate link from README 2025-06-14 15:11:39 -05:00
Matthijs Velsink
edc17dc2df 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".
2025-06-13 00:10:57 +02:00
Matthijs Velsink
e16e723a81 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.
2025-06-13 00:10:54 +02:00
Matthijs Velsink
dc71762c9c 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.
2025-06-13 00:00:26 +02:00
Matthijs Velsink
ed867269dd 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.
2025-06-12 19:00:56 +02:00
7 changed files with 22 additions and 22 deletions

View file

@ -82,10 +82,6 @@ Visual Studio Code
- [Blueprint Language Plugin by bodil](https://github.com/bodil/vscode-blueprint)
## Donate
You can support my work on GitHub Sponsors! <https://github.com/sponsors/jameswestman>
## Getting in Touch
Matrix room: [#blueprint-language:matrix.org](https://matrix.to/#/#blueprint-language:matrix.org)

View file

@ -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)})"

View file

@ -179,24 +179,26 @@ 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
line_type = LineType.COMMENT
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
line_type = prev_line_type
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
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()

View file

@ -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 += "),"

View file

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

View file

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

View file

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