| 49 | } |
| 50 | |
| 51 | pub fn push_str(&mut self, src: &str) { |
| 52 | let lines = src.lines().collect::<Vec<_>>(); |
| 53 | for (i, line) in lines.iter().enumerate() { |
| 54 | if !self.continuing_line { |
| 55 | if !line.is_empty() { |
| 56 | for _ in 0..self.indent { |
| 57 | self.s.push_str(" "); |
| 58 | } |
| 59 | } |
| 60 | self.continuing_line = true; |
| 61 | } |
| 62 | |
| 63 | let trimmed = line.trim(); |
| 64 | if trimmed.starts_with("//") { |
| 65 | self.in_line_comment = true; |
| 66 | } |
| 67 | |
| 68 | if !self.in_line_comment { |
| 69 | if trimmed.starts_with('}') && self.s.ends_with(" ") { |
| 70 | self.s.pop(); |
| 71 | self.s.pop(); |
| 72 | } |
| 73 | } |
| 74 | self.s.push_str(if lines.len() == 1 { |
| 75 | line |
| 76 | } else { |
| 77 | line.trim_start() |
| 78 | }); |
| 79 | if !self.in_line_comment { |
| 80 | if trimmed.ends_with('{') { |
| 81 | self.indent += 1; |
| 82 | } |
| 83 | if trimmed.starts_with('}') { |
| 84 | // Note that a `saturating_sub` is used here to prevent a panic |
| 85 | // here in the case of invalid code being generated in debug |
| 86 | // mode. It's typically easier to debug those issues through |
| 87 | // looking at the source code rather than getting a panic. |
| 88 | self.indent = self.indent.saturating_sub(1); |
| 89 | } |
| 90 | } |
| 91 | if i != lines.len() - 1 || src.ends_with('\n') { |
| 92 | self.newline(); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | pub fn indent(&mut self, amt: usize) { |
| 98 | self.indent += amt; |