mirror of
https://github.com/apple/pkl.git
synced 2026-07-05 12:41:38 +02:00
Fix Map formatting (#1312)
This commit is contained in:
@@ -229,9 +229,14 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
|
|||||||
if (children.size == 1) return format(children[0])
|
if (children.size == 1) return format(children[0])
|
||||||
val firstNode = node.firstProperChild()!!
|
val firstNode = node.firstProperChild()!!
|
||||||
return if (firstNode.text() == "Map") {
|
return if (firstNode.text() == "Map") {
|
||||||
val nodes = mutableListOf<FormatNode>()
|
val nodes =
|
||||||
nodes += format(firstNode)
|
formatGenericWithGen(children, null) { node, _ ->
|
||||||
nodes += formatArgumentList(children[1], twoBy2 = true)
|
if (node.type == NodeType.ARGUMENT_LIST) {
|
||||||
|
formatArgumentList(node, twoBy2 = true)
|
||||||
|
} else {
|
||||||
|
format(node)
|
||||||
|
}
|
||||||
|
}
|
||||||
Nodes(nodes)
|
Nodes(nodes)
|
||||||
} else {
|
} else {
|
||||||
Nodes(formatGeneric(children, null))
|
Nodes(formatGeneric(children, null))
|
||||||
@@ -591,7 +596,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
|
|||||||
twoBy2: Boolean = false,
|
twoBy2: Boolean = false,
|
||||||
): FormatNode {
|
): FormatNode {
|
||||||
val children = node.children
|
val children = node.children
|
||||||
val shouldMultiline = shouldMultlineNodes(node) { it.isTerminal(",") }
|
val shouldMultiline = shouldMultilineNodes(node) { it.isTerminal(",") }
|
||||||
val sep: (Node, Node) -> FormatNode = { _, _ ->
|
val sep: (Node, Node) -> FormatNode = { _, _ ->
|
||||||
if (shouldMultiline) forceSpaceyLine() else spaceOrLine()
|
if (shouldMultiline) forceSpaceyLine() else spaceOrLine()
|
||||||
}
|
}
|
||||||
@@ -626,7 +631,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun shouldMultlineNodes(node: Node, predicate: (Node) -> Boolean): Boolean {
|
private fun shouldMultilineNodes(node: Node, predicate: (Node) -> Boolean): Boolean {
|
||||||
for (idx in 0..<node.children.lastIndex) {
|
for (idx in 0..<node.children.lastIndex) {
|
||||||
val prev = node.children[idx]
|
val prev = node.children[idx]
|
||||||
val next = node.children[idx + 1]
|
val next = node.children[idx + 1]
|
||||||
@@ -684,13 +689,24 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
|
|||||||
if (node.isTerminal(",")) {
|
if (node.isTerminal(",")) {
|
||||||
commas++
|
commas++
|
||||||
if (commas == 2) {
|
if (commas == 2) {
|
||||||
|
val suffixes = mutableListOf<Node>()
|
||||||
|
while (tmp.isNotEmpty() && tmp.last().type.isAffix) {
|
||||||
|
// trailing comments should not be paired
|
||||||
|
suffixes += tmp.removeLast()
|
||||||
|
}
|
||||||
res += Node(NodeType.ARGUMENT_LIST_ELEMENTS, tmp)
|
res += Node(NodeType.ARGUMENT_LIST_ELEMENTS, tmp)
|
||||||
|
while (suffixes.isNotEmpty()) {
|
||||||
|
res += suffixes.removeFirst()
|
||||||
|
}
|
||||||
res += node
|
res += node
|
||||||
commas = 0
|
commas = 0
|
||||||
tmp = mutableListOf()
|
tmp = mutableListOf()
|
||||||
} else {
|
} else {
|
||||||
tmp += node
|
tmp += node
|
||||||
}
|
}
|
||||||
|
} else if (tmp.isEmpty() && node.type.isAffix) {
|
||||||
|
// leading comments should not be paired
|
||||||
|
res += node
|
||||||
} else {
|
} else {
|
||||||
tmp += node
|
tmp += node
|
||||||
}
|
}
|
||||||
@@ -1021,7 +1037,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
|
|||||||
|
|
||||||
private fun formatBinaryOpExpr(node: Node): FormatNode {
|
private fun formatBinaryOpExpr(node: Node): FormatNode {
|
||||||
val flat = flattenBinaryOperatorExprs(node)
|
val flat = flattenBinaryOperatorExprs(node)
|
||||||
val shouldMultiline = shouldMultlineNodes(node) { it.type == NodeType.OPERATOR }
|
val shouldMultiline = shouldMultilineNodes(node) { it.type == NodeType.OPERATOR }
|
||||||
val nodes =
|
val nodes =
|
||||||
formatGeneric(flat) { prev, next ->
|
formatGeneric(flat) { prev, next ->
|
||||||
val sep = if (shouldMultiline) forceSpaceyLine() else spaceOrLine()
|
val sep = if (shouldMultiline) forceSpaceyLine() else spaceOrLine()
|
||||||
|
|||||||
@@ -2,3 +2,19 @@
|
|||||||
foo = Map(1000, "some random string", 20000, "another random string", 30000, "yet another random string")
|
foo = Map(1000, "some random string", 20000, "another random string", 30000, "yet another random string")
|
||||||
|
|
||||||
incorrect = Map("This has", 1000000, "an incorrect number", 2000000, "of parameters", 30000000, "passed to Map")
|
incorrect = Map("This has", 1000000, "an incorrect number", 2000000, "of parameters", 30000000, "passed to Map")
|
||||||
|
|
||||||
|
bar =
|
||||||
|
Map(
|
||||||
|
// leading
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
// between
|
||||||
|
3,
|
||||||
|
4, // trailing
|
||||||
|
5,
|
||||||
|
6,
|
||||||
|
7, // mid
|
||||||
|
8,
|
||||||
|
9,
|
||||||
|
10
|
||||||
|
)
|
||||||
|
|||||||
@@ -12,3 +12,15 @@ incorrect =
|
|||||||
"of parameters", 30000000,
|
"of parameters", 30000000,
|
||||||
"passed to Map",
|
"passed to Map",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
bar =
|
||||||
|
Map(
|
||||||
|
// leading
|
||||||
|
1, 2,
|
||||||
|
// between
|
||||||
|
3, 4, // trailing
|
||||||
|
5, 6,
|
||||||
|
7, // mid
|
||||||
|
8,
|
||||||
|
9, 10,
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user