| 10 | |
| 11 | impl Source { |
| 12 | pub fn push_str(&mut self, src: &str) { |
| 13 | let lines = src.lines().collect::<Vec<_>>(); |
| 14 | for (i, line) in lines.iter().enumerate() { |
| 15 | let trimmed = line.trim(); |
| 16 | if trimmed.starts_with('}') && self.s.ends_with(" ") { |
| 17 | self.s.pop(); |
| 18 | self.s.pop(); |
| 19 | } |
| 20 | self.s.push_str(if lines.len() == 1 { |
| 21 | line |
| 22 | } else { |
| 23 | line.trim_start() |
| 24 | }); |
| 25 | if trimmed.ends_with('{') { |
| 26 | self.indent += 1; |
| 27 | } |
| 28 | if trimmed.starts_with('}') { |
| 29 | // Note that a `saturating_sub` is used here to prevent a panic |
| 30 | // here in the case of invalid code being generated in debug |
| 31 | // mode. It's typically easier to debug those issues through |
| 32 | // looking at the source code rather than getting a panic. |
| 33 | self.indent = self.indent.saturating_sub(1); |
| 34 | } |
| 35 | if i != lines.len() - 1 || src.ends_with('\n') { |
| 36 | self.newline(); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | pub fn indent(&mut self, amt: usize) { |
| 42 | self.indent += amt; |