@@ -49,6 +49,16 @@ impl Source {
4949 }
5050
5151 pub fn push_str ( & mut self , src : & str ) {
52+ self . push_str_impl ( src, true ) ;
53+ }
54+
55+ /// Appends literal text with the current indentation without interpreting
56+ /// braces or line comments as source syntax.
57+ pub fn push_str_literal ( & mut self , src : & str ) {
58+ self . push_str_impl ( src, false ) ;
59+ }
60+
61+ fn push_str_impl ( & mut self , src : & str , interpret_syntax : bool ) {
5262 let lines = src. lines ( ) . collect :: < Vec < _ > > ( ) ;
5363 for ( i, line) in lines. iter ( ) . enumerate ( ) {
5464 if !self . continuing_line {
@@ -61,11 +71,11 @@ impl Source {
6171 }
6272
6373 let trimmed = line. trim ( ) ;
64- if trimmed. starts_with ( "//" ) {
74+ if interpret_syntax && trimmed. starts_with ( "//" ) {
6575 self . in_line_comment = true ;
6676 }
6777
68- if !self . in_line_comment {
78+ if interpret_syntax && !self . in_line_comment {
6979 if trimmed. starts_with ( '}' ) && self . s . ends_with ( " " ) {
7080 self . s . pop ( ) ;
7181 self . s . pop ( ) ;
@@ -76,7 +86,7 @@ impl Source {
7686 } else {
7787 line. trim_start ( )
7888 } ) ;
79- if !self . in_line_comment {
89+ if interpret_syntax && !self . in_line_comment {
8090 if trimmed. ends_with ( '{' ) {
8191 self . indent += 1 ;
8292 }
@@ -219,4 +229,13 @@ mod tests {
219229 ) ;
220230 assert_eq ! ( s. s, "function() {\n x\n }" ) ;
221231 }
232+
233+ #[ test]
234+ fn literal_text_does_not_change_indentation ( ) {
235+ let mut s = Source :: default ( ) ;
236+ s. indent ( 1 ) ;
237+ s. push_str_literal ( "}\n {" ) ;
238+ s. deindent ( 1 ) ;
239+ assert_eq ! ( s. s, " }\n {" ) ;
240+ }
222241}
0 commit comments