Skip to content

Commit 430a782

Browse files
committed
Avoid breaking dot chains containing a lambda(s), fixes #326
1 parent 9779049 commit 430a782

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Fixed support for `get_node` syntax to accommodate for `$/(...)`
1111
- Changed formatting of some uni-statement lambdas
1212
- Changed formatting of multi-statement, inline lambdas
13+
- Changed formatting of dot-chains containing a lambda(s)
1314

1415
## [4.3.1] 2024-08-24
1516

gdtoolkit/formatter/expression.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
remove_outer_parentheses,
1313
is_foldable,
1414
is_expression_forcing_multiple_lines,
15+
expression_contains_lambda,
1516
is_any_comma,
1617
is_trailing_comma,
1718
)
@@ -716,6 +717,12 @@ def _format_dot_chain_to_multiple_lines(
716717
expression_context: ExpressionContext,
717718
context: Context,
718719
) -> FormattedLines:
720+
# temporary workaround for likely Godot bug
721+
# TODO: remove once not needed anymore (this probably should be kept until Godot 5 for compat)
722+
if expression_contains_lambda(dot_chain):
723+
return _format_dot_chain_to_multiple_lines_bottom_up(
724+
dot_chain, expression_context, context
725+
)
719726
if is_expression_forcing_multiple_lines(dot_chain, context.standalone_comments):
720727
return _format_operator_chain_based_expression_to_multiple_lines(
721728
dot_chain, expression_context, context

gdtoolkit/formatter/expression_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ def is_expression_forcing_multiple_lines(
6363
return False
6464

6565

66+
def expression_contains_lambda(expression: Node):
67+
if isinstance(expression, Token):
68+
return False
69+
if expression.data == "lambda":
70+
return True
71+
for child in expression.children:
72+
if expression_contains_lambda(child):
73+
return True
74+
return False
75+
76+
6677
def is_any_comma(expression: Node) -> bool:
6778
return (isinstance(expression, Tree) and expression.data == "trailing_comma") or (
6879
isinstance(expression, Token) and expression.type == "COMMA"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
extends Node
2+
3+
func _ready() -> void:
4+
get_tree().create_timer(1.0).timeout.connect(
5+
func():
6+
print("Hello world!")
7+
print("This is a bug test.")
8+
)
9+
func foo():
10+
get_tree().create_timer(1.0).timeout.connect(
11+
func():
12+
print("Hello world!")
13+
return [1,]
14+
)
15+
func bar():
16+
get_tree().create_timer(1.0).timeout.connect(func(): return [1,])
17+
func baz(x):
18+
x.aaaaaaaaaaaaa.bbbbbbbbbbbb.cccccccccccccc.dddddddddddddd.eeeeeeeeeeeeee.fffffffffffff.ggggggggggggg.hhhhh(func(): return 1)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
extends Node
2+
3+
4+
func _ready() -> void:
5+
get_tree().create_timer(1.0).timeout.connect(
6+
func():
7+
print("Hello world!")
8+
print("This is a bug test.")
9+
)
10+
11+
12+
func foo():
13+
get_tree().create_timer(1.0).timeout.connect(
14+
func():
15+
print("Hello world!")
16+
return [
17+
1,
18+
]
19+
)
20+
21+
22+
func bar():
23+
get_tree().create_timer(1.0).timeout.connect(
24+
func():
25+
return [
26+
1,
27+
]
28+
)
29+
30+
31+
func baz(x):
32+
x.aaaaaaaaaaaaa.bbbbbbbbbbbb.cccccccccccccc.dddddddddddddd.eeeeeeeeeeeeee.fffffffffffff.ggggggggggggg.hhhhh(
33+
func(): return 1
34+
)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
extends Node
2+
3+
func foo():
4+
pass
5+
6+
func _ready() -> void:
7+
get_tree().create_timer(1.0).timeout.connect(foo) # works
8+
get_tree().create_timer(1.0).timeout.connect(func(): foo()) # works
9+
get_tree().create_timer(1.0).timeout.connect(
10+
func():
11+
foo()
12+
) # works
13+
(
14+
get_tree()
15+
. create_timer(1.0)
16+
. timeout
17+
. connect(foo)
18+
) # works
19+
(
20+
get_tree()
21+
. create_timer(1.0)
22+
. timeout
23+
. connect(func(): foo())
24+
) # works
25+
(
26+
get_tree()
27+
. create_timer(1.0)
28+
. timeout
29+
. connect(
30+
func(): foo()
31+
)
32+
) # works
33+
(
34+
get_tree()
35+
. create_timer(1.0)
36+
. timeout
37+
. connect(
38+
func():
39+
foo())
40+
) # works !
41+
(
42+
get_tree()
43+
. create_timer(1.0)
44+
. timeout
45+
. connect(
46+
func():
47+
foo()
48+
)
49+
) # doesn't work !

0 commit comments

Comments
 (0)