File tree 5 files changed +73
-5
lines changed
5 files changed +73
-5
lines changed Original file line number Diff line number Diff line change @@ -79,6 +79,7 @@ mod slice;
79
79
80
80
use crate :: ast;
81
81
pub use errors:: { ErrorKind , ParserError } ;
82
+ pub ( crate ) use slice:: matches_fluent_ws;
82
83
pub use slice:: Slice ;
83
84
84
85
/// Parser result always returns an AST representation of the input,
Original file line number Diff line number Diff line change 1
1
use std:: ops:: Range ;
2
+
3
+ pub ( crate ) fn matches_fluent_ws ( c : char ) -> bool {
4
+ c == ' ' || c == '\r' || c == '\n'
5
+ }
6
+
2
7
pub trait Slice < ' s > : AsRef < str > + Clone + PartialEq {
3
8
fn slice ( & self , range : Range < usize > ) -> Self ;
4
9
fn trim ( & mut self ) ;
@@ -10,7 +15,7 @@ impl<'s> Slice<'s> for String {
10
15
}
11
16
12
17
fn trim ( & mut self ) {
13
- * self = self . trim_end ( ) . to_string ( ) ;
18
+ * self = self . trim_end_matches ( matches_fluent_ws ) . to_string ( ) ;
14
19
}
15
20
}
16
21
@@ -20,6 +25,6 @@ impl<'s> Slice<'s> for &'s str {
20
25
}
21
26
22
27
fn trim ( & mut self ) {
23
- * self = self . trim_end ( ) ;
28
+ * self = self . trim_end_matches ( matches_fluent_ws ) ;
24
29
}
25
30
}
Original file line number Diff line number Diff line change 21
21
//! assert_eq!(ftl, serialized);
22
22
//! ```
23
23
24
- use crate :: { ast:: * , parser:: Slice } ;
24
+ use crate :: { ast:: * , parser:: Slice , parser :: matches_fluent_ws } ;
25
25
use std:: fmt:: Write ;
26
26
27
27
/// Serializes an abstract syntax tree representing a Fluent Translation List into a
@@ -118,7 +118,11 @@ impl Serializer {
118
118
for line in & comment. content {
119
119
self . writer . write_literal ( prefix) ;
120
120
121
- if !line. as_ref ( ) . trim ( ) . is_empty ( ) {
121
+ if !line
122
+ . as_ref ( )
123
+ . trim_matches ( matches_fluent_ws)
124
+ . is_empty ( )
125
+ {
122
126
self . writer . write_literal ( " " ) ;
123
127
self . writer . write_literal ( line. as_ref ( ) ) ;
124
128
}
Original file line number Diff line number Diff line change @@ -12,3 +12,10 @@ key03 =
12
12
key04 =
13
13
This line is indented by 4 spaces,
14
14
whereas this line by 1 tab.
15
+
16
+ # OK (value is a single tab)
17
+ key05 =
18
+
19
+ # OK (attribute value is two tabs)
20
+ key06 =
21
+ .attr =
Original file line number Diff line number Diff line change 64
64
{
65
65
"type" : " Junk" ,
66
66
"annotations" : [],
67
- "content" : " \t whereas this line by 1 tab.\n "
67
+ "content" : " \t whereas this line by 1 tab.\n\n "
68
+ },
69
+ {
70
+ "type" : " Message" ,
71
+ "id" : {
72
+ "type" : " Identifier" ,
73
+ "name" : " key05"
74
+ },
75
+ "value" : {
76
+ "type" : " Pattern" ,
77
+ "elements" : [
78
+ {
79
+ "type" : " TextElement" ,
80
+ "value" : " \t "
81
+ }
82
+ ]
83
+ },
84
+ "attributes" : [],
85
+ "comment" : {
86
+ "type" : " Comment" ,
87
+ "content" : " OK (value is a single tab)"
88
+ }
89
+ },
90
+ {
91
+ "type" : " Message" ,
92
+ "id" : {
93
+ "type" : " Identifier" ,
94
+ "name" : " key06"
95
+ },
96
+ "value" : null ,
97
+ "attributes" : [
98
+ {
99
+ "type" : " Attribute" ,
100
+ "id" : {
101
+ "type" : " Identifier" ,
102
+ "name" : " attr"
103
+ },
104
+ "value" : {
105
+ "type" : " Pattern" ,
106
+ "elements" : [
107
+ {
108
+ "type" : " TextElement" ,
109
+ "value" : " \t\t "
110
+ }
111
+ ]
112
+ }
113
+ }
114
+ ],
115
+ "comment" : {
116
+ "type" : " Comment" ,
117
+ "content" : " OK (attribute value is two tabs)"
118
+ }
68
119
}
69
120
]
70
121
}
You can’t perform that action at this time.
0 commit comments