1 //! Defines token tags we use for syntax highlighting.
2 //! A tag is not unlike a CSS class.
9 use ide_db::SymbolKind;
11 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
12 pub struct Highlight {
17 #[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
18 pub struct HlMods(u32);
20 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
39 // For things which don't have a specific highlight.
43 // Don't forget to adjust the feature description in crates/ide/src/syntax_highlighting.rs.
44 // And make sure to use the lsp strings used when converting to the protocol in crates\rust-analyzer\src\semantic_tokens.rs, not the names of the variants here.
45 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
48 /// Used for items in traits and impls.
50 /// Used with keywords like `async` and `await`.
52 /// Used to differentiate individual elements within attributes.
54 /// Callable item or value.
56 /// Value that is being consumed in a function call
58 /// Used with keywords like `if` and `break`.
60 /// Used for crate names, like `serde`.
62 /// Used for items from built-in crates (std, core, alloc, test and proc_macro).
64 /// `foo` in `fn foo(x: i32)` is a definition, `foo` in `foo(90 + 2)` is
67 /// Doc-strings like this one.
69 /// Highlighting injection like rust code in doc strings or ra_fixture.
71 /// Used for intra doc links in doc injection.
73 /// Used for items from other crates.
77 /// Used for public items.
79 /// Immutable reference.
81 /// Used for associated functions.
83 /// Used for items in traits and trait impls.
86 /// Used for unsafe functions, unsafe traits, mutable statics, union accesses and unsafe operations.
90 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
108 /// ! (only for macro calls)
114 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
115 pub enum HlOperator {
116 /// |, &, !, ^, |=, &=, ^=
118 /// +, -, *, /, +=, -=, *=, /=
122 /// >, <, ==, >=, <=, !=
129 fn as_str(self) -> &'static str {
131 HlTag::Symbol(symbol) => match symbol {
132 SymbolKind::Attribute => "attribute",
133 SymbolKind::BuiltinAttr => "builtin_attr",
134 SymbolKind::Const => "constant",
135 SymbolKind::ConstParam => "const_param",
136 SymbolKind::Derive => "derive",
137 SymbolKind::Enum => "enum",
138 SymbolKind::Field => "field",
139 SymbolKind::Function => "function",
140 SymbolKind::Impl => "self_type",
141 SymbolKind::Label => "label",
142 SymbolKind::LifetimeParam => "lifetime",
143 SymbolKind::Local => "variable",
144 SymbolKind::Macro => "macro",
145 SymbolKind::Module => "module",
146 SymbolKind::SelfParam => "self_keyword",
147 SymbolKind::SelfType => "self_type_keyword",
148 SymbolKind::Static => "static",
149 SymbolKind::Struct => "struct",
150 SymbolKind::ToolModule => "tool_module",
151 SymbolKind::Trait => "trait",
152 SymbolKind::TypeAlias => "type_alias",
153 SymbolKind::TypeParam => "type_param",
154 SymbolKind::Union => "union",
155 SymbolKind::ValueParam => "value_param",
156 SymbolKind::Variant => "enum_variant",
158 HlTag::AttributeBracket => "attribute_bracket",
159 HlTag::BoolLiteral => "bool_literal",
160 HlTag::BuiltinType => "builtin_type",
161 HlTag::ByteLiteral => "byte_literal",
162 HlTag::CharLiteral => "char_literal",
163 HlTag::Comment => "comment",
164 HlTag::EscapeSequence => "escape_sequence",
165 HlTag::FormatSpecifier => "format_specifier",
166 HlTag::Keyword => "keyword",
167 HlTag::Punctuation(punct) => match punct {
168 HlPunct::Bracket => "bracket",
169 HlPunct::Brace => "brace",
170 HlPunct::Parenthesis => "parenthesis",
171 HlPunct::Angle => "angle",
172 HlPunct::Comma => "comma",
173 HlPunct::Dot => "dot",
174 HlPunct::Colon => "colon",
175 HlPunct::Semi => "semicolon",
176 HlPunct::MacroBang => "macro_bang",
177 HlPunct::Other => "punctuation",
179 HlTag::NumericLiteral => "numeric_literal",
180 HlTag::Operator(op) => match op {
181 HlOperator::Bitwise => "bitwise",
182 HlOperator::Arithmetic => "arithmetic",
183 HlOperator::Logical => "logical",
184 HlOperator::Comparison => "comparison",
185 HlOperator::Other => "operator",
187 HlTag::StringLiteral => "string_literal",
188 HlTag::UnresolvedReference => "unresolved_reference",
189 HlTag::None => "none",
194 impl fmt::Display for HlTag {
195 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
196 fmt::Display::fmt(self.as_str(), f)
201 const ALL: &'static [HlMod; HlMod::Unsafe as u8 as usize + 1] = &[
209 HlMod::DefaultLibrary,
211 HlMod::Documentation,
223 fn as_str(self) -> &'static str {
225 HlMod::Associated => "associated",
226 HlMod::Async => "async",
227 HlMod::Attribute => "attribute",
228 HlMod::Callable => "callable",
229 HlMod::Consuming => "consuming",
230 HlMod::ControlFlow => "control",
231 HlMod::CrateRoot => "crate_root",
232 HlMod::DefaultLibrary => "default_library",
233 HlMod::Definition => "declaration",
234 HlMod::Documentation => "documentation",
235 HlMod::Injected => "injected",
236 HlMod::IntraDocLink => "intra_doc_link",
237 HlMod::Library => "library",
238 HlMod::Mutable => "mutable",
239 HlMod::Public => "public",
240 HlMod::Reference => "reference",
241 HlMod::Static => "static",
242 HlMod::Trait => "trait",
243 HlMod::Unsafe => "unsafe",
247 fn mask(self) -> u32 {
252 impl fmt::Display for HlMod {
253 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
254 fmt::Display::fmt(self.as_str(), f)
258 impl fmt::Display for Highlight {
259 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
261 for modifier in self.mods.iter() {
269 impl From<HlTag> for Highlight {
270 fn from(tag: HlTag) -> Highlight {
275 impl From<HlOperator> for Highlight {
276 fn from(op: HlOperator) -> Highlight {
277 Highlight::new(HlTag::Operator(op))
281 impl From<HlPunct> for Highlight {
282 fn from(punct: HlPunct) -> Highlight {
283 Highlight::new(HlTag::Punctuation(punct))
287 impl From<SymbolKind> for Highlight {
288 fn from(sym: SymbolKind) -> Highlight {
289 Highlight::new(HlTag::Symbol(sym))
294 pub(crate) fn new(tag: HlTag) -> Highlight {
295 Highlight { tag, mods: HlMods::default() }
297 pub fn is_empty(&self) -> bool {
298 self.tag == HlTag::None && self.mods == HlMods::default()
302 impl ops::BitOr<HlMod> for HlTag {
303 type Output = Highlight;
305 fn bitor(self, rhs: HlMod) -> Highlight {
306 Highlight::new(self) | rhs
310 impl ops::BitOrAssign<HlMod> for HlMods {
311 fn bitor_assign(&mut self, rhs: HlMod) {
312 self.0 |= rhs.mask();
316 impl ops::BitOrAssign<HlMod> for Highlight {
317 fn bitor_assign(&mut self, rhs: HlMod) {
322 impl ops::BitOr<HlMod> for Highlight {
323 type Output = Highlight;
325 fn bitor(mut self, rhs: HlMod) -> Highlight {
332 pub fn contains(self, m: HlMod) -> bool {
333 self.0 & m.mask() == m.mask()
336 pub fn iter(self) -> impl Iterator<Item = HlMod> {
337 HlMod::ALL.iter().copied().filter(move |it| self.0 & it.mask() == it.mask())