Skip to content

Commit

Permalink
Fix formatting of lambda participating in dot chain, fixes #334
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Oct 15, 2024
1 parent 31ae4f4 commit 8167a96
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
16 changes: 12 additions & 4 deletions gdtoolkit/formatter/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,10 +746,18 @@ def _format_dot_chain_to_multiple_lines_bottom_up(
context: Context,
) -> FormattedLines:
last_chain_element = dot_chain.children[-1]
if isinstance(last_chain_element, Token) or last_chain_element.data not in [
"actual_getattr_call",
"actual_subscr_expr",
]:
if (
isinstance(last_chain_element, Token)
or last_chain_element.data
not in [
"actual_getattr_call",
"actual_subscr_expr",
]
or (
last_chain_element.data == "actual_getattr_call"
and any(expression_contains_lambda(e) for e in dot_chain.children[:-1])
)
):
return _format_operator_chain_based_expression_to_multiple_lines(
dot_chain, expression_context, context
)
Expand Down
12 changes: 12 additions & 0 deletions tests/formatter/input-output-pairs/bug_334_multiline_lambda.in.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class WeaponSystemBullet extends Node:
pass
func foo():
var bullet_scene
assert(
(func() -> bool:
var test_instance: Node = bullet_scene.instantiate()
var is_needed_class: bool = test_instance is WeaponSystemBullet
test_instance.free()
return is_needed_class)
.call()
)
17 changes: 17 additions & 0 deletions tests/formatter/input-output-pairs/bug_334_multiline_lambda.out.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class WeaponSystemBullet:
extends Node
pass


func foo():
var bullet_scene
assert(
(
(func() -> bool:
var test_instance: Node = bullet_scene.instantiate()
var is_needed_class: bool = test_instance is WeaponSystemBullet
test_instance.free()
return is_needed_class)
. call()
)
)

0 comments on commit 8167a96

Please sign in to comment.