mirror of
https://github.com/apple/pkl.git
synced 2026-03-29 13:21:58 +02:00
Respect line breaks in operator chains and argument lists (#1268)
If an operator chain or method call is multiline, keep those newlines in the formatted output. Help preserve code like: ``` foo |> (it) -> it + 2 |> (it) -> it / 2 ```
This commit is contained in:
@@ -29,3 +29,6 @@ res10 = foo.bar /* some comment */ (5)
|
||||
|
||||
res11 = foo.bar /* some comment */.baz(new { foooooooooooooooooooooooooo = 1; baaaaaaaaaaaaaaaaaaaaar = 2 }).buz
|
||||
|
||||
res12 = foo.biz
|
||||
.bar((it) -> it + 2)
|
||||
.qux((it) -> it + 1)
|
||||
|
||||
58
pkl-formatter/src/test/files/FormatterSnippetTests/input/line-breaks3.pkl
vendored
Normal file
58
pkl-formatter/src/test/files/FormatterSnippetTests/input/line-breaks3.pkl
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
// respect line breaks on:
|
||||
// * operator chains
|
||||
// * argument lists in method calls
|
||||
//
|
||||
// only look for line breaks on either side of the operator, or the comma.
|
||||
res1 =
|
||||
foo
|
||||
* bar
|
||||
* baz
|
||||
|
||||
res2 = foo
|
||||
|> bar + bar
|
||||
|> baz
|
||||
|
||||
res3 = foo
|
||||
|> bar
|
||||
+ baz
|
||||
|> qux
|
||||
|
||||
res4 = foo(
|
||||
(it) -> it + 1,
|
||||
(it) -> it + 2
|
||||
)
|
||||
|
||||
res5 = foo(
|
||||
1,
|
||||
2, 3, 4
|
||||
)
|
||||
|
||||
// don't respect newlines right after opening paren and closing paren
|
||||
res6 = foo.bar(
|
||||
1, 2, 3
|
||||
)
|
||||
|
||||
// only respect newlines on either side of a binary operator (we still want to inline `bar(3)` here).
|
||||
res7 = foo + bar(
|
||||
3
|
||||
) + baz
|
||||
|
||||
// only the newlines after each "entry" are respected
|
||||
res8 = Map(
|
||||
1, 2,
|
||||
3, 4
|
||||
)
|
||||
|
||||
res9 = Map(
|
||||
1,
|
||||
2,
|
||||
3, 4
|
||||
)
|
||||
|
||||
res10 = foo(1
|
||||
,2
|
||||
,3)
|
||||
|
||||
res11 = foo +
|
||||
bar +
|
||||
baz
|
||||
@@ -63,3 +63,8 @@ res11 =
|
||||
foo.bar /* some comment */
|
||||
.baz(new { foooooooooooooooooooooooooo = 1; baaaaaaaaaaaaaaaaaaaaar = 2 })
|
||||
.buz
|
||||
|
||||
res12 =
|
||||
foo.biz
|
||||
.bar((it) -> it + 2)
|
||||
.qux((it) -> it + 1)
|
||||
|
||||
65
pkl-formatter/src/test/files/FormatterSnippetTests/output/line-breaks3.pkl
vendored
Normal file
65
pkl-formatter/src/test/files/FormatterSnippetTests/output/line-breaks3.pkl
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
// respect line breaks on:
|
||||
// * operator chains
|
||||
// * argument lists in method calls
|
||||
//
|
||||
// only look for line breaks on either side of the operator, or the comma.
|
||||
res1 =
|
||||
foo
|
||||
* bar
|
||||
* baz
|
||||
|
||||
res2 =
|
||||
foo
|
||||
|> bar + bar
|
||||
|> baz
|
||||
|
||||
res3 =
|
||||
foo
|
||||
|> bar
|
||||
+ baz
|
||||
|> qux
|
||||
|
||||
res4 =
|
||||
foo(
|
||||
(it) -> it + 1,
|
||||
(it) -> it + 2,
|
||||
)
|
||||
|
||||
res5 =
|
||||
foo(
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
)
|
||||
|
||||
// don't respect newlines right after opening paren and closing paren
|
||||
res6 = foo.bar(1, 2, 3)
|
||||
|
||||
// only respect newlines on either side of a binary operator (we still want to inline `bar(3)` here).
|
||||
res7 = foo + bar(3) + baz
|
||||
|
||||
// only the newlines after each "entry" are respected
|
||||
res8 =
|
||||
Map(
|
||||
1, 2,
|
||||
3, 4,
|
||||
)
|
||||
|
||||
res9 =
|
||||
Map(
|
||||
1, 2,
|
||||
3, 4,
|
||||
)
|
||||
|
||||
res10 =
|
||||
foo(
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
)
|
||||
|
||||
res11 =
|
||||
foo
|
||||
+ bar
|
||||
+ baz
|
||||
Reference in New Issue
Block a user