diff --git a/pkl-formatter/src/main/java/org/pkl/formatter/Builder.java b/pkl-formatter/src/main/java/org/pkl/formatter/Builder.java index a2c5471a..247ee8a0 100644 --- a/pkl-formatter/src/main/java/org/pkl/formatter/Builder.java +++ b/pkl-formatter/src/main/java/org/pkl/formatter/Builder.java @@ -166,7 +166,13 @@ final class Builder { var nodes = formatGeneric( 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); } @@ -697,9 +703,20 @@ final class Builder { /** * Tells if an argument list has a trailing lambda, new expr, or amends expr. * - *

Only considered trailing lamdba if: 1. There is only one lambda/new expr/amends expr in the - * 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. + *

Only considered trailing lambda if and only if: + * + *

    + *
  1. There is only one lambda/new expr/amends expr in the list. + *

    E.g. avoid formatting {@code toMap()} weirdly: + *

    {@code
    +   * foo.toMap((it) -> makeSomeKey(it), (it) ->
    +   *   makeSomevalue(it)
    +   * )
    +   *
    +   * }
    + *
  2. The lambda does not have leading or trailing line comment. + *
  3. The user has not broken 3+ arguments across lines. + *
*/ private boolean hasTrailingLambda(Node argList) { var elementsNode = firstProperChild(argList); @@ -707,6 +724,10 @@ final class Builder { var children = elementsNode.children; var seenLambda = 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--) { var child = children.get(i); if (!isProper(child)) continue; @@ -719,7 +740,7 @@ final class Builder { return false; } } - return true; + return properArgCount <= 2 || !shouldMultilineNodes(elementsNode, n -> isTerminal(n, ",")); } private List pairArguments(List nodes) { diff --git a/pkl-formatter/src/test/files/FormatterSnippetTests/input/module.pkl b/pkl-formatter/src/test/files/FormatterSnippetTests/input/module.pkl new file mode 100644 index 00000000..0d31f86c --- /dev/null +++ b/pkl-formatter/src/test/files/FormatterSnippetTests/input/module.pkl @@ -0,0 +1,3 @@ +module foo +import "pkl:json" +foo: Int = 10 diff --git a/pkl-formatter/src/test/files/FormatterSnippetTests/input/trailing-objects.pkl b/pkl-formatter/src/test/files/FormatterSnippetTests/input/trailing-objects.pkl new file mode 100644 index 00000000..08673cdc --- /dev/null +++ b/pkl-formatter/src/test/files/FormatterSnippetTests/input/trailing-objects.pkl @@ -0,0 +1,7 @@ + +foo = + bar("string", + true, new Baz { + name = "name" + num = 10 + }) diff --git a/pkl-formatter/src/test/files/FormatterSnippetTests/output/module.pkl b/pkl-formatter/src/test/files/FormatterSnippetTests/output/module.pkl new file mode 100644 index 00000000..a7f0a666 --- /dev/null +++ b/pkl-formatter/src/test/files/FormatterSnippetTests/output/module.pkl @@ -0,0 +1,5 @@ +module foo + +import "pkl:json" + +foo: Int = 10 diff --git a/pkl-formatter/src/test/files/FormatterSnippetTests/output/trailing-objects.pkl b/pkl-formatter/src/test/files/FormatterSnippetTests/output/trailing-objects.pkl new file mode 100644 index 00000000..61ce6b7c --- /dev/null +++ b/pkl-formatter/src/test/files/FormatterSnippetTests/output/trailing-objects.pkl @@ -0,0 +1,9 @@ +foo = + bar( + "string", + true, + new Baz { + name = "name" + num = 10 + }, + )