Skip to content

Commit 2774242

Browse files
committed
(fluent.syntax) Don't drop to newline when serializing patterns starting with special chars
1 parent aa7d65e commit 2774242

File tree

4 files changed

+147
-12
lines changed

4 files changed

+147
-12
lines changed

fluent.syntax/fluent/syntax/serializer.py

+22-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from . import ast
33

44

5-
def indent(content):
5+
def indent_except_first_line(content):
66
return " ".join(
77
content.splitlines(True)
88
)
@@ -18,6 +18,20 @@ def is_select_expr(elem):
1818
isinstance(elem.expression, ast.SelectExpression))
1919

2020

21+
def should_start_on_new_line(pattern):
22+
is_multiline = any(is_select_expr(elem) for elem in pattern.elements) \
23+
or any(includes_new_line(elem) for elem in pattern.elements)
24+
25+
if is_multiline:
26+
first_element = pattern.elements[0]
27+
if isinstance(first_element, ast.TextElement):
28+
first_char = first_element.value[0]
29+
if first_char in ("[", ".", "*"):
30+
return False
31+
return True
32+
return False
33+
34+
2135
class FluentSerializer(object):
2236
"""FluentSerializer converts :class:`.ast.SyntaxNode` objects to unicode strings.
2337
@@ -119,19 +133,16 @@ def serialize_term(term):
119133
def serialize_attribute(attribute):
120134
return "\n .{} ={}".format(
121135
attribute.id.name,
122-
indent(serialize_pattern(attribute.value))
136+
indent_except_first_line(serialize_pattern(attribute.value))
123137
)
124138

125139

126140
def serialize_pattern(pattern):
127-
content = "".join([
128-
serialize_element(elem)
129-
for elem in pattern.elements])
130-
start_on_new_line = any(
131-
includes_new_line(elem) or is_select_expr(elem)
132-
for elem in pattern.elements)
133-
if start_on_new_line:
134-
return '\n {}'.format(indent(content))
141+
content = "".join(serialize_element(elem) for elem in pattern.elements)
142+
content = indent_except_first_line(content)
143+
144+
if should_start_on_new_line(pattern):
145+
return '\n {}'.format(content)
135146

136147
return ' {}'.format(content)
137148

@@ -193,7 +204,7 @@ def serialize_variant(variant):
193204
return "\n{}[{}]{}".format(
194205
" *" if variant.default else " ",
195206
serialize_variant_key(variant.key),
196-
indent(serialize_pattern(variant.value))
207+
indent_except_first_line(serialize_pattern(variant.value))
197208
)
198209

199210

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## OK
2+
3+
bracket-inline = [Value]
4+
dot-inline = .Value
5+
star-inline = *Value
6+
7+
## ERRORS
8+
9+
bracket-newline =
10+
[Value]
11+
dot-newline =
12+
.Value
13+
star-newline =
14+
*Value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"type": "Resource",
3+
"body": [
4+
{
5+
"type": "GroupComment",
6+
"content": "OK"
7+
},
8+
{
9+
"type": "Message",
10+
"id": {
11+
"type": "Identifier",
12+
"name": "bracket-inline"
13+
},
14+
"value": {
15+
"type": "Pattern",
16+
"elements": [
17+
{
18+
"type": "TextElement",
19+
"value": "[Value]"
20+
}
21+
]
22+
},
23+
"attributes": [],
24+
"comment": null
25+
},
26+
{
27+
"type": "Message",
28+
"id": {
29+
"type": "Identifier",
30+
"name": "dot-inline"
31+
},
32+
"value": {
33+
"type": "Pattern",
34+
"elements": [
35+
{
36+
"type": "TextElement",
37+
"value": ".Value"
38+
}
39+
]
40+
},
41+
"attributes": [],
42+
"comment": null
43+
},
44+
{
45+
"type": "Message",
46+
"id": {
47+
"type": "Identifier",
48+
"name": "star-inline"
49+
},
50+
"value": {
51+
"type": "Pattern",
52+
"elements": [
53+
{
54+
"type": "TextElement",
55+
"value": "*Value"
56+
}
57+
]
58+
},
59+
"attributes": [],
60+
"comment": null
61+
},
62+
{
63+
"type": "GroupComment",
64+
"content": "ERRORS"
65+
},
66+
{
67+
"type": "Junk",
68+
"annotations": [],
69+
"content": "bracket-newline =\n [Value]\n"
70+
},
71+
{
72+
"type": "Junk",
73+
"annotations": [],
74+
"content": "dot-newline =\n .Value\n"
75+
},
76+
{
77+
"type": "Junk",
78+
"annotations": [],
79+
"content": "star-newline =\n *Value\n"
80+
}
81+
]
82+
}

fluent.syntax/tests/syntax/test_serializer.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,31 @@ def test_comment_standalone(self):
139139
input = """\
140140
foo = Foo
141141
142-
# A multiline
142+
# A standalone comment
143143
144144
bar = Bar
145145
"""
146146
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))
147147

148+
def test_multiline_starting_inline(self):
149+
input = """\
150+
foo = Foo
151+
Bar
152+
"""
153+
output = """\
154+
foo =
155+
Foo
156+
Bar
157+
"""
158+
self.assertEqual(self.pretty_ftl(input), dedent_ftl(output))
159+
160+
def test_multiline_starting_inline_with_special_char(self):
161+
input = """\
162+
foo = *Foo
163+
Bar
164+
"""
165+
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))
166+
148167
def test_multiline_with_placeable(self):
149168
input = """\
150169
foo =
@@ -269,6 +288,15 @@ def test_select_expression_in_inline_value(self):
269288
"""
270289
self.assertEqual(self.pretty_ftl(input), dedent_ftl(output))
271290

291+
def test_select_expression_in_inline_value_starting_with_special_char(self):
292+
input = """\
293+
foo = .Foo { $sel ->
294+
*[a] A
295+
[b] B
296+
}
297+
"""
298+
self.assertEqual(self.pretty_ftl(input), dedent_ftl(input))
299+
272300
def test_select_expression_in_multi_multiline(self):
273301
input = """\
274302
foo =

0 commit comments

Comments
 (0)