Skip to content

Commit 5e6bb3e

Browse files
committed
Keep track of line number in visitor
1 parent 97fd517 commit 5e6bb3e

File tree

5 files changed

+57
-46
lines changed

5 files changed

+57
-46
lines changed

src/imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl<'a> FmtVisitor<'a> {
285285
}
286286
Some(ref s) => {
287287
self.format_missing_with_indent(source!(self, span).lo());
288-
self.buffer.push_str(s);
288+
self.push_str(s);
289289
self.last_pos = source!(self, span).hi();
290290
}
291291
None => {

src/items.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,12 @@ impl<'a> FnSig<'a> {
240240

241241
impl<'a> FmtVisitor<'a> {
242242
fn format_item(&mut self, item: Item) {
243-
self.buffer.push_str(&item.abi);
243+
self.push_str(&item.abi);
244244

245245
let snippet = self.snippet(item.span);
246246
let brace_pos = snippet.find_uncommented("{").unwrap();
247247

248-
self.buffer.push_str("{");
248+
self.push_str("{");
249249
if !item.body.is_empty() || contains_comment(&snippet[brace_pos..]) {
250250
// FIXME: this skips comments between the extern keyword and the opening
251251
// brace.
@@ -255,9 +255,8 @@ impl<'a> FmtVisitor<'a> {
255255
if item.body.is_empty() {
256256
self.format_missing_no_indent(item.span.hi() - BytePos(1));
257257
self.block_indent = self.block_indent.block_unindent(self.config);
258-
259-
self.buffer
260-
.push_str(&self.block_indent.to_string(self.config));
258+
let indent_str = self.block_indent.to_string(self.config);
259+
self.push_str(&indent_str);
261260
} else {
262261
for item in &item.body {
263262
self.format_body_element(item);
@@ -268,7 +267,7 @@ impl<'a> FmtVisitor<'a> {
268267
}
269268
}
270269

271-
self.buffer.push_str("}");
270+
self.push_str("}");
272271
self.last_pos = item.span.hi();
273272
}
274273

@@ -423,7 +422,7 @@ impl<'a> FmtVisitor<'a> {
423422
span: Span,
424423
) {
425424
let enum_header = format_header("enum ", ident, vis);
426-
self.buffer.push_str(&enum_header);
425+
self.push_str(&enum_header);
427426

428427
let enum_snippet = self.snippet(span);
429428
let brace_pos = enum_snippet.find_uncommented("{").unwrap();
@@ -441,23 +440,23 @@ impl<'a> FmtVisitor<'a> {
441440
mk_sp(span.lo(), body_start),
442441
last_line_width(&enum_header),
443442
).unwrap();
444-
self.buffer.push_str(&generics_str);
443+
self.push_str(&generics_str);
445444

446445
self.last_pos = body_start;
447446

448447
self.block_indent = self.block_indent.block_indent(self.config);
449448
let variant_list = self.format_variant_list(enum_def, body_start, span.hi() - BytePos(1));
450449
match variant_list {
451-
Some(ref body_str) => self.buffer.push_str(body_str),
450+
Some(ref body_str) => self.push_str(body_str),
452451
None => self.format_missing_no_indent(span.hi() - BytePos(1)),
453452
}
454453
self.block_indent = self.block_indent.block_unindent(self.config);
455454

456455
if variant_list.is_some() || contains_comment(&enum_snippet[brace_pos..]) {
457-
self.buffer
458-
.push_str(&self.block_indent.to_string(self.config));
456+
let indent_str = self.block_indent.to_string(self.config);
457+
self.push_str(&indent_str);
459458
}
460-
self.buffer.push_str("}");
459+
self.push_str("}");
461460
self.last_pos = span.hi();
462461
}
463462

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ where
338338
visitor.format_separate_mod(module, &*filemap);
339339
};
340340

341+
assert_eq!(
342+
visitor.line_number,
343+
::utils::count_newlines(&format!("{}", visitor.buffer))
344+
);
345+
341346
has_diff |= match after_file(path_str, &mut visitor.buffer) {
342347
Ok(result) => result,
343348
Err(e) => {

src/missed_spans.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,25 @@ impl<'a> FmtVisitor<'a> {
2828
// TODO these format_missing methods are ugly. Refactor and add unit tests
2929
// for the central whitespace stripping loop.
3030
pub fn format_missing(&mut self, end: BytePos) {
31-
self.format_missing_inner(end, |this, last_snippet, _| {
32-
this.buffer.push_str(last_snippet)
33-
})
31+
self.format_missing_inner(end, |this, last_snippet, _| this.push_str(last_snippet))
3432
}
3533

3634
pub fn format_missing_with_indent(&mut self, end: BytePos) {
3735
let config = self.config;
3836
self.format_missing_inner(end, |this, last_snippet, snippet| {
39-
this.buffer.push_str(last_snippet.trim_right());
37+
this.push_str(last_snippet.trim_right());
4038
if last_snippet == snippet && !this.output_at_start() {
4139
// No new lines in the snippet.
42-
this.buffer.push_str("\n");
40+
this.push_str("\n");
4341
}
4442
let indent = this.block_indent.to_string(config);
45-
this.buffer.push_str(&indent);
43+
this.push_str(&indent);
4644
})
4745
}
4846

4947
pub fn format_missing_no_indent(&mut self, end: BytePos) {
5048
self.format_missing_inner(end, |this, last_snippet, _| {
51-
this.buffer.push_str(last_snippet.trim_right());
49+
this.push_str(last_snippet.trim_right());
5250
})
5351
}
5452

@@ -97,7 +95,7 @@ impl<'a> FmtVisitor<'a> {
9795
newline_count = newline_lower_bound;
9896
}
9997
let blank_lines: String = repeat('\n').take(newline_count).collect();
100-
self.buffer.push_str(&blank_lines);
98+
self.push_str(&blank_lines);
10199
}
102100

103101
fn write_snippet<F>(&mut self, span: Span, process_last_snippet: F)
@@ -154,12 +152,12 @@ impl<'a> FmtVisitor<'a> {
154152
if status.rewrite_next_comment {
155153
if fix_indent {
156154
if let Some('{') = last_char {
157-
self.buffer.push_str("\n");
155+
self.push_str("\n");
158156
}
159-
self.buffer
160-
.push_str(&self.block_indent.to_string(self.config));
157+
let indent_str = self.block_indent.to_string(self.config);
158+
self.push_str(&indent_str);
161159
} else {
162-
self.buffer.push_str(" ");
160+
self.push_str(" ");
163161
}
164162

165163
let comment_width = ::std::cmp::min(
@@ -170,7 +168,7 @@ impl<'a> FmtVisitor<'a> {
170168
let comment_shape = Shape::legacy(comment_width, comment_indent);
171169
let comment_str = rewrite_comment(subslice, false, comment_shape, self.config)
172170
.unwrap_or_else(|| String::from(subslice));
173-
self.buffer.push_str(&comment_str);
171+
self.push_str(&comment_str);
174172

175173
status.last_wspace = None;
176174
status.line_start = offset + subslice.len();
@@ -183,13 +181,13 @@ impl<'a> FmtVisitor<'a> {
183181
.any(|s| s.len() >= 2 && &s[0..2] == "/*")
184182
{
185183
// Add a newline after line comments
186-
self.buffer.push_str("\n");
184+
self.push_str("\n");
187185
}
188186
} else if status.line_start <= snippet.len() {
189187
// For other comments add a newline if there isn't one at the end already
190188
match snippet[status.line_start..].chars().next() {
191189
Some('\n') | Some('\r') => (),
192-
_ => self.buffer.push_str("\n"),
190+
_ => self.push_str("\n"),
193191
}
194192
}
195193

@@ -277,10 +275,10 @@ impl<'a> FmtVisitor<'a> {
277275
}
278276

279277
if let Some(lw) = status.last_wspace {
280-
self.buffer.push_str(&snippet[status.line_start..lw]);
281-
self.buffer.push_str("\n");
278+
self.push_str(&snippet[status.line_start..lw]);
279+
self.push_str("\n");
282280
} else {
283-
self.buffer.push_str(&snippet[status.line_start..i + 1]);
281+
self.push_str(&snippet[status.line_start..i + 1]);
284282
}
285283

286284
status.cur_line += 1;
@@ -306,7 +304,7 @@ impl<'a> FmtVisitor<'a> {
306304

307305
let remaining = snippet[status.line_start..subslice.len() + offset].trim();
308306
if !remaining.is_empty() {
309-
self.buffer.push_str(remaining);
307+
self.push_str(remaining);
310308
status.line_start = subslice.len() + offset;
311309
status.rewrite_next_comment = true;
312310
}

src/visitor.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub struct FmtVisitor<'a> {
8181
pub config: &'a Config,
8282
pub is_if_else_block: bool,
8383
pub snippet_provider: &'a SnippetProvider<'a>,
84+
pub line_number: usize,
8485
}
8586

8687
impl<'b, 'a: 'b> FmtVisitor<'a> {
@@ -132,7 +133,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
132133

133134
self.last_pos = self.last_pos + brace_compensation;
134135
self.block_indent = self.block_indent.block_indent(self.config);
135-
self.buffer.push_str("{");
136+
self.push_str("{");
136137

137138
if self.config.remove_blank_lines_at_start_or_end_of_block() {
138139
if let Some(first_stmt) = b.stmts.first() {
@@ -195,7 +196,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
195196
if !b.stmts.is_empty() {
196197
if let Some(expr) = utils::stmt_expr(&b.stmts[b.stmts.len() - 1]) {
197198
if utils::semicolon_for_expr(&self.get_context(), expr) {
198-
self.buffer.push_str(";");
199+
self.push_str(";");
199200
}
200201
}
201202
}
@@ -255,7 +256,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
255256
self.config.tab_spaces()
256257
};
257258
self.buffer.truncate(total_len - chars_too_many);
258-
self.buffer.push_str("}");
259+
self.push_str("}");
259260
self.block_indent = self.block_indent.block_unindent(self.config);
260261
}
261262

@@ -288,7 +289,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
288289

289290
if let Some(fn_str) = rewrite {
290291
self.format_missing_with_indent(source!(self, s).lo());
291-
self.buffer.push_str(&fn_str);
292+
self.push_str(&fn_str);
292293
if let Some(c) = fn_str.chars().last() {
293294
if c == '}' {
294295
self.last_pos = source!(self, block.span).hi();
@@ -519,13 +520,18 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
519520
self.push_rewrite(mac.span, rewrite);
520521
}
521522

523+
pub fn push_str(&mut self, s: &str) {
524+
self.line_number += count_newlines(s);
525+
self.buffer.push_str(s);
526+
}
527+
522528
pub fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
523529
self.format_missing_with_indent(source!(self, span).lo());
524530
if let Some(ref s) = rewrite {
525-
self.buffer.push_str(s);
531+
self.push_str(s);
526532
} else {
527533
let snippet = self.snippet(span);
528-
self.buffer.push_str(snippet);
534+
self.push_str(snippet);
529535
}
530536
self.last_pos = source!(self, span).hi();
531537
}
@@ -548,6 +554,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
548554
config: config,
549555
is_if_else_block: false,
550556
snippet_provider: snippet_provider,
557+
line_number: 0,
551558
}
552559
}
553560

@@ -692,23 +699,25 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
692699
let is_internal = !(inner_span.lo().0 == 0 && inner_span.hi().0 == 0)
693700
&& local_file_name == self.codemap.span_to_filename(inner_span);
694701

695-
self.buffer.push_str(&*utils::format_visibility(vis));
696-
self.buffer.push_str("mod ");
697-
self.buffer.push_str(&ident.to_string());
702+
self.push_str(&*utils::format_visibility(vis));
703+
self.push_str("mod ");
704+
self.push_str(&ident.to_string());
698705

699706
if is_internal {
700707
match self.config.brace_style() {
701-
BraceStyle::AlwaysNextLine => self.buffer
702-
.push_str(&format!("\n{}{{", self.block_indent.to_string(self.config))),
703-
_ => self.buffer.push_str(" {"),
708+
BraceStyle::AlwaysNextLine => {
709+
let sep_str = format!("\n{}{{", self.block_indent.to_string(self.config));
710+
self.push_str(&sep_str);
711+
}
712+
_ => self.push_str(" {"),
704713
}
705714
// Hackery to account for the closing }.
706715
let mod_lo = self.codemap.span_after(source!(self, s), "{");
707716
let body_snippet =
708717
self.snippet(mk_sp(mod_lo, source!(self, m.inner).hi() - BytePos(1)));
709718
let body_snippet = body_snippet.trim();
710719
if body_snippet.is_empty() {
711-
self.buffer.push_str("}");
720+
self.push_str("}");
712721
} else {
713722
self.last_pos = mod_lo;
714723
self.block_indent = self.block_indent.block_indent(self.config);
@@ -719,7 +728,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
719728
}
720729
self.last_pos = source!(self, m.inner).hi();
721730
} else {
722-
self.buffer.push_str(";");
731+
self.push_str(";");
723732
self.last_pos = source!(self, s).hi();
724733
}
725734
}

0 commit comments

Comments
 (0)