Skip to content

Commit 805c67d

Browse files
committed
feat: Allow setting the primary level for a group
1 parent dd5b3a2 commit 805c67d

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

examples/elide_header.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def foobar(door, bar={}):
99
"#;
1010

1111
let message = &[Group::new()
12+
.primary_level(Level::NOTE)
1213
.element(
1314
Snippet::source(source)
1415
.fold(false)

examples/elide_header.svg

Lines changed: 2 additions & 2 deletions
Loading

src/renderer/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,16 @@ impl Renderer {
275275
if og_primary_path.is_none() && primary_path.is_some() {
276276
og_primary_path = primary_path;
277277
}
278-
let level = group
279-
.elements
280-
.first()
281-
.and_then(|s| match &s {
282-
Element::Title(title) => Some(title.level.clone()),
283-
_ => None,
284-
})
285-
.unwrap_or(Level::ERROR);
278+
let level = group.primary_level.clone().unwrap_or_else(|| {
279+
group
280+
.elements
281+
.first()
282+
.and_then(|s| match &s {
283+
Element::Title(title) => Some(title.level.clone()),
284+
_ => None,
285+
})
286+
.unwrap_or(Level::ERROR)
287+
});
286288
let mut source_map_annotated_lines = VecDeque::new();
287289
let mut max_depth = 0;
288290
for e in &group.elements {

src/snippet.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub(crate) struct Id<'a> {
2020
/// An [`Element`] container
2121
#[derive(Clone, Debug)]
2222
pub struct Group<'a> {
23+
pub(crate) primary_level: Option<Level<'a>>,
2324
pub(crate) elements: Vec<Element<'a>>,
2425
}
2526

@@ -31,7 +32,10 @@ impl Default for Group<'_> {
3132

3233
impl<'a> Group<'a> {
3334
pub fn new() -> Self {
34-
Self { elements: vec![] }
35+
Self {
36+
primary_level: None,
37+
elements: vec![],
38+
}
3539
}
3640

3741
pub fn element(mut self, section: impl Into<Element<'a>>) -> Self {
@@ -44,6 +48,16 @@ impl<'a> Group<'a> {
4448
self
4549
}
4650

51+
/// Set the primary [`Level`] for this [`Group`].
52+
///
53+
/// If not specified, if the first element in a [`Group`] is a [`Title`]
54+
/// then its [`Level`] will be used, if no it will default to
55+
/// [`Level::ERROR`].
56+
pub fn primary_level(mut self, level: Level<'a>) -> Self {
57+
self.primary_level = Some(level);
58+
self
59+
}
60+
4761
pub fn is_empty(&self) -> bool {
4862
self.elements.is_empty()
4963
}
@@ -264,7 +278,8 @@ impl<'a> Annotation<'a> {
264278
pub enum AnnotationKind {
265279
/// An [`Annotation`] which will use the "primary" underline character ('^'
266280
/// for [`OutputTheme::Ascii`], and '━' for [`OutputTheme::Unicode`]) and
267-
/// be colored according to the primary [`Level`] of the [`Group`].
281+
/// be colored according to the primary [`Level`] of the [`Group`], see
282+
/// [`Group::primary_level`] for details about how this is set.
268283
Primary,
269284
/// An [`Annotation`] which will use the "secondary" underline character (
270285
/// '-' for [`OutputTheme::Ascii`], and '─' for [`OutputTheme::Unicode`])

0 commit comments

Comments
 (0)