]> git.lizzy.rs Git - rust.git/blob - crates/ide/src/syntax_highlighting/highlight.rs
Merge #9267 #9279
[rust.git] / crates / ide / src / syntax_highlighting / highlight.rs
1 //! Computes color for a single element.
2
3 use hir::{AsAssocItem, Semantics};
4 use ide_db::{
5     defs::{Definition, NameClass, NameRefClass},
6     RootDatabase, SymbolKind,
7 };
8 use rustc_hash::FxHashMap;
9 use syntax::{
10     ast, AstNode, AstToken, NodeOrToken, SyntaxElement,
11     SyntaxKind::{self, *},
12     SyntaxNode, SyntaxToken, T,
13 };
14
15 use crate::{
16     syntax_highlighting::tags::{HlOperator, HlPunct},
17     Highlight, HlMod, HlTag,
18 };
19
20 pub(super) fn element(
21     sema: &Semantics<RootDatabase>,
22     krate: Option<hir::Crate>,
23     bindings_shadow_count: &mut FxHashMap<hir::Name, u32>,
24     syntactic_name_ref_highlighting: bool,
25     element: SyntaxElement,
26 ) -> Option<(Highlight, Option<u64>)> {
27     let db = sema.db;
28     let mut binding_hash = None;
29     let highlight: Highlight = match element.kind() {
30         FN => {
31             bindings_shadow_count.clear();
32             return None;
33         }
34
35         // Highlight definitions depending on the "type" of the definition.
36         NAME => {
37             let name = element.into_node().and_then(ast::Name::cast).unwrap();
38             let name_kind = NameClass::classify(sema, &name);
39
40             if let Some(NameClass::Definition(Definition::Local(local))) = &name_kind {
41                 if let Some(name) = local.name(db) {
42                     let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
43                     *shadow_count += 1;
44                     binding_hash = Some(calc_binding_hash(&name, *shadow_count))
45                 }
46             };
47
48             match name_kind {
49                 Some(NameClass::ExternCrate(_)) => SymbolKind::Module.into(),
50                 Some(NameClass::Definition(def)) => {
51                     let mut h = highlight_def(db, krate, def) | HlMod::Definition;
52                     if let Definition::ModuleDef(hir::ModuleDef::Trait(trait_)) = &def {
53                         if trait_.is_unsafe(db) {
54                             h |= HlMod::Unsafe;
55                         }
56                     }
57                     h
58                 }
59                 Some(NameClass::ConstReference(def)) => highlight_def(db, krate, def),
60                 Some(NameClass::PatFieldShorthand { field_ref, .. }) => {
61                     let mut h = HlTag::Symbol(SymbolKind::Field).into();
62                     if let Definition::Field(field) = field_ref {
63                         if let hir::VariantDef::Union(_) = field.parent_def(db) {
64                             h |= HlMod::Unsafe;
65                         }
66                     }
67                     h
68                 }
69                 None => highlight_name_by_syntax(name) | HlMod::Definition,
70             }
71         }
72         // Highlight references like the definitions they resolve to
73         NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => {
74             // even though we track whether we are in an attribute or not we still need this special case
75             // as otherwise we would emit unresolved references for name refs inside attributes
76             SymbolKind::Function.into()
77         }
78         NAME_REF => {
79             let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
80             highlight_func_by_name_ref(sema, krate, &name_ref).unwrap_or_else(|| {
81                 let is_self = name_ref.self_token().is_some();
82                 let h = match NameRefClass::classify(sema, &name_ref) {
83                     Some(name_kind) => match name_kind {
84                         NameRefClass::ExternCrate(_) => SymbolKind::Module.into(),
85                         NameRefClass::Definition(def) => {
86                             if let Definition::Local(local) = &def {
87                                 if let Some(name) = local.name(db) {
88                                     let shadow_count =
89                                         bindings_shadow_count.entry(name.clone()).or_default();
90                                     binding_hash = Some(calc_binding_hash(&name, *shadow_count))
91                                 }
92                             };
93
94                             let mut h = highlight_def(db, krate, def);
95
96                             match def {
97                                 Definition::Local(local)
98                                     if is_consumed_lvalue(
99                                         name_ref.syntax().clone().into(),
100                                         &local,
101                                         db,
102                                     ) =>
103                                 {
104                                     h |= HlMod::Consuming;
105                                 }
106                                 Definition::ModuleDef(hir::ModuleDef::Trait(trait_))
107                                     if trait_.is_unsafe(db) =>
108                                 {
109                                     if ast::Impl::for_trait_name_ref(&name_ref).is_some() {
110                                         h |= HlMod::Unsafe;
111                                     }
112                                 }
113                                 Definition::Field(field) => {
114                                     if let Some(parent) = name_ref.syntax().parent() {
115                                         if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) {
116                                             if let hir::VariantDef::Union(_) = field.parent_def(db)
117                                             {
118                                                 h |= HlMod::Unsafe;
119                                             }
120                                         }
121                                     }
122                                 }
123                                 _ => (),
124                             }
125
126                             h
127                         }
128                         NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(),
129                     },
130                     None if syntactic_name_ref_highlighting => {
131                         highlight_name_ref_by_syntax(name_ref, sema, krate)
132                     }
133                     None => HlTag::UnresolvedReference.into(),
134                 };
135                 if h.tag == HlTag::Symbol(SymbolKind::Module) && is_self {
136                     SymbolKind::SelfParam.into()
137                 } else {
138                     h
139                 }
140             })
141         }
142
143         // Simple token-based highlighting
144         COMMENT => {
145             let comment = element.into_token().and_then(ast::Comment::cast)?;
146             let h = HlTag::Comment;
147             match comment.kind().doc {
148                 Some(_) => h | HlMod::Documentation,
149                 None => h.into(),
150             }
151         }
152         STRING | BYTE_STRING => HlTag::StringLiteral.into(),
153         ATTR => HlTag::Attribute.into(),
154         INT_NUMBER if element.ancestors().nth(1).map_or(false, |it| it.kind() == FIELD_EXPR) => {
155             SymbolKind::Field.into()
156         }
157         INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(),
158         BYTE => HlTag::ByteLiteral.into(),
159         CHAR => HlTag::CharLiteral.into(),
160         QUESTION => HlTag::Operator(HlOperator::Other) | HlMod::ControlFlow,
161         LIFETIME => {
162             let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap();
163
164             match NameClass::classify_lifetime(sema, &lifetime) {
165                 Some(NameClass::Definition(def)) => {
166                     highlight_def(db, krate, def) | HlMod::Definition
167                 }
168                 None => match NameRefClass::classify_lifetime(sema, &lifetime) {
169                     Some(NameRefClass::Definition(def)) => highlight_def(db, krate, def),
170                     _ => SymbolKind::LifetimeParam.into(),
171                 },
172                 _ => Highlight::from(SymbolKind::LifetimeParam) | HlMod::Definition,
173             }
174         }
175         p if p.is_punct() => match p {
176             T![&] if parent_matches::<ast::BinExpr>(&element) => HlOperator::Bitwise.into(),
177             T![&] => {
178                 let h = HlTag::Operator(HlOperator::Other).into();
179                 let is_unsafe = element
180                     .parent()
181                     .and_then(ast::RefExpr::cast)
182                     .map_or(false, |ref_expr| sema.is_unsafe_ref_expr(&ref_expr));
183                 if is_unsafe {
184                     h | HlMod::Unsafe
185                 } else {
186                     h
187                 }
188             }
189             T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => HlOperator::Other.into(),
190             T![!] if parent_matches::<ast::MacroCall>(&element) => SymbolKind::Macro.into(),
191             T![!] if parent_matches::<ast::NeverType>(&element) => HlTag::BuiltinType.into(),
192             T![!] if parent_matches::<ast::PrefixExpr>(&element) => HlOperator::Logical.into(),
193             T![*] if parent_matches::<ast::PtrType>(&element) => HlTag::Keyword.into(),
194             T![*] if parent_matches::<ast::PrefixExpr>(&element) => {
195                 let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
196
197                 let expr = prefix_expr.expr()?;
198                 let ty = sema.type_of_expr(&expr)?;
199                 if ty.is_raw_ptr() {
200                     HlTag::Operator(HlOperator::Other) | HlMod::Unsafe
201                 } else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() {
202                     HlOperator::Other.into()
203                 } else {
204                     HlPunct::Other.into()
205                 }
206             }
207             T![-] if parent_matches::<ast::PrefixExpr>(&element) => {
208                 let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
209
210                 let expr = prefix_expr.expr()?;
211                 match expr {
212                     ast::Expr::Literal(_) => HlTag::NumericLiteral,
213                     _ => HlTag::Operator(HlOperator::Other),
214                 }
215                 .into()
216             }
217             _ if parent_matches::<ast::PrefixExpr>(&element) => HlOperator::Other.into(),
218             T![+] | T![-] | T![*] | T![/] | T![+=] | T![-=] | T![*=] | T![/=]
219                 if parent_matches::<ast::BinExpr>(&element) =>
220             {
221                 HlOperator::Arithmetic.into()
222             }
223             T![|] | T![&] | T![!] | T![^] | T![|=] | T![&=] | T![^=]
224                 if parent_matches::<ast::BinExpr>(&element) =>
225             {
226                 HlOperator::Bitwise.into()
227             }
228             T![&&] | T![||] if parent_matches::<ast::BinExpr>(&element) => {
229                 HlOperator::Logical.into()
230             }
231             T![>] | T![<] | T![==] | T![>=] | T![<=] | T![!=]
232                 if parent_matches::<ast::BinExpr>(&element) =>
233             {
234                 HlOperator::Comparison.into()
235             }
236             _ if parent_matches::<ast::BinExpr>(&element) => HlOperator::Other.into(),
237             _ if parent_matches::<ast::RangeExpr>(&element) => HlOperator::Other.into(),
238             _ if parent_matches::<ast::RangePat>(&element) => HlOperator::Other.into(),
239             _ if parent_matches::<ast::RestPat>(&element) => HlOperator::Other.into(),
240             _ if parent_matches::<ast::Attr>(&element) => HlTag::Attribute.into(),
241             kind => match kind {
242                 T!['['] | T![']'] => HlPunct::Bracket,
243                 T!['{'] | T!['}'] => HlPunct::Brace,
244                 T!['('] | T![')'] => HlPunct::Parenthesis,
245                 T![<] | T![>] => HlPunct::Angle,
246                 T![,] => HlPunct::Comma,
247                 T![:] => HlPunct::Colon,
248                 T![;] => HlPunct::Semi,
249                 T![.] => HlPunct::Dot,
250                 _ => HlPunct::Other,
251             }
252             .into(),
253         },
254
255         k if k.is_keyword() => {
256             let h = Highlight::new(HlTag::Keyword);
257             match k {
258                 T![await] => h | HlMod::Async | HlMod::ControlFlow,
259                 T![break]
260                 | T![continue]
261                 | T![else]
262                 | T![if]
263                 | T![in]
264                 | T![loop]
265                 | T![match]
266                 | T![return]
267                 | T![while]
268                 | T![yield] => h | HlMod::ControlFlow,
269                 T![for] if !is_child_of_impl(&element) => h | HlMod::ControlFlow,
270                 T![unsafe] => h | HlMod::Unsafe,
271                 T![true] | T![false] => HlTag::BoolLiteral.into(),
272                 // self is handled as either a Name or NameRef already
273                 T![self] => return None,
274                 T![ref] => element
275                     .parent()
276                     .and_then(ast::IdentPat::cast)
277                     .and_then(|ident_pat| {
278                         if sema.is_unsafe_ident_pat(&ident_pat) {
279                             Some(HlMod::Unsafe)
280                         } else {
281                             None
282                         }
283                     })
284                     .map(|modifier| h | modifier)
285                     .unwrap_or(h),
286                 T![async] => h | HlMod::Async,
287                 _ => h,
288             }
289         }
290
291         _ => return None,
292     };
293
294     return Some((highlight, binding_hash));
295
296     fn calc_binding_hash(name: &hir::Name, shadow_count: u32) -> u64 {
297         fn hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 {
298             use std::{collections::hash_map::DefaultHasher, hash::Hasher};
299
300             let mut hasher = DefaultHasher::new();
301             x.hash(&mut hasher);
302             hasher.finish()
303         }
304
305         hash((name, shadow_count))
306     }
307 }
308 fn highlight_def(db: &RootDatabase, krate: Option<hir::Crate>, def: Definition) -> Highlight {
309     let mut h = match def {
310         Definition::Macro(_) => Highlight::new(HlTag::Symbol(SymbolKind::Macro)),
311         Definition::Field(_) => Highlight::new(HlTag::Symbol(SymbolKind::Field)),
312         Definition::ModuleDef(def) => match def {
313             hir::ModuleDef::Module(_) => Highlight::new(HlTag::Symbol(SymbolKind::Module)),
314             hir::ModuleDef::Function(func) => {
315                 let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Function));
316                 if let Some(item) = func.as_assoc_item(db) {
317                     h |= HlMod::Associated;
318                     match func.self_param(db) {
319                         Some(sp) => {
320                             if let hir::Access::Exclusive = sp.access(db) {
321                                 h |= HlMod::Mutable;
322                             }
323                         }
324                         None => h |= HlMod::Static,
325                     }
326
327                     match item.container(db) {
328                         hir::AssocItemContainer::Impl(i) => {
329                             if i.trait_(db).is_some() {
330                                 h |= HlMod::Trait;
331                             }
332                         }
333                         hir::AssocItemContainer::Trait(_t) => {
334                             h |= HlMod::Trait;
335                         }
336                     }
337                 }
338
339                 if func.is_unsafe(db) {
340                     h |= HlMod::Unsafe;
341                 }
342                 if func.is_async(db) {
343                     h |= HlMod::Async;
344                 }
345
346                 h
347             }
348             hir::ModuleDef::Adt(adt) => {
349                 let h = match adt {
350                     hir::Adt::Struct(_) => HlTag::Symbol(SymbolKind::Struct),
351                     hir::Adt::Enum(_) => HlTag::Symbol(SymbolKind::Enum),
352                     hir::Adt::Union(_) => HlTag::Symbol(SymbolKind::Union),
353                 };
354
355                 Highlight::new(h)
356             }
357             hir::ModuleDef::Variant(_) => Highlight::new(HlTag::Symbol(SymbolKind::Variant)),
358             hir::ModuleDef::Const(konst) => {
359                 let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const));
360
361                 if let Some(item) = konst.as_assoc_item(db) {
362                     h |= HlMod::Associated;
363                     match item.container(db) {
364                         hir::AssocItemContainer::Impl(i) => {
365                             if i.trait_(db).is_some() {
366                                 h |= HlMod::Trait;
367                             }
368                         }
369                         hir::AssocItemContainer::Trait(_t) => {
370                             h |= HlMod::Trait;
371                         }
372                     }
373                 }
374
375                 h
376             }
377             hir::ModuleDef::Trait(_) => Highlight::new(HlTag::Symbol(SymbolKind::Trait)),
378             hir::ModuleDef::TypeAlias(type_) => {
379                 let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias));
380
381                 if let Some(item) = type_.as_assoc_item(db) {
382                     h |= HlMod::Associated;
383                     match item.container(db) {
384                         hir::AssocItemContainer::Impl(i) => {
385                             if i.trait_(db).is_some() {
386                                 h |= HlMod::Trait;
387                             }
388                         }
389                         hir::AssocItemContainer::Trait(_t) => {
390                             h |= HlMod::Trait;
391                         }
392                     }
393                 }
394
395                 h
396             }
397             hir::ModuleDef::BuiltinType(_) => Highlight::new(HlTag::BuiltinType),
398             hir::ModuleDef::Static(s) => {
399                 let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Static));
400
401                 if s.is_mut(db) {
402                     h |= HlMod::Mutable;
403                     h |= HlMod::Unsafe;
404                 }
405
406                 h
407             }
408         },
409         Definition::SelfType(_) => Highlight::new(HlTag::Symbol(SymbolKind::Impl)),
410         Definition::GenericParam(it) => match it {
411             hir::GenericParam::TypeParam(_) => Highlight::new(HlTag::Symbol(SymbolKind::TypeParam)),
412             hir::GenericParam::ConstParam(_) => {
413                 Highlight::new(HlTag::Symbol(SymbolKind::ConstParam))
414             }
415             hir::GenericParam::LifetimeParam(_) => {
416                 Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam))
417             }
418         },
419         Definition::Local(local) => {
420             let tag = if local.is_self(db) {
421                 HlTag::Symbol(SymbolKind::SelfParam)
422             } else if local.is_param(db) {
423                 HlTag::Symbol(SymbolKind::ValueParam)
424             } else {
425                 HlTag::Symbol(SymbolKind::Local)
426             };
427             let mut h = Highlight::new(tag);
428             let ty = local.ty(db);
429             if local.is_mut(db) || ty.is_mutable_reference() {
430                 h |= HlMod::Mutable;
431             }
432             if ty.as_callable(db).is_some() || ty.impls_fnonce(db) {
433                 h |= HlMod::Callable;
434             }
435             h
436         }
437         Definition::Label(_) => Highlight::new(HlTag::Symbol(SymbolKind::Label)),
438     };
439
440     let is_from_other_crate = def.module(db).map(hir::Module::krate) != krate;
441     let is_builtin_type = matches!(def, Definition::ModuleDef(hir::ModuleDef::BuiltinType(_)));
442
443     if is_from_other_crate && !is_builtin_type {
444         h |= HlMod::Library;
445     }
446
447     h
448 }
449
450 fn highlight_func_by_name_ref(
451     sema: &Semantics<RootDatabase>,
452     krate: Option<hir::Crate>,
453     name_ref: &ast::NameRef,
454 ) -> Option<Highlight> {
455     let mc = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
456     highlight_method_call(sema, krate, &mc)
457 }
458
459 fn highlight_method_call(
460     sema: &Semantics<RootDatabase>,
461     krate: Option<hir::Crate>,
462     method_call: &ast::MethodCallExpr,
463 ) -> Option<Highlight> {
464     let func = sema.resolve_method_call(method_call)?;
465
466     let mut h = SymbolKind::Function.into();
467     h |= HlMod::Associated;
468
469     if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(method_call) {
470         h |= HlMod::Unsafe;
471     }
472     if func.is_async(sema.db) {
473         h |= HlMod::Async;
474     }
475     if func.as_assoc_item(sema.db).and_then(|it| it.containing_trait(sema.db)).is_some() {
476         h |= HlMod::Trait;
477     }
478     if Some(func.module(sema.db).krate()) != krate {
479         h |= HlMod::Library;
480     }
481
482     if let Some(self_param) = func.self_param(sema.db) {
483         match self_param.access(sema.db) {
484             hir::Access::Shared => (),
485             hir::Access::Exclusive => h |= HlMod::Mutable,
486             hir::Access::Owned => {
487                 if let Some(receiver_ty) =
488                     method_call.receiver().and_then(|it| sema.type_of_expr(&it))
489                 {
490                     if !receiver_ty.is_copy(sema.db) {
491                         h |= HlMod::Consuming
492                     }
493                 }
494             }
495         }
496     }
497     Some(h)
498 }
499
500 fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
501     let default = HlTag::UnresolvedReference;
502
503     let parent = match name.syntax().parent() {
504         Some(it) => it,
505         _ => return default.into(),
506     };
507
508     let tag = match parent.kind() {
509         STRUCT => SymbolKind::Struct,
510         ENUM => SymbolKind::Enum,
511         VARIANT => SymbolKind::Variant,
512         UNION => SymbolKind::Union,
513         TRAIT => SymbolKind::Trait,
514         TYPE_ALIAS => SymbolKind::TypeAlias,
515         TYPE_PARAM => SymbolKind::TypeParam,
516         RECORD_FIELD => SymbolKind::Field,
517         MODULE => SymbolKind::Module,
518         FN => SymbolKind::Function,
519         CONST => SymbolKind::Const,
520         STATIC => SymbolKind::Static,
521         IDENT_PAT => SymbolKind::Local,
522         _ => return default.into(),
523     };
524
525     tag.into()
526 }
527
528 fn highlight_name_ref_by_syntax(
529     name: ast::NameRef,
530     sema: &Semantics<RootDatabase>,
531     krate: Option<hir::Crate>,
532 ) -> Highlight {
533     let default = HlTag::UnresolvedReference;
534
535     let parent = match name.syntax().parent() {
536         Some(it) => it,
537         _ => return default.into(),
538     };
539
540     match parent.kind() {
541         METHOD_CALL_EXPR => ast::MethodCallExpr::cast(parent)
542             .and_then(|it| highlight_method_call(sema, krate, &it))
543             .unwrap_or_else(|| SymbolKind::Function.into()),
544         FIELD_EXPR => {
545             let h = HlTag::Symbol(SymbolKind::Field);
546             let is_union = ast::FieldExpr::cast(parent)
547                 .and_then(|field_expr| sema.resolve_field(&field_expr))
548                 .map_or(false, |field| {
549                     matches!(field.parent_def(sema.db), hir::VariantDef::Union(_))
550                 });
551             if is_union {
552                 h | HlMod::Unsafe
553             } else {
554                 h.into()
555             }
556         }
557         PATH_SEGMENT => {
558             let path = match parent.parent().and_then(ast::Path::cast) {
559                 Some(it) => it,
560                 _ => return default.into(),
561             };
562             let expr = match path.syntax().parent().and_then(ast::PathExpr::cast) {
563                 Some(it) => it,
564                 _ => {
565                     // within path, decide whether it is module or adt by checking for uppercase name
566                     return if name.text().chars().next().unwrap_or_default().is_uppercase() {
567                         SymbolKind::Struct
568                     } else {
569                         SymbolKind::Module
570                     }
571                     .into();
572                 }
573             };
574             let parent = match expr.syntax().parent() {
575                 Some(it) => it,
576                 None => return default.into(),
577             };
578
579             match parent.kind() {
580                 CALL_EXPR => SymbolKind::Function.into(),
581                 _ => if name.text().chars().next().unwrap_or_default().is_uppercase() {
582                     SymbolKind::Struct
583                 } else {
584                     SymbolKind::Const
585                 }
586                 .into(),
587             }
588         }
589         _ => default.into(),
590     }
591 }
592
593 fn is_consumed_lvalue(
594     node: NodeOrToken<SyntaxNode, SyntaxToken>,
595     local: &hir::Local,
596     db: &RootDatabase,
597 ) -> bool {
598     // When lvalues are passed as arguments and they're not Copy, then mark them as Consuming.
599     parents_match(node, &[PATH_SEGMENT, PATH, PATH_EXPR, ARG_LIST]) && !local.ty(db).is_copy(db)
600 }
601
602 /// Returns true if the parent nodes of `node` all match the `SyntaxKind`s in `kinds` exactly.
603 fn parents_match(mut node: NodeOrToken<SyntaxNode, SyntaxToken>, mut kinds: &[SyntaxKind]) -> bool {
604     while let (Some(parent), [kind, rest @ ..]) = (&node.parent(), kinds) {
605         if parent.kind() != *kind {
606             return false;
607         }
608
609         // FIXME: Would be nice to get parent out of the match, but binding by-move and by-value
610         // in the same pattern is unstable: rust-lang/rust#68354.
611         node = node.parent().unwrap().into();
612         kinds = rest;
613     }
614
615     // Only true if we matched all expected kinds
616     kinds.len() == 0
617 }
618
619 #[inline]
620 fn parent_matches<N: AstNode>(element: &SyntaxElement) -> bool {
621     element.parent().map_or(false, |it| N::can_cast(it.kind()))
622 }
623
624 fn is_child_of_impl(element: &SyntaxElement) -> bool {
625     match element.parent() {
626         Some(e) => e.kind() == IMPL,
627         _ => false,
628     }
629 }