|
| 1 | +use anstyle::Style; |
| 2 | + |
| 3 | +use crate::{renderer::stylesheet::Stylesheet, snippet}; |
| 4 | + |
| 5 | +use super::{ |
| 6 | + constants::{ERROR_TXT, HELP_TXT, INFO_TXT, NOTE_TXT, WARNING_TXT}, |
| 7 | + display_text::DisplayTextFragment, |
| 8 | +}; |
| 9 | + |
| 10 | +/// A type of the `Annotation` which may impact the sigils, style or text displayed. |
| 11 | +/// |
| 12 | +/// There are several ways to uses this information when formatting the `DisplayList`: |
| 13 | +/// |
| 14 | +/// * An annotation may display the name of the type like `error` or `info`. |
| 15 | +/// * An underline for `Error` may be `^^^` while for `Warning` it could be `---`. |
| 16 | +/// * `ColorStylesheet` may use different colors for different annotations. |
| 17 | +#[derive(Debug, Clone, PartialEq)] |
| 18 | +pub(crate) enum DisplayAnnotationType { |
| 19 | + None, |
| 20 | + Error, |
| 21 | + Warning, |
| 22 | + Info, |
| 23 | + Note, |
| 24 | + Help, |
| 25 | +} |
| 26 | + |
| 27 | +/// An inline text |
| 28 | +/// An indicator of what part of the annotation a given `Annotation` is. |
| 29 | +#[derive(Debug, Clone, PartialEq)] |
| 30 | +pub(crate) enum DisplayAnnotationPart { |
| 31 | + /// A standalone, single-line annotation. |
| 32 | + Standalone, |
| 33 | + /// A continuation of a multi-line label of an annotation. |
| 34 | + LabelContinuation, |
| 35 | + /// A line starting a multiline annotation. |
| 36 | + MultilineStart(usize), |
| 37 | + /// A line ending a multiline annotation. |
| 38 | + MultilineEnd(usize), |
| 39 | +} |
| 40 | + |
| 41 | +/// Inline annotation which can be used in either Raw or Source line. |
| 42 | +#[derive(Clone, Debug, PartialEq)] |
| 43 | +pub(crate) struct Annotation<'a> { |
| 44 | + pub(crate) annotation_type: DisplayAnnotationType, |
| 45 | + pub(crate) id: Option<&'a str>, |
| 46 | + pub(crate) label: Vec<DisplayTextFragment<'a>>, |
| 47 | +} |
| 48 | + |
| 49 | +#[derive(Clone, Debug, PartialEq)] |
| 50 | +pub(crate) struct DisplaySourceAnnotation<'a> { |
| 51 | + pub(crate) annotation: Annotation<'a>, |
| 52 | + pub(crate) range: (usize, usize), |
| 53 | + pub(crate) annotation_type: DisplayAnnotationType, |
| 54 | + pub(crate) annotation_part: DisplayAnnotationPart, |
| 55 | +} |
| 56 | + |
| 57 | +impl DisplaySourceAnnotation<'_> { |
| 58 | + pub(crate) fn has_label(&self) -> bool { |
| 59 | + !self |
| 60 | + .annotation |
| 61 | + .label |
| 62 | + .iter() |
| 63 | + .all(|label| label.content.is_empty()) |
| 64 | + } |
| 65 | + |
| 66 | + // Length of this annotation as displayed in the stderr output |
| 67 | + pub(crate) fn len(&self) -> usize { |
| 68 | + // Account for usize underflows |
| 69 | + if self.range.1 > self.range.0 { |
| 70 | + self.range.1 - self.range.0 |
| 71 | + } else { |
| 72 | + self.range.0 - self.range.1 |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + pub(crate) fn takes_space(&self) -> bool { |
| 77 | + // Multiline annotations always have to keep vertical space. |
| 78 | + matches!( |
| 79 | + self.annotation_part, |
| 80 | + DisplayAnnotationPart::MultilineStart(_) | DisplayAnnotationPart::MultilineEnd(_) |
| 81 | + ) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl From<snippet::Level> for DisplayAnnotationType { |
| 86 | + fn from(at: snippet::Level) -> Self { |
| 87 | + match at { |
| 88 | + snippet::Level::Error => DisplayAnnotationType::Error, |
| 89 | + snippet::Level::Warning => DisplayAnnotationType::Warning, |
| 90 | + snippet::Level::Info => DisplayAnnotationType::Info, |
| 91 | + snippet::Level::Note => DisplayAnnotationType::Note, |
| 92 | + snippet::Level::Help => DisplayAnnotationType::Help, |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[inline] |
| 98 | +pub(crate) fn annotation_type_str(annotation_type: &DisplayAnnotationType) -> &'static str { |
| 99 | + match annotation_type { |
| 100 | + DisplayAnnotationType::Error => ERROR_TXT, |
| 101 | + DisplayAnnotationType::Help => HELP_TXT, |
| 102 | + DisplayAnnotationType::Info => INFO_TXT, |
| 103 | + DisplayAnnotationType::Note => NOTE_TXT, |
| 104 | + DisplayAnnotationType::Warning => WARNING_TXT, |
| 105 | + DisplayAnnotationType::None => "", |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +pub(crate) fn annotation_type_len(annotation_type: &DisplayAnnotationType) -> usize { |
| 110 | + match annotation_type { |
| 111 | + DisplayAnnotationType::Error => ERROR_TXT.len(), |
| 112 | + DisplayAnnotationType::Help => HELP_TXT.len(), |
| 113 | + DisplayAnnotationType::Info => INFO_TXT.len(), |
| 114 | + DisplayAnnotationType::Note => NOTE_TXT.len(), |
| 115 | + DisplayAnnotationType::Warning => WARNING_TXT.len(), |
| 116 | + DisplayAnnotationType::None => 0, |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +pub(crate) fn get_annotation_style<'a>( |
| 121 | + annotation_type: &DisplayAnnotationType, |
| 122 | + stylesheet: &'a Stylesheet, |
| 123 | +) -> &'a Style { |
| 124 | + match annotation_type { |
| 125 | + DisplayAnnotationType::Error => stylesheet.error(), |
| 126 | + DisplayAnnotationType::Warning => stylesheet.warning(), |
| 127 | + DisplayAnnotationType::Info => stylesheet.info(), |
| 128 | + DisplayAnnotationType::Note => stylesheet.note(), |
| 129 | + DisplayAnnotationType::Help => stylesheet.help(), |
| 130 | + DisplayAnnotationType::None => stylesheet.none(), |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +#[inline] |
| 135 | +pub(crate) fn is_annotation_empty(annotation: &Annotation<'_>) -> bool { |
| 136 | + annotation |
| 137 | + .label |
| 138 | + .iter() |
| 139 | + .all(|fragment| fragment.content.is_empty()) |
| 140 | +} |
0 commit comments