• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rust-lang / annotate-snippets-rs / 13903310413

17 Mar 2025 03:26PM UTC coverage: 85.793%. Remained the same
13903310413

Pull #185

github

web-flow
Merge 34a69b497 into 5c6ce17f3
Pull Request #185: refactor(display-list): split into separate modules (#184)

636 of 729 new or added lines in 6 files covered. (87.24%)

779 of 908 relevant lines covered (85.79%)

4.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.74
/src/renderer/display/display_list.rs
1
use std::{
2
    cmp,
3
    fmt::{self, Display},
4
};
5

6
use crate::{
7
    renderer::{
8
        display::{
9
            constants::ANONYMIZED_LINE_NUM, display_annotations::DisplayAnnotationPart,
10
            display_line::DisplayLine,
11
        },
12
        styled_buffer::StyledBuffer,
13
        stylesheet::Stylesheet,
14
    },
15
    snippet,
16
};
17

18
use super::{display_set::DisplaySet, format_message};
19

20
/// List of lines to be displayed.
21
pub(crate) struct DisplayList<'a> {
22
    pub(crate) body: Vec<DisplaySet<'a>>,
23
    pub(crate) stylesheet: &'a Stylesheet,
24
    pub(crate) anonymized_line_numbers: bool,
25
}
26

27
impl PartialEq for DisplayList<'_> {
NEW
28
    fn eq(&self, other: &Self) -> bool {
×
NEW
29
        self.body == other.body && self.anonymized_line_numbers == other.anonymized_line_numbers
×
30
    }
31
}
32

33
impl fmt::Debug for DisplayList<'_> {
NEW
34
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
×
NEW
35
        f.debug_struct("DisplayList")
×
36
            .field("body", &self.body)
NEW
37
            .field("anonymized_line_numbers", &self.anonymized_line_numbers)
×
38
            .finish()
39
    }
40
}
41

42
impl Display for DisplayList<'_> {
43
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7✔
44
        let lineno_width = self.body.iter().fold(0, |max, set| {
10✔
45
            set.display_lines.iter().fold(max, |max, line| match line {
13✔
46
                DisplayLine::Source { lineno, .. } => cmp::max(lineno.unwrap_or(0), max),
4✔
47
                _ => max,
6✔
48
            })
49
        });
50
        let lineno_width = if lineno_width == 0 {
7✔
51
            lineno_width
1✔
52
        } else if self.anonymized_line_numbers {
10✔
53
            ANONYMIZED_LINE_NUM.len()
2✔
54
        } else {
55
            ((lineno_width as f64).log10().floor() as usize) + 1
10✔
56
        };
57

58
        let multiline_depth = self.body.iter().fold(0, |max, set| {
10✔
59
            set.display_lines.iter().fold(max, |max2, line| match line {
14✔
60
                DisplayLine::Source { annotations, .. } => cmp::max(
10✔
61
                    annotations.iter().fold(max2, |max3, line| {
10✔
62
                        cmp::max(
5✔
63
                            match line.annotation_part {
6✔
64
                                DisplayAnnotationPart::Standalone => 0,
3✔
NEW
65
                                DisplayAnnotationPart::LabelContinuation => 0,
×
66
                                DisplayAnnotationPart::MultilineStart(depth) => depth + 1,
7✔
67
                                DisplayAnnotationPart::MultilineEnd(depth) => depth + 1,
7✔
68
                            },
69
                            max3,
70
                        )
71
                    }),
72
                    max,
3✔
73
                ),
74
                _ => max2,
6✔
75
            })
76
        });
77
        let mut buffer = StyledBuffer::new();
3✔
78
        for set in self.body.iter() {
17✔
79
            self.format_set(set, lineno_width, multiline_depth, &mut buffer)?;
9✔
80
        }
81
        write!(f, "{}", buffer.render(self.stylesheet)?)
13✔
82
    }
83
}
84

85
impl<'a> DisplayList<'a> {
86
    pub(crate) fn new(
5✔
87
        message: snippet::Message<'a>,
88
        stylesheet: &'a Stylesheet,
89
        anonymized_line_numbers: bool,
90
        term_width: usize,
91
    ) -> DisplayList<'a> {
92
        let body = format_message(message, term_width, anonymized_line_numbers, true);
5✔
93

94
        Self {
95
            body,
96
            stylesheet,
97
            anonymized_line_numbers,
98
        }
99
    }
100

101
    fn format_set(
6✔
102
        &self,
103
        set: &DisplaySet<'_>,
104
        lineno_width: usize,
105
        multiline_depth: usize,
106
        buffer: &mut StyledBuffer,
107
    ) -> fmt::Result {
108
        for line in &set.display_lines {
9✔
109
            set.format_line(
5✔
NEW
110
                line,
×
NEW
111
                lineno_width,
×
NEW
112
                multiline_depth,
×
113
                self.stylesheet,
5✔
114
                self.anonymized_line_numbers,
8✔
NEW
115
                buffer,
×
116
            )?;
117
        }
118
        Ok(())
5✔
119
    }
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc