Skip to content

Commit 929d8a9

Browse files
authored
Merge pull request rust-lang#3459 from scampi/issue-3442
fix line numbering in missed spans and handle file_lines in edge cases
2 parents a5cc780 + cdd08da commit 929d8a9

File tree

4 files changed

+91
-36
lines changed

4 files changed

+91
-36
lines changed

src/missed_spans.rs

+37-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::borrow::Cow;
33
use syntax::source_map::{BytePos, Pos, Span};
44

55
use crate::comment::{rewrite_comment, CodeCharKind, CommentCodeSlices};
6+
use crate::config::file_lines::FileLines;
67
use crate::config::{EmitMode, FileName};
78
use crate::shape::{Indent, Shape};
89
use crate::source_map::LineRangeUtils;
@@ -156,7 +157,7 @@ impl<'a> FmtVisitor<'a> {
156157
fn write_snippet_inner<F>(
157158
&mut self,
158159
big_snippet: &str,
159-
big_diff: usize,
160+
mut big_diff: usize,
160161
old_snippet: &str,
161162
span: Span,
162163
process_last_snippet: F,
@@ -175,16 +176,36 @@ impl<'a> FmtVisitor<'a> {
175176
_ => Cow::from(old_snippet),
176177
};
177178

178-
for (kind, offset, subslice) in CommentCodeSlices::new(snippet) {
179-
debug!("{:?}: {:?}", kind, subslice);
179+
// if the snippet starts with a new line, then information about the lines needs to be
180+
// adjusted since it is off by 1.
181+
let snippet = if snippet.starts_with('\n') {
182+
// this takes into account the blank_lines_* options
183+
self.push_vertical_spaces(1);
184+
// include the newline character into the big_diff
185+
big_diff += 1;
186+
status.cur_line += 1;
187+
&snippet[1..]
188+
} else {
189+
snippet
190+
};
180191

181-
let newline_count = count_newlines(subslice);
182-
let within_file_lines_range = self.config.file_lines().contains_range(
192+
let slice_within_file_lines_range = |file_lines: FileLines, cur_line, s| -> (usize, bool) {
193+
let newline_count = count_newlines(s);
194+
let within_file_lines_range = file_lines.contains_range(
183195
file_name,
184-
status.cur_line,
185-
status.cur_line + newline_count,
196+
cur_line,
197+
// if a newline character is at the end of the slice, then the number of newlines
198+
// needs to be decreased by 1 so that the range checked against the file_lines is
199+
// the visual range one would expect.
200+
cur_line + newline_count - if s.ends_with('\n') { 1 } else { 0 },
186201
);
202+
(newline_count, within_file_lines_range)
203+
};
204+
for (kind, offset, subslice) in CommentCodeSlices::new(snippet) {
205+
debug!("{:?}: {:?}", kind, subslice);
187206

207+
let (newline_count, within_file_lines_range) =
208+
slice_within_file_lines_range(self.config.file_lines(), status.cur_line, subslice);
188209
if CodeCharKind::Comment == kind && within_file_lines_range {
189210
// 1: comment.
190211
self.process_comment(
@@ -205,7 +226,15 @@ impl<'a> FmtVisitor<'a> {
205226
}
206227
}
207228

208-
process_last_snippet(self, &snippet[status.line_start..], snippet);
229+
let last_snippet = &snippet[status.line_start..];
230+
let (_, within_file_lines_range) =
231+
slice_within_file_lines_range(self.config.file_lines(), status.cur_line, last_snippet);
232+
if within_file_lines_range {
233+
process_last_snippet(self, last_snippet, snippet);
234+
} else {
235+
// just append what's left
236+
self.push_str(last_snippet);
237+
}
209238
}
210239

211240
fn process_comment(

src/source_map.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ impl<'a> SpanUtils for SnippetProvider<'a> {
7171

7272
impl LineRangeUtils for SourceMap {
7373
fn lookup_line_range(&self, span: Span) -> LineRange {
74+
let snippet = self.span_to_snippet(span).unwrap_or(String::new());
7475
let lo = self.lookup_line(span.lo()).unwrap();
7576
let hi = self.lookup_line(span.hi()).unwrap();
7677

@@ -80,11 +81,14 @@ impl LineRangeUtils for SourceMap {
8081
lo, hi
8182
);
8283

84+
// in case the span starts with a newline, the line range is off by 1 without the
85+
// adjustment below
86+
let offset = 1 + if snippet.starts_with('\n') { 1 } else { 0 };
8387
// Line numbers start at 1
8488
LineRange {
8589
file: lo.sf.clone(),
86-
lo: lo.line + 1,
87-
hi: hi.line + 1,
90+
lo: lo.line + offset,
91+
hi: hi.line + offset,
8892
}
8993
}
9094
}

src/visitor.rs

+38-26
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use syntax::{ast, visit};
66

77
use crate::attr::*;
88
use crate::comment::{CodeCharKind, CommentCodeSlices, FindUncommented};
9+
use crate::config::file_lines::FileName;
910
use crate::config::{BraceStyle, Config, Version};
1011
use crate::expr::{format_expr, ExprType};
1112
use crate::items::{
@@ -171,7 +172,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
171172

172173
if skip_rewrite {
173174
self.push_rewrite(b.span, None);
174-
self.close_block(false);
175+
self.close_block(false, b.span);
175176
self.last_pos = source!(self, b.span).hi();
176177
return;
177178
}
@@ -188,21 +189,25 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
188189

189190
let mut remove_len = BytePos(0);
190191
if let Some(stmt) = b.stmts.last() {
191-
let snippet = self.snippet(mk_sp(
192+
let span_after_last_stmt = mk_sp(
192193
stmt.span.hi(),
193194
source!(self, b.span).hi() - brace_compensation,
194-
));
195-
let len = CommentCodeSlices::new(snippet)
196-
.last()
197-
.and_then(|(kind, _, s)| {
198-
if kind == CodeCharKind::Normal && s.trim().is_empty() {
199-
Some(s.len())
200-
} else {
201-
None
202-
}
203-
});
204-
if let Some(len) = len {
205-
remove_len = BytePos::from_usize(len);
195+
);
196+
// if the span is outside of a file_lines range, then do not try to remove anything
197+
if !out_of_file_lines_range!(self, span_after_last_stmt) {
198+
let snippet = self.snippet(span_after_last_stmt);
199+
let len = CommentCodeSlices::new(snippet)
200+
.last()
201+
.and_then(|(kind, _, s)| {
202+
if kind == CodeCharKind::Normal && s.trim().is_empty() {
203+
Some(s.len())
204+
} else {
205+
None
206+
}
207+
});
208+
if let Some(len) = len {
209+
remove_len = BytePos::from_usize(len);
210+
}
206211
}
207212
}
208213

@@ -221,24 +226,31 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
221226
if unindent_comment {
222227
self.block_indent = self.block_indent.block_indent(self.config);
223228
}
224-
self.close_block(unindent_comment);
229+
self.close_block(unindent_comment, b.span);
225230
self.last_pos = source!(self, b.span).hi();
226231
}
227232

228233
// FIXME: this is a terrible hack to indent the comments between the last
229234
// item in the block and the closing brace to the block's level.
230235
// The closing brace itself, however, should be indented at a shallower
231236
// level.
232-
fn close_block(&mut self, unindent_comment: bool) {
233-
let total_len = self.buffer.len();
234-
let chars_too_many = if unindent_comment {
235-
0
236-
} else if self.config.hard_tabs() {
237-
1
238-
} else {
239-
self.config.tab_spaces()
240-
};
241-
self.buffer.truncate(total_len - chars_too_many);
237+
fn close_block(&mut self, unindent_comment: bool, span: Span) {
238+
let file_name: FileName = self.source_map.span_to_filename(span).into();
239+
let skip_this_line = !self
240+
.config
241+
.file_lines()
242+
.contains_line(&file_name, self.line_number);
243+
if !skip_this_line {
244+
let total_len = self.buffer.len();
245+
let chars_too_many = if unindent_comment {
246+
0
247+
} else if self.config.hard_tabs() {
248+
1
249+
} else {
250+
self.config.tab_spaces()
251+
};
252+
self.buffer.truncate(total_len - chars_too_many);
253+
}
242254
self.push_str("}");
243255
self.block_indent = self.block_indent.block_unindent(self.config);
244256
}
@@ -789,7 +801,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
789801
self.visit_attrs(attrs, ast::AttrStyle::Inner);
790802
self.walk_mod_items(m);
791803
self.format_missing_with_indent(source!(self, m.inner).hi() - BytePos(1));
792-
self.close_block(false);
804+
self.close_block(false, m.inner);
793805
}
794806
self.last_pos = source!(self, m.inner).hi();
795807
} else {

tests/target/issue-3442.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-file_lines: [{"file":"tests/target/issue-3442.rs","range":[5,5]},{"file":"tests/target/issue-3442.rs","range":[8,8]}]
2+
3+
extern crate alpha; // comment 1
4+
extern crate beta; // comment 2
5+
#[allow(aaa)] // comment 3
6+
#[macro_use]
7+
extern crate gamma;
8+
#[allow(bbb)] // comment 4
9+
#[macro_use]
10+
extern crate lazy_static;

0 commit comments

Comments
 (0)