Skip to content

Commit

Permalink
Fix bug with comments in commands with condition syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankSpruce committed May 27, 2024
1 parent 6cf1a5b commit fa4e0e0
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit fa4e0e0

Please sign in to comment.