Fix formatter bugs (#1619)

This commit is contained in:
Islon Scherer
2026-05-26 19:38:35 +02:00
committed by GitHub
parent ff319faef3
commit d6f35dd49e
5 changed files with 50 additions and 5 deletions
@@ -166,7 +166,13 @@ final class Builder {
var nodes = var nodes =
formatGeneric( formatGeneric(
node.children, node.children,
(prev, next) -> linesBetween(prev, next) > 1 ? TWO_NEWLINES : forceLine()); (prev, next) -> {
var sep = linesBetween(prev, next) > 1 ? TWO_NEWLINES : forceLine();
if (prev.type == NodeType.MODULE_DECLARATION || prev.type == NodeType.IMPORT_LIST) {
sep = TWO_NEWLINES;
}
return sep;
});
return new Nodes(nodes); return new Nodes(nodes);
} }
@@ -697,9 +703,20 @@ final class Builder {
/** /**
* Tells if an argument list has a trailing lambda, new expr, or amends expr. * Tells if an argument list has a trailing lambda, new expr, or amends expr.
* *
* <p>Only considered trailing lamdba if: 1. There is only one lambda/new expr/amends expr in the * <p>Only considered trailing lambda if and only if:
* list. E.g. avoid formatting `toMap()` weirdly: ``` foo.toMap( (it) -> makeSomeKey(it), (it) -> *
* makeSomeValue(it), ) ``` 2. The lambda does not have leading or trailing line comment. * <ol>
* <li>There is only one lambda/new expr/amends expr in the list.
* <p>E.g. avoid formatting {@code toMap()} weirdly:
* <pre>{@code
* foo.toMap((it) -> makeSomeKey(it), (it) ->
* makeSomevalue(it)
* )
*
* }</pre>
* <li>The lambda does not have leading or trailing line comment.
* <li>The user has not broken 3+ arguments across lines.
* </ol>
*/ */
private boolean hasTrailingLambda(Node argList) { private boolean hasTrailingLambda(Node argList) {
var elementsNode = firstProperChild(argList); var elementsNode = firstProperChild(argList);
@@ -707,6 +724,10 @@ final class Builder {
var children = elementsNode.children; var children = elementsNode.children;
var seenLambda = false; var seenLambda = false;
if (children.get(children.size() - 1).type == NodeType.LINE_COMMENT) return false; if (children.get(children.size() - 1).type == NodeType.LINE_COMMENT) return false;
var properArgCount = 0;
for (var child : children) {
if (isProper(child)) properArgCount++;
}
for (var i = children.size() - 1; i >= 0; i--) { for (var i = children.size() - 1; i >= 0; i--) {
var child = children.get(i); var child = children.get(i);
if (!isProper(child)) continue; if (!isProper(child)) continue;
@@ -719,7 +740,7 @@ final class Builder {
return false; return false;
} }
} }
return true; return properArgCount <= 2 || !shouldMultilineNodes(elementsNode, n -> isTerminal(n, ","));
} }
private List<Node> pairArguments(List<Node> nodes) { private List<Node> pairArguments(List<Node> nodes) {
@@ -0,0 +1,3 @@
module foo
import "pkl:json"
foo: Int = 10
@@ -0,0 +1,7 @@
foo =
bar("string",
true, new Baz {
name = "name"
num = 10
})
@@ -0,0 +1,5 @@
module foo
import "pkl:json"
foo: Int = 10
@@ -0,0 +1,9 @@
foo =
bar(
"string",
true,
new Baz {
name = "name"
num = 10
},
)