1 //! Defines token tags we use for syntax highlighting.
2 //! A tag is not unlike a CSS class.
8 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
14 #[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
15 pub struct HlMods(u32);
17 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
36 // For things which don't have a specific highlight.
40 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
43 /// Used to differentiate individual elements within attributes.
45 /// Used with keywords like `if` and `break`.
47 /// `foo` in `fn foo(x: i32)` is a definition, `foo` in `foo(90 + 2)` is
55 /// Used for associated functions
57 /// Used for items in impls&traits.
65 fn as_str(self) -> &'static str {
67 HlTag::Symbol(symbol) => match symbol {
68 SymbolKind::Const => "constant",
69 SymbolKind::Static => "static",
70 SymbolKind::Enum => "enum",
71 SymbolKind::Variant => "enum_variant",
72 SymbolKind::Struct => "struct",
73 SymbolKind::Union => "union",
74 SymbolKind::Field => "field",
75 SymbolKind::Module => "module",
76 SymbolKind::Trait => "trait",
77 SymbolKind::Function => "function",
78 SymbolKind::TypeAlias => "type_alias",
79 SymbolKind::TypeParam => "type_param",
80 SymbolKind::ConstParam => "const_param",
81 SymbolKind::LifetimeParam => "lifetime",
82 SymbolKind::Macro => "macro",
83 SymbolKind::Local => "variable",
84 SymbolKind::Label => "label",
85 SymbolKind::ValueParam => "value_param",
86 SymbolKind::SelfParam => "self_keyword",
87 SymbolKind::Impl => "self_type",
89 HlTag::Attribute => "attribute",
90 HlTag::BoolLiteral => "bool_literal",
91 HlTag::BuiltinType => "builtin_type",
92 HlTag::ByteLiteral => "byte_literal",
93 HlTag::CharLiteral => "char_literal",
94 HlTag::Comment => "comment",
95 HlTag::EscapeSequence => "escape_sequence",
96 HlTag::FormatSpecifier => "format_specifier",
97 HlTag::Keyword => "keyword",
98 HlTag::Punctuation => "punctuation",
99 HlTag::NumericLiteral => "numeric_literal",
100 HlTag::Operator => "operator",
101 HlTag::StringLiteral => "string_literal",
102 HlTag::UnresolvedReference => "unresolved_reference",
103 HlTag::None => "none",
108 impl fmt::Display for HlTag {
109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110 fmt::Display::fmt(self.as_str(), f)
115 const ALL: &'static [HlMod; HlMod::Unsafe as u8 as usize + 1] = &[
119 HlMod::Documentation,
129 fn as_str(self) -> &'static str {
131 HlMod::Attribute => "attribute",
132 HlMod::ControlFlow => "control",
133 HlMod::Definition => "declaration",
134 HlMod::Documentation => "documentation",
135 HlMod::Injected => "injected",
136 HlMod::Mutable => "mutable",
137 HlMod::Consuming => "consuming",
138 HlMod::Unsafe => "unsafe",
139 HlMod::Callable => "callable",
140 HlMod::Static => "static",
141 HlMod::Associated => "associated",
145 fn mask(self) -> u32 {
150 impl fmt::Display for HlMod {
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 fmt::Display::fmt(self.as_str(), f)
156 impl fmt::Display for Highlight {
157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158 write!(f, "{}", self.tag)?;
159 for modifier in self.mods.iter() {
160 write!(f, ".{}", modifier)?
166 impl From<HlTag> for Highlight {
167 fn from(tag: HlTag) -> Highlight {
173 pub(crate) fn new(tag: HlTag) -> Highlight {
174 Highlight { tag, mods: HlMods::default() }
176 pub fn is_empty(&self) -> bool {
177 self.tag == HlTag::None && self.mods == HlMods::default()
181 impl ops::BitOr<HlMod> for HlTag {
182 type Output = Highlight;
184 fn bitor(self, rhs: HlMod) -> Highlight {
185 Highlight::new(self) | rhs
189 impl ops::BitOrAssign<HlMod> for HlMods {
190 fn bitor_assign(&mut self, rhs: HlMod) {
191 self.0 |= rhs.mask();
195 impl ops::BitOrAssign<HlMod> for Highlight {
196 fn bitor_assign(&mut self, rhs: HlMod) {
201 impl ops::BitOr<HlMod> for Highlight {
202 type Output = Highlight;
204 fn bitor(mut self, rhs: HlMod) -> Highlight {
211 pub fn contains(self, m: HlMod) -> bool {
212 self.0 & m.mask() == m.mask()
215 pub fn iter(self) -> impl Iterator<Item = HlMod> {
216 HlMod::ALL.iter().copied().filter(move |it| self.0 & it.mask() == it.mask())