Skip to content

Commit

Permalink
Serialize values with Select Expression on a new line
Browse files Browse the repository at this point in the history
Patterns with Select Expressions should be serialized starting on a new line to
make the indentation clear.

Before:

    key = { $selector ->
           *[variant name] Variant Value
        }

After:

    key =
        { $selector ->
           *[variant name] Variant Value
        }

See https://bugzilla.mozilla.org/show_bug.cgi?id=1441105.
  • Loading branch information
stasm committed Feb 26, 2018
1 parent 3a1965d commit 978f177
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 41 deletions.
19 changes: 12 additions & 7 deletions fluent/syntax/serializer.py
Expand Up @@ -8,11 +8,14 @@ def indent(content):
)


def contain_new_line(elems):
return bool([
elem for elem in elems
if isinstance(elem, ast.TextElement) and "\n" in elem.value
])
def includes_new_line(elem):
return isinstance(elem, ast.TextElement) and "\n" in elem.value


def is_select_expr(elem):
return (
isinstance(elem, ast.Placeable) and
isinstance(elem.expression, ast.SelectExpression))


class FluentSerializer(object):
Expand Down Expand Up @@ -116,8 +119,10 @@ def serialize_attribute(attribute):


def serialize_value(pattern):
multi = contain_new_line(pattern.elements)
schema = "\n {}" if multi else " {}"
start_on_new_line = any(
includes_new_line(elem) or is_select_expr(elem)
for elem in pattern.elements)
schema = "\n {}" if start_on_new_line else " {}"

content = serialize_pattern(pattern)
return schema.format(indent(content))
Expand Down
87 changes: 53 additions & 34 deletions tests/syntax/test_serializer.py
Expand Up @@ -44,14 +44,26 @@ def test_two_simple_messages(self):
"""
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))

def test_multiline_simple(self):
def test_block_multiline(self):
input = """\
foo =
Foo
Bar
"""
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))

def test_inline_multiline(self):
input = """\
foo = Foo
Bar
"""
output = """\
foo =
Foo
Bar
"""
self.assertEqual(self.pretty_ftl(input), dedent_ftl(output))

def test_message_reference(self):
input = """\
foo = Foo { bar }
Expand Down Expand Up @@ -210,7 +222,8 @@ def test_multiline_value_and_attributes(self):

def test_select_expression_no_selector(self):
input = """\
foo = {
foo =
{
*[a] A
[b] B
}
Expand All @@ -219,60 +232,62 @@ def test_select_expression_no_selector(self):

def test_select_expression(self):
input = """\
foo = { $sel ->
foo =
{ $sel ->
*[a] A
[b] B
}
"""
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))

# XXX The variant contains a new-line so the serializer defaults to
# multiline formatting for all of its contents.
# https://bugzilla.mozilla.org/show_bug.cgi?id=1397760
def test_variant_multiline_first_inline(self):
def test_variant_multiline(self):
input = """\
foo = {
*[a] AAA
BBB
}
"""
output = """\
foo = {
foo =
{
*[a]
AAA
BBB
}
"""
self.assertEqual(self.pretty_ftl(input), dedent_ftl(output))
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))

def test_variant_multiline(self):
def test_variant_multiline_first_inline(self):
input = """\
foo = {
foo =
{
*[a] AAA
BBB
}
"""
output = """\
foo =
{
*[a]
AAA
BBB
}
"""
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))
self.assertEqual(self.pretty_ftl(input), dedent_ftl(output))

def test_variant_key_words(self):
input = """\
foo = {
foo =
{
*[a b c] A B C
}
"""
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))

def test_variant_key_number(self):
input = """\
foo = {
foo =
{
*[1] 1
}
"""
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))

@unittest.skip("The serializer doesn't know it's multiline.")
def test_select_expression_in_simple_multiline(self):
def test_select_expression_in_block_value(self):
input = """\
foo =
Foo { $sel ->
Expand All @@ -282,18 +297,16 @@ def test_select_expression_in_simple_multiline(self):
"""
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))

# None of the Text elements contain a new-line, so the serializer outputs
# a single-line value.
def test_select_expression_in_simple_multiline_current(self):
def test_select_expression_in_inline_value(self):
input = """\
foo =
Foo { $sel ->
foo = Foo { $sel ->
*[a] A
[b] B
}
"""
output = """\
foo = Foo { $sel ->
foo =
Foo { $sel ->
*[a] A
[b] B
}
Expand All @@ -313,8 +326,10 @@ def test_select_expression_in_multi_multiline(self):

def test_select_expression_nested(self):
input = """\
foo = { $a ->
*[a] { $b ->
foo =
{ $a ->
*[a]
{ $b ->
*[b] Foo
}
}
Expand All @@ -323,31 +338,35 @@ def test_select_expression_nested(self):

def test_selector_external_argument(self):
input = """\
foo = { $bar ->
foo =
{ $bar ->
*[a] A
}
"""
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))

def test_selector_number_expression(self):
input = """\
foo = { 1 ->
foo =
{ 1 ->
*[a] A
}
"""
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))

def test_selector_string_expression(self):
input = """\
foo = { "bar" ->
foo =
{ "bar" ->
*[a] A
}
"""
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))

def test_selector_attribute_expression(self):
input = """\
foo = { -bar.baz ->
foo =
{ -bar.baz ->
*[a] A
}
"""
Expand Down

0 comments on commit 978f177

Please sign in to comment.