Skip to content

Commit

Permalink
Merge branch 'multiline-patterns'
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Dec 6, 2023
2 parents 07ad42d + 7c3ea13 commit f97551e
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 16 deletions.
23 changes: 22 additions & 1 deletion gdtoolkit/formatter/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,27 @@ def _format_foldable_to_multiple_lines(
"dot_chain": _format_dot_chain_to_multiple_lines,
"actual_getattr_call": _format_call_expression_to_multiple_lines,
"actual_subscr_expr": _format_subscription_to_multiple_lines,
# patterns (fake expressions):
"list_pattern": _format_concrete_expression_to_single_line,
"test_pattern": _format_operator_chain_based_expression_to_multiple_lines,
"par_pattern": _format_parentheses_to_multiple_lines,
"or_pattern": _format_operator_chain_based_expression_to_multiple_lines,
"and_pattern": _format_operator_chain_based_expression_to_multiple_lines,
"not_pattern": _format_concrete_expression_to_single_line,
"comp_pattern": _format_operator_chain_based_expression_to_multiple_lines,
"bitw_or_pattern": _format_operator_chain_based_expression_to_multiple_lines,
"bitw_xor_pattern": _format_operator_chain_based_expression_to_multiple_lines,
"bitw_and_pattern": _format_operator_chain_based_expression_to_multiple_lines,
"shift_pattern": _format_operator_chain_based_expression_to_multiple_lines,
"arith_pattern": _format_operator_chain_based_expression_to_multiple_lines,
"mdr_pattern": _format_operator_chain_based_expression_to_multiple_lines,
"neg_pattern": _format_concrete_expression_to_single_line,
"bitw_not_pattern": _format_concrete_expression_to_single_line,
"attr_pattern": _format_operator_chain_based_expression_to_multiple_lines,
"call_pattern": _format_call_expression_to_multiple_lines,
"array_pattern": _format_concrete_expression_to_single_line,
"dict_pattern": _format_concrete_expression_to_single_line,
"kv_pair_pattern": _format_kv_pair_to_multiple_lines,
} # type: Dict[str, Callable]
return handlers[expression.data](expression, expression_context, context)

Expand Down Expand Up @@ -240,7 +261,7 @@ def _format_args_to_multiple_lines(
def _format_kv_pair_to_multiple_lines(
expression: Tree, expression_context: ExpressionContext, context: Context
) -> FormattedLines:
suffix = ":" if expression.data == "c_dict_element" else " ="
suffix = ":" if expression.data in ["c_dict_element", "kv_pair_pattern"] else " ="
key_expression_context = ExpressionContext(
expression_context.prefix_string,
expression_context.prefix_line,
Expand Down
28 changes: 13 additions & 15 deletions gdtoolkit/formatter/expression_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@ def remove_outer_parentheses(expression: Node) -> Node:
def is_foldable(expression: Node) -> bool:
if _is_multiline_string(expression):
return True
return (
not isinstance(expression, Token)
and expression.data
not in [
"string",
"get_node",
"node_path",
"string_name",
"unique_node_path",
"signal_arg_typed",
"signal_arg_regular",
"non_foldable_dot_chain",
]
and not expression.data.endswith("_pattern")
)
return not isinstance(expression, Token) and expression.data not in [
"string",
"get_node",
"node_path",
"string_name",
"unique_node_path",
"signal_arg_typed",
"signal_arg_regular",
"non_foldable_dot_chain",
"var_capture_pattern",
"etc_pattern",
"wildcard_pattern",
]


def has_trailing_comma(expression: Node) -> bool:
Expand Down
4 changes: 4 additions & 0 deletions gdtoolkit/formatter/safety_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)


# pylint: disable-next=too-many-public-methods
class LoosenTreeTransformer(Transformer):
def par_expr(self, args):
return args[0] if len(args) > 0 else args
Expand Down Expand Up @@ -68,6 +69,9 @@ def string(self, args):
string_token = args[0]
return expression_to_str(string_token)

def par_pattern(self, args):
return args[0] if len(args) > 0 else args

def signal_stmt(self, args):
if len(args) > 1 and len(args[1].children) == 0:
return Tree("signal_stmt", args[:-1])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class X:
func foo(x):
match x:
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1:
pass
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" if true else "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb":
pass
true or false or true or false or true or false or true or false or true or false or true or false or true or false or true:
pass
true and false and true and false and true and false and true and false and true and false and true and false and true and false and true:
pass
not "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":
pass
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" < "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb":
pass
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1:
pass
1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1:
pass
1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1:
pass
1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1 << 1:
pass
1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1:
pass
1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1 * 1:
pass
-------------------------------------------------------------------------------------------1:
pass
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~1:
pass
x.aaaa.aaaa.aaaa.aaaa.aaaa.aaaa.aaaa.aaaa.aaaa.aaaa.aaaa.aaaa.aaaa.aaaa.aaaa.aaaa.aaaa.aaaa:
pass
Vector3(1,1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1,1):
pass
var xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:
pass
# TODO: format to multiple lines once multiline syntax is fixed in Godot
[_,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]:
pass
# TODO: format to multiple lines once multiline syntax is fixed in Godot
{'aaaaa', 'bbbbbbbbbbbbbbbbbbbbb': 1111, 'ccccccccccccccccccccc': 1111, 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd': 1111}:
pass
254 changes: 254 additions & 0 deletions tests/formatter/input-output-pairs/multiline_match_statements.out.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
class X:
func foo(x):
match x:
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1:
pass
(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
if true
else "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
):
pass
(
true
or false
or true
or false
or true
or false
or true
or false
or true
or false
or true
or false
or true
or false
or true
):
pass
(
true
and false
and true
and false
and true
and false
and true
and false
and true
and false
and true
and false
and true
and false
and true
):
pass
not "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":
pass
(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
< "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
):
pass
(
1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
| 1
):
pass
(
1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
^ 1
):
pass
(
1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
& 1
):
pass
(
1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
<< 1
):
pass
(
1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
- 1
):
pass
(
1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
* 1
):
pass
-------------------------------------------------------------------------------------------1:
pass
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~1:
pass
(
x
. aaaa
. aaaa
. aaaa
. aaaa
. aaaa
. aaaa
. aaaa
. aaaa
. aaaa
. aaaa
. aaaa
. aaaa
. aaaa
. aaaa
. aaaa
. aaaa
. aaaa
. aaaa
):
pass
Vector3(
1, 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1, 1
):
pass
var xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:
pass
# TODO: format to multiple lines once multiline syntax is fixed in Godot
[_, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]:
pass
# TODO: format to multiple lines once multiline syntax is fixed in Godot
{"aaaaa", "bbbbbbbbbbbbbbbbbbbbb": 1111, "ccccccccccccccccccccc": 1111, "ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd": 1111}:
pass
Loading

0 comments on commit f97551e

Please sign in to comment.