diff --git a/gersemi/command_invocation_dumpers/condition_syntax_command_invocation_dumper.py b/gersemi/command_invocation_dumpers/condition_syntax_command_invocation_dumper.py index 6760b6e..d5c42fa 100644 --- a/gersemi/command_invocation_dumpers/condition_syntax_command_invocation_dumper.py +++ b/gersemi/command_invocation_dumpers/condition_syntax_command_invocation_dumper.py @@ -1,7 +1,7 @@ from typing import List from lark import Tree from lark.visitors import Transformer, TransformerChain, Transformer_InPlace -from gersemi.ast_helpers import contains_line_comment, is_one_of_keywords +from gersemi.ast_helpers import contains_line_comment, is_one_of_keywords, is_comment from gersemi.base_command_invocation_dumper import BaseCommandInvocationDumper from gersemi.configuration import Spaces from gersemi.types import Nodes @@ -20,7 +20,11 @@ def arguments(self, children: Nodes) -> Tree: is_one_of_unary_operators = is_one_of_keywords(self.unary_operators) for one_behind, current in iterator: if is_one_of_unary_operators(one_behind): - new_children += [Tree("unary_operation", [one_behind, current])] + if is_comment(current): + new_children += [Tree("unary_operation", [one_behind])] + new_children += [current] + else: + new_children += [Tree("unary_operation", [one_behind, current])] _, current = advance(iterator, times=1, default=(None, None)) if current is None: break @@ -125,9 +129,12 @@ def unary_operation(self, tree): if result is not None: return result - operation, arg = tree.children + operation, *rest = tree.children formatted_operation = self.visit(operation) + if len(rest) == 0: + return formatted_operation + arg, *_ = rest if ( (not contains_line_comment([operation])) and isinstance(self.indent_type, Spaces) @@ -151,10 +158,6 @@ def binary_operation(self, tree): formatted_rhs = self.visit(rhs) return f"{formatted_lhs}\n{formatted_operation}\n{formatted_rhs}" - def arguments(self, tree): - preprocessed = IsolateConditions().transform(tree) - return super().arguments(preprocessed) - def _preprocess_arguments(self, arguments): return IsolateConditions().transform(arguments) diff --git a/tests/formatter/comments_in_commands_with_condition_syntax.in.cmake b/tests/formatter/comments_in_commands_with_condition_syntax.in.cmake new file mode 100644 index 0000000..7c9ee1b --- /dev/null +++ b/tests/formatter/comments_in_commands_with_condition_syntax.in.cmake @@ -0,0 +1,59 @@ +if( + FOO STREQUAL "bar" OR + + # line comment + FOO STREQUAL "baz" +) +endif() + + +if( + FOO STREQUAL "bar" OR + + # line comment + # with + # multiple lines + FOO STREQUAL "baz" +) +endif() + +if( + FOO STREQUAL "bar" OR + + # line comment + # with + # multiple lines + + FOO STREQUAL "baz" +) +endif() + +if( + FOO STREQUAL "bar" OR + + #[ bracket comment ] + FOO STREQUAL "baz" +) +endif() + +if( + FOO STREQUAL "bar" OR + + #[[ bracket comment + with + multiple lines ]] + FOO STREQUAL "baz" +) +endif() + + +if( + FOO STREQUAL "bar" OR + + #[[ bracket comment + with + multiple lines ]] + + FOO STREQUAL "baz" +) +endif() diff --git a/tests/formatter/comments_in_commands_with_condition_syntax.out.cmake b/tests/formatter/comments_in_commands_with_condition_syntax.out.cmake new file mode 100644 index 0000000..0179efa --- /dev/null +++ b/tests/formatter/comments_in_commands_with_condition_syntax.out.cmake @@ -0,0 +1,55 @@ +if( + FOO STREQUAL "bar" + OR + # line comment + FOO STREQUAL "baz" +) +endif() + +if( + FOO STREQUAL "bar" + OR + # line comment + # with + # multiple lines + FOO STREQUAL "baz" +) +endif() + +if( + FOO STREQUAL "bar" + OR + # line comment + # with + # multiple lines + FOO STREQUAL "baz" +) +endif() + +if( + FOO STREQUAL "bar" + OR + #[ bracket comment ] + FOO STREQUAL "baz" +) +endif() + +if( + FOO STREQUAL "bar" + OR + #[[ bracket comment + with + multiple lines ]] + FOO STREQUAL "baz" +) +endif() + +if( + FOO STREQUAL "bar" + OR + #[[ bracket comment + with + multiple lines ]] + FOO STREQUAL "baz" +) +endif()