diff options
Diffstat (limited to 'crates/ide/src/syntax_highlighting/tags.rs')
-rw-r--r-- | crates/ide/src/syntax_highlighting/tags.rs | 173 |
1 files changed, 104 insertions, 69 deletions
diff --git a/crates/ide/src/syntax_highlighting/tags.rs b/crates/ide/src/syntax_highlighting/tags.rs index 8b8867079..8dd05ac52 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, |
@@ -29,17 +29,17 @@ pub enum HighlightTag { | |||
29 | EscapeSequence, | 29 | EscapeSequence, |
30 | FormatSpecifier, | 30 | FormatSpecifier, |
31 | Keyword, | 31 | Keyword, |
32 | Punctuation, | 32 | Punctuation(HlPunct), |
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,32 @@ pub enum HighlightModifier { | |||
61 | Unsafe, | 61 | Unsafe, |
62 | } | 62 | } |
63 | 63 | ||
64 | impl HighlightTag { | 64 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] |
65 | pub enum HlPunct { | ||
66 | /// [] | ||
67 | Bracket, | ||
68 | /// {} | ||
69 | Brace, | ||
70 | /// () | ||
71 | Parenthesis, | ||
72 | /// <> | ||
73 | Angle, | ||
74 | /// , | ||
75 | Comma, | ||
76 | /// . | ||
77 | Dot, | ||
78 | /// : | ||
79 | Colon, | ||
80 | /// ; | ||
81 | Semi, | ||
82 | /// | ||
83 | Other, | ||
84 | } | ||
85 | |||
86 | impl HlTag { | ||
65 | fn as_str(self) -> &'static str { | 87 | fn as_str(self) -> &'static str { |
66 | match self { | 88 | match self { |
67 | HighlightTag::Symbol(symbol) => match symbol { | 89 | HlTag::Symbol(symbol) => match symbol { |
68 | SymbolKind::Const => "constant", | 90 | SymbolKind::Const => "constant", |
69 | SymbolKind::Static => "static", | 91 | SymbolKind::Static => "static", |
70 | SymbolKind::Enum => "enum", | 92 | SymbolKind::Enum => "enum", |
@@ -86,59 +108,69 @@ impl HighlightTag { | |||
86 | SymbolKind::SelfParam => "self_keyword", | 108 | SymbolKind::SelfParam => "self_keyword", |
87 | SymbolKind::Impl => "self_type", | 109 | SymbolKind::Impl => "self_type", |
88 | }, | 110 | }, |
89 | HighlightTag::Attribute => "attribute", | 111 | HlTag::Attribute => "attribute", |
90 | HighlightTag::BoolLiteral => "bool_literal", | 112 | HlTag::BoolLiteral => "bool_literal", |
91 | HighlightTag::BuiltinType => "builtin_type", | 113 | HlTag::BuiltinType => "builtin_type", |
92 | HighlightTag::ByteLiteral => "byte_literal", | 114 | HlTag::ByteLiteral => "byte_literal", |
93 | HighlightTag::CharLiteral => "char_literal", | 115 | HlTag::CharLiteral => "char_literal", |
94 | HighlightTag::Comment => "comment", | 116 | HlTag::Comment => "comment", |
95 | HighlightTag::EscapeSequence => "escape_sequence", | 117 | HlTag::EscapeSequence => "escape_sequence", |
96 | HighlightTag::FormatSpecifier => "format_specifier", | 118 | HlTag::FormatSpecifier => "format_specifier", |
97 | HighlightTag::Dummy => "dummy", | 119 | HlTag::Keyword => "keyword", |
98 | HighlightTag::Keyword => "keyword", | 120 | HlTag::Punctuation(punct) => match punct { |
99 | HighlightTag::Punctuation => "punctuation", | 121 | HlPunct::Bracket => "bracket", |
100 | HighlightTag::NumericLiteral => "numeric_literal", | 122 | HlPunct::Brace => "brace", |
101 | HighlightTag::Operator => "operator", | 123 | HlPunct::Parenthesis => "parenthesis", |
102 | HighlightTag::StringLiteral => "string_literal", | 124 | HlPunct::Angle => "angle", |
103 | HighlightTag::UnresolvedReference => "unresolved_reference", | 125 | HlPunct::Comma => "comma", |
126 | HlPunct::Dot => "dot", | ||
127 | HlPunct::Colon => "colon", | ||
128 | HlPunct::Semi => "semicolon", | ||
129 | HlPunct::Other => "punctuation", | ||
130 | }, | ||
131 | HlTag::NumericLiteral => "numeric_literal", | ||
132 | HlTag::Operator => "operator", | ||
133 | HlTag::StringLiteral => "string_literal", | ||
134 | HlTag::UnresolvedReference => "unresolved_reference", | ||
135 | HlTag::None => "none", | ||
104 | } | 136 | } |
105 | } | 137 | } |
106 | } | 138 | } |
107 | 139 | ||
108 | impl fmt::Display for HighlightTag { | 140 | impl fmt::Display for HlTag { |
109 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 141 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
110 | fmt::Display::fmt(self.as_str(), f) | 142 | fmt::Display::fmt(self.as_str(), f) |
111 | } | 143 | } |
112 | } | 144 | } |
113 | 145 | ||
114 | impl HighlightModifier { | 146 | impl HlMod { |
115 | const ALL: &'static [HighlightModifier; HighlightModifier::Unsafe as u8 as usize + 1] = &[ | 147 | const ALL: &'static [HlMod; HlMod::Unsafe as u8 as usize + 1] = &[ |
116 | HighlightModifier::Attribute, | 148 | HlMod::Attribute, |
117 | HighlightModifier::ControlFlow, | 149 | HlMod::ControlFlow, |
118 | HighlightModifier::Definition, | 150 | HlMod::Definition, |
119 | HighlightModifier::Documentation, | 151 | HlMod::Documentation, |
120 | HighlightModifier::Injected, | 152 | HlMod::Injected, |
121 | HighlightModifier::Mutable, | 153 | HlMod::Mutable, |
122 | HighlightModifier::Consuming, | 154 | HlMod::Consuming, |
123 | HighlightModifier::Callable, | 155 | HlMod::Callable, |
124 | HighlightModifier::Static, | 156 | HlMod::Static, |
125 | HighlightModifier::Associated, | 157 | HlMod::Associated, |
126 | HighlightModifier::Unsafe, | 158 | HlMod::Unsafe, |
127 | ]; | 159 | ]; |
128 | 160 | ||
129 | fn as_str(self) -> &'static str { | 161 | fn as_str(self) -> &'static str { |
130 | match self { | 162 | match self { |
131 | HighlightModifier::Attribute => "attribute", | 163 | HlMod::Attribute => "attribute", |
132 | HighlightModifier::ControlFlow => "control", | 164 | HlMod::ControlFlow => "control", |
133 | HighlightModifier::Definition => "declaration", | 165 | HlMod::Definition => "declaration", |
134 | HighlightModifier::Documentation => "documentation", | 166 | HlMod::Documentation => "documentation", |
135 | HighlightModifier::Injected => "injected", | 167 | HlMod::Injected => "injected", |
136 | HighlightModifier::Mutable => "mutable", | 168 | HlMod::Mutable => "mutable", |
137 | HighlightModifier::Consuming => "consuming", | 169 | HlMod::Consuming => "consuming", |
138 | HighlightModifier::Unsafe => "unsafe", | 170 | HlMod::Unsafe => "unsafe", |
139 | HighlightModifier::Callable => "callable", | 171 | HlMod::Callable => "callable", |
140 | HighlightModifier::Static => "static", | 172 | HlMod::Static => "static", |
141 | HighlightModifier::Associated => "associated", | 173 | HlMod::Associated => "associated", |
142 | } | 174 | } |
143 | } | 175 | } |
144 | 176 | ||
@@ -147,7 +179,7 @@ impl HighlightModifier { | |||
147 | } | 179 | } |
148 | } | 180 | } |
149 | 181 | ||
150 | impl fmt::Display for HighlightModifier { | 182 | impl fmt::Display for HlMod { |
151 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 183 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
152 | fmt::Display::fmt(self.as_str(), f) | 184 | fmt::Display::fmt(self.as_str(), f) |
153 | } | 185 | } |
@@ -156,60 +188,63 @@ impl fmt::Display for HighlightModifier { | |||
156 | impl fmt::Display for Highlight { | 188 | impl fmt::Display for Highlight { |
157 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 189 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
158 | write!(f, "{}", self.tag)?; | 190 | write!(f, "{}", self.tag)?; |
159 | for modifier in self.modifiers.iter() { | 191 | for modifier in self.mods.iter() { |
160 | write!(f, ".{}", modifier)? | 192 | write!(f, ".{}", modifier)? |
161 | } | 193 | } |
162 | Ok(()) | 194 | Ok(()) |
163 | } | 195 | } |
164 | } | 196 | } |
165 | 197 | ||
166 | impl From<HighlightTag> for Highlight { | 198 | impl From<HlTag> for Highlight { |
167 | fn from(tag: HighlightTag) -> Highlight { | 199 | fn from(tag: HlTag) -> Highlight { |
168 | Highlight::new(tag) | 200 | Highlight::new(tag) |
169 | } | 201 | } |
170 | } | 202 | } |
171 | 203 | ||
172 | impl Highlight { | 204 | impl Highlight { |
173 | pub(crate) fn new(tag: HighlightTag) -> Highlight { | 205 | pub(crate) fn new(tag: HlTag) -> Highlight { |
174 | Highlight { tag, modifiers: HighlightModifiers::default() } | 206 | Highlight { tag, mods: HlMods::default() } |
207 | } | ||
208 | pub fn is_empty(&self) -> bool { | ||
209 | self.tag == HlTag::None && self.mods == HlMods::default() | ||
175 | } | 210 | } |
176 | } | 211 | } |
177 | 212 | ||
178 | impl ops::BitOr<HighlightModifier> for HighlightTag { | 213 | impl ops::BitOr<HlMod> for HlTag { |
179 | type Output = Highlight; | 214 | type Output = Highlight; |
180 | 215 | ||
181 | fn bitor(self, rhs: HighlightModifier) -> Highlight { | 216 | fn bitor(self, rhs: HlMod) -> Highlight { |
182 | Highlight::new(self) | rhs | 217 | Highlight::new(self) | rhs |
183 | } | 218 | } |
184 | } | 219 | } |
185 | 220 | ||
186 | impl ops::BitOrAssign<HighlightModifier> for HighlightModifiers { | 221 | impl ops::BitOrAssign<HlMod> for HlMods { |
187 | fn bitor_assign(&mut self, rhs: HighlightModifier) { | 222 | fn bitor_assign(&mut self, rhs: HlMod) { |
188 | self.0 |= rhs.mask(); | 223 | self.0 |= rhs.mask(); |
189 | } | 224 | } |
190 | } | 225 | } |
191 | 226 | ||
192 | impl ops::BitOrAssign<HighlightModifier> for Highlight { | 227 | impl ops::BitOrAssign<HlMod> for Highlight { |
193 | fn bitor_assign(&mut self, rhs: HighlightModifier) { | 228 | fn bitor_assign(&mut self, rhs: HlMod) { |
194 | self.modifiers |= rhs; | 229 | self.mods |= rhs; |
195 | } | 230 | } |
196 | } | 231 | } |
197 | 232 | ||
198 | impl ops::BitOr<HighlightModifier> for Highlight { | 233 | impl ops::BitOr<HlMod> for Highlight { |
199 | type Output = Highlight; | 234 | type Output = Highlight; |
200 | 235 | ||
201 | fn bitor(mut self, rhs: HighlightModifier) -> Highlight { | 236 | fn bitor(mut self, rhs: HlMod) -> Highlight { |
202 | self |= rhs; | 237 | self |= rhs; |
203 | self | 238 | self |
204 | } | 239 | } |
205 | } | 240 | } |
206 | 241 | ||
207 | impl HighlightModifiers { | 242 | impl HlMods { |
208 | pub fn contains(self, m: HighlightModifier) -> bool { | 243 | pub fn contains(self, m: HlMod) -> bool { |
209 | self.0 & m.mask() == m.mask() | 244 | self.0 & m.mask() == m.mask() |
210 | } | 245 | } |
211 | 246 | ||
212 | pub fn iter(self) -> impl Iterator<Item = HighlightModifier> { | 247 | pub fn iter(self) -> impl Iterator<Item = HlMod> { |
213 | HighlightModifier::ALL.iter().copied().filter(move |it| self.0 & it.mask() == it.mask()) | 248 | HlMod::ALL.iter().copied().filter(move |it| self.0 & it.mask() == it.mask()) |
214 | } | 249 | } |
215 | } | 250 | } |