Skip to content

Commit 5c96f1b

Browse files
committed
Use rewrite buffer to determine if comment should be on a newline
Rewriting may change the context of a later item (in this case, a comment), so looking at the original snippet to determine the context may not be correct. Since for any given item, all previous items should already be rewritten, it is enough to determine the item's context in the new source but looking back at the rewrite buffer. This commit also gets rid of variables no longer used due to the change. Closes #4227
1 parent 9549c3b commit 5c96f1b

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

src/formatting/missed_spans.rs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_span::{BytePos, Pos, Span};
1+
use rustc_span::{BytePos, Span};
22

33
use crate::config::{file_lines::FileLines, FileName};
44

@@ -140,28 +140,15 @@ impl<'a> FmtVisitor<'a> {
140140
where
141141
F: Fn(&mut FmtVisitor<'_>, &str, &str),
142142
{
143-
// Get a snippet from the file start to the span's hi without allocating.
144-
// We need it to determine what precedes the current comment. If the comment
145-
// follows code on the same line, we won't touch it.
146-
let big_span_lo = self.snippet_provider.start_pos();
147-
let big_snippet = self.snippet_provider.entire_snippet();
148-
let big_diff = (span.lo() - big_span_lo).to_usize();
149-
150143
let snippet = self.snippet(span);
151144

152145
debug!("write_snippet `{}`", snippet);
153146

154-
self.write_snippet_inner(big_snippet, snippet, big_diff, span, process_last_snippet);
147+
self.write_snippet_inner(snippet, span, process_last_snippet);
155148
}
156149

157-
fn write_snippet_inner<F>(
158-
&mut self,
159-
big_snippet: &str,
160-
snippet: &str,
161-
big_diff: usize,
162-
span: Span,
163-
process_last_snippet: F,
164-
) where
150+
fn write_snippet_inner<F>(&mut self, snippet: &str, span: Span, process_last_snippet: F)
151+
where
165152
F: Fn(&mut FmtVisitor<'_>, &str, &str),
166153
{
167154
// Trim whitespace from the right hand side of each line.
@@ -200,13 +187,7 @@ impl<'a> FmtVisitor<'a> {
200187
let newline_count = lf_count + crlf_count;
201188
if CodeCharKind::Comment == kind && within_file_lines_range {
202189
// 1: comment.
203-
self.process_comment(
204-
&mut status,
205-
snippet,
206-
&big_snippet[..(offset + big_diff)],
207-
offset,
208-
subslice,
209-
);
190+
self.process_comment(&mut status, snippet, offset, subslice);
210191
} else if subslice.trim().is_empty() && newline_count > 0 && within_file_lines_range {
211192
// 2: blank lines.
212193
self.push_vertical_spaces(newline_count);
@@ -233,11 +214,11 @@ impl<'a> FmtVisitor<'a> {
233214
&mut self,
234215
status: &mut SnippetStatus,
235216
snippet: &str,
236-
big_snippet: &str,
237217
offset: usize,
238218
subslice: &str,
239219
) {
240-
let last_char = big_snippet
220+
let last_char = self
221+
.buffer
241222
.chars()
242223
.rev()
243224
.find(|rev_c| ![' ', '\t'].contains(rev_c));

tests/source/issue-4227.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
/*
3+
Block comment
4+
*/ // Line comment
5+
println!("Hello");
6+
}

tests/target/issue-4227.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
/*
3+
Block comment
4+
*/
5+
// Line comment
6+
println!("Hello");
7+
}

0 commit comments

Comments
 (0)