1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
//! Defines token tags we use for syntax highlighting.
//! A tag is not unlike a CSS class.
use std::{fmt, ops};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Highlight {
pub tag: HighlightTag,
pub modifiers: HighlightModifiers,
}
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct HighlightModifiers(u32);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum HighlightTag {
Field,
Function,
Module,
Constant,
Macro,
Variable,
Type,
TypeSelf,
TypeParam,
TypeLifetime,
LiteralByte,
LiteralNumeric,
LiteralChar,
Comment,
LiteralString,
Attribute,
Keyword,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(u8)]
pub enum HighlightModifier {
Mutable = 0,
Unsafe,
/// Used with keywords like `if` and `break`.
Control,
Builtin,
}
impl HighlightTag {
fn as_str(self) -> &'static str {
match self {
HighlightTag::Field => "field",
HighlightTag::Function => "function",
HighlightTag::Module => "module",
HighlightTag::Constant => "constant",
HighlightTag::Macro => "macro",
HighlightTag::Variable => "variable",
HighlightTag::Type => "type",
HighlightTag::TypeSelf => "type.self",
HighlightTag::TypeParam => "type.param",
HighlightTag::TypeLifetime => "type.lifetime",
HighlightTag::LiteralByte => "literal.byte",
HighlightTag::LiteralNumeric => "literal.numeric",
HighlightTag::LiteralChar => "literal.char",
HighlightTag::Comment => "comment",
HighlightTag::LiteralString => "string",
HighlightTag::Attribute => "attribute",
HighlightTag::Keyword => "keyword",
}
}
}
impl fmt::Display for HighlightTag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_str(), f)
}
}
impl HighlightModifier {
const ALL: &'static [HighlightModifier] = &[
HighlightModifier::Mutable,
HighlightModifier::Unsafe,
HighlightModifier::Control,
HighlightModifier::Builtin,
];
fn as_str(self) -> &'static str {
match self {
HighlightModifier::Mutable => "mutable",
HighlightModifier::Unsafe => "unsafe",
HighlightModifier::Control => "control",
HighlightModifier::Builtin => "builtin",
}
}
fn mask(self) -> u32 {
1 << (self as u32)
}
}
impl fmt::Display for HighlightModifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_str(), f)
}
}
impl fmt::Display for Highlight {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.tag)?;
for modifier in self.modifiers.iter() {
write!(f, ".{}", modifier)?
}
Ok(())
}
}
impl From<HighlightTag> for Highlight {
fn from(tag: HighlightTag) -> Highlight {
Highlight::new(tag)
}
}
impl Highlight {
pub(crate) fn new(tag: HighlightTag) -> Highlight {
Highlight { tag, modifiers: HighlightModifiers::default() }
}
}
impl ops::BitOr<HighlightModifier> for HighlightTag {
type Output = Highlight;
fn bitor(self, rhs: HighlightModifier) -> Highlight {
Highlight::new(self) | rhs
}
}
impl ops::BitOrAssign<HighlightModifier> for HighlightModifiers {
fn bitor_assign(&mut self, rhs: HighlightModifier) {
self.0 |= rhs.mask();
}
}
impl ops::BitOrAssign<HighlightModifier> for Highlight {
fn bitor_assign(&mut self, rhs: HighlightModifier) {
self.modifiers |= rhs;
}
}
impl ops::BitOr<HighlightModifier> for Highlight {
type Output = Highlight;
fn bitor(mut self, rhs: HighlightModifier) -> Highlight {
self |= rhs;
self
}
}
impl HighlightModifiers {
pub fn iter(self) -> impl Iterator<Item = HighlightModifier> {
HighlightModifier::ALL.iter().copied().filter(move |it| self.0 & it.mask() == it.mask())
}
}
|