Skip to content

Commit 4572320

Browse files
committed
Add progressive dot chain formatting, closes #193
1 parent 8db6a90 commit 4572320

File tree

7 files changed

+73
-8
lines changed

7 files changed

+73
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
### Changed
1717
- Removed support for legacy (Godot `3.x`) GDScript from core testcases
1818
- Updated `lark` dependency to the latest release - `0.12.0`
19+
- Improved `subscr_expr`/`getattr`/`getattr_call` chains formatting
1920

2021
## [3.4.0] 2022-04-21
2122

gdtoolkit/formatter/expression.py

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def _format_foldable_to_multiple_lines(
188188
"func_var_inf": lambda e, ec, c: _append_to_expression_context_and_pass_standalone(
189189
f"var {expression_to_str(e.children[0])} := ", e.children[1], ec, c
190190
),
191-
"dot_chain": _format_operator_chain_based_expression_to_multiple_lines,
191+
"dot_chain": _format_dot_chain_to_multiple_lines,
192192
"actual_getattr_call": _format_call_expression_to_multiple_lines,
193193
"actual_subscr_expr": _format_subscription_to_multiple_lines,
194194
} # type: Dict[str, Callable]
@@ -420,7 +420,7 @@ def _format_subscription_to_multiple_lines(
420420
expression_context.suffix_line,
421421
)
422422
subscript = expression.children[1]
423-
subscript_lines = _format_standalone_expression(
423+
subscript_lines = _format_concrete_expression(
424424
subscript, subscript_expression_context, context
425425
)
426426
return subscriptee_lines[:-1] + subscript_lines
@@ -797,3 +797,59 @@ def _collapse_subscr_expr_tree_to_dot_chain(expression: Tree) -> Tree:
797797
fake_meta,
798798
)
799799
return fake_expression
800+
801+
802+
def _format_dot_chain_to_multiple_lines(
803+
dot_chain: Tree,
804+
expression_context: ExpressionContext,
805+
context: Context,
806+
) -> FormattedLines:
807+
if is_expression_forcing_multiple_lines(dot_chain, context.standalone_comments):
808+
return _format_operator_chain_based_expression_to_multiple_lines(
809+
dot_chain, expression_context, context
810+
)
811+
lines_formatted_bottom_up = _format_dot_chain_to_multiple_lines_bottom_up(
812+
dot_chain, expression_context, context
813+
)
814+
if all(
815+
len(line.replace("\t", " " * INDENT_SIZE)) <= context.max_line_length
816+
for line_number, line in lines_formatted_bottom_up
817+
):
818+
return lines_formatted_bottom_up
819+
return _format_operator_chain_based_expression_to_multiple_lines(
820+
dot_chain, expression_context, context
821+
)
822+
823+
824+
def _format_dot_chain_to_multiple_lines_bottom_up(
825+
dot_chain: Tree,
826+
expression_context: ExpressionContext,
827+
context: Context,
828+
) -> FormattedLines:
829+
last_chain_element = dot_chain.children[-1]
830+
if isinstance(last_chain_element, Token) or last_chain_element.data not in [
831+
"actual_getattr_call",
832+
"actual_subscr_expr",
833+
]:
834+
return _format_operator_chain_based_expression_to_multiple_lines(
835+
dot_chain, expression_context, context
836+
)
837+
838+
fake_meta = Meta()
839+
fake_meta.line = dot_chain.line
840+
fake_meta.end_line = last_chain_element.children[0].end_line
841+
new_dot_chain = Tree(
842+
"non_foldable_dot_chain",
843+
dot_chain.children[:-1] + [last_chain_element.children[0]],
844+
fake_meta,
845+
)
846+
847+
fake_meta = Meta()
848+
fake_meta.line = new_dot_chain.line
849+
fake_meta.end_line = last_chain_element.end_line
850+
new_actual_expr = Tree(
851+
last_chain_element.data,
852+
[new_dot_chain] + last_chain_element.children[1:],
853+
fake_meta,
854+
)
855+
return _format_concrete_expression(new_actual_expr, expression_context, context)

gdtoolkit/formatter/expression_to_str.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def expression_to_str(expression: Node) -> str:
138138
e.children[1].value,
139139
standalone_expression_to_str(e.children[2]),
140140
),
141-
"dot_chain": _operator_chain_based_expression_to_str,
141+
"non_foldable_dot_chain": lambda e: "".join(map(expression_to_str, e.children)),
142142
"actual_getattr_call": _getattr_call_to_str,
143143
"actual_subscr_expr": _subscription_to_str,
144144
# patterns (fake expressions):

gdtoolkit/formatter/expression_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def is_foldable(expression: Node) -> bool:
2424
"string_name",
2525
"unique_node_path",
2626
"signal_arg_typed",
27+
"non_foldable_dot_chain",
2728
]
2829
and not expression.data.endswith("_pattern")
2930
)

tests/formatter/input-output-pairs/call-expressions.out.gd

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ class X:
8787
1,
8888
1,
8989
)
90-
var e = (
91-
a
92-
. f(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)
90+
var e = a.f(
91+
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
9392
)

tests/formatter/input-output-pairs/complex-dot-chains.in.gd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ func foo(aaaaa):
77
var q = aaaaa.bbbbb.cccc(1,2,3)[1].ddd().ffff.ggg.hhh().iii.jjj.kkkkkkk.llllllll.mmmmmmm.nnnnnnnn.ooooo.ppppppppppp
88
var w = cccc(1,2,3)[1].ddd().ffff.ggg.hhh().iii.jjj.kkkkkkk.llllllll.mmmmmmm.nnnnnnnn.ooooo.ppppppppppp.aaaaa.bbbbb
99
var e = cccc(1,2,3)[1].ddd().ffff.ggg.hhh().iii.jjj.kkkkkkk.llllllll.mmmmmmm.nnnnnnnn.ooooo.ppppppppppp.aaaaa.bbbbb[1]
10-
var f = {1:1}[1].ddd().ffff.ggg.hhh().iii.jjj.kkkkkkk.llllllll.mmmmmmm.nnnnnnnn.ooooo.ppppppppppp.aaaaa.bbbbb[1]
10+
var r = {1:1}[1].ddd().ffff.ggg.hhh().iii.jjj.kkkkkkk.llllllll.mmmmmmm.nnnnnnnn.ooooo.ppppppppppp.aaaaa.bbbbb[1]
11+
var t = aaaaa.bbbbb.cccc[1].ddd().ffff.ggg.hhh().iii.jjj.kkkkkkk.llllllll.mmmmmmm.nnnnnnnn(1,2,3,4,5,6,7,8,9)
12+
var u = aaaaa.bbbbb.cccc[1].ddd().ffff.ggg.hhh().iii.jjj.kkkkkkk.llllllll.mmmmmmm.nnnnnnnn[(1+2+3+4+5+6+7+8+9)]

tests/formatter/input-output-pairs/complex-dot-chains.out.gd

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func foo(aaaaa):
162162
. aaaaa
163163
. bbbbb[1]
164164
)
165-
var f = (
165+
var r = (
166166
{1: 1}[1]
167167
. ddd()
168168
. ffff
@@ -179,3 +179,9 @@ func foo(aaaaa):
179179
. aaaaa
180180
. bbbbb[1]
181181
)
182+
var t = aaaaa.bbbbb.cccc[1].ddd().ffff.ggg.hhh().iii.jjj.kkkkkkk.llllllll.mmmmmmm.nnnnnnnn(
183+
1, 2, 3, 4, 5, 6, 7, 8, 9
184+
)
185+
var u = aaaaa.bbbbb.cccc[1].ddd().ffff.ggg.hhh().iii.jjj.kkkkkkk.llllllll.mmmmmmm.nnnnnnnn[(
186+
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9
187+
)]

0 commit comments

Comments
 (0)