Skip to content

Commit

Permalink
Added parentheses formatting to WHILE/CASE
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoRoos committed Mar 11, 2024
1 parent 6c28d26 commit 6958bfe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/tctools/format_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,22 +340,30 @@ def __init__(self, *args):

self._re_needs_parentheses = re.compile(
r"""
^ # Look for start of string or new line
\s*IF\s+ # Match IF with surrounding ws
([^(\r\n].+?) # Match any characters NOT starting with (
# We cannot match the closing bracket, as this could be
# from a function call
\s+THEN # Match THEN with preceding ws
# Look for start of string or new line:
^
# Match keyword with surrounding ws:
\s*(?:IF|WHILE|CASE)\s+
# Match any characters NOT starting with "("
# We cannot match the closing bracket, as this could be from a
# function call
([^(\r\n].+?)
# Match keyword with preceding ws:
\s+(?:THEN|DO|OF)
""",
re.VERBOSE | re.MULTILINE,
)

self._re_removes_parentheses = re.compile(
r"""
^ # Look for start of string or new line
\s*IF\s* # Match IF with surrounding ws
\((.+)\) # Match any characters whitin ()
\s*THEN # Match THEN with preceding ws
# Look for start of string or new line:
^
# Match IF with surrounding ws:
\s*(?:IF|WHILE|CASE)\s*
# Match any characters within ():
\((.+)\)
# Match THEN with preceding ws:
\s*(?:THEN|DO|OF)
""",
re.VERBOSE | re.MULTILINE,
)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_formatting_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,22 @@ def test_variable_align(content, expected, settings):
"IF (func(arg1 := 1, args2 := func2())) THEN\n",
],
),
(
[
"WHILE func() DO // comment!\n",
],
[
"WHILE (func()) DO // comment!\n",
],
),
(
[
"CASE idx OF\n",
],
[
"CASE (idx) OF\n",
],
),
# ( # This case fails, because we cannot identify matching parentheses:
# [
# "IF (1+1)*2 = 3*(x-1) THEN\n",
Expand Down

0 comments on commit 6958bfe

Please sign in to comment.