Skip to content

Don't drop to newline when serializing patterns starting with special chars #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,33 @@ package org.projectfluent.syntax.serializer

import org.projectfluent.syntax.ast.* // ktlint-disable no-wildcard-imports

private fun indent(content: CharSequence) = content.split("\n").joinToString("\n ")
private fun indentExceptFirstLine(content: CharSequence) =
content.split("\n").joinToString("\n ")

private fun PatternElement.includesLine() =
this is TextElement && value.contains("\n")

private fun PatternElement.isSelectExpr() =
this is Placeable && expression is SelectExpression

private fun Pattern.shouldStartOnNewLine(): Boolean {
val isMultiline = this.elements.any { it.isSelectExpr() || it.includesLine() }
if (isMultiline) {
val firstElement = this.elements.elementAtOrNull(0)
if (firstElement is TextElement) {
val firstChar = firstElement.value.elementAtOrNull(0)
// Due to the indentation requirement the following characters may not appear
// as the first character on a new line.
if (firstChar == '[' || firstChar == '.' || firstChar == '*') {
return false
}
}

private fun PatternElement.includesLine() = this is TextElement && value.contains("\n")
return true
}

private fun PatternElement.isSelectExpr() = this is Placeable && expression is SelectExpression
return false
}

/**
* Serialize Fluent nodes to `CharSequence`.
Expand Down Expand Up @@ -117,16 +139,15 @@ class FluentSerializer(private val withJunk: Boolean = false) {
}

private fun serializeAttribute(attribute: Attribute): CharSequence {
val value = indent(serializePattern(attribute.value))
val value = indentExceptFirstLine(serializePattern(attribute.value))
return "\n .${attribute.id.name} =$value"
}

private fun serializePattern(pattern: Pattern): CharSequence {
val startOnLine = pattern.elements.any { it.isSelectExpr() || it.includesLine() }
val elements = pattern.elements.map(::serializeElement)
val content = indent(elements.joinToString(""))
val content = indentExceptFirstLine(elements.joinToString(""))

return if (startOnLine) {
return if (pattern.shouldStartOnNewLine()) {
"\n $content"
} else {
" $content"
Expand Down Expand Up @@ -187,7 +208,7 @@ class FluentSerializer(private val withJunk: Boolean = false) {

private fun serializeVariant(variant: Variant): CharSequence {
val key = serializeVariantKey(variant.key)
val value = indent(serializePattern(variant.value))
val value = indentExceptFirstLine(serializePattern(variant.value))

return if (variant.default) {
"\n *[$key]$value"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,35 @@ class SerializeResourceTest {
assertEquals(input, this.pretty(input))
}

@Test
fun multiline_starting_inline() {
val input =
"""
foo = Foo
Baz

""".trimIndent()
val output =
"""
foo =
Foo
Baz

""".trimIndent()
assertEquals(output, this.pretty(input))
}

@Test
fun multiline_starting_inline_with_a_special_char() {
val input =
"""
foo = *Foo
Baz

""".trimIndent()
assertEquals(input, this.pretty(input))
}

@Test
fun multiline_with_placeable() {
val input =
Expand Down Expand Up @@ -378,6 +407,19 @@ class SerializeResourceTest {
assertEquals(expected, this.pretty(input))
}

@Test
fun select_expression_in_inline_pattern_starting_with_a_special_char() {
val input =
"""
foo = .Foo { ${'$'}sel ->
*[a] A
[b] B
}

""".trimIndent()
assertEquals(input, this.pretty(input))
}

@Test
fun select_expression_in_multiline_pattern() {
val input =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## OK

bracket-inline = [Value]
dot-inline = .Value
star-inline = *Value

## ERRORS

bracket-newline =
[Value]
dot-newline =
.Value
star-newline =
*Value
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"type": "Resource",
"body": [
{
"type": "GroupComment",
"content": "OK"
},
{
"type": "Message",
"id": {
"type": "Identifier",
"name": "bracket-inline"
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "TextElement",
"value": "[Value]"
}
]
},
"attributes": [],
"comment": null
},
{
"type": "Message",
"id": {
"type": "Identifier",
"name": "dot-inline"
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "TextElement",
"value": ".Value"
}
]
},
"attributes": [],
"comment": null
},
{
"type": "Message",
"id": {
"type": "Identifier",
"name": "star-inline"
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "TextElement",
"value": "*Value"
}
]
},
"attributes": [],
"comment": null
},
{
"type": "GroupComment",
"content": "ERRORS"
},
{
"type": "Junk",
"annotations": [],
"content": "bracket-newline =\n [Value]\n"
},
{
"type": "Junk",
"annotations": [],
"content": "dot-newline =\n .Value\n"
},
{
"type": "Junk",
"annotations": [],
"content": "star-newline =\n *Value\n"
}
]
}