1use rustc_span::{BytePos, Pos, Span};
2use tracing::debug;
3
4use crate::comment::{CodeCharKind, CommentCodeSlices, is_last_comment_block, rewrite_comment};
5use crate::config::FileName;
6use crate::config::StyleEdition;
7use crate::config::file_lines::FileLines;
8use crate::coverage::transform_missing_snippet;
9use crate::shape::{Indent, Shape};
10use crate::source_map::LineRangeUtils;
11use crate::utils::{count_lf_crlf, count_newlines, last_line_width, mk_sp};
12use crate::visitor::FmtVisitor;
13
14struct SnippetStatus {
15 line_start: usize,
17 last_wspace: Option<usize>,
19 cur_line: usize,
21}
22
23impl SnippetStatus {
24 fn new(cur_line: usize) -> Self {
25 SnippetStatus {
26 line_start: 0,
27 last_wspace: None,
28 cur_line,
29 }
30 }
31}
32
33impl<'a> FmtVisitor<'a> {
34 fn output_at_start(&self) -> bool {
35 self.buffer.is_empty()
36 }
37
38 pub(crate) fn format_missing(&mut self, end: BytePos) {
39 let missing_snippet = self.snippet(mk_sp(self.last_pos, end));
46 if missing_snippet.trim() == ";" {
47 self.push_str(";");
48 self.last_pos = end;
49 return;
50 }
51 self.format_missing_inner(end, |this, last_snippet, _| this.push_str(last_snippet))
52 }
53
54 pub(crate) fn format_missing_with_indent(&mut self, end: BytePos) {
55 self.format_missing_indent(end, true)
56 }
57
58 pub(crate) fn format_missing_no_indent(&mut self, end: BytePos) {
59 self.format_missing_indent(end, false)
60 }
61
62 fn format_missing_indent(&mut self, end: BytePos, should_indent: bool) {
63 let config = self.config;
64 self.format_missing_inner(end, |this, last_snippet, snippet| {
65 this.push_str(last_snippet.trim_end());
66 if last_snippet == snippet
67 && !this.output_at_start()
68 && !out_of_file_lines_range!(this, mk_sp(this.last_pos, end))
69 {
70 this.push_str("\n");
72 }
73 if should_indent {
74 let indent = this.block_indent.to_string(config);
75 this.push_str(&indent);
76 }
77 })
78 }
79
80 fn format_missing_inner<F: Fn(&mut FmtVisitor<'_>, &str, &str)>(
81 &mut self,
82 end: BytePos,
83 process_last_snippet: F,
84 ) {
85 let start = self.last_pos;
86
87 if start == end {
88 if !self.output_at_start() {
90 process_last_snippet(self, "", "");
91 }
92 return;
93 }
94
95 assert!(
96 start < end,
97 "Request to format inverted span: {}",
98 self.psess.span_to_debug_info(mk_sp(start, end)),
99 );
100
101 self.last_pos = end;
102 let span = mk_sp(start, end);
103 let snippet = self.snippet(span);
104
105 if start == BytePos(0)
107 && end.0 as usize == snippet.len()
108 && snippet.trim().is_empty()
109 && !out_of_file_lines_range!(self, span)
110 {
111 return;
112 }
113
114 if snippet.trim().is_empty() && !out_of_file_lines_range!(self, span) {
115 self.push_vertical_spaces(count_newlines(snippet));
117 process_last_snippet(self, "", snippet);
118 } else {
119 self.write_snippet(span, &process_last_snippet);
120 }
121 }
122
123 fn push_vertical_spaces(&mut self, mut newline_count: usize) {
124 let offset = self.buffer.chars().rev().take_while(|c| *c == '\n').count();
125 let newline_upper_bound = self.config.blank_lines_upper_bound() + 1;
126 let newline_lower_bound = self.config.blank_lines_lower_bound() + 1;
127
128 if newline_count + offset > newline_upper_bound {
129 if offset >= newline_upper_bound {
130 newline_count = 0;
131 } else {
132 newline_count = newline_upper_bound - offset;
133 }
134 } else if newline_count + offset < newline_lower_bound {
135 if offset >= newline_lower_bound {
136 newline_count = 0;
137 } else {
138 newline_count = newline_lower_bound - offset;
139 }
140 }
141
142 let blank_lines = "\n".repeat(newline_count);
143 self.push_str(&blank_lines);
144 }
145
146 fn write_snippet<F>(&mut self, span: Span, process_last_snippet: F)
147 where
148 F: Fn(&mut FmtVisitor<'_>, &str, &str),
149 {
150 let big_span_lo = self.snippet_provider.start_pos();
154 let big_snippet = self.snippet_provider.entire_snippet();
155 let big_diff = (span.lo() - big_span_lo).to_usize();
156
157 let snippet = self.snippet(span);
158
159 debug!("write_snippet `{}`", snippet);
160
161 self.write_snippet_inner(big_snippet, snippet, big_diff, span, process_last_snippet);
162 }
163
164 fn write_snippet_inner<F>(
165 &mut self,
166 big_snippet: &str,
167 old_snippet: &str,
168 big_diff: usize,
169 span: Span,
170 process_last_snippet: F,
171 ) where
172 F: Fn(&mut FmtVisitor<'_>, &str, &str),
173 {
174 let line = self.psess.line_of_byte_pos(span.lo());
178 let file_name = &self.psess.span_to_filename(span);
179 let mut status = SnippetStatus::new(line);
180
181 let snippet = &*transform_missing_snippet(self.config, old_snippet);
182
183 let slice_within_file_lines_range =
184 |file_lines: FileLines, cur_line, s| -> (usize, usize, bool) {
185 let (lf_count, crlf_count) = count_lf_crlf(s);
186 let newline_count = lf_count + crlf_count;
187 let within_file_lines_range = file_lines.contains_range(
188 file_name,
189 cur_line,
190 cur_line + newline_count - if s.ends_with('\n') { 1 } else { 0 },
194 );
195 (lf_count, crlf_count, within_file_lines_range)
196 };
197 for (kind, offset, subslice) in CommentCodeSlices::new(snippet) {
198 debug!("{:?}: {:?}", kind, subslice);
199
200 let (lf_count, crlf_count, within_file_lines_range) =
201 slice_within_file_lines_range(self.config.file_lines(), status.cur_line, subslice);
202 let newline_count = lf_count + crlf_count;
203 if CodeCharKind::Comment == kind && within_file_lines_range {
204 self.process_comment(
206 &mut status,
207 snippet,
208 &big_snippet[..(offset + big_diff)],
209 offset,
210 subslice,
211 );
212 } else if subslice.trim().is_empty() && newline_count > 0 && within_file_lines_range {
213 self.push_vertical_spaces(newline_count);
215 status.cur_line += newline_count;
216 status.line_start = offset + subslice.len()
218 } else {
219 self.process_missing_code(&mut status, snippet, subslice, offset, file_name);
221 }
222 }
223
224 let last_snippet = &snippet[status.line_start..];
225 let (_, _, within_file_lines_range) =
226 slice_within_file_lines_range(self.config.file_lines(), status.cur_line, last_snippet);
227 if within_file_lines_range {
228 process_last_snippet(self, last_snippet, snippet);
229 } else {
230 self.push_str(last_snippet);
232 }
233 }
234
235 fn process_comment(
236 &mut self,
237 status: &mut SnippetStatus,
238 snippet: &str,
239 big_snippet: &str,
240 offset: usize,
241 subslice: &str,
242 ) {
243 let last_char = big_snippet
244 .chars()
245 .rev()
246 .find(|rev_c| ![' ', '\t'].contains(rev_c));
247
248 let fix_indent = last_char.map_or(true, |rev_c| ['{', '\n'].contains(&rev_c));
249 let mut on_same_line = false;
250
251 let comment_indent = if fix_indent {
252 if let Some('{') = last_char {
253 self.push_str("\n");
254 }
255 let indent_str = self.block_indent.to_string(self.config);
256 self.push_str(&indent_str);
257 self.block_indent
258 } else if self.config.style_edition() >= StyleEdition::Edition2024
259 && !snippet.starts_with('\n')
260 {
261 on_same_line = true;
265 self.push_str(" ");
266 self.block_indent
267 } else {
268 self.push_str(" ");
269 Indent::from_width(self.config, last_line_width(&self.buffer))
270 };
271
272 let comment_width = ::std::cmp::min(
273 self.config.comment_width(),
274 self.config.max_width() - self.block_indent.width(),
275 );
276 let comment_shape = Shape::legacy(comment_width, comment_indent);
277
278 if on_same_line {
279 match subslice.find('\n') {
280 None => {
281 self.push_str(subslice);
282 }
283 Some(offset) if offset + 1 == subslice.len() => {
284 self.push_str(&subslice[..offset]);
285 }
286 Some(offset) => {
287 let first_line = &subslice[..offset];
290 self.push_str(first_line);
291 self.push_str(&comment_indent.to_string_with_newline(self.config));
292
293 let other_lines = &subslice[offset + 1..];
294 let comment_str =
295 rewrite_comment(other_lines, false, comment_shape, self.config)
296 .unwrap_or_else(|_| String::from(other_lines));
297 self.push_str(&comment_str);
298 }
299 }
300 } else {
301 let comment_str = rewrite_comment(subslice, false, comment_shape, self.config)
302 .unwrap_or_else(|_| String::from(subslice));
303 self.push_str(&comment_str);
304 }
305
306 status.last_wspace = None;
307 status.line_start = offset + subslice.len();
308
309 if status.line_start <= snippet.len() {
313 match snippet[status.line_start..]
314 .chars()
315 .find(|c| !(*c == ' ' || *c == '\t'))
317 {
318 Some('\n') | Some('\r') => {
319 if !is_last_comment_block(subslice) {
320 self.push_str("\n");
321 }
322 }
323 _ => self.push_str("\n"),
324 }
325 }
326
327 status.cur_line += count_newlines(subslice);
328 }
329
330 fn process_missing_code(
331 &mut self,
332 status: &mut SnippetStatus,
333 snippet: &str,
334 subslice: &str,
335 offset: usize,
336 file_name: &FileName,
337 ) {
338 for (mut i, c) in subslice.char_indices() {
339 i += offset;
340
341 if c == '\n' {
342 let skip_this_line = !self
343 .config
344 .file_lines()
345 .contains_line(file_name, status.cur_line);
346 if skip_this_line {
347 status.last_wspace = None;
348 }
349
350 if let Some(lw) = status.last_wspace {
351 self.push_str(&snippet[status.line_start..lw]);
352 self.push_str("\n");
353 status.last_wspace = None;
354 } else {
355 self.push_str(&snippet[status.line_start..=i]);
356 }
357
358 status.cur_line += 1;
359 status.line_start = i + 1;
360 } else if c.is_whitespace() && status.last_wspace.is_none() {
361 status.last_wspace = Some(i);
362 } else {
363 status.last_wspace = None;
364 }
365 }
366
367 let mut remaining = &snippet[status.line_start..subslice.len() + offset];
368 status.line_start = subslice.len() + offset;
369
370 let skip_this_line = !self
371 .config
372 .file_lines()
373 .contains_line(file_name, status.cur_line);
374 if !skip_this_line {
375 remaining = remaining.trim();
376 if !remaining.is_empty() {
377 self.push_str(&self.block_indent.to_string(self.config));
378 }
379 }
380
381 self.push_str(remaining);
382 }
383}