Skip to content

Commit 9b9254a

Browse files
committed
add initial inline-lambda multiline formatting, #154
1 parent ca530da commit 9b9254a

File tree

3 files changed

+150
-1
lines changed

3 files changed

+150
-1
lines changed

gdtoolkit/formatter/expression.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from functools import partial
22
from typing import Dict, Callable, List
33

4-
from lark import Tree
4+
from lark import Tree, Token
55
from lark.tree import Meta
66

77
from .context import Context, ExpressionContext
@@ -157,6 +157,8 @@ def _format_foldable_to_multiple_lines(
157157
),
158158
"annotation": _format_annotation_to_multiple_lines,
159159
"annotation_args": _format_args_to_multiple_lines,
160+
"inline_lambda": _format_inline_lambda_to_multiple_lines,
161+
"lambda_header": _format_lambda_header_to_multiple_lines,
160162
} # type: Dict[str, Callable]
161163
return handlers[expression.data](expression, expression_context, context)
162164

@@ -561,3 +563,44 @@ def _format_annotation_to_multiple_lines(
561563
return _format_concrete_expression(
562564
annotation.children[-1], new_expression_context, context
563565
)
566+
567+
568+
def _format_inline_lambda_to_multiple_lines(
569+
inline_lambda: Tree,
570+
expression_context: ExpressionContext,
571+
context: Context,
572+
) -> FormattedLines:
573+
header_lines = _format_concrete_expression(
574+
inline_lambda.children[0], expression_context, context
575+
)
576+
# TODO: statements
577+
return header_lines[:-1] + [(header_lines[-1][0], header_lines[-1][1] + " pass")]
578+
579+
580+
def _format_lambda_header_to_multiple_lines(
581+
lambda_header: Tree,
582+
expression_context: ExpressionContext,
583+
context: Context,
584+
) -> FormattedLines:
585+
append_to_prefix = (
586+
f"func {lambda_header.children[0].value}"
587+
if isinstance(lambda_header.children[0], Token)
588+
else "func"
589+
)
590+
args_offset = 1 if isinstance(lambda_header.children[0], Token) else 0
591+
theres_something_after_args = len(lambda_header.children) > args_offset + 1
592+
optional_type_hint = (
593+
f" -> {lambda_header.children[args_offset+1]}"
594+
if theres_something_after_args
595+
else ""
596+
)
597+
prepend_to_suffix = f"{optional_type_hint}:"
598+
new_expression_context = ExpressionContext(
599+
f"{expression_context.prefix_string}{append_to_prefix}",
600+
expression_context.prefix_line,
601+
f"{prepend_to_suffix}{expression_context.suffix_string}",
602+
expression_context.suffix_line,
603+
)
604+
return _format_concrete_expression(
605+
lambda_header.children[args_offset], new_expression_context, context
606+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
func foo():
2+
var x1 = func(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23): pass
3+
var x2 = func(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23) -> int: pass
4+
var x3 = func bar(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23): pass
5+
var x4 = func baz(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23) -> int: pass
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
func foo():
2+
var x1 = func(
3+
p1,
4+
p2,
5+
p3,
6+
p4,
7+
p5,
8+
p6,
9+
p7,
10+
p8,
11+
p9,
12+
p10,
13+
p11,
14+
p12,
15+
p13,
16+
p14,
17+
p15,
18+
p16,
19+
p17,
20+
p18,
21+
p19,
22+
p20,
23+
p21,
24+
p22,
25+
p23
26+
): pass
27+
var x2 = func(
28+
p1,
29+
p2,
30+
p3,
31+
p4,
32+
p5,
33+
p6,
34+
p7,
35+
p8,
36+
p9,
37+
p10,
38+
p11,
39+
p12,
40+
p13,
41+
p14,
42+
p15,
43+
p16,
44+
p17,
45+
p18,
46+
p19,
47+
p20,
48+
p21,
49+
p22,
50+
p23
51+
) -> int: pass
52+
var x3 = func bar(
53+
p1,
54+
p2,
55+
p3,
56+
p4,
57+
p5,
58+
p6,
59+
p7,
60+
p8,
61+
p9,
62+
p10,
63+
p11,
64+
p12,
65+
p13,
66+
p14,
67+
p15,
68+
p16,
69+
p17,
70+
p18,
71+
p19,
72+
p20,
73+
p21,
74+
p22,
75+
p23
76+
): pass
77+
var x4 = func baz(
78+
p1,
79+
p2,
80+
p3,
81+
p4,
82+
p5,
83+
p6,
84+
p7,
85+
p8,
86+
p9,
87+
p10,
88+
p11,
89+
p12,
90+
p13,
91+
p14,
92+
p15,
93+
p16,
94+
p17,
95+
p18,
96+
p19,
97+
p20,
98+
p21,
99+
p22,
100+
p23
101+
) -> int: pass

0 commit comments

Comments
 (0)