Build a sequence of spans from a formatter.
(
fmter: &'p Formatter<'e, E>,
)
| 164 | impl<'p> Spans<'p> { |
| 165 | /// Build a sequence of spans from a formatter. |
| 166 | fn from_formatter<'e, E: fmt::Display>( |
| 167 | fmter: &'p Formatter<'e, E>, |
| 168 | ) -> Spans<'p> { |
| 169 | let mut line_count = fmter.pattern.lines().count(); |
| 170 | // If the pattern ends with a `\n` literal, then our line count is |
| 171 | // off by one, since a span can occur immediately after the last `\n`, |
| 172 | // which is consider to be an additional line. |
| 173 | if fmter.pattern.ends_with('\n') { |
| 174 | line_count += 1; |
| 175 | } |
| 176 | let line_number_width = |
| 177 | if line_count <= 1 { |
| 178 | 0 |
| 179 | } else { |
| 180 | line_count.to_string().len() |
| 181 | }; |
| 182 | let mut spans = Spans { |
| 183 | pattern: &fmter.pattern, |
| 184 | line_number_width: line_number_width, |
| 185 | by_line: vec![vec![]; line_count], |
| 186 | multi_line: vec![], |
| 187 | }; |
| 188 | spans.add(fmter.span.clone()); |
| 189 | if let Some(span) = fmter.aux_span { |
| 190 | spans.add(span.clone()); |
| 191 | } |
| 192 | spans |
| 193 | } |
| 194 | |
| 195 | /// Add the given span to this sequence, putting it in the right place. |
| 196 | fn add(&mut self, span: ast::Span) { |