diff options
Diffstat (limited to 'crates/ide/src/syntax_highlighting/tags.rs')
-rw-r--r-- | crates/ide/src/syntax_highlighting/tags.rs | 139 |
1 files changed, 71 insertions, 68 deletions
diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index 8b8867079..2f39bcc8e 100644 --- a/crates/ide/src/syntax_highlighting/tags.rs +++ b/crates/ide/src/syntax_highlighting/tags.rs | |||
@@ -7,15 +7,15 @@ use crate::SymbolKind; | |||
7 | 7 | ||
8 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] | 8 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] |
9 | pub struct Highlight { | 9 | pub struct Highlight { |
10 | pub tag: HighlightTag, | 10 | pub tag: HlTag, |
11 | pub modifiers: HighlightModifiers, | 11 | pub mods: HlMods, |
12 | } | 12 | } |
13 | 13 | ||
14 | #[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] | 14 | #[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] |
15 | pub struct HighlightModifiers(u32); | 15 | pub struct HlMods(u32); |
16 | 16 | ||
17 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] | 17 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] |
18 | pub enum HighlightTag { | 18 | pub enum HlTag { |
19 | Symbol(SymbolKind), | 19 | Symbol(SymbolKind), |
20 | 20 | ||
21 | BoolLiteral, | 21 | BoolLiteral, |
@@ -33,13 +33,13 @@ pub enum HighlightTag { | |||
33 | Operator, | 33 | Operator, |
34 | UnresolvedReference, | 34 | UnresolvedReference, |
35 | 35 | ||
36 | // For things which don't have proper Tag, but want to use modifiers. | 36 | // For things which don't have a specific highlight. |
37 | Dummy, | 37 | None, |
38 | } | 38 | } |
39 | 39 | ||
40 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] | 40 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] |
41 | #[repr(u8)] | 41 | #[repr(u8)] |
42 | pub enum HighlightModifier { | 42 | pub enum HlMod { |
43 | /// Used to differentiate individual elements within attributes. | 43 | /// Used to differentiate individual elements within attributes. |
44 | Attribute = 0, | 44 | Attribute = 0, |
45 | /// Used with keywords like `if` and `break`. | 45 | /// Used with keywords like `if` and `break`. |
@@ -61,10 +61,10 @@ pub enum HighlightModifier { | |||
61 | Unsafe, | 61 | Unsafe, |
62 | } | 62 | } |
63 | 63 | ||
64 | impl HighlightTag { | 64 | impl HlTag { |
65 | fn as_str(self) -> &'static str { | 65 | fn as_str(self) -> &'static str { |
66 | match self { | 66 | match self { |
67 | HighlightTag::Symbol(symbol) => match symbol { | 67 | HlTag::Symbol(symbol) => match symbol { |
68 | SymbolKind::Const => "constant", | 68 | SymbolKind::Const => "constant", |
69 | SymbolKind::Static => "static", | 69 | SymbolKind::Static => "static", |
70 | SymbolKind::Enum => "enum", | 70 | SymbolKind::Enum => "enum", |
@@ -86,59 +86,59 @@ impl HighlightTag { | |||
86 | SymbolKind::SelfParam => "self_keyword", | 86 | SymbolKind::SelfParam => "self_keyword", |
87 | SymbolKind::Impl => "self_type", | 87 | SymbolKind::Impl => "self_type", |
88 | }, | 88 | }, |
89 | HighlightTag::Attribute => "attribute", | 89 | HlTag::Attribute => "attribute", |
90 | HighlightTag::BoolLiteral => "bool_literal", | 90 | HlTag::BoolLiteral => "bool_literal", |
91 | HighlightTag::BuiltinType => "builtin_type", | 91 | HlTag::BuiltinType => "builtin_type", |
92 | HighlightTag::ByteLiteral => "byte_literal", | 92 | HlTag::ByteLiteral => "byte_literal", |
93 | HighlightTag::CharLiteral => "char_literal", | 93 | HlTag::CharLiteral => "char_literal", |
94 | HighlightTag::Comment => "comment", | 94 | HlTag::Comment => "comment", |
95 | HighlightTag::EscapeSequence => "escape_sequence", | 95 | HlTag::EscapeSequence => "escape_sequence", |
96 | HighlightTag::FormatSpecifier => "format_specifier", | 96 | HlTag::FormatSpecifier => "format_specifier", |
97 | HighlightTag::Dummy => "dummy", | 97 | HlTag::Keyword => "keyword", |
98 | HighlightTag::Keyword => "keyword", | 98 | HlTag::Punctuation => "punctuation", |
99 | HighlightTag::Punctuation => "punctuation", | 99 | HlTag::NumericLiteral => "numeric_literal", |
100 | HighlightTag::NumericLiteral => "numeric_literal", | 100 | HlTag::Operator => "operator", |
101 | HighlightTag::Operator => "operator", | 101 | HlTag::StringLiteral => "string_literal", |
102 | HighlightTag::StringLiteral => "string_literal", | 102 | HlTag::UnresolvedReference => "unresolved_reference", |
103 | HighlightTag::UnresolvedReference => "unresolved_reference", | 103 | HlTag::None => "none", |
104 | } | 104 | } |
105 | } | 105 | } |
106 | } | 106 | } |
107 | 107 | ||
108 | impl fmt::Display for HighlightTag { | 108 | impl fmt::Display for HlTag { |
109 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 109 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
110 | fmt::Display::fmt(self.as_str(), f) | 110 | fmt::Display::fmt(self.as_str(), f) |
111 | } | 111 | } |
112 | } | 112 | } |
113 | 113 | ||
114 | impl HighlightModifier { | 114 | impl HlMod { |
115 | const ALL: &'static [HighlightModifier; HighlightModifier::Unsafe as u8 as usize + 1] = &[ | 115 | const ALL: &'static [HlMod; HlMod::Unsafe as u8 as usize + 1] = &[ |
116 | HighlightModifier::Attribute, | 116 | HlMod::Attribute, |
117 | HighlightModifier::ControlFlow, | 117 | HlMod::ControlFlow, |
118 | HighlightModifier::Definition, | 118 | HlMod::Definition, |
119 | HighlightModifier::Documentation, | 119 | HlMod::Documentation, |
120 | HighlightModifier::Injected, | 120 | HlMod::Injected, |
121 | HighlightModifier::Mutable, | 121 | HlMod::Mutable, |
122 | HighlightModifier::Consuming, | 122 | HlMod::Consuming, |
123 | HighlightModifier::Callable, | 123 | HlMod::Callable, |
124 | HighlightModifier::Static, | 124 | HlMod::Static, |
125 | HighlightModifier::Associated, | 125 | HlMod::Associated, |
126 | HighlightModifier::Unsafe, | 126 | HlMod::Unsafe, |
127 | ]; | 127 | ]; |
128 | 128 | ||
129 | fn as_str(self) -> &'static str { | 129 | fn as_str(self) -> &'static str { |
130 | match self { | 130 | match self { |
131 | HighlightModifier::Attribute => "attribute", | 131 | HlMod::Attribute => "attribute", |
132 | HighlightModifier::ControlFlow => "control", | 132 | HlMod::ControlFlow => "control", |
133 | HighlightModifier::Definition => "declaration", | 133 | HlMod::Definition => "declaration", |
134 | HighlightModifier::Documentation => "documentation", | 134 | HlMod::Documentation => "documentation", |
135 | HighlightModifier::Injected => "injected", | 135 | HlMod::Injected => "injected", |
136 | HighlightModifier::Mutable => "mutable", | 136 | HlMod::Mutable => "mutable", |
137 | HighlightModifier::Consuming => "consuming", | 137 | HlMod::Consuming => "consuming", |
138 | HighlightModifier::Unsafe => "unsafe", | 138 | HlMod::Unsafe => "unsafe", |
139 | HighlightModifier::Callable => "callable", | 139 | HlMod::Callable => "callable", |
140 | HighlightModifier::Static => "static", | 140 | HlMod::Static => "static", |
141 | HighlightModifier::Associated => "associated", | 141 | HlMod::Associated => "associated", |
142 | } | 142 | } |
143 | } | 143 | } |
144 | 144 | ||
@@ -147,7 +147,7 @@ impl HighlightModifier { | |||
147 | } | 147 | } |
148 | } | 148 | } |
149 | 149 | ||
150 | impl fmt::Display for HighlightModifier { | 150 | impl fmt::Display for HlMod { |
151 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 151 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
152 | fmt::Display::fmt(self.as_str(), f) | 152 | fmt::Display::fmt(self.as_str(), f) |
153 | } | 153 | } |
@@ -156,60 +156,63 @@ impl fmt::Display for HighlightModifier { | |||
156 | impl fmt::Display for Highlight { | 156 | impl fmt::Display for Highlight { |
157 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 157 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
158 | write!(f, "{}", self.tag)?; | 158 | write!(f, "{}", self.tag)?; |
159 | for modifier in self.modifiers.iter() { | 159 | for modifier in self.mods.iter() { |
160 | write!(f, ".{}", modifier)? | 160 | write!(f, ".{}", modifier)? |
161 | } | 161 | } |
162 | Ok(()) | 162 | Ok(()) |
163 | } | 163 | } |
164 | } | 164 | } |
165 | 165 | ||
166 | impl From<HighlightTag> for Highlight { | 166 | impl From<HlTag> for Highlight { |
167 | fn from(tag: HighlightTag) -> Highlight { | 167 | fn from(tag: HlTag) -> Highlight { |
168 | Highlight::new(tag) | 168 | Highlight::new(tag) |
169 | } | 169 | } |
170 | } | 170 | } |
171 | 171 | ||
172 | impl Highlight { | 172 | impl Highlight { |
173 | pub(crate) fn new(tag: HighlightTag) -> Highlight { | 173 | pub(crate) fn new(tag: HlTag) -> Highlight { |
174 | Highlight { tag, modifiers: HighlightModifiers::default() } | 174 | Highlight { tag, mods: HlMods::default() } |
175 | } | ||
176 | pub fn is_empty(&self) -> bool { | ||
177 | self.tag == HlTag::None && self.mods == HlMods::default() | ||
175 | } | 178 | } |
176 | } | 179 | } |
177 | 180 | ||
178 | impl ops::BitOr<HighlightModifier> for HighlightTag { | 181 | impl ops::BitOr<HlMod> for HlTag { |
179 | type Output = Highlight; | 182 | type Output = Highlight; |
180 | 183 | ||
181 | fn bitor(self, rhs: HighlightModifier) -> Highlight { | 184 | fn bitor(self, rhs: HlMod) -> Highlight { |
182 | Highlight::new(self) | rhs | 185 | Highlight::new(self) | rhs |
183 | } | 186 | } |
184 | } | 187 | } |
185 | 188 | ||
186 | impl ops::BitOrAssign<HighlightModifier> for HighlightModifiers { | 189 | impl ops::BitOrAssign<HlMod> for HlMods { |
187 | fn bitor_assign(&mut self, rhs: HighlightModifier) { | 190 | fn bitor_assign(&mut self, rhs: HlMod) { |
188 | self.0 |= rhs.mask(); | 191 | self.0 |= rhs.mask(); |
189 | } | 192 | } |
190 | } | 193 | } |
191 | 194 | ||
192 | impl ops::BitOrAssign<HighlightModifier> for Highlight { | 195 | impl ops::BitOrAssign<HlMod> for Highlight { |
193 | fn bitor_assign(&mut self, rhs: HighlightModifier) { | 196 | fn bitor_assign(&mut self, rhs: HlMod) { |
194 | self.modifiers |= rhs; | 197 | self.mods |= rhs; |
195 | } | 198 | } |
196 | } | 199 | } |
197 | 200 | ||
198 | impl ops::BitOr<HighlightModifier> for Highlight { | 201 | impl ops::BitOr<HlMod> for Highlight { |
199 | type Output = Highlight; | 202 | type Output = Highlight; |
200 | 203 | ||
201 | fn bitor(mut self, rhs: HighlightModifier) -> Highlight { | 204 | fn bitor(mut self, rhs: HlMod) -> Highlight { |
202 | self |= rhs; | 205 | self |= rhs; |
203 | self | 206 | self |
204 | } | 207 | } |
205 | } | 208 | } |
206 | 209 | ||
207 | impl HighlightModifiers { | 210 | impl HlMods { |
208 | pub fn contains(self, m: HighlightModifier) -> bool { | 211 | pub fn contains(self, m: HlMod) -> bool { |
209 | self.0 & m.mask() == m.mask() | 212 | self.0 & m.mask() == m.mask() |
210 | } | 213 | } |
211 | 214 | ||
212 | pub fn iter(self) -> impl Iterator<Item = HighlightModifier> { | 215 | pub fn iter(self) -> impl Iterator<Item = HlMod> { |
213 | HighlightModifier::ALL.iter().copied().filter(move |it| self.0 & it.mask() == it.mask()) | 216 | HlMod::ALL.iter().copied().filter(move |it| self.0 & it.mask() == it.mask()) |
214 | } | 217 | } |
215 | } | 218 | } |