Skip to main content

annotate_snippets/renderer/
render.rs

1// Most of this file is adapted from https://github.com/rust-lang/rust/blob/160905b6253f42967ed4aef4b98002944c7df24c/compiler/rustc_errors/src/emitter.rs
2
3use alloc::borrow::{Cow, ToOwned};
4use alloc::collections::BTreeMap;
5use alloc::string::{String, ToString};
6use alloc::{format, vec, vec::Vec};
7use core::cmp::{Ordering, Reverse, max, min};
8use core::fmt;
9
10use anstyle::Style;
11
12use super::DecorStyle;
13use super::Renderer;
14use super::margin::Margin;
15use super::stylesheet::Stylesheet;
16use crate::level::{Level, LevelInner};
17use crate::renderer::source_map::{
18    AnnotatedLineInfo, LineInfo, Loc, SourceMap, SplicedLines, SubstitutionHighlight, TrimmedPatch,
19};
20use crate::renderer::styled_buffer::StyledBuffer;
21use crate::snippet::Id;
22use crate::{
23    Annotation, AnnotationKind, Element, Group, Message, Origin, Padding, Patch, Report, Snippet,
24    Title,
25};
26
27const ANONYMIZED_LINE_NUM: &str = "LL";
28
29pub(crate) fn render(renderer: &Renderer, groups: Report<'_>) -> String {
30    if renderer.short_message {
31        render_short_message(renderer, groups).unwrap()
32    } else {
33        let (max_line_num, og_primary_path, groups) = pre_process(groups);
34        let max_line_num_len = if renderer.anonymized_line_numbers {
35            ANONYMIZED_LINE_NUM.len()
36        } else {
37            num_decimal_digits(max_line_num)
38        };
39        let mut out_string = String::new();
40        let group_len = groups.len();
41        for (
42            g,
43            PreProcessedGroup {
44                group,
45                elements,
46                primary_path,
47                max_depth,
48            },
49        ) in groups.into_iter().enumerate()
50        {
51            let mut buffer = StyledBuffer::new();
52            let level = group.primary_level.clone();
53            let mut message_iter = elements.into_iter().enumerate().peekable();
54            if let Some(title) = &group.title {
55                let peek = message_iter.peek().map(|(_, s)| s);
56                let title_style = if title.allows_styling {
57                    TitleStyle::Header
58                } else {
59                    TitleStyle::MainHeader
60                };
61                let buffer_msg_line_offset = buffer.num_lines();
62                render_title(
63                    renderer,
64                    &mut buffer,
65                    title,
66                    max_line_num_len,
67                    title_style,
68                    matches!(peek, Some(PreProcessedElement::Message(_))),
69                    buffer_msg_line_offset,
70                );
71                let buffer_msg_line_offset = buffer.num_lines();
72
73                if matches!(peek, Some(PreProcessedElement::Message(_))) {
74                    draw_col_separator_no_space(
75                        renderer,
76                        &mut buffer,
77                        buffer_msg_line_offset,
78                        max_line_num_len + 1,
79                    );
80                }
81                if peek.is_none()
82                    && title_style == TitleStyle::MainHeader
83                    && g == 0
84                    && group_len > 1
85                {
86                    draw_col_separator_end(
87                        renderer,
88                        &mut buffer,
89                        buffer_msg_line_offset,
90                        max_line_num_len + 1,
91                    );
92                }
93            }
94            let mut seen_primary = false;
95            let mut last_suggestion_path = None;
96            while let Some((i, section)) = message_iter.next() {
97                let peek = message_iter.peek().map(|(_, s)| s);
98                let is_first = i == 0;
99                match section {
100                    PreProcessedElement::Message(title) => {
101                        let title_style = TitleStyle::Secondary;
102                        let buffer_msg_line_offset = buffer.num_lines();
103                        render_title(
104                            renderer,
105                            &mut buffer,
106                            title,
107                            max_line_num_len,
108                            title_style,
109                            peek.is_some(),
110                            buffer_msg_line_offset,
111                        );
112                    }
113                    PreProcessedElement::Cause((cause, source_map, annotated_lines)) => {
114                        let is_primary = primary_path == cause.path.as_ref() && !seen_primary;
115                        seen_primary |= is_primary;
116                        render_snippet_annotations(
117                            renderer,
118                            &mut buffer,
119                            max_line_num_len,
120                            cause,
121                            is_primary,
122                            &source_map,
123                            &annotated_lines,
124                            max_depth,
125                            peek.is_some() || (g == 0 && group_len > 1),
126                            is_first,
127                        );
128
129                        if g == 0 {
130                            let current_line = buffer.num_lines();
131                            match peek {
132                                Some(PreProcessedElement::Message(_)) => {
133                                    draw_col_separator_no_space(
134                                        renderer,
135                                        &mut buffer,
136                                        current_line,
137                                        max_line_num_len + 1,
138                                    );
139                                }
140                                None if group_len > 1 => draw_col_separator_end(
141                                    renderer,
142                                    &mut buffer,
143                                    current_line,
144                                    max_line_num_len + 1,
145                                ),
146                                _ => {}
147                            }
148                        }
149                    }
150                    PreProcessedElement::Suggestion((
151                        suggestion,
152                        source_map,
153                        spliced_lines,
154                        display_suggestion,
155                    )) => {
156                        let matches_previous_suggestion =
157                            last_suggestion_path == Some(suggestion.path.as_ref());
158                        emit_suggestion_default(
159                            renderer,
160                            &mut buffer,
161                            suggestion,
162                            spliced_lines,
163                            display_suggestion,
164                            max_line_num_len,
165                            &source_map,
166                            primary_path.or(og_primary_path),
167                            matches_previous_suggestion,
168                            is_first,
169                            //matches!(peek, Some(Element::Message(_) | Element::Padding(_))),
170                            peek.is_some(),
171                        );
172
173                        if matches!(peek, Some(PreProcessedElement::Suggestion(_))) {
174                            last_suggestion_path = Some(suggestion.path.as_ref());
175                        } else {
176                            last_suggestion_path = None;
177                        }
178                    }
179
180                    PreProcessedElement::Origin(origin) => {
181                        let buffer_msg_line_offset = buffer.num_lines();
182                        let is_primary = primary_path == Some(&origin.path) && !seen_primary;
183                        seen_primary |= is_primary;
184                        render_origin(
185                            renderer,
186                            &mut buffer,
187                            max_line_num_len,
188                            origin,
189                            is_primary,
190                            is_first,
191                            peek.is_none(),
192                            buffer_msg_line_offset,
193                        );
194                        let current_line = buffer.num_lines();
195                        if g == 0 && peek.is_none() && group_len > 1 {
196                            draw_col_separator_end(
197                                renderer,
198                                &mut buffer,
199                                current_line,
200                                max_line_num_len + 1,
201                            );
202                        }
203                    }
204                    PreProcessedElement::Padding(_) => {
205                        let current_line = buffer.num_lines();
206                        if peek.is_none() {
207                            draw_col_separator_end(
208                                renderer,
209                                &mut buffer,
210                                current_line,
211                                max_line_num_len + 1,
212                            );
213                        } else {
214                            draw_col_separator_no_space(
215                                renderer,
216                                &mut buffer,
217                                current_line,
218                                max_line_num_len + 1,
219                            );
220                        }
221                    }
222                }
223            }
224            buffer
225                .render(&level, &renderer.stylesheet, &mut out_string)
226                .unwrap();
227            if g != group_len - 1 {
228                use core::fmt::Write;
229
230                writeln!(out_string).unwrap();
231            }
232        }
233        out_string
234    }
235}
236
237fn render_short_message(renderer: &Renderer, groups: &[Group<'_>]) -> Result<String, fmt::Error> {
238    let mut buffer = StyledBuffer::new();
239    let mut labels = None;
240    let group = groups.first().expect("Expected at least one group");
241
242    let Some(title) = &group.title else {
243        panic!("Expected a Title");
244    };
245
246    if let Some(Element::Cause(cause)) = group
247        .elements
248        .iter()
249        .find(|e| matches!(e, Element::Cause(_)))
250    {
251        let labels_inner = cause
252            .markers
253            .iter()
254            .filter_map(|ann| match &ann.label {
255                Some(msg) if ann.kind.is_primary() => {
256                    if !msg.trim().is_empty() {
257                        Some(msg.to_string())
258                    } else {
259                        None
260                    }
261                }
262                _ => None,
263            })
264            .collect::<Vec<_>>()
265            .join(", ");
266        if !labels_inner.is_empty() {
267            labels = Some(labels_inner);
268        }
269
270        if let Some(path) = &cause.path {
271            let mut origin = Origin::path(path.as_ref());
272
273            let source_map = SourceMap::new(&cause.source, cause.line_start);
274            let (_depth, annotated_lines) =
275                source_map.annotated_lines(cause.markers.clone(), cause.fold);
276
277            if let Some(primary_line) = annotated_lines
278                .iter()
279                .find(|l| l.annotations.iter().any(LineAnnotation::is_primary))
280                .or(annotated_lines.iter().find(|l| !l.annotations.is_empty()))
281            {
282                origin.line = Some(primary_line.line_index);
283                if let Some(first_annotation) = primary_line
284                    .annotations
285                    .iter()
286                    .min_by_key(|a| (Reverse(a.is_primary()), a.start.char))
287                {
288                    origin.char_column = Some(first_annotation.start.char + 1);
289                }
290            }
291
292            render_origin(renderer, &mut buffer, 0, &origin, true, true, true, 0);
293            buffer.append(0, ": ", ElementStyle::LineAndColumn);
294        }
295    }
296
297    render_title(
298        renderer,
299        &mut buffer,
300        title,
301        0, // No line numbers in short messages
302        TitleStyle::MainHeader,
303        false,
304        0,
305    );
306
307    if let Some(labels) = labels {
308        buffer.append(0, &format!(": {labels}"), ElementStyle::NoStyle);
309    }
310
311    let mut out_string = String::new();
312    buffer.render(&title.level, &renderer.stylesheet, &mut out_string)?;
313
314    Ok(out_string)
315}
316
317#[allow(clippy::too_many_arguments)]
318fn render_title(
319    renderer: &Renderer,
320    buffer: &mut StyledBuffer,
321    title: &dyn MessageOrTitle,
322    max_line_num_len: usize,
323    title_style: TitleStyle,
324    is_cont: bool,
325    buffer_msg_line_offset: usize,
326) {
327    let (label_style, title_element_style) = match title_style {
328        TitleStyle::MainHeader => (
329            ElementStyle::Level(title.level().level),
330            if renderer.short_message {
331                ElementStyle::NoStyle
332            } else {
333                ElementStyle::MainHeaderMsg
334            },
335        ),
336        TitleStyle::Header => (
337            ElementStyle::Level(title.level().level),
338            ElementStyle::HeaderMsg,
339        ),
340        TitleStyle::Secondary => {
341            for _ in 0..max_line_num_len {
342                buffer.prepend(buffer_msg_line_offset, " ", ElementStyle::NoStyle);
343            }
344
345            draw_note_separator(
346                renderer,
347                buffer,
348                buffer_msg_line_offset,
349                max_line_num_len + 1,
350                is_cont,
351            );
352            (ElementStyle::MainHeaderMsg, ElementStyle::NoStyle)
353        }
354    };
355    let mut label_width = 0;
356
357    if title.level().name != Some(None) {
358        buffer.append(buffer_msg_line_offset, title.level().as_str(), label_style);
359        label_width += title.level().as_str().len();
360        if let Some(Id { id: Some(id), url }) = &title.id() {
361            buffer.append(buffer_msg_line_offset, "[", label_style);
362            if let Some(url) = url.as_ref() {
363                buffer.append(
364                    buffer_msg_line_offset,
365                    &format!("\x1B]8;;{url}\x1B\\"),
366                    label_style,
367                );
368            }
369            buffer.append(buffer_msg_line_offset, id, label_style);
370            if url.is_some() {
371                buffer.append(buffer_msg_line_offset, "\x1B]8;;\x1B\\", label_style);
372            }
373            buffer.append(buffer_msg_line_offset, "]", label_style);
374            label_width += 2 + id.len();
375        }
376        buffer.append(buffer_msg_line_offset, ": ", title_element_style);
377        label_width += 2;
378    }
379
380    let padding = " ".repeat(if title_style == TitleStyle::Secondary {
381        // The extra 3 ` ` is padding that's always needed to align to the
382        // label i.e. `note: `:
383        //
384        //   error: message
385        //     --> file.rs:13:20
386        //      |
387        //   13 |     <CODE>
388        //      |      ^^^^
389        //      |
390        //      = note: multiline
391        //              message
392        //   ++^^^------
393        //    |  |     |
394        //    |  |     |
395        //    |  |     width of label
396        //    |  magic `3`
397        //    `max_line_num_len`
398        max_line_num_len + 3 + label_width
399    } else {
400        label_width
401    });
402
403    let (title_str, style) = if title.allows_styling() {
404        (title.text().to_owned(), ElementStyle::NoStyle)
405    } else {
406        (normalize_whitespace(title.text()), title_element_style)
407    };
408    for (i, text) in title_str.split('\n').enumerate() {
409        if i != 0 {
410            buffer.append(buffer_msg_line_offset + i, &padding, ElementStyle::NoStyle);
411            if title_style == TitleStyle::Secondary
412                && is_cont
413                && matches!(renderer.decor_style, DecorStyle::Unicode)
414            {
415                // There's another note after this one, associated to the subwindow above.
416                // We write additional vertical lines to join them:
417                //   ╭▸ test.rs:3:3
418                //   │
419                // 3 │   code
420                //   │   ━━━━
421                //   │
422                //   ├ note: foo
423                //   │       bar
424                //   ╰ note: foo
425                //           bar
426                draw_col_separator_no_space(
427                    renderer,
428                    buffer,
429                    buffer_msg_line_offset + i,
430                    max_line_num_len + 1,
431                );
432            }
433        }
434        buffer.append(buffer_msg_line_offset + i, text, style);
435    }
436}
437
438#[allow(clippy::too_many_arguments)]
439fn render_origin(
440    renderer: &Renderer,
441    buffer: &mut StyledBuffer,
442    max_line_num_len: usize,
443    origin: &Origin<'_>,
444    is_primary: bool,
445    is_first: bool,
446    alone: bool,
447    buffer_msg_line_offset: usize,
448) {
449    if is_primary && !renderer.short_message {
450        buffer.prepend(
451            buffer_msg_line_offset,
452            renderer.decor_style.file_start(is_first, alone),
453            ElementStyle::LineNumber,
454        );
455    } else if !renderer.short_message {
456        // if !origin.standalone {
457        //     // Add spacing line, as shown:
458        //     //   --> $DIR/file:54:15
459        //     //    |
460        //     // LL |         code
461        //     //    |         ^^^^
462        //     //    | (<- It prints *this* line)
463        //     //   ::: $DIR/other_file.rs:15:5
464        //     //    |
465        //     // LL |     code
466        //     //    |     ----
467        //     draw_col_separator_no_space(renderer,
468        //         buffer,
469        //         buffer_msg_line_offset,
470        //         max_line_num_len + 1,
471        //     );
472        //
473        //     buffer_msg_line_offset += 1;
474        // }
475        // Then, the secondary file indicator
476        buffer.prepend(
477            buffer_msg_line_offset,
478            renderer.decor_style.secondary_file_start(),
479            ElementStyle::LineNumber,
480        );
481    }
482
483    let str = match (&origin.line, &origin.char_column) {
484        (Some(line), Some(col)) => {
485            format!("{}:{}:{}", origin.path, line, col)
486        }
487        (Some(line), None) => format!("{}:{}", origin.path, line),
488        _ => origin.path.to_string(),
489    };
490
491    buffer.append(buffer_msg_line_offset, &str, ElementStyle::LineAndColumn);
492    if !renderer.short_message {
493        for _ in 0..max_line_num_len {
494            buffer.prepend(buffer_msg_line_offset, " ", ElementStyle::NoStyle);
495        }
496    }
497}
498
499#[allow(clippy::too_many_arguments)]
500fn render_snippet_annotations(
501    renderer: &Renderer,
502    buffer: &mut StyledBuffer,
503    max_line_num_len: usize,
504    snippet: &Snippet<'_, Annotation<'_>>,
505    is_primary: bool,
506    sm: &SourceMap<'_>,
507    annotated_lines: &[AnnotatedLineInfo<'_>],
508    multiline_depth: usize,
509    is_cont: bool,
510    is_first: bool,
511) {
512    if let Some(path) = &snippet.path {
513        let mut origin = Origin::path(path.as_ref());
514        // print out the span location and spacer before we print the annotated source
515        // to do this, we need to know if this span will be primary
516        //let is_primary = primary_path == Some(&origin.path);
517
518        if is_primary {
519            if let Some(primary_line) = annotated_lines
520                .iter()
521                .find(|l| l.annotations.iter().any(LineAnnotation::is_primary))
522                .or(annotated_lines.iter().find(|l| !l.annotations.is_empty()))
523            {
524                origin.line = Some(primary_line.line_index);
525                if let Some(first_annotation) = primary_line
526                    .annotations
527                    .iter()
528                    .min_by_key(|a| (Reverse(a.is_primary()), a.start.char))
529                {
530                    origin.char_column = Some(first_annotation.start.char + 1);
531                }
532            }
533        } else {
534            let buffer_msg_line_offset = buffer.num_lines();
535            // Add spacing line, as shown:
536            //   --> $DIR/file:54:15
537            //    |
538            // LL |         code
539            //    |         ^^^^
540            //    | (<- It prints *this* line)
541            //   ::: $DIR/other_file.rs:15:5
542            //    |
543            // LL |     code
544            //    |     ----
545            draw_col_separator_no_space(
546                renderer,
547                buffer,
548                buffer_msg_line_offset,
549                max_line_num_len + 1,
550            );
551            if let Some(first_line) = annotated_lines.first() {
552                origin.line = Some(first_line.line_index);
553                if let Some(first_annotation) = first_line.annotations.first() {
554                    origin.char_column = Some(first_annotation.start.char + 1);
555                }
556            }
557        }
558        let buffer_msg_line_offset = buffer.num_lines();
559        render_origin(
560            renderer,
561            buffer,
562            max_line_num_len,
563            &origin,
564            is_primary,
565            is_first,
566            false,
567            buffer_msg_line_offset,
568        );
569        // Put in the spacer between the location and annotated source
570        draw_col_separator_no_space(
571            renderer,
572            buffer,
573            buffer_msg_line_offset + 1,
574            max_line_num_len + 1,
575        );
576    } else {
577        let buffer_msg_line_offset = buffer.num_lines();
578        if is_primary {
579            if renderer.decor_style == DecorStyle::Unicode {
580                buffer.puts(
581                    buffer_msg_line_offset,
582                    max_line_num_len,
583                    renderer.decor_style.file_start(is_first, false),
584                    ElementStyle::LineNumber,
585                );
586            } else {
587                draw_col_separator_no_space(
588                    renderer,
589                    buffer,
590                    buffer_msg_line_offset,
591                    max_line_num_len + 1,
592                );
593            }
594        } else {
595            // Add spacing line, as shown:
596            //   --> $DIR/file:54:15
597            //    |
598            // LL |         code
599            //    |         ^^^^
600            //    | (<- It prints *this* line)
601            //   ::: $DIR/other_file.rs:15:5
602            //    |
603            // LL |     code
604            //    |     ----
605            draw_col_separator_no_space(
606                renderer,
607                buffer,
608                buffer_msg_line_offset,
609                max_line_num_len + 1,
610            );
611
612            buffer.puts(
613                buffer_msg_line_offset + 1,
614                max_line_num_len,
615                renderer.decor_style.secondary_file_start(),
616                ElementStyle::LineNumber,
617            );
618        }
619    }
620
621    // Contains the vertical lines' positions for active multiline annotations
622    let mut multilines = Vec::new();
623
624    // Get the left-side margin to remove it
625    let mut whitespace_margin = usize::MAX;
626    for line_info in annotated_lines {
627        let leading_whitespace = line_info
628            .line
629            .chars()
630            .take_while(|c| c.is_whitespace())
631            .map(|c| {
632                match c {
633                    // Tabs are displayed as 4 spaces
634                    '\t' => 4,
635                    _ => 1,
636                }
637            })
638            .sum();
639        if line_info.line.chars().any(|c| !c.is_whitespace()) {
640            whitespace_margin = min(whitespace_margin, leading_whitespace);
641        }
642    }
643    if whitespace_margin == usize::MAX {
644        whitespace_margin = 0;
645    }
646
647    // Left-most column any visible span points at.
648    let mut span_left_margin = usize::MAX;
649    for line_info in annotated_lines {
650        for ann in &line_info.annotations {
651            span_left_margin = min(span_left_margin, ann.start.display);
652            span_left_margin = min(span_left_margin, ann.end.display);
653        }
654    }
655    if span_left_margin == usize::MAX {
656        span_left_margin = 0;
657    }
658
659    // Right-most column any visible span points at.
660    let mut span_right_margin = 0;
661    let mut label_right_margin = 0;
662    let mut max_line_len = 0;
663    for line_info in annotated_lines {
664        max_line_len = max(max_line_len, str_width(line_info.line));
665        for ann in &line_info.annotations {
666            span_right_margin = max(span_right_margin, ann.start.display);
667            span_right_margin = max(span_right_margin, ann.end.display);
668            // FIXME: account for labels not in the same line
669            let label_right = ann.label.as_ref().map_or(0, |l| str_width(l) + 1);
670            label_right_margin = max(label_right_margin, ann.end.display + label_right);
671        }
672    }
673    let width_offset = 3 + max_line_num_len;
674    let code_offset = if multiline_depth == 0 {
675        width_offset
676    } else {
677        width_offset + multiline_depth + 1
678    };
679
680    let column_width = renderer.term_width.saturating_sub(code_offset);
681
682    let margin = Margin::new(
683        whitespace_margin,
684        span_left_margin,
685        span_right_margin,
686        label_right_margin,
687        column_width,
688        max_line_len,
689    );
690
691    // Next, output the annotate source for this file
692    for annotated_line_idx in 0..annotated_lines.len() {
693        let previous_buffer_line = buffer.num_lines();
694
695        let depths = render_source_line(
696            renderer,
697            &annotated_lines[annotated_line_idx],
698            buffer,
699            width_offset,
700            code_offset,
701            max_line_num_len,
702            margin,
703            !is_cont && annotated_line_idx + 1 == annotated_lines.len(),
704        );
705
706        let mut to_add = BTreeMap::new();
707
708        for (depth, style) in depths {
709            if let Some(index) = multilines.iter().position(|(d, _)| d == &depth) {
710                multilines.swap_remove(index);
711            } else {
712                to_add.insert(depth, style);
713            }
714        }
715
716        // Set the multiline annotation vertical lines to the left of
717        // the code in this line.
718        for (depth, style) in &multilines {
719            for line in previous_buffer_line..buffer.num_lines() {
720                draw_multiline_line(renderer, buffer, line, width_offset, *depth, *style);
721            }
722        }
723        // check to see if we need to print out or elide lines that come between
724        // this annotated line and the next one.
725        if annotated_line_idx < (annotated_lines.len() - 1) {
726            let line_idx_delta = annotated_lines[annotated_line_idx + 1].line_index
727                - annotated_lines[annotated_line_idx].line_index;
728            match line_idx_delta.cmp(&2) {
729                Ordering::Greater => {
730                    let last_buffer_line_num = buffer.num_lines();
731
732                    draw_line_separator(renderer, buffer, last_buffer_line_num, width_offset);
733
734                    // Set the multiline annotation vertical lines on `...` bridging line.
735                    for (depth, style) in &multilines {
736                        draw_multiline_line(
737                            renderer,
738                            buffer,
739                            last_buffer_line_num,
740                            width_offset,
741                            *depth,
742                            *style,
743                        );
744                    }
745                    if let Some(line) = annotated_lines.get(annotated_line_idx) {
746                        for ann in &line.annotations {
747                            if let LineAnnotationType::MultilineStart(pos) = ann.annotation_type {
748                                // In the case where we have elided the entire start of the
749                                // multispan because those lines were empty, we still need
750                                // to draw the `|`s across the `...`.
751                                draw_multiline_line(
752                                    renderer,
753                                    buffer,
754                                    last_buffer_line_num,
755                                    width_offset,
756                                    pos,
757                                    if ann.is_primary() {
758                                        ElementStyle::UnderlinePrimary
759                                    } else {
760                                        ElementStyle::UnderlineSecondary
761                                    },
762                                );
763                            }
764                        }
765                    }
766                }
767
768                Ordering::Equal => {
769                    let unannotated_line = sm
770                        .get_line(annotated_lines[annotated_line_idx].line_index + 1)
771                        .unwrap_or("");
772
773                    let last_buffer_line_num = buffer.num_lines();
774
775                    draw_line(
776                        renderer,
777                        buffer,
778                        &normalize_whitespace(unannotated_line),
779                        annotated_lines[annotated_line_idx + 1].line_index - 1,
780                        last_buffer_line_num,
781                        width_offset,
782                        code_offset,
783                        max_line_num_len,
784                        margin,
785                    );
786
787                    for (depth, style) in &multilines {
788                        draw_multiline_line(
789                            renderer,
790                            buffer,
791                            last_buffer_line_num,
792                            width_offset,
793                            *depth,
794                            *style,
795                        );
796                    }
797                    if let Some(line) = annotated_lines.get(annotated_line_idx) {
798                        for ann in &line.annotations {
799                            if let LineAnnotationType::MultilineStart(pos) = ann.annotation_type {
800                                draw_multiline_line(
801                                    renderer,
802                                    buffer,
803                                    last_buffer_line_num,
804                                    width_offset,
805                                    pos,
806                                    if ann.is_primary() {
807                                        ElementStyle::UnderlinePrimary
808                                    } else {
809                                        ElementStyle::UnderlineSecondary
810                                    },
811                                );
812                            }
813                        }
814                    }
815                }
816                Ordering::Less => {}
817            }
818        }
819
820        multilines.extend(to_add);
821    }
822}
823
824#[allow(clippy::too_many_arguments)]
825fn render_source_line(
826    renderer: &Renderer,
827    line_info: &AnnotatedLineInfo<'_>,
828    buffer: &mut StyledBuffer,
829    width_offset: usize,
830    code_offset: usize,
831    max_line_num_len: usize,
832    margin: Margin,
833    close_window: bool,
834) -> Vec<(usize, ElementStyle)> {
835    // Draw:
836    //
837    //   LL | ... code ...
838    //      |     ^^-^ span label
839    //      |       |
840    //      |       secondary span label
841    //
842    //   ^^ ^ ^^^ ^^^^ ^^^ we don't care about code too far to the right of a span, we trim it
843    //   |  | |   |
844    //   |  | |   actual code found in your source code and the spans we use to mark it
845    //   |  | when there's too much wasted space to the left, trim it
846    //   |  vertical divider between the column number and the code
847    //   column number
848
849    let source_string = normalize_whitespace(line_info.line);
850
851    let line_offset = buffer.num_lines();
852
853    let left = draw_line(
854        renderer,
855        buffer,
856        &source_string,
857        line_info.line_index,
858        line_offset,
859        width_offset,
860        code_offset,
861        max_line_num_len,
862        margin,
863    );
864
865    // If there are no annotations, we are done
866    if line_info.annotations.is_empty() {
867        // `close_window` normally gets handled later, but we are early
868        // returning, so it needs to be handled here
869        if close_window {
870            draw_col_separator_end(renderer, buffer, line_offset + 1, width_offset - 2);
871        }
872        return vec![];
873    }
874
875    // Special case when there's only one annotation involved, it is the start of a multiline
876    // span and there's no text at the beginning of the code line. Instead of doing the whole
877    // graph:
878    //
879    // 2 |   fn foo() {
880    //   |  _^
881    // 3 | |
882    // 4 | | }
883    //   | |_^ test
884    //
885    // we simplify the output to:
886    //
887    // 2 | / fn foo() {
888    // 3 | |
889    // 4 | | }
890    //   | |_^ test
891    let mut buffer_ops = vec![];
892    let mut annotations = vec![];
893    let mut short_start = true;
894    for ann in &line_info.annotations {
895        if let LineAnnotationType::MultilineStart(depth) = ann.annotation_type {
896            if source_string
897                .chars()
898                .take(ann.start.display)
899                .all(char::is_whitespace)
900            {
901                let uline = renderer.decor_style.underline(ann.is_primary());
902                let chr = uline.multiline_whole_line;
903                annotations.push((depth, uline.style));
904                buffer_ops.push((line_offset, width_offset + depth - 1, chr, uline.style));
905            } else {
906                short_start = false;
907                break;
908            }
909        } else if let LineAnnotationType::MultilineLine(_) = ann.annotation_type {
910        } else {
911            short_start = false;
912            break;
913        }
914    }
915    if short_start {
916        for (y, x, c, s) in buffer_ops {
917            buffer.putc(y, x, c, s);
918        }
919        return annotations;
920    }
921
922    // We want to display like this:
923    //
924    //      vec.push(vec.pop().unwrap());
925    //      ---      ^^^               - previous borrow ends here
926    //      |        |
927    //      |        error occurs here
928    //      previous borrow of `vec` occurs here
929    //
930    // But there are some weird edge cases to be aware of:
931    //
932    //      vec.push(vec.pop().unwrap());
933    //      --------                    - previous borrow ends here
934    //      ||
935    //      |this makes no sense
936    //      previous borrow of `vec` occurs here
937    //
938    // For this reason, we group the lines into "highlight lines"
939    // and "annotations lines", where the highlight lines have the `^`.
940
941    // Sort the annotations by (start, end col)
942    // The labels are reversed, sort and then reversed again.
943    // Consider a list of annotations (A1, A2, C1, C2, B1, B2) where
944    // the letter signifies the span. Here we are only sorting by the
945    // span and hence, the order of the elements with the same span will
946    // not change. On reversing the ordering (|a, b| but b.cmp(a)), you get
947    // (C1, C2, B1, B2, A1, A2). All the elements with the same span are
948    // still ordered first to last, but all the elements with different
949    // spans are ordered by their spans in last to first order. Last to
950    // first order is important, because the jiggly lines and | are on
951    // the left, so the rightmost span needs to be rendered first,
952    // otherwise the lines would end up needing to go over a message.
953
954    let mut annotations = line_info.annotations.clone();
955    annotations.sort_by_key(|a| Reverse((a.start.display, a.start.char)));
956
957    // First, figure out where each label will be positioned.
958    //
959    // In the case where you have the following annotations:
960    //
961    //      vec.push(vec.pop().unwrap());
962    //      --------                    - previous borrow ends here [C]
963    //      ||
964    //      |this makes no sense [B]
965    //      previous borrow of `vec` occurs here [A]
966    //
967    // `annotations_position` will hold [(2, A), (1, B), (0, C)].
968    //
969    // We try, when possible, to stick the rightmost annotation at the end
970    // of the highlight line:
971    //
972    //      vec.push(vec.pop().unwrap());
973    //      ---      ---               - previous borrow ends here
974    //
975    // But sometimes that's not possible because one of the other
976    // annotations overlaps it. For example, from the test
977    // `span_overlap_label`, we have the following annotations
978    // (written on distinct lines for clarity):
979    //
980    //      fn foo(x: u32) {
981    //      --------------
982    //             -
983    //
984    // In this case, we can't stick the rightmost-most label on
985    // the highlight line, or we would get:
986    //
987    //      fn foo(x: u32) {
988    //      -------- x_span
989    //      |
990    //      fn_span
991    //
992    // which is totally weird. Instead we want:
993    //
994    //      fn foo(x: u32) {
995    //      --------------
996    //      |      |
997    //      |      x_span
998    //      fn_span
999    //
1000    // which is...less weird, at least. In fact, in general, if
1001    // the rightmost span overlaps with any other span, we should
1002    // use the "hang below" version, so we can at least make it
1003    // clear where the span *starts*. There's an exception for this
1004    // logic, when the labels do not have a message:
1005    //
1006    //      fn foo(x: u32) {
1007    //      --------------
1008    //             |
1009    //             x_span
1010    //
1011    // instead of:
1012    //
1013    //      fn foo(x: u32) {
1014    //      --------------
1015    //      |      |
1016    //      |      x_span
1017    //      <EMPTY LINE>
1018    //
1019    let mut overlap = vec![false; annotations.len()];
1020    let mut annotations_position = vec![];
1021    let mut line_len: usize = 0;
1022    let mut p = 0;
1023    for (i, annotation) in annotations.iter().enumerate() {
1024        for (j, next) in annotations.iter().enumerate() {
1025            if overlaps(next, annotation, 0) && j > 1 {
1026                overlap[i] = true;
1027                overlap[j] = true;
1028            }
1029            if overlaps(next, annotation, 0)  // This label overlaps with another one and both
1030                    && annotation.has_label()     // take space (they have text and are not
1031                    && j > i                      // multiline lines).
1032                    && p == 0
1033            // We're currently on the first line, move the label one line down
1034            {
1035                // If we're overlapping with an un-labelled annotation with the same span
1036                // we can just merge them in the output
1037                if next.start.display == annotation.start.display
1038                    && next.start.char == annotation.start.char
1039                    && next.end.display == annotation.end.display
1040                    && next.end.char == annotation.end.char
1041                    && !next.has_label()
1042                {
1043                    continue;
1044                }
1045
1046                // This annotation needs a new line in the output.
1047                p += 1;
1048                break;
1049            }
1050        }
1051        annotations_position.push((p, annotation));
1052        for (j, next) in annotations.iter().enumerate() {
1053            if j > i {
1054                let l = next.label.as_ref().map_or(0, |label| label.len() + 2);
1055                if (overlaps(next, annotation, l) // Do not allow two labels to be in the same
1056                        // line if they overlap including padding, to
1057                        // avoid situations like:
1058                        //
1059                        //      fn foo(x: u32) {
1060                        //      -------^------
1061                        //      |      |
1062                        //      fn_spanx_span
1063                        //
1064                        && annotation.has_label()    // Both labels must have some text, otherwise
1065                        && next.has_label())         // they are not overlapping.
1066                        // Do not add a new line if this annotation
1067                        // or the next are vertical line placeholders.
1068                        || (annotation.takes_space() // If either this or the next annotation is
1069                        && next.has_label())     // multiline start/end, move it to a new line
1070                        || (annotation.has_label()   // so as not to overlap the horizontal lines.
1071                        && next.takes_space())
1072                        || (annotation.takes_space() && next.takes_space())
1073                        || (overlaps(next, annotation, l)
1074                        && (next.end.display, next.end.char) <= (annotation.end.display, annotation.end.char)
1075                        && next.has_label()
1076                        && p == 0)
1077                // Avoid #42595.
1078                {
1079                    // This annotation needs a new line in the output.
1080                    p += 1;
1081                    break;
1082                }
1083            }
1084        }
1085        line_len = max(line_len, p);
1086    }
1087
1088    if line_len != 0 {
1089        line_len += 1;
1090    }
1091
1092    // If there are no annotations or the only annotations on this line are
1093    // MultilineLine, then there's only code being shown, stop processing.
1094    if line_info.annotations.iter().all(LineAnnotation::is_line) {
1095        return vec![];
1096    }
1097
1098    if annotations_position
1099        .iter()
1100        .all(|(_, ann)| matches!(ann.annotation_type, LineAnnotationType::MultilineStart(_)))
1101    {
1102        if let Some(max_pos) = annotations_position.iter().map(|(pos, _)| *pos).max() {
1103            // Special case the following, so that we minimize overlapping multiline spans.
1104            //
1105            // 3 │       X0 Y0 Z0
1106            //   │ ┏━━━━━┛  │  │     < We are writing these lines
1107            //   │ ┃┌───────┘  │     < by reverting the "depth" of
1108            //   │ ┃│┌─────────┘     < their multiline spans.
1109            // 4 │ ┃││   X1 Y1 Z1
1110            // 5 │ ┃││   X2 Y2 Z2
1111            //   │ ┃│└────╿──│──┘ `Z` label
1112            //   │ ┃└─────│──┤
1113            //   │ ┗━━━━━━┥  `Y` is a good letter too
1114            //   ╰╴       `X` is a good letter
1115            for (pos, _) in &mut annotations_position {
1116                *pos = max_pos - *pos;
1117            }
1118            // We know then that we don't need an additional line for the span label, saving us
1119            // one line of vertical space.
1120            line_len = line_len.saturating_sub(1);
1121        }
1122    }
1123
1124    // Write the column separator.
1125    //
1126    // After this we will have:
1127    //
1128    // 2 |   fn foo() {
1129    //   |
1130    //   |
1131    //   |
1132    // 3 |
1133    // 4 |   }
1134    //   |
1135    for pos in 0..=line_len {
1136        draw_col_separator_no_space(renderer, buffer, line_offset + pos + 1, width_offset - 2);
1137    }
1138    if close_window {
1139        draw_col_separator_end(
1140            renderer,
1141            buffer,
1142            line_offset + line_len + 1,
1143            width_offset - 2,
1144        );
1145    }
1146    // Write the horizontal lines for multiline annotations
1147    // (only the first and last lines need this).
1148    //
1149    // After this we will have:
1150    //
1151    // 2 |   fn foo() {
1152    //   |  __________
1153    //   |
1154    //   |
1155    // 3 |
1156    // 4 |   }
1157    //   |  _
1158    for &(pos, annotation) in &annotations_position {
1159        let underline = renderer.decor_style.underline(annotation.is_primary());
1160        let pos = pos + 1;
1161        match annotation.annotation_type {
1162            LineAnnotationType::MultilineStart(depth) | LineAnnotationType::MultilineEnd(depth) => {
1163                draw_range(
1164                    buffer,
1165                    underline.multiline_horizontal,
1166                    line_offset + pos,
1167                    width_offset + depth,
1168                    (code_offset + annotation.start.display).saturating_sub(left),
1169                    underline.style,
1170                );
1171            }
1172            _ if annotation.highlight_source => {
1173                buffer.set_style_range(
1174                    line_offset,
1175                    (code_offset + annotation.start.char).saturating_sub(left),
1176                    (code_offset + annotation.end.char).saturating_sub(left),
1177                    underline.style,
1178                    annotation.is_primary(),
1179                );
1180            }
1181            _ => {}
1182        }
1183    }
1184
1185    // Write the vertical lines for labels that are on a different line as the underline.
1186    //
1187    // After this we will have:
1188    //
1189    // 2 |   fn foo() {
1190    //   |  __________
1191    //   | |    |
1192    //   | |
1193    // 3 | |
1194    // 4 | | }
1195    //   | |_
1196    for &(pos, annotation) in &annotations_position {
1197        let underline = renderer.decor_style.underline(annotation.is_primary());
1198        let pos = pos + 1;
1199
1200        if pos > 1 && (annotation.has_label() || annotation.takes_space()) {
1201            for p in line_offset + 1..=line_offset + pos {
1202                buffer.putc(
1203                    p,
1204                    (code_offset + annotation.start.display).saturating_sub(left),
1205                    match annotation.annotation_type {
1206                        LineAnnotationType::MultilineLine(_) => underline.multiline_vertical,
1207                        _ => underline.vertical_text_line,
1208                    },
1209                    underline.style,
1210                );
1211            }
1212            if let LineAnnotationType::MultilineStart(_) = annotation.annotation_type {
1213                buffer.putc(
1214                    line_offset + pos,
1215                    (code_offset + annotation.start.display).saturating_sub(left),
1216                    underline.bottom_right,
1217                    underline.style,
1218                );
1219            }
1220            if matches!(
1221                annotation.annotation_type,
1222                LineAnnotationType::MultilineEnd(_)
1223            ) && annotation.has_label()
1224            {
1225                buffer.putc(
1226                    line_offset + pos,
1227                    (code_offset + annotation.start.display).saturating_sub(left),
1228                    underline.multiline_bottom_right_with_text,
1229                    underline.style,
1230                );
1231            }
1232        }
1233        match annotation.annotation_type {
1234            LineAnnotationType::MultilineStart(depth) => {
1235                buffer.putc(
1236                    line_offset + pos,
1237                    width_offset + depth - 1,
1238                    underline.top_left,
1239                    underline.style,
1240                );
1241                for p in line_offset + pos + 1..line_offset + line_len + 2 {
1242                    buffer.putc(
1243                        p,
1244                        width_offset + depth - 1,
1245                        underline.multiline_vertical,
1246                        underline.style,
1247                    );
1248                }
1249            }
1250            LineAnnotationType::MultilineEnd(depth) => {
1251                for p in line_offset..line_offset + pos {
1252                    buffer.putc(
1253                        p,
1254                        width_offset + depth - 1,
1255                        underline.multiline_vertical,
1256                        underline.style,
1257                    );
1258                }
1259                buffer.putc(
1260                    line_offset + pos,
1261                    width_offset + depth - 1,
1262                    underline.bottom_left,
1263                    underline.style,
1264                );
1265            }
1266            _ => (),
1267        }
1268    }
1269
1270    // Write the labels on the annotations that actually have a label.
1271    //
1272    // After this we will have:
1273    //
1274    // 2 |   fn foo() {
1275    //   |  __________
1276    //   |      |
1277    //   |      something about `foo`
1278    // 3 |
1279    // 4 |   }
1280    //   |  _  test
1281    for &(pos, annotation) in &annotations_position {
1282        let style = if annotation.is_primary() {
1283            ElementStyle::LabelPrimary
1284        } else {
1285            ElementStyle::LabelSecondary
1286        };
1287        let (pos, col) = if pos == 0 {
1288            if annotation.end.display == 0 {
1289                (pos + 1, (annotation.end.display + 2).saturating_sub(left))
1290            } else {
1291                (pos + 1, (annotation.end.display + 1).saturating_sub(left))
1292            }
1293        } else {
1294            (pos + 2, annotation.start.display.saturating_sub(left))
1295        };
1296        if let Some(label) = &annotation.label {
1297            buffer.puts(line_offset + pos, code_offset + col, label, style);
1298        }
1299    }
1300
1301    // Sort from biggest span to smallest span so that smaller spans are
1302    // represented in the output:
1303    //
1304    // x | fn foo()
1305    //   | ^^^---^^
1306    //   | |  |
1307    //   | |  something about `foo`
1308    //   | something about `fn foo()`
1309    annotations_position.sort_by_key(|(_, ann)| {
1310        // Decreasing order. When annotations share the same length, prefer `Primary`.
1311        (Reverse(ann.len()), ann.is_primary())
1312    });
1313
1314    // Write the underlines.
1315    //
1316    // After this we will have:
1317    //
1318    // 2 |   fn foo() {
1319    //   |  ____-_____^
1320    //   |      |
1321    //   |      something about `foo`
1322    // 3 |
1323    // 4 |   }
1324    //   |  _^  test
1325    for &(pos, annotation) in &annotations_position {
1326        let uline = renderer.decor_style.underline(annotation.is_primary());
1327        for p in annotation.start.display..annotation.end.display {
1328            // The default span label underline.
1329            buffer.putc(
1330                line_offset + 1,
1331                (code_offset + p).saturating_sub(left),
1332                uline.underline,
1333                uline.style,
1334            );
1335        }
1336
1337        if pos == 0
1338            && matches!(
1339                annotation.annotation_type,
1340                LineAnnotationType::MultilineStart(_) | LineAnnotationType::MultilineEnd(_)
1341            )
1342        {
1343            // The beginning of a multiline span with its leftward moving line on the same line.
1344            buffer.putc(
1345                line_offset + 1,
1346                (code_offset + annotation.start.display).saturating_sub(left),
1347                match annotation.annotation_type {
1348                    LineAnnotationType::MultilineStart(_) => uline.top_right_flat,
1349                    LineAnnotationType::MultilineEnd(_) => uline.multiline_end_same_line,
1350                    _ => panic!("unexpected annotation type: {annotation:?}"),
1351                },
1352                uline.style,
1353            );
1354        } else if pos != 0
1355            && matches!(
1356                annotation.annotation_type,
1357                LineAnnotationType::MultilineStart(_) | LineAnnotationType::MultilineEnd(_)
1358            )
1359        {
1360            // The beginning of a multiline span with its leftward moving line on another line,
1361            // so we start going down first.
1362            buffer.putc(
1363                line_offset + 1,
1364                (code_offset + annotation.start.display).saturating_sub(left),
1365                match annotation.annotation_type {
1366                    LineAnnotationType::MultilineStart(_) => uline.multiline_start_down,
1367                    LineAnnotationType::MultilineEnd(_) => uline.multiline_end_up,
1368                    _ => panic!("unexpected annotation type: {annotation:?}"),
1369                },
1370                uline.style,
1371            );
1372        } else if pos != 0 && annotation.has_label() {
1373            // The beginning of a span label with an actual label, we'll point down.
1374            buffer.putc(
1375                line_offset + 1,
1376                (code_offset + annotation.start.display).saturating_sub(left),
1377                uline.label_start,
1378                uline.style,
1379            );
1380        }
1381    }
1382
1383    // We look for individual *long* spans, and we trim the *middle*, so that we render
1384    // LL | ...= [0, 0, 0, ..., 0, 0];
1385    //    |      ^^^^^^^^^^...^^^^^^^ expected `&[u8]`, found `[{integer}; 1680]`
1386    for (i, (_pos, annotation)) in annotations_position.iter().enumerate() {
1387        // Skip cases where multiple spans overlap eachother.
1388        if overlap[i] {
1389            continue;
1390        };
1391        let LineAnnotationType::Singleline = annotation.annotation_type else {
1392            continue;
1393        };
1394        let width = annotation.end.display - annotation.start.display;
1395
1396        static MIN_PAD: usize = 5;
1397        let margin_width = str_width(renderer.decor_style.margin());
1398        if width > margin.term_width * 2 && width > (MIN_PAD * 2 + margin_width) {
1399            // If the terminal is *too* small, we keep at least a tiny bit of the span for
1400            // display.
1401            let pad = max(margin.term_width / 3, MIN_PAD);
1402            // Code line
1403            buffer.replace(
1404                line_offset,
1405                code_offset + (annotation.start.display + pad).saturating_sub(left),
1406                code_offset + (annotation.end.display - pad).saturating_sub(left),
1407                renderer.decor_style.margin(),
1408            );
1409            // Underline line
1410            buffer.replace(
1411                line_offset + 1,
1412                code_offset + (annotation.start.display + pad).saturating_sub(left),
1413                code_offset + (annotation.end.display - pad).saturating_sub(left),
1414                renderer.decor_style.margin(),
1415            );
1416        }
1417    }
1418    annotations_position
1419        .iter()
1420        .filter_map(|&(_, annotation)| match annotation.annotation_type {
1421            LineAnnotationType::MultilineStart(p) | LineAnnotationType::MultilineEnd(p) => {
1422                let style = if annotation.is_primary() {
1423                    ElementStyle::LabelPrimary
1424                } else {
1425                    ElementStyle::LabelSecondary
1426                };
1427                Some((p, style))
1428            }
1429            _ => None,
1430        })
1431        .collect::<Vec<_>>()
1432}
1433
1434#[allow(clippy::too_many_arguments)]
1435fn emit_suggestion_default(
1436    renderer: &Renderer,
1437    buffer: &mut StyledBuffer,
1438    suggestion: &Snippet<'_, Patch<'_>>,
1439    spliced_lines: SplicedLines<'_>,
1440    show_code_change: DisplaySuggestion,
1441    max_line_num_len: usize,
1442    sm: &SourceMap<'_>,
1443    primary_path: Option<&Cow<'_, str>>,
1444    matches_previous_suggestion: bool,
1445    is_first: bool,
1446    is_cont: bool,
1447) {
1448    let buffer_offset = buffer.num_lines();
1449    let mut row_num = buffer_offset + usize::from(!matches_previous_suggestion);
1450    let (complete, parts, highlights, replaced_highlights) = spliced_lines;
1451    let is_multiline = complete.lines().count() > 1;
1452
1453    if matches_previous_suggestion {
1454        buffer.puts(
1455            row_num - 1,
1456            max_line_num_len + 1,
1457            renderer.decor_style.multi_suggestion_separator(),
1458            ElementStyle::LineNumber,
1459        );
1460    } else {
1461        draw_col_separator_start(renderer, buffer, row_num - 1, max_line_num_len + 1);
1462    }
1463    if suggestion.path.as_ref() != primary_path {
1464        if let Some(path) = suggestion.path.as_ref() {
1465            if !matches_previous_suggestion {
1466                let (loc, _) = sm.span_to_locations(parts[0].span.clone());
1467                // --> file.rs:line:col
1468                //  |
1469                let arrow = renderer.decor_style.file_start(is_first, false);
1470                buffer.puts(row_num - 1, 0, arrow, ElementStyle::LineNumber);
1471                let message = format!("{}:{}:{}", path, loc.line, loc.char + 1);
1472                let col = usize::max(max_line_num_len + 1, str_width(arrow));
1473                buffer.puts(row_num - 1, col, &message, ElementStyle::LineAndColumn);
1474                for _ in 0..max_line_num_len {
1475                    buffer.prepend(row_num - 1, " ", ElementStyle::NoStyle);
1476                }
1477                draw_col_separator_no_space(renderer, buffer, row_num, max_line_num_len + 1);
1478                row_num += 1;
1479            }
1480        }
1481    }
1482
1483    if let DisplaySuggestion::Diff = show_code_change {
1484        row_num += 1;
1485    }
1486
1487    let lo = parts.iter().map(|p| p.span.start).min().unwrap();
1488    let hi = parts.iter().map(|p| p.span.end).max().unwrap();
1489
1490    let file_lines = sm.span_to_lines(lo..hi);
1491    let (line_start, line_end) = if suggestion.fold {
1492        // We use the original span to get original line_start
1493        sm.span_to_locations(parts[0].original_span.clone())
1494    } else {
1495        sm.span_to_locations(0..sm.source.len())
1496    };
1497    let mut lines = complete.lines();
1498    if lines.clone().next().is_none() {
1499        // Account for a suggestion to completely remove a line(s) with whitespace (#94192).
1500        for line in line_start.line..=line_end.line {
1501            buffer.puts(
1502                row_num - 1 + line - line_start.line,
1503                0,
1504                &maybe_anonymized(renderer, line, max_line_num_len),
1505                ElementStyle::LineNumber,
1506            );
1507            buffer.puts(
1508                row_num - 1 + line - line_start.line,
1509                max_line_num_len + 1,
1510                "- ",
1511                ElementStyle::Removal,
1512            );
1513            buffer.puts(
1514                row_num - 1 + line - line_start.line,
1515                max_line_num_len + 3,
1516                &normalize_whitespace(sm.get_line(line).unwrap()),
1517                ElementStyle::Removal,
1518            );
1519        }
1520        row_num += line_end.line - line_start.line;
1521    }
1522    let mut unhighlighted_lines = Vec::new();
1523    for (line_pos, (line, highlight_parts)) in lines.by_ref().zip(highlights).enumerate() {
1524        // Remember lines that are not highlighted to hide them if needed
1525        if highlight_parts.is_empty() && suggestion.fold {
1526            unhighlighted_lines.push((line_pos, line));
1527            continue;
1528        }
1529
1530        match unhighlighted_lines.len() {
1531            0 => (),
1532            // Since we show first line, "..." line and last line,
1533            // There is no reason to hide if there are 3 or less lines
1534            // (because then we just replace a line with ... which is
1535            // not helpful)
1536            n if n <= 3 => unhighlighted_lines.drain(..).for_each(|(p, l)| {
1537                draw_code_line(
1538                    renderer,
1539                    buffer,
1540                    &mut row_num,
1541                    &[],
1542                    &[],
1543                    p + line_start.line,
1544                    l,
1545                    show_code_change,
1546                    max_line_num_len,
1547                    &file_lines,
1548                    is_multiline,
1549                );
1550            }),
1551            // Print first unhighlighted line, "..." and last unhighlighted line, like so:
1552            //
1553            // LL | this line was highlighted
1554            // LL | this line is just for context
1555            // ...
1556            // LL | this line is just for context
1557            // LL | this line was highlighted
1558            _ => {
1559                let last_line = unhighlighted_lines.pop();
1560                let first_line = unhighlighted_lines.drain(..).next();
1561
1562                if let Some((p, l)) = first_line {
1563                    draw_code_line(
1564                        renderer,
1565                        buffer,
1566                        &mut row_num,
1567                        &[],
1568                        &[],
1569                        p + line_start.line,
1570                        l,
1571                        show_code_change,
1572                        max_line_num_len,
1573                        &file_lines,
1574                        is_multiline,
1575                    );
1576                }
1577
1578                let placeholder = renderer.decor_style.margin();
1579                let padding = str_width(placeholder);
1580                buffer.puts(
1581                    row_num,
1582                    max_line_num_len.saturating_sub(padding),
1583                    placeholder,
1584                    ElementStyle::LineNumber,
1585                );
1586                row_num += 1;
1587
1588                if let Some((p, l)) = last_line {
1589                    draw_code_line(
1590                        renderer,
1591                        buffer,
1592                        &mut row_num,
1593                        &[],
1594                        &[],
1595                        p + line_start.line,
1596                        l,
1597                        show_code_change,
1598                        max_line_num_len,
1599                        &file_lines,
1600                        is_multiline,
1601                    );
1602                }
1603            }
1604        }
1605        draw_code_line(
1606            renderer,
1607            buffer,
1608            &mut row_num,
1609            &highlight_parts,
1610            &replaced_highlights,
1611            line_pos + line_start.line,
1612            line,
1613            show_code_change,
1614            max_line_num_len,
1615            &file_lines,
1616            is_multiline,
1617        );
1618    }
1619
1620    // This offset and the ones below need to be signed to account for replacement code
1621    // that is shorter than the original code.
1622    let mut offsets: Vec<(usize, isize)> = Vec::new();
1623    // Only show an underline in the suggestions if the suggestion is not the
1624    // entirety of the code being shown and the displayed code is not multiline.
1625    if let DisplaySuggestion::Diff | DisplaySuggestion::Underline | DisplaySuggestion::Add =
1626        show_code_change
1627    {
1628        for part in parts {
1629            let (span_start, span_end) = sm.span_to_locations(part.span.clone());
1630            let span_start_pos = span_start.display;
1631            let span_end_pos = span_end.display;
1632
1633            // If this addition is _only_ whitespace, then don't trim it,
1634            // or else we're just not rendering anything.
1635            let is_whitespace_addition = part.replacement.trim().is_empty();
1636
1637            // Do not underline the leading...
1638            let start = if is_whitespace_addition {
1639                0
1640            } else {
1641                part.replacement
1642                    .len()
1643                    .saturating_sub(part.replacement.trim_start().len())
1644            };
1645            // ...or trailing spaces. Account for substitutions containing unicode
1646            // characters.
1647            let sub_len: usize = str_width(if is_whitespace_addition {
1648                &part.replacement
1649            } else {
1650                part.replacement.trim()
1651            });
1652
1653            let offset: isize = offsets
1654                .iter()
1655                .filter_map(|(start, v)| {
1656                    if span_start_pos < *start {
1657                        None
1658                    } else {
1659                        Some(v)
1660                    }
1661                })
1662                .sum();
1663            let underline_start = (span_start_pos + start) as isize + offset;
1664            let underline_end = (span_start_pos + start + sub_len) as isize + offset;
1665            assert!(underline_start >= 0 && underline_end >= 0);
1666            let padding: usize = max_line_num_len + 3;
1667            for p in underline_start..underline_end {
1668                if matches!(show_code_change, DisplaySuggestion::Underline) {
1669                    // If this is a replacement, underline with `~`, if this is an addition
1670                    // underline with `+`.
1671                    buffer.putc(
1672                        row_num,
1673                        (padding as isize + p) as usize,
1674                        if part.is_addition(sm) {
1675                            '+'
1676                        } else {
1677                            renderer.decor_style.diff()
1678                        },
1679                        ElementStyle::Addition,
1680                    );
1681                }
1682            }
1683
1684            // length of the code after substitution
1685            let full_sub_len = str_width(&part.replacement) as isize;
1686
1687            // length of the code to be substituted
1688            let snippet_len = span_end_pos as isize - span_start_pos as isize;
1689            // For multiple substitutions, use the position *after* the previous
1690            // substitutions have happened, only when further substitutions are
1691            // located strictly after.
1692            offsets.push((span_end_pos, full_sub_len - snippet_len));
1693        }
1694        row_num += 1;
1695    }
1696
1697    // if we elided some lines, add an ellipsis
1698    if lines.next().is_some() {
1699        let placeholder = renderer.decor_style.margin();
1700        let padding = str_width(placeholder);
1701        buffer.puts(
1702            row_num,
1703            max_line_num_len.saturating_sub(padding),
1704            placeholder,
1705            ElementStyle::LineNumber,
1706        );
1707    } else {
1708        let row = match show_code_change {
1709            DisplaySuggestion::Diff | DisplaySuggestion::Add | DisplaySuggestion::Underline => {
1710                row_num - 1
1711            }
1712            DisplaySuggestion::None => row_num,
1713        };
1714        if is_cont {
1715            draw_col_separator_no_space(renderer, buffer, row, max_line_num_len + 1);
1716        } else {
1717            draw_col_separator_end(renderer, buffer, row, max_line_num_len + 1);
1718        }
1719    }
1720}
1721
1722#[allow(clippy::too_many_arguments)]
1723fn draw_code_line(
1724    renderer: &Renderer,
1725    buffer: &mut StyledBuffer,
1726    row_num: &mut usize,
1727    highlight_parts: &[SubstitutionHighlight],
1728    replaced_parts: &[Vec<SubstitutionHighlight>],
1729    line_num: usize,
1730    line_to_add: &str,
1731    show_code_change: DisplaySuggestion,
1732    max_line_num_len: usize,
1733    file_lines: &[&LineInfo<'_>],
1734    is_multiline: bool,
1735) {
1736    if let DisplaySuggestion::Diff = show_code_change {
1737        // We need to print more than one line if the span we need to remove is multiline.
1738        // For more info: https://github.com/rust-lang/rust/issues/92741
1739        let lines_to_remove = file_lines.iter().take(file_lines.len() - 1);
1740        for (index, (line_to_remove, parts)) in lines_to_remove.zip(replaced_parts).enumerate() {
1741            buffer.puts(
1742                *row_num - 1,
1743                0,
1744                &maybe_anonymized(renderer, line_num + index, max_line_num_len),
1745                ElementStyle::LineNumber,
1746            );
1747            buffer.puts(
1748                *row_num - 1,
1749                max_line_num_len + 1,
1750                "- ",
1751                ElementStyle::Removal,
1752            );
1753            let line = normalize_whitespace(line_to_remove.line);
1754            buffer.puts(
1755                *row_num - 1,
1756                max_line_num_len + 3,
1757                &line,
1758                ElementStyle::NoStyle,
1759            );
1760            style_substitution_highlights(
1761                parts,
1762                ElementStyle::Removal,
1763                *row_num - 1,
1764                line_to_remove.line,
1765                max_line_num_len,
1766                buffer,
1767            );
1768            *row_num += 1;
1769        }
1770        // If the last line is exactly equal to the line we need to add, we can skip both of
1771        // them. This allows us to avoid output like the following:
1772        // 2 - &
1773        // 2 + if true { true } else { false }
1774        // 3 - if true { true } else { false }
1775        // If those lines aren't equal, we print their diff
1776        let last_line = &file_lines.last().unwrap();
1777        if last_line.line == line_to_add {
1778            *row_num -= 2;
1779            // The last original line collapses into the previous drawn row, so
1780            // fold its replaced-code highlights onto that row too.
1781            style_substitution_highlights(
1782                replaced_parts.last().unwrap(),
1783                ElementStyle::Removal,
1784                *row_num,
1785                last_line.line,
1786                max_line_num_len,
1787                buffer,
1788            );
1789        } else {
1790            buffer.puts(
1791                *row_num - 1,
1792                0,
1793                &maybe_anonymized(renderer, line_num + file_lines.len() - 1, max_line_num_len),
1794                ElementStyle::LineNumber,
1795            );
1796            buffer.puts(
1797                *row_num - 1,
1798                max_line_num_len + 1,
1799                "- ",
1800                ElementStyle::Removal,
1801            );
1802            buffer.puts(
1803                *row_num - 1,
1804                max_line_num_len + 3,
1805                &normalize_whitespace(last_line.line),
1806                ElementStyle::NoStyle,
1807            );
1808            style_substitution_highlights(
1809                replaced_parts.last().unwrap(),
1810                ElementStyle::Removal,
1811                *row_num - 1,
1812                last_line.line,
1813                max_line_num_len,
1814                buffer,
1815            );
1816
1817            if line_to_add.trim().is_empty() {
1818                *row_num -= 1;
1819            } else {
1820                // Check if after the removal, the line is left with only whitespace. If so, we
1821                // will not show an "addition" line, as removing the whole line is what the user
1822                // would really want.
1823                // For example, for the following:
1824                //   |
1825                // 2 -     .await
1826                // 2 +     (note the left over whitespace)
1827                //   |
1828                // We really want
1829                //   |
1830                // 2 -     .await
1831                //   |
1832                // *row_num -= 1;
1833                buffer.puts(
1834                    *row_num,
1835                    0,
1836                    &maybe_anonymized(renderer, line_num, max_line_num_len),
1837                    ElementStyle::LineNumber,
1838                );
1839                buffer.puts(*row_num, max_line_num_len + 1, "+ ", ElementStyle::Addition);
1840                buffer.append(
1841                    *row_num,
1842                    &normalize_whitespace(line_to_add),
1843                    ElementStyle::NoStyle,
1844                );
1845            }
1846        }
1847    } else if is_multiline {
1848        buffer.puts(
1849            *row_num,
1850            0,
1851            &maybe_anonymized(renderer, line_num, max_line_num_len),
1852            ElementStyle::LineNumber,
1853        );
1854        match &highlight_parts {
1855            [SubstitutionHighlight { start: 0, end }] if *end == line_to_add.len() => {
1856                buffer.puts(*row_num, max_line_num_len + 1, "+ ", ElementStyle::Addition);
1857            }
1858            [] | [SubstitutionHighlight { start: 0, end: 0 }] => {
1859                // FIXME: needed? Doesn't get exercised in any test.
1860                draw_col_separator_no_space(renderer, buffer, *row_num, max_line_num_len + 1);
1861            }
1862            _ => {
1863                let diff = renderer.decor_style.diff();
1864                buffer.puts(
1865                    *row_num,
1866                    max_line_num_len + 1,
1867                    &format!("{diff} "),
1868                    ElementStyle::Addition,
1869                );
1870            }
1871        }
1872        //   LL | line_to_add
1873        //   ++^^^
1874        //    |  |
1875        //    |  magic `3`
1876        //    `max_line_num_len`
1877        buffer.puts(
1878            *row_num,
1879            max_line_num_len + 3,
1880            &normalize_whitespace(line_to_add),
1881            ElementStyle::NoStyle,
1882        );
1883    } else if let DisplaySuggestion::Add = show_code_change {
1884        buffer.puts(
1885            *row_num,
1886            0,
1887            &maybe_anonymized(renderer, line_num, max_line_num_len),
1888            ElementStyle::LineNumber,
1889        );
1890        buffer.puts(*row_num, max_line_num_len + 1, "+ ", ElementStyle::Addition);
1891        buffer.append(
1892            *row_num,
1893            &normalize_whitespace(line_to_add),
1894            ElementStyle::NoStyle,
1895        );
1896    } else {
1897        buffer.puts(
1898            *row_num,
1899            0,
1900            &maybe_anonymized(renderer, line_num, max_line_num_len),
1901            ElementStyle::LineNumber,
1902        );
1903        draw_col_separator(renderer, buffer, *row_num, max_line_num_len + 1);
1904        buffer.append(
1905            *row_num,
1906            &normalize_whitespace(line_to_add),
1907            ElementStyle::NoStyle,
1908        );
1909    }
1910
1911    style_substitution_highlights(
1912        highlight_parts,
1913        ElementStyle::Addition,
1914        *row_num,
1915        line_to_add,
1916        max_line_num_len,
1917        buffer,
1918    );
1919
1920    *row_num += 1;
1921}
1922
1923fn style_substitution_highlights(
1924    highlight_parts: &[SubstitutionHighlight],
1925    style: ElementStyle,
1926    row_num: usize,
1927    unnormalized_line: &str,
1928    max_line_num_len: usize,
1929    buffer: &mut StyledBuffer,
1930) {
1931    for &SubstitutionHighlight { start, end } in highlight_parts {
1932        // This is a no-op for empty ranges
1933        if start != end {
1934            // We calculate the extra width from tabs for both the start and end
1935            // of the span, as tabs could be present in the middle of the span
1936            let extra_width_start: usize = extra_width_from_tabs(unnormalized_line, start);
1937            let extra_width_end: usize = extra_width_from_tabs(unnormalized_line, end);
1938            buffer.set_style_range(
1939                row_num,
1940                max_line_num_len + 3 + start + extra_width_start,
1941                max_line_num_len + 3 + end + extra_width_end,
1942                style,
1943                true,
1944            );
1945        }
1946    }
1947}
1948
1949#[allow(clippy::too_many_arguments)]
1950fn draw_line(
1951    renderer: &Renderer,
1952    buffer: &mut StyledBuffer,
1953    source_string: &str,
1954    line_index: usize,
1955    line_offset: usize,
1956    width_offset: usize,
1957    code_offset: usize,
1958    max_line_num_len: usize,
1959    margin: Margin,
1960) -> usize {
1961    // Tabs are assumed to have been replaced by spaces in calling code.
1962    debug_assert!(!source_string.contains('\t'));
1963    let line_len = str_width(source_string);
1964    // Create the source line we will highlight.
1965    let mut left = margin.left(line_len);
1966    let right = margin.right(line_len);
1967
1968    let mut taken = 0;
1969    let mut skipped = 0;
1970    let code: String = source_string
1971        .chars()
1972        .skip_while(|ch| {
1973            let w = char_width(*ch);
1974            // If `skipped` is less than `left`, always skip the next `ch`,
1975            // even if `ch` is a multi-width char that would make `skipped`
1976            // exceed `left`. This ensures that we do not exceed term width on
1977            // source lines.
1978            if skipped < left {
1979                skipped += w;
1980                true
1981            } else {
1982                false
1983            }
1984        })
1985        .take_while(|ch| {
1986            // Make sure that the trimming on the right will fall within the terminal width.
1987            taken += char_width(*ch);
1988            taken <= (right - left)
1989        })
1990        .collect();
1991    // If we skipped more than `left`, adjust `left` to account for it.
1992    if skipped > left {
1993        left += skipped - left;
1994    }
1995    let placeholder = renderer.decor_style.margin();
1996    let padding = str_width(placeholder);
1997    let (width_taken, bytes_taken) = if margin.was_cut_left() {
1998        // We have stripped some code/whitespace from the beginning, make it clear.
1999        let mut bytes_taken = 0;
2000        let mut width_taken = 0;
2001        for ch in code.chars() {
2002            width_taken += char_width(ch);
2003            bytes_taken += ch.len_utf8();
2004
2005            if width_taken >= padding {
2006                break;
2007            }
2008        }
2009
2010        buffer.puts(
2011            line_offset,
2012            code_offset,
2013            placeholder,
2014            ElementStyle::LineNumber,
2015        );
2016        (width_taken, bytes_taken)
2017    } else {
2018        (0, 0)
2019    };
2020
2021    buffer.puts(
2022        line_offset,
2023        code_offset + width_taken,
2024        &code[bytes_taken..],
2025        ElementStyle::Quotation,
2026    );
2027
2028    if line_len > right {
2029        // We have stripped some code/whitespace from the beginning, make it clear.
2030        let mut char_taken = 0;
2031        let mut width_taken_inner = 0;
2032        for ch in code.chars().rev() {
2033            width_taken_inner += char_width(ch);
2034            char_taken += 1;
2035
2036            if width_taken_inner >= padding {
2037                break;
2038            }
2039        }
2040
2041        buffer.puts(
2042            line_offset,
2043            code_offset + width_taken + code[bytes_taken..].chars().count() - char_taken,
2044            placeholder,
2045            ElementStyle::LineNumber,
2046        );
2047    }
2048
2049    buffer.puts(
2050        line_offset,
2051        0,
2052        &maybe_anonymized(renderer, line_index, max_line_num_len),
2053        ElementStyle::LineNumber,
2054    );
2055
2056    draw_col_separator_no_space(renderer, buffer, line_offset, width_offset - 2);
2057
2058    left
2059}
2060
2061fn draw_range(
2062    buffer: &mut StyledBuffer,
2063    symbol: char,
2064    line: usize,
2065    col_from: usize,
2066    col_to: usize,
2067    style: ElementStyle,
2068) {
2069    for col in col_from..col_to {
2070        buffer.putc(line, col, symbol, style);
2071    }
2072}
2073
2074fn draw_multiline_line(
2075    renderer: &Renderer,
2076    buffer: &mut StyledBuffer,
2077    line: usize,
2078    offset: usize,
2079    depth: usize,
2080    style: ElementStyle,
2081) {
2082    let chr = match (style, renderer.decor_style) {
2083        (ElementStyle::UnderlinePrimary | ElementStyle::LabelPrimary, DecorStyle::Ascii) => '|',
2084        (_, DecorStyle::Ascii) => '|',
2085        (ElementStyle::UnderlinePrimary | ElementStyle::LabelPrimary, DecorStyle::Unicode) => '┃',
2086        (_, DecorStyle::Unicode) => '│',
2087    };
2088    buffer.putc(line, offset + depth - 1, chr, style);
2089}
2090
2091fn draw_col_separator(renderer: &Renderer, buffer: &mut StyledBuffer, line: usize, col: usize) {
2092    let chr = renderer.decor_style.col_separator();
2093    buffer.puts(line, col, &format!("{chr} "), ElementStyle::LineNumber);
2094}
2095
2096fn draw_col_separator_no_space(
2097    renderer: &Renderer,
2098    buffer: &mut StyledBuffer,
2099    line: usize,
2100    col: usize,
2101) {
2102    let chr = renderer.decor_style.col_separator();
2103    draw_col_separator_no_space_with_style(buffer, chr, line, col, ElementStyle::LineNumber);
2104}
2105
2106fn draw_col_separator_start(
2107    renderer: &Renderer,
2108    buffer: &mut StyledBuffer,
2109    line: usize,
2110    col: usize,
2111) {
2112    match renderer.decor_style {
2113        DecorStyle::Ascii => {
2114            draw_col_separator_no_space_with_style(
2115                buffer,
2116                '|',
2117                line,
2118                col,
2119                ElementStyle::LineNumber,
2120            );
2121        }
2122        DecorStyle::Unicode => {
2123            draw_col_separator_no_space_with_style(
2124                buffer,
2125                '╭',
2126                line,
2127                col,
2128                ElementStyle::LineNumber,
2129            );
2130            draw_col_separator_no_space_with_style(
2131                buffer,
2132                '╴',
2133                line,
2134                col + 1,
2135                ElementStyle::LineNumber,
2136            );
2137        }
2138    }
2139}
2140
2141fn draw_col_separator_end(renderer: &Renderer, buffer: &mut StyledBuffer, line: usize, col: usize) {
2142    match renderer.decor_style {
2143        DecorStyle::Ascii => {
2144            draw_col_separator_no_space_with_style(
2145                buffer,
2146                '|',
2147                line,
2148                col,
2149                ElementStyle::LineNumber,
2150            );
2151        }
2152        DecorStyle::Unicode => {
2153            draw_col_separator_no_space_with_style(
2154                buffer,
2155                '╰',
2156                line,
2157                col,
2158                ElementStyle::LineNumber,
2159            );
2160            draw_col_separator_no_space_with_style(
2161                buffer,
2162                '╴',
2163                line,
2164                col + 1,
2165                ElementStyle::LineNumber,
2166            );
2167        }
2168    }
2169}
2170
2171fn draw_col_separator_no_space_with_style(
2172    buffer: &mut StyledBuffer,
2173    chr: char,
2174    line: usize,
2175    col: usize,
2176    style: ElementStyle,
2177) {
2178    buffer.putc(line, col, chr, style);
2179}
2180
2181fn maybe_anonymized(renderer: &Renderer, line_num: usize, max_line_num_len: usize) -> String {
2182    format!(
2183        "{:>max_line_num_len$}",
2184        if renderer.anonymized_line_numbers {
2185            Cow::Borrowed(ANONYMIZED_LINE_NUM)
2186        } else {
2187            Cow::Owned(line_num.to_string())
2188        }
2189    )
2190}
2191
2192fn draw_note_separator(
2193    renderer: &Renderer,
2194    buffer: &mut StyledBuffer,
2195    line: usize,
2196    col: usize,
2197    is_cont: bool,
2198) {
2199    let chr = renderer.decor_style.note_separator(is_cont);
2200    buffer.puts(line, col, chr, ElementStyle::LineNumber);
2201}
2202
2203fn draw_line_separator(renderer: &Renderer, buffer: &mut StyledBuffer, line: usize, col: usize) {
2204    let (column, dots) = match renderer.decor_style {
2205        DecorStyle::Ascii => (0, "..."),
2206        DecorStyle::Unicode => (col - 2, "‡"),
2207    };
2208    buffer.puts(line, column, dots, ElementStyle::LineNumber);
2209}
2210
2211trait MessageOrTitle {
2212    fn level(&self) -> &Level<'_>;
2213    fn id(&self) -> Option<&Id<'_>>;
2214    fn text(&self) -> &str;
2215    fn allows_styling(&self) -> bool;
2216}
2217
2218impl MessageOrTitle for Title<'_> {
2219    fn level(&self) -> &Level<'_> {
2220        &self.level
2221    }
2222    fn id(&self) -> Option<&Id<'_>> {
2223        self.id.as_ref()
2224    }
2225    fn text(&self) -> &str {
2226        self.text.as_ref()
2227    }
2228    fn allows_styling(&self) -> bool {
2229        self.allows_styling
2230    }
2231}
2232
2233impl MessageOrTitle for Message<'_> {
2234    fn level(&self) -> &Level<'_> {
2235        &self.level
2236    }
2237    fn id(&self) -> Option<&Id<'_>> {
2238        None
2239    }
2240    fn text(&self) -> &str {
2241        self.text.as_ref()
2242    }
2243    fn allows_styling(&self) -> bool {
2244        true
2245    }
2246}
2247
2248/// Count extra display columns from tabs in the first `n` chars of `s`.
2249/// Each tab is displayed as 4 spaces, so the extra width per tab is 3.
2250fn extra_width_from_tabs(s: &str, n: usize) -> usize {
2251    s.chars().take(n).filter(|&ch| ch == '\t').count() * 3
2252}
2253
2254// instead of taking the String length or dividing by 10 while > 0, we multiply a limit by 10 until
2255// we're higher. If the loop isn't exited by the `return`, the last multiplication will wrap, which
2256// is OK, because while we cannot fit a higher power of 10 in a usize, the loop will end anyway.
2257// This is also why we need the max number of decimal digits within a `usize`.
2258fn num_decimal_digits(num: usize) -> usize {
2259    #[cfg(target_pointer_width = "64")]
2260    const MAX_DIGITS: usize = 20;
2261
2262    #[cfg(target_pointer_width = "32")]
2263    const MAX_DIGITS: usize = 10;
2264
2265    #[cfg(target_pointer_width = "16")]
2266    const MAX_DIGITS: usize = 5;
2267
2268    let mut lim = 10;
2269    for num_digits in 1..MAX_DIGITS {
2270        if num < lim {
2271            return num_digits;
2272        }
2273        lim = lim.wrapping_mul(10);
2274    }
2275    MAX_DIGITS
2276}
2277
2278fn str_width(s: &str) -> usize {
2279    s.chars().map(char_width).sum()
2280}
2281
2282pub(crate) fn char_width(ch: char) -> usize {
2283    // FIXME: `unicode_width` sometimes disagrees with terminals on how wide a `char` is. For now,
2284    // just accept that sometimes the code line will be longer than desired.
2285    match ch {
2286        '\t' => 4,
2287        // Keep the following list in sync with `rustc_errors::emitter::OUTPUT_REPLACEMENTS`. These
2288        // are control points that we replace before printing with a visible codepoint for the sake
2289        // of being able to point at them with underlines.
2290        '\u{0000}' | '\u{0001}' | '\u{0002}' | '\u{0003}' | '\u{0004}' | '\u{0005}'
2291        | '\u{0006}' | '\u{0007}' | '\u{0008}' | '\u{000B}' | '\u{000C}' | '\u{000D}'
2292        | '\u{000E}' | '\u{000F}' | '\u{0010}' | '\u{0011}' | '\u{0012}' | '\u{0013}'
2293        | '\u{0014}' | '\u{0015}' | '\u{0016}' | '\u{0017}' | '\u{0018}' | '\u{0019}'
2294        | '\u{001A}' | '\u{001B}' | '\u{001C}' | '\u{001D}' | '\u{001E}' | '\u{001F}'
2295        | '\u{007F}' | '\u{202A}' | '\u{202B}' | '\u{202D}' | '\u{202E}' | '\u{2066}'
2296        | '\u{2067}' | '\u{2068}' | '\u{202C}' | '\u{2069}' => 1,
2297        _ => unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1),
2298    }
2299}
2300
2301pub(crate) fn num_overlap(
2302    a_start: usize,
2303    a_end: usize,
2304    b_start: usize,
2305    b_end: usize,
2306    inclusive: bool,
2307) -> bool {
2308    let extra = usize::from(inclusive);
2309    (b_start..b_end + extra).contains(&a_start) || (a_start..a_end + extra).contains(&b_start)
2310}
2311
2312fn overlaps(a1: &LineAnnotation<'_>, a2: &LineAnnotation<'_>, padding: usize) -> bool {
2313    num_overlap(
2314        a1.start.display,
2315        a1.end.display + padding,
2316        a2.start.display,
2317        a2.end.display,
2318        false,
2319    )
2320}
2321
2322#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
2323pub(crate) enum LineAnnotationType {
2324    /// Annotation under a single line of code
2325    Singleline,
2326
2327    // The Multiline type above is replaced with the following three in order
2328    // to reuse the current label drawing code.
2329    //
2330    // Each of these corresponds to one part of the following diagram:
2331    //
2332    //     x |   foo(1 + bar(x,
2333    //       |  _________^              < MultilineStart
2334    //     x | |             y),        < MultilineLine
2335    //       | |______________^ label   < MultilineEnd
2336    //     x |       z);
2337    /// Annotation marking the first character of a fully shown multiline span
2338    MultilineStart(usize),
2339    /// Annotation marking the last character of a fully shown multiline span
2340    MultilineEnd(usize),
2341    /// Line at the left enclosing the lines of a fully shown multiline span
2342    // Just a placeholder for the drawing algorithm, to know that it shouldn't skip the first 4
2343    // and last 2 lines of code. The actual line is drawn in `emit_message_default` and not in
2344    // `draw_multiline_line`.
2345    MultilineLine(usize),
2346}
2347
2348#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
2349pub(crate) struct LineAnnotation<'a> {
2350    /// Start column.
2351    /// Note that it is important that this field goes
2352    /// first, so that when we sort, we sort orderings by start
2353    /// column.
2354    pub start: Loc,
2355
2356    /// End column within the line (exclusive)
2357    pub end: Loc,
2358
2359    /// level
2360    pub kind: AnnotationKind,
2361
2362    /// Optional label to display adjacent to the annotation.
2363    pub label: Option<Cow<'a, str>>,
2364
2365    /// Is this a single line, multiline or multiline span minimized down to a
2366    /// smaller span.
2367    pub annotation_type: LineAnnotationType,
2368
2369    /// Whether the source code should be highlighted
2370    pub highlight_source: bool,
2371}
2372
2373impl LineAnnotation<'_> {
2374    pub(crate) fn is_primary(&self) -> bool {
2375        self.kind == AnnotationKind::Primary
2376    }
2377
2378    /// Whether this annotation is a vertical line placeholder.
2379    pub(crate) fn is_line(&self) -> bool {
2380        matches!(self.annotation_type, LineAnnotationType::MultilineLine(_))
2381    }
2382
2383    /// Length of this annotation as displayed in the stderr output
2384    pub(crate) fn len(&self) -> usize {
2385        // Account for usize underflows
2386        self.end.display.abs_diff(self.start.display)
2387    }
2388
2389    pub(crate) fn has_label(&self) -> bool {
2390        if let Some(label) = &self.label {
2391            // Consider labels with no text as effectively not being there
2392            // to avoid weird output with unnecessary vertical lines, like:
2393            //
2394            //     X | fn foo(x: u32) {
2395            //       | -------^------
2396            //       | |      |
2397            //       | |
2398            //       |
2399            //
2400            // Note that this would be the complete output users would see.
2401            !label.is_empty()
2402        } else {
2403            false
2404        }
2405    }
2406
2407    pub(crate) fn takes_space(&self) -> bool {
2408        // Multiline annotations always have to keep vertical space.
2409        matches!(
2410            self.annotation_type,
2411            LineAnnotationType::MultilineStart(_) | LineAnnotationType::MultilineEnd(_)
2412        )
2413    }
2414}
2415
2416#[derive(Clone, Copy, Debug)]
2417pub(crate) enum DisplaySuggestion {
2418    Underline,
2419    Diff,
2420    None,
2421    Add,
2422}
2423
2424impl DisplaySuggestion {
2425    fn new(complete: &str, patches: &[TrimmedPatch<'_>], sm: &SourceMap<'_>) -> Self {
2426        let has_deletion = patches
2427            .iter()
2428            .any(|p| p.is_deletion(sm) || p.is_destructive_replacement(sm));
2429        let is_multiline = complete.lines().count() > 1;
2430        if has_deletion && !is_multiline {
2431            DisplaySuggestion::Diff
2432        } else if patches.len() == 1
2433            && patches.first().is_some_and(|p| {
2434                p.replacement.ends_with('\n') && p.replacement.trim() == complete.trim()
2435            })
2436        {
2437            // We are adding a line(s) of code before code that was already there.
2438            DisplaySuggestion::Add
2439        } else if (patches.len() != 1 || patches[0].replacement.trim() != complete.trim())
2440            && !is_multiline
2441        {
2442            DisplaySuggestion::Underline
2443        } else {
2444            DisplaySuggestion::None
2445        }
2446    }
2447}
2448
2449// We replace some characters so the CLI output is always consistent and underlines aligned.
2450// Keep the following list in sync with `rustc_span::char_width`.
2451const OUTPUT_REPLACEMENTS: &[(char, &str)] = &[
2452    // In terminals without Unicode support the following will be garbled, but in *all* terminals
2453    // the underlying codepoint will be as well. We could gate this replacement behind a "unicode
2454    // support" gate.
2455    ('\0', "␀"),
2456    ('\u{0001}', "␁"),
2457    ('\u{0002}', "␂"),
2458    ('\u{0003}', "␃"),
2459    ('\u{0004}', "␄"),
2460    ('\u{0005}', "␅"),
2461    ('\u{0006}', "␆"),
2462    ('\u{0007}', "␇"),
2463    ('\u{0008}', "␈"),
2464    ('\t', "    "), // We do our own tab replacement
2465    ('\u{000b}', "␋"),
2466    ('\u{000c}', "␌"),
2467    ('\u{000d}', "␍"),
2468    ('\u{000e}', "␎"),
2469    ('\u{000f}', "␏"),
2470    ('\u{0010}', "␐"),
2471    ('\u{0011}', "␑"),
2472    ('\u{0012}', "␒"),
2473    ('\u{0013}', "␓"),
2474    ('\u{0014}', "␔"),
2475    ('\u{0015}', "␕"),
2476    ('\u{0016}', "␖"),
2477    ('\u{0017}', "␗"),
2478    ('\u{0018}', "␘"),
2479    ('\u{0019}', "␙"),
2480    ('\u{001a}', "␚"),
2481    ('\u{001b}', "␛"),
2482    ('\u{001c}', "␜"),
2483    ('\u{001d}', "␝"),
2484    ('\u{001e}', "␞"),
2485    ('\u{001f}', "␟"),
2486    ('\u{007f}', "␡"),
2487    ('\u{200d}', ""), // Replace ZWJ for consistent terminal output of grapheme clusters.
2488    ('\u{202a}', "�"), // The following unicode text flow control characters are inconsistently
2489    ('\u{202b}', "�"), // supported across CLIs and can cause confusion due to the bytes on disk
2490    ('\u{202c}', "�"), // not corresponding to the visible source code, so we replace them always.
2491    ('\u{202d}', "�"),
2492    ('\u{202e}', "�"),
2493    ('\u{2066}', "�"),
2494    ('\u{2067}', "�"),
2495    ('\u{2068}', "�"),
2496    ('\u{2069}', "�"),
2497];
2498
2499pub(crate) fn normalize_whitespace(s: &str) -> String {
2500    // Scan the input string for a character in the ordered table above.
2501    // If it's present, replace it with its alternative string (it can be more than 1 char!).
2502    // Otherwise, retain the input char.
2503    s.chars().fold(String::with_capacity(s.len()), |mut s, c| {
2504        match OUTPUT_REPLACEMENTS.binary_search_by_key(&c, |(k, _)| *k) {
2505            Ok(i) => s.push_str(OUTPUT_REPLACEMENTS[i].1),
2506            _ => s.push(c),
2507        }
2508        s
2509    })
2510}
2511
2512#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq)]
2513pub(crate) enum ElementStyle {
2514    MainHeaderMsg,
2515    HeaderMsg,
2516    LineAndColumn,
2517    LineNumber,
2518    Quotation,
2519    UnderlinePrimary,
2520    UnderlineSecondary,
2521    LabelPrimary,
2522    LabelSecondary,
2523    NoStyle,
2524    Level(LevelInner),
2525    Addition,
2526    Removal,
2527}
2528
2529impl ElementStyle {
2530    pub(crate) fn color_spec(&self, level: &Level<'_>, stylesheet: &Stylesheet) -> Style {
2531        match self {
2532            ElementStyle::Addition => stylesheet.addition,
2533            ElementStyle::Removal => stylesheet.removal,
2534            ElementStyle::LineAndColumn => stylesheet.none,
2535            ElementStyle::LineNumber => stylesheet.line_num,
2536            ElementStyle::Quotation => stylesheet.none,
2537            ElementStyle::MainHeaderMsg => stylesheet.emphasis,
2538            ElementStyle::UnderlinePrimary | ElementStyle::LabelPrimary => level.style(stylesheet),
2539            ElementStyle::UnderlineSecondary | ElementStyle::LabelSecondary => stylesheet.context,
2540            ElementStyle::HeaderMsg | ElementStyle::NoStyle => stylesheet.none,
2541            ElementStyle::Level(lvl) => lvl.style(stylesheet),
2542        }
2543    }
2544}
2545
2546#[derive(Debug, Clone, Copy)]
2547pub(crate) struct UnderlineParts {
2548    pub(crate) style: ElementStyle,
2549    pub(crate) underline: char,
2550    pub(crate) label_start: char,
2551    pub(crate) vertical_text_line: char,
2552    pub(crate) multiline_vertical: char,
2553    pub(crate) multiline_horizontal: char,
2554    pub(crate) multiline_whole_line: char,
2555    pub(crate) multiline_start_down: char,
2556    pub(crate) bottom_right: char,
2557    pub(crate) top_left: char,
2558    pub(crate) top_right_flat: char,
2559    pub(crate) bottom_left: char,
2560    pub(crate) multiline_end_up: char,
2561    pub(crate) multiline_end_same_line: char,
2562    pub(crate) multiline_bottom_right_with_text: char,
2563}
2564
2565#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2566enum TitleStyle {
2567    MainHeader,
2568    Header,
2569    Secondary,
2570}
2571
2572struct PreProcessedGroup<'a> {
2573    group: &'a Group<'a>,
2574    elements: Vec<PreProcessedElement<'a>>,
2575    primary_path: Option<&'a Cow<'a, str>>,
2576    max_depth: usize,
2577}
2578
2579enum PreProcessedElement<'a> {
2580    Message(&'a Message<'a>),
2581    Cause(
2582        (
2583            &'a Snippet<'a, Annotation<'a>>,
2584            SourceMap<'a>,
2585            Vec<AnnotatedLineInfo<'a>>,
2586        ),
2587    ),
2588    Suggestion(
2589        (
2590            &'a Snippet<'a, Patch<'a>>,
2591            SourceMap<'a>,
2592            SplicedLines<'a>,
2593            DisplaySuggestion,
2594        ),
2595    ),
2596    Origin(&'a Origin<'a>),
2597    Padding(Padding),
2598}
2599
2600fn pre_process<'a>(
2601    groups: &'a [Group<'a>],
2602) -> (usize, Option<&'a Cow<'a, str>>, Vec<PreProcessedGroup<'a>>) {
2603    let mut max_line_num = 0;
2604    let mut og_primary_path = None;
2605    let mut out = Vec::with_capacity(groups.len());
2606    for group in groups {
2607        let mut elements = Vec::with_capacity(group.elements.len());
2608        let mut primary_path = None;
2609        let mut max_depth = 0;
2610        for element in &group.elements {
2611            match element {
2612                Element::Message(message) => {
2613                    elements.push(PreProcessedElement::Message(message));
2614                }
2615                Element::Cause(cause) => {
2616                    let sm = SourceMap::new(&cause.source, cause.line_start);
2617                    let (depth, annotated_lines) =
2618                        sm.annotated_lines(cause.markers.clone(), cause.fold);
2619
2620                    if cause.fold {
2621                        let end = cause
2622                            .markers
2623                            .iter()
2624                            .map(|a| a.span.end)
2625                            .max()
2626                            .unwrap_or(cause.source.len())
2627                            .min(cause.source.len());
2628
2629                        max_line_num = max(
2630                            cause.line_start + newline_count(&cause.source[..end]),
2631                            max_line_num,
2632                        );
2633                    } else {
2634                        max_line_num = max(
2635                            cause.line_start + newline_count(&cause.source),
2636                            max_line_num,
2637                        );
2638                    }
2639
2640                    if primary_path.is_none() {
2641                        primary_path = Some(cause.path.as_ref());
2642                    }
2643                    max_depth = max(depth, max_depth);
2644                    elements.push(PreProcessedElement::Cause((cause, sm, annotated_lines)));
2645                }
2646                Element::Suggestion(suggestion) => {
2647                    let sm = SourceMap::new(&suggestion.source, suggestion.line_start);
2648                    if let Some((complete, patches, highlights, replaced_highlights)) =
2649                        sm.splice_lines(suggestion.markers.clone(), suggestion.fold)
2650                    {
2651                        let display_suggestion = DisplaySuggestion::new(&complete, &patches, &sm);
2652
2653                        if suggestion.fold {
2654                            if let Some(first) = patches.first() {
2655                                let (l_start, _) =
2656                                    sm.span_to_locations(first.original_span.clone());
2657                                let nc = newline_count(&complete);
2658                                let sugg_max_line_num = match display_suggestion {
2659                                    DisplaySuggestion::Underline => l_start.line,
2660                                    DisplaySuggestion::Diff => {
2661                                        let file_lines = sm.span_to_lines(first.span.clone());
2662                                        file_lines
2663                                            .last()
2664                                            .map_or(l_start.line + nc, |line| line.line_index)
2665                                    }
2666                                    DisplaySuggestion::None => l_start.line + nc,
2667                                    DisplaySuggestion::Add => l_start.line + nc,
2668                                };
2669                                max_line_num = max(sugg_max_line_num, max_line_num);
2670                            }
2671                        } else {
2672                            max_line_num = max(
2673                                suggestion.line_start + newline_count(&complete),
2674                                max_line_num,
2675                            );
2676                        }
2677
2678                        elements.push(PreProcessedElement::Suggestion((
2679                            suggestion,
2680                            sm,
2681                            (complete, patches, highlights, replaced_highlights),
2682                            display_suggestion,
2683                        )));
2684                    }
2685                }
2686                Element::Origin(origin) => {
2687                    if primary_path.is_none() {
2688                        primary_path = Some(Some(&origin.path));
2689                    }
2690                    elements.push(PreProcessedElement::Origin(origin));
2691                }
2692                Element::Padding(padding) => {
2693                    elements.push(PreProcessedElement::Padding(padding.clone()));
2694                }
2695            }
2696        }
2697        let group = PreProcessedGroup {
2698            group,
2699            elements,
2700            primary_path: primary_path.unwrap_or_default(),
2701            max_depth,
2702        };
2703        if og_primary_path.is_none() && group.primary_path.is_some() {
2704            og_primary_path = group.primary_path;
2705        }
2706        out.push(group);
2707    }
2708
2709    (max_line_num, og_primary_path, out)
2710}
2711
2712fn newline_count(body: &str) -> usize {
2713    #[cfg(feature = "simd")]
2714    {
2715        memchr::memchr_iter(b'\n', body.as_bytes()).count()
2716    }
2717    #[cfg(not(feature = "simd"))]
2718    {
2719        body.lines().count().saturating_sub(1)
2720    }
2721}
2722
2723#[cfg(test)]
2724mod test {
2725    use super::{OUTPUT_REPLACEMENTS, newline_count};
2726    use snapbox::IntoData;
2727
2728    fn format_replacements(replacements: Vec<(char, &str)>) -> String {
2729        replacements
2730            .into_iter()
2731            .map(|r| format!("    {r:?}"))
2732            .collect::<Vec<_>>()
2733            .join("\n")
2734    }
2735
2736    #[test]
2737    /// The [`OUTPUT_REPLACEMENTS`] array must be sorted (for binary search to
2738    /// work) and must contain no duplicate entries
2739    fn ensure_output_replacements_is_sorted() {
2740        let mut expected = OUTPUT_REPLACEMENTS.to_owned();
2741        expected.sort_by_key(|r| r.0);
2742        expected.dedup_by_key(|r| r.0);
2743        let expected = format_replacements(expected);
2744        let actual = format_replacements(OUTPUT_REPLACEMENTS.to_owned());
2745        snapbox::assert_data_eq!(actual, expected.into_data().raw());
2746    }
2747
2748    #[test]
2749    fn ensure_newline_count_correct() {
2750        let source = r#"
2751                cargo-features = ["path-bases"]
2752
2753                [package]
2754                name = "foo"
2755                version = "0.5.0"
2756                authors = ["wycats@example.com"]
2757
2758                [dependencies]
2759                bar = { base = '^^not-valid^^', path = 'bar' }
2760            "#;
2761        let actual_count = newline_count(source);
2762        let expected_count = 10;
2763
2764        assert_eq!(expected_count, actual_count);
2765    }
2766}