Skip to main content

iced_core/
code.rs

1use crate::Theme;
2use crate::font;
3use crate::text::highlighter;
4
5/// A specific region of code.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum Code {
8    /// A comment.
9    Comment,
10    /// A string or character literal.
11    String,
12    /// A keyword or storage word.
13    Keyword,
14    /// A constant, numeric, or boolean literal.
15    Constant,
16    /// A function or method name.
17    Function,
18    /// A type, class, or tag name.
19    Type,
20    /// A variable.
21    Variable,
22    /// A built-in or support symbol.
23    Support,
24    /// Punctuation.
25    Punctuation,
26    /// A path component.
27    Path,
28    /// An invalid or erroneous construct.
29    Invalid,
30    /// Anything that does not match another class.
31    Other,
32}
33
34impl Code {
35    /// Highlights the [`Code`] with the given [`Theme`].
36    pub fn highlight(self, theme: &Theme) -> highlighter::Style {
37        let palette = theme.palette();
38
39        let color = match self {
40            Code::Keyword => Some(palette.primary.base.color),
41            Code::Type | Code::Path | Code::Function => Some(palette.warning.base.color),
42
43            Code::Variable => Some(palette.danger.base.color),
44            Code::Constant => Some(palette.danger.base.color),
45            Code::String => Some(palette.success.base.color),
46            Code::Support => Some(palette.primary.base.color),
47
48            Code::Punctuation => Some(palette.secondary.strong.color),
49            Code::Comment => Some(palette.secondary.base.color),
50
51            Code::Invalid => Some(palette.danger.base.color),
52            Code::Other => None,
53        };
54
55        highlighter::Style {
56            color,
57            style: (self == Code::Comment).then_some(font::Style::Italic),
58        }
59    }
60}