From ed867269dd3a004932abcf1903fe68d6b461a49b Mon Sep 17 00:00:00 2001 From: Matthijs Velsink Date: Thu, 12 Jun 2025 19:00:56 +0200 Subject: [PATCH] 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")