ide/syntax_highlighting/
highlight.rs

1//! Computes color for a single element.
2
3use std::ops::ControlFlow;
4
5use either::Either;
6use hir::{AsAssocItem, HasAttrs, HasVisibility, Semantics};
7use ide_db::{
8    RootDatabase, SymbolKind,
9    defs::{Definition, IdentClass, NameClass, NameRefClass},
10    syntax_helpers::node_ext::walk_pat,
11};
12use span::Edition;
13use syntax::{
14    AstNode, AstPtr, AstToken, NodeOrToken,
15    SyntaxKind::{self, *},
16    SyntaxNode, SyntaxNodePtr, SyntaxToken, T, ast, match_ast,
17};
18
19use crate::{
20    Highlight, HlMod, HlTag,
21    syntax_highlighting::tags::{HlOperator, HlPunct},
22};
23
24pub(super) fn token(
25    sema: &Semantics<'_, RootDatabase>,
26    token: SyntaxToken,
27    edition: Edition,
28    is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
29    in_tt: bool,
30) -> Option<Highlight> {
31    if let Some(comment) = ast::Comment::cast(token.clone()) {
32        let h = HlTag::Comment;
33        return Some(match comment.kind().doc {
34            Some(_) => h | HlMod::Documentation,
35            None => h.into(),
36        });
37    }
38
39    let h = match token.kind() {
40        STRING | BYTE_STRING | C_STRING => HlTag::StringLiteral.into(),
41        INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(),
42        BYTE => HlTag::ByteLiteral.into(),
43        CHAR => HlTag::CharLiteral.into(),
44        IDENT if in_tt => {
45            // from this point on we are inside a token tree, this only happens for identifiers
46            // that were not mapped down into macro invocations
47            HlTag::None.into()
48        }
49        p if p.is_punct() => punctuation(sema, token, p, is_unsafe_node),
50        k if k.is_keyword(edition) => {
51            if in_tt && token.prev_token().is_some_and(|t| t.kind() == T![$]) {
52                // we are likely within a macro definition where our keyword is a fragment name
53                HlTag::None.into()
54            } else {
55                keyword(token, k)
56            }
57        }
58        _ => return None,
59    };
60    Some(h)
61}
62
63pub(super) fn name_like(
64    sema: &Semantics<'_, RootDatabase>,
65    krate: Option<hir::Crate>,
66    is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
67    syntactic_name_ref_highlighting: bool,
68    name_like: ast::NameLike,
69    edition: Edition,
70) -> Option<(Highlight, Option<u64>)> {
71    let mut binding_hash = None;
72    let highlight = match name_like {
73        ast::NameLike::NameRef(name_ref) => highlight_name_ref(
74            sema,
75            krate,
76            &mut binding_hash,
77            is_unsafe_node,
78            syntactic_name_ref_highlighting,
79            name_ref,
80            edition,
81        ),
82        ast::NameLike::Name(name) => {
83            highlight_name(sema, &mut binding_hash, is_unsafe_node, krate, name, edition)
84        }
85        ast::NameLike::Lifetime(lifetime) => match IdentClass::classify_lifetime(sema, &lifetime) {
86            Some(IdentClass::NameClass(NameClass::Definition(def))) => {
87                highlight_def(sema, krate, def, edition, false) | HlMod::Definition
88            }
89            Some(IdentClass::NameRefClass(NameRefClass::Definition(def, _))) => {
90                highlight_def(sema, krate, def, edition, true)
91            }
92            // FIXME: Fallback for '_, as we do not resolve these yet
93            _ => SymbolKind::LifetimeParam.into(),
94        },
95    };
96    Some((highlight, binding_hash))
97}
98
99fn punctuation(
100    sema: &Semantics<'_, RootDatabase>,
101    token: SyntaxToken,
102    kind: SyntaxKind,
103    is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
104) -> Highlight {
105    let operator_parent = token.parent();
106    let parent_kind = operator_parent.as_ref().map_or(EOF, SyntaxNode::kind);
107
108    match (kind, parent_kind) {
109        (T![?], TRY_EXPR) => HlTag::Operator(HlOperator::Other) | HlMod::ControlFlow,
110        (T![&], BIN_EXPR) => HlOperator::Bitwise.into(),
111        (T![&], REF_EXPR | REF_PAT) => HlTag::Operator(HlOperator::Other).into(),
112        (T![..] | T![..=], _) => match token.parent().and_then(ast::Pat::cast) {
113            Some(pat) if is_unsafe_node(AstPtr::new(&pat).wrap_right()) => {
114                Highlight::from(HlOperator::Other) | HlMod::Unsafe
115            }
116            _ => HlOperator::Other.into(),
117        },
118        (T![::] | T![->] | T![=>] | T![=] | T![@] | T![.], _) => HlOperator::Other.into(),
119        (T![!], MACRO_CALL) => {
120            if operator_parent
121                .and_then(ast::MacroCall::cast)
122                .is_some_and(|macro_call| sema.is_unsafe_macro_call(&macro_call))
123            {
124                Highlight::from(HlPunct::MacroBang) | HlMod::Unsafe
125            } else {
126                HlPunct::MacroBang.into()
127            }
128        }
129        (T![!], MACRO_RULES) => HlPunct::MacroBang.into(),
130        (T![!], NEVER_TYPE) => HlTag::BuiltinType.into(),
131        (T![!], PREFIX_EXPR) => HlOperator::Negation.into(),
132        (T![*], PTR_TYPE) => HlTag::Keyword.into(),
133        (T![*], PREFIX_EXPR) => {
134            let h = HlTag::Operator(HlOperator::Other).into();
135            let ptr = operator_parent
136                .as_ref()
137                .and_then(|it| AstPtr::try_from_raw(SyntaxNodePtr::new(it)));
138            if ptr.is_some_and(is_unsafe_node) { h | HlMod::Unsafe } else { h }
139        }
140        (T![-], PREFIX_EXPR) => {
141            let prefix_expr =
142                operator_parent.and_then(ast::PrefixExpr::cast).and_then(|e| e.expr());
143            match prefix_expr {
144                Some(ast::Expr::Literal(_)) => HlTag::NumericLiteral,
145                _ => HlTag::Operator(HlOperator::Other),
146            }
147            .into()
148        }
149        (T![+] | T![-] | T![*] | T![/] | T![%], BIN_EXPR) => HlOperator::Arithmetic.into(),
150        (T![+=] | T![-=] | T![*=] | T![/=] | T![%=], BIN_EXPR) => {
151            Highlight::from(HlOperator::Arithmetic) | HlMod::Mutable
152        }
153        (T![|] | T![&] | T![^] | T![>>] | T![<<], BIN_EXPR) => HlOperator::Bitwise.into(),
154        (T![|=] | T![&=] | T![^=] | T![>>=] | T![<<=], BIN_EXPR) => {
155            Highlight::from(HlOperator::Bitwise) | HlMod::Mutable
156        }
157        (T![&&] | T![||], BIN_EXPR) => HlOperator::Logical.into(),
158        (T![>] | T![<] | T![==] | T![>=] | T![<=] | T![!=], BIN_EXPR) => {
159            HlOperator::Comparison.into()
160        }
161        (_, ATTR) => HlTag::AttributeBracket.into(),
162        (T![>], _)
163            if operator_parent
164                .as_ref()
165                .and_then(SyntaxNode::parent)
166                .is_some_and(|it| it.kind() == MACRO_RULES) =>
167        {
168            HlOperator::Other.into()
169        }
170        (kind, _) => match kind {
171            T!['['] | T![']'] => {
172                let is_unsafe_macro = operator_parent
173                    .as_ref()
174                    .and_then(|it| ast::TokenTree::cast(it.clone())?.syntax().parent())
175                    .and_then(ast::MacroCall::cast)
176                    .is_some_and(|macro_call| sema.is_unsafe_macro_call(&macro_call));
177                let is_unsafe = is_unsafe_macro
178                    || operator_parent
179                        .as_ref()
180                        .and_then(|it| AstPtr::try_from_raw(SyntaxNodePtr::new(it)))
181                        .is_some_and(is_unsafe_node);
182                if is_unsafe {
183                    return Highlight::from(HlPunct::Bracket) | HlMod::Unsafe;
184                } else {
185                    HlPunct::Bracket
186                }
187            }
188            T!['{'] | T!['}'] => {
189                let is_unsafe_macro = operator_parent
190                    .as_ref()
191                    .and_then(|it| ast::TokenTree::cast(it.clone())?.syntax().parent())
192                    .and_then(ast::MacroCall::cast)
193                    .is_some_and(|macro_call| sema.is_unsafe_macro_call(&macro_call));
194                let is_unsafe = is_unsafe_macro
195                    || operator_parent
196                        .as_ref()
197                        .and_then(|it| AstPtr::try_from_raw(SyntaxNodePtr::new(it)))
198                        .is_some_and(is_unsafe_node);
199                if is_unsafe {
200                    return Highlight::from(HlPunct::Brace) | HlMod::Unsafe;
201                } else {
202                    HlPunct::Brace
203                }
204            }
205            T!['('] | T![')'] => {
206                let is_unsafe_macro = operator_parent
207                    .as_ref()
208                    .and_then(|it| ast::TokenTree::cast(it.clone())?.syntax().parent())
209                    .and_then(ast::MacroCall::cast)
210                    .is_some_and(|macro_call| sema.is_unsafe_macro_call(&macro_call));
211                let is_unsafe = is_unsafe_macro
212                    || operator_parent
213                        .and_then(|it| {
214                            if ast::ArgList::can_cast(it.kind()) { it.parent() } else { Some(it) }
215                        })
216                        .and_then(|it| AstPtr::try_from_raw(SyntaxNodePtr::new(&it)))
217                        .is_some_and(is_unsafe_node);
218
219                if is_unsafe {
220                    return Highlight::from(HlPunct::Parenthesis) | HlMod::Unsafe;
221                } else {
222                    HlPunct::Parenthesis
223                }
224            }
225            T![<] | T![>] => HlPunct::Angle,
226            // Early return as otherwise we'd highlight these in
227            // asm expressions
228            T![,] => return HlPunct::Comma.into(),
229            T![:] => HlPunct::Colon,
230            T![;] => HlPunct::Semi,
231            T![.] => HlPunct::Dot,
232            _ => HlPunct::Other,
233        }
234        .into(),
235    }
236}
237
238fn keyword(token: SyntaxToken, kind: SyntaxKind) -> Highlight {
239    let h = Highlight::new(HlTag::Keyword);
240    match kind {
241        T![await] => h | HlMod::Async | HlMod::ControlFlow,
242        T![async] => h | HlMod::Async,
243        T![break]
244        | T![continue]
245        | T![else]
246        | T![if]
247        | T![in]
248        | T![loop]
249        | T![match]
250        | T![return]
251        | T![while]
252        | T![yield] => h | HlMod::ControlFlow,
253        T![do] | T![yeet] if parent_matches::<ast::YeetExpr>(&token) => h | HlMod::ControlFlow,
254        T![for] if parent_matches::<ast::ForExpr>(&token) => h | HlMod::ControlFlow,
255        T![unsafe] => h | HlMod::Unsafe,
256        T![const] => h | HlMod::Const,
257        T![true] | T![false] => HlTag::BoolLiteral.into(),
258        // crate is handled just as a token if it's in an `extern crate`
259        T![crate] if parent_matches::<ast::ExternCrate>(&token) => h,
260        _ => h,
261    }
262}
263
264fn highlight_name_ref(
265    sema: &Semantics<'_, RootDatabase>,
266    krate: Option<hir::Crate>,
267    binding_hash: &mut Option<u64>,
268    is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
269    syntactic_name_ref_highlighting: bool,
270    name_ref: ast::NameRef,
271    edition: Edition,
272) -> Highlight {
273    let db = sema.db;
274    if let Some(res) = highlight_method_call_by_name_ref(sema, krate, &name_ref, is_unsafe_node) {
275        return res;
276    }
277
278    let name_class = match NameRefClass::classify(sema, &name_ref) {
279        Some(name_kind) => name_kind,
280        None if syntactic_name_ref_highlighting => {
281            return highlight_name_ref_by_syntax(name_ref, sema, krate, is_unsafe_node);
282        }
283        // FIXME: This is required for helper attributes used by proc-macros, as those do not map down
284        // to anything when used.
285        // We can fix this for derive attributes since derive helpers are recorded, but not for
286        // general attributes.
287        None if name_ref.syntax().ancestors().any(|it| it.kind() == ATTR)
288            && !sema
289                .hir_file_for(name_ref.syntax())
290                .macro_file()
291                .is_some_and(|it| it.is_derive_attr_pseudo_expansion(sema.db)) =>
292        {
293            return HlTag::Symbol(SymbolKind::Attribute).into();
294        }
295        None => return HlTag::UnresolvedReference.into(),
296    };
297    let mut h = match name_class {
298        NameRefClass::Definition(def, _) => {
299            if let Definition::Local(local) = &def {
300                *binding_hash = Some(local.as_id() as u64);
301            };
302
303            let mut h = highlight_def(sema, krate, def, edition, true);
304
305            match def {
306                Definition::Local(local) if is_consumed_lvalue(name_ref.syntax(), &local, db) => {
307                    h |= HlMod::Consuming;
308                }
309                // highlight unsafe traits as unsafe only in their implementations
310                Definition::Trait(trait_) if trait_.is_unsafe(db) => {
311                    if ast::Impl::for_trait_name_ref(&name_ref)
312                        .is_some_and(|impl_| impl_.unsafe_token().is_some())
313                    {
314                        h |= HlMod::Unsafe;
315                    }
316                }
317                Definition::Function(_) => {
318                    let is_unsafe = name_ref
319                        .syntax()
320                        .parent()
321                        .and_then(|it| ast::PathSegment::cast(it)?.parent_path().syntax().parent())
322                        .and_then(ast::PathExpr::cast)
323                        .and_then(|it| it.syntax().parent())
324                        .and_then(ast::CallExpr::cast)
325                        .is_some_and(|it| {
326                            is_unsafe_node(AstPtr::new(&ast::Expr::CallExpr(it)).wrap_left())
327                        });
328                    if is_unsafe {
329                        h |= HlMod::Unsafe;
330                    }
331                }
332                Definition::Macro(_) => {
333                    let is_unsafe = name_ref
334                        .syntax()
335                        .parent()
336                        .and_then(|it| ast::PathSegment::cast(it)?.parent_path().syntax().parent())
337                        .and_then(ast::MacroCall::cast)
338                        .is_some_and(|macro_call| sema.is_unsafe_macro_call(&macro_call));
339                    if is_unsafe {
340                        h |= HlMod::Unsafe;
341                    }
342                }
343                Definition::Field(_) => {
344                    let is_unsafe = name_ref
345                        .syntax()
346                        .parent()
347                        .and_then(|it| {
348                            match_ast! { match it {
349                                ast::FieldExpr(expr) => Some(is_unsafe_node(AstPtr::new(&Either::Left(expr.into())))),
350                                ast::RecordPatField(pat) => {
351                                    walk_pat(&pat.pat()?, &mut |pat| {
352                                        if is_unsafe_node(AstPtr::new(&Either::Right(pat))) {
353                                            ControlFlow::Break(true)
354                                        }
355                                         else {ControlFlow::Continue(())}
356                                    }).break_value()
357                                },
358                                _ => None,
359                            }}
360                        })
361                        .unwrap_or(false);
362                    if is_unsafe {
363                        h |= HlMod::Unsafe;
364                    }
365                }
366                Definition::Static(_) => {
367                    let is_unsafe = name_ref
368                        .syntax()
369                        .parent()
370                        .and_then(|it| ast::PathSegment::cast(it)?.parent_path().syntax().parent())
371                        .and_then(ast::PathExpr::cast)
372                        .is_some_and(|it| {
373                            is_unsafe_node(AstPtr::new(&ast::Expr::PathExpr(it)).wrap_left())
374                        });
375                    if is_unsafe {
376                        h |= HlMod::Unsafe;
377                    }
378                }
379                _ => (),
380            }
381
382            h
383        }
384        NameRefClass::FieldShorthand { field_ref, .. } => {
385            highlight_def(sema, krate, field_ref.into(), edition, true)
386        }
387        NameRefClass::ExternCrateShorthand { decl, krate: resolved_krate } => {
388            let mut h = HlTag::Symbol(SymbolKind::CrateRoot).into();
389
390            if krate.as_ref().is_some_and(|krate| resolved_krate != *krate) {
391                h |= HlMod::Library;
392            }
393
394            let is_public = decl.visibility(db) == hir::Visibility::Public;
395            if is_public {
396                h |= HlMod::Public
397            }
398            let is_from_builtin_crate = resolved_krate.is_builtin(db);
399            if is_from_builtin_crate {
400                h |= HlMod::DefaultLibrary;
401            }
402            let is_deprecated = resolved_krate.attrs(sema.db).is_deprecated();
403            if is_deprecated {
404                h |= HlMod::Deprecated;
405            }
406            h
407        }
408    };
409
410    h.tag = match name_ref.token_kind() {
411        T![Self] => HlTag::Symbol(SymbolKind::SelfType),
412        T![self] => HlTag::Symbol(SymbolKind::SelfParam),
413        T![super] | T![crate] => HlTag::Keyword,
414        _ => h.tag,
415    };
416    h
417}
418
419fn highlight_name(
420    sema: &Semantics<'_, RootDatabase>,
421    binding_hash: &mut Option<u64>,
422    is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
423    krate: Option<hir::Crate>,
424    name: ast::Name,
425    edition: Edition,
426) -> Highlight {
427    let name_kind = NameClass::classify(sema, &name);
428    if let Some(NameClass::Definition(Definition::Local(local))) = &name_kind {
429        *binding_hash = Some(local.as_id() as u64);
430    };
431    match name_kind {
432        Some(NameClass::Definition(def)) => {
433            let mut h = highlight_def(sema, krate, def, edition, false) | HlMod::Definition;
434            if let Definition::Trait(trait_) = &def
435                && trait_.is_unsafe(sema.db)
436            {
437                h |= HlMod::Unsafe;
438            }
439            h
440        }
441        Some(NameClass::ConstReference(def)) => highlight_def(sema, krate, def, edition, true),
442        Some(NameClass::PatFieldShorthand { .. }) => {
443            let mut h = HlTag::Symbol(SymbolKind::Field).into();
444            let is_unsafe =
445                name.syntax().parent().and_then(ast::IdentPat::cast).is_some_and(|it| {
446                    is_unsafe_node(AstPtr::new(&ast::Pat::IdentPat(it)).wrap_right())
447                });
448            if is_unsafe {
449                h |= HlMod::Unsafe;
450            }
451            h
452        }
453        None => highlight_name_by_syntax(name) | HlMod::Definition,
454    }
455}
456
457pub(super) fn highlight_def(
458    sema: &Semantics<'_, RootDatabase>,
459    krate: Option<hir::Crate>,
460    def: Definition,
461    edition: Edition,
462    is_ref: bool,
463) -> Highlight {
464    let db = sema.db;
465    let (mut h, attrs) = match def {
466        Definition::Macro(m) => {
467            (Highlight::new(HlTag::Symbol(m.kind(sema.db).into())), Some(m.attrs(sema.db)))
468        }
469        Definition::Field(field) => {
470            (Highlight::new(HlTag::Symbol(SymbolKind::Field)), Some(field.attrs(sema.db)))
471        }
472        Definition::TupleField(_) => (Highlight::new(HlTag::Symbol(SymbolKind::Field)), None),
473        Definition::Crate(krate) => {
474            (Highlight::new(HlTag::Symbol(SymbolKind::CrateRoot)), Some(krate.attrs(sema.db)))
475        }
476        Definition::Module(module) => {
477            let h = Highlight::new(HlTag::Symbol(if module.is_crate_root(db) {
478                SymbolKind::CrateRoot
479            } else {
480                SymbolKind::Module
481            }));
482            (h, Some(module.attrs(sema.db)))
483        }
484        Definition::Function(func) => {
485            let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Function));
486            if let Some(item) = func.as_assoc_item(db) {
487                h |= HlMod::Associated;
488                match func.self_param(db) {
489                    Some(sp) => {
490                        h.tag = HlTag::Symbol(SymbolKind::Method);
491                        match sp.access(db) {
492                            hir::Access::Exclusive => {
493                                h |= HlMod::Mutable;
494                                h |= HlMod::Reference;
495                            }
496                            hir::Access::Shared => h |= HlMod::Reference,
497                            hir::Access::Owned => h |= HlMod::Consuming,
498                        }
499                    }
500                    None => h |= HlMod::Static,
501                }
502
503                match item.container(db) {
504                    hir::AssocItemContainer::Impl(i) => {
505                        if i.trait_(db).is_some() {
506                            h |= HlMod::Trait;
507                        }
508                    }
509                    hir::AssocItemContainer::Trait(_t) => {
510                        h |= HlMod::Trait;
511                    }
512                }
513            }
514
515            // FIXME: Passing `None` here means not-unsafe functions with `#[target_feature]` will be
516            // highlighted as unsafe, even when the current target features set is a superset (RFC 2396).
517            // We probably should consider checking the current function, but I found no easy way to do
518            // that (also I'm worried about perf). There's also an instance below.
519            // FIXME: This should be the edition of the call.
520            if !is_ref && func.is_unsafe_to_call(db, None, edition) {
521                h |= HlMod::Unsafe;
522            }
523            if func.is_async(db) {
524                h |= HlMod::Async;
525            }
526            if func.is_const(db) {
527                h |= HlMod::Const;
528            }
529
530            (h, Some(func.attrs(sema.db)))
531        }
532        Definition::Adt(adt) => {
533            let h = match adt {
534                hir::Adt::Struct(_) => HlTag::Symbol(SymbolKind::Struct),
535                hir::Adt::Enum(_) => HlTag::Symbol(SymbolKind::Enum),
536                hir::Adt::Union(_) => HlTag::Symbol(SymbolKind::Union),
537            };
538
539            (Highlight::new(h), Some(adt.attrs(sema.db)))
540        }
541        Definition::Variant(variant) => {
542            (Highlight::new(HlTag::Symbol(SymbolKind::Variant)), Some(variant.attrs(sema.db)))
543        }
544        Definition::Const(konst) => {
545            let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const)) | HlMod::Const;
546            if let Some(item) = konst.as_assoc_item(db) {
547                h |= HlMod::Associated;
548                h |= HlMod::Static;
549                match item.container(db) {
550                    hir::AssocItemContainer::Impl(i) => {
551                        if i.trait_(db).is_some() {
552                            h |= HlMod::Trait;
553                        }
554                    }
555                    hir::AssocItemContainer::Trait(_t) => {
556                        h |= HlMod::Trait;
557                    }
558                }
559            }
560
561            (h, Some(konst.attrs(sema.db)))
562        }
563        Definition::Trait(trait_) => {
564            (Highlight::new(HlTag::Symbol(SymbolKind::Trait)), Some(trait_.attrs(sema.db)))
565        }
566        Definition::TypeAlias(type_) => {
567            let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias));
568
569            if let Some(item) = type_.as_assoc_item(db) {
570                h |= HlMod::Associated;
571                h |= HlMod::Static;
572                match item.container(db) {
573                    hir::AssocItemContainer::Impl(i) => {
574                        if i.trait_(db).is_some() {
575                            h |= HlMod::Trait;
576                        }
577                    }
578                    hir::AssocItemContainer::Trait(_t) => {
579                        h |= HlMod::Trait;
580                    }
581                }
582            }
583
584            (h, Some(type_.attrs(sema.db)))
585        }
586        Definition::BuiltinType(_) => (Highlight::new(HlTag::BuiltinType), None),
587        Definition::BuiltinLifetime(_) => {
588            (Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam)), None)
589        }
590        Definition::Static(s) => {
591            let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Static));
592
593            if s.is_mut(db) {
594                h |= HlMod::Mutable;
595                if !is_ref {
596                    h |= HlMod::Unsafe;
597                }
598            }
599
600            (h, Some(s.attrs(sema.db)))
601        }
602        Definition::SelfType(_) => (Highlight::new(HlTag::Symbol(SymbolKind::Impl)), None),
603        Definition::GenericParam(it) => (
604            match it {
605                hir::GenericParam::TypeParam(_) => {
606                    Highlight::new(HlTag::Symbol(SymbolKind::TypeParam))
607                }
608                hir::GenericParam::ConstParam(_) => {
609                    Highlight::new(HlTag::Symbol(SymbolKind::ConstParam)) | HlMod::Const
610                }
611                hir::GenericParam::LifetimeParam(_) => {
612                    Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam))
613                }
614            },
615            None,
616        ),
617        Definition::Local(local) => {
618            let tag = if local.is_self(db) {
619                HlTag::Symbol(SymbolKind::SelfParam)
620            } else if local.is_param(db) {
621                HlTag::Symbol(SymbolKind::ValueParam)
622            } else {
623                HlTag::Symbol(SymbolKind::Local)
624            };
625            let mut h = Highlight::new(tag);
626            let ty = local.ty(db);
627            if local.is_mut(db) || ty.is_mutable_reference() {
628                h |= HlMod::Mutable;
629            }
630            if local.is_ref(db) || ty.is_reference() {
631                h |= HlMod::Reference;
632            }
633            if ty.as_callable(db).is_some() || ty.impls_fnonce(db) {
634                h |= HlMod::Callable;
635            }
636            (h, None)
637        }
638        Definition::ExternCrateDecl(extern_crate) => {
639            let mut highlight = Highlight::new(HlTag::Symbol(SymbolKind::CrateRoot));
640            if extern_crate.alias(db).is_none() {
641                highlight |= HlMod::Library;
642            }
643            (highlight, Some(extern_crate.attrs(sema.db)))
644        }
645        Definition::Label(_) => (Highlight::new(HlTag::Symbol(SymbolKind::Label)), None),
646        Definition::BuiltinAttr(_) => {
647            (Highlight::new(HlTag::Symbol(SymbolKind::BuiltinAttr)), None)
648        }
649        Definition::ToolModule(_) => (Highlight::new(HlTag::Symbol(SymbolKind::ToolModule)), None),
650        Definition::DeriveHelper(_) => {
651            (Highlight::new(HlTag::Symbol(SymbolKind::DeriveHelper)), None)
652        }
653        Definition::InlineAsmRegOrRegClass(_) => {
654            (Highlight::new(HlTag::Symbol(SymbolKind::InlineAsmRegOrRegClass)), None)
655        }
656        Definition::InlineAsmOperand(_) => (Highlight::new(HlTag::Symbol(SymbolKind::Local)), None),
657    };
658
659    let def_crate = def.krate(db);
660    let is_from_other_crate = def_crate != krate;
661    let is_from_builtin_crate = def_crate.is_some_and(|def_crate| def_crate.is_builtin(db));
662    let is_builtin = matches!(
663        def,
664        Definition::BuiltinType(_) | Definition::BuiltinLifetime(_) | Definition::BuiltinAttr(_)
665    );
666    match is_from_other_crate {
667        true if !is_builtin => h |= HlMod::Library,
668        false if def.visibility(db) == Some(hir::Visibility::Public) => h |= HlMod::Public,
669        _ => (),
670    }
671
672    if is_from_builtin_crate {
673        h |= HlMod::DefaultLibrary;
674    }
675
676    if let Some(attrs) = attrs
677        && attrs.is_deprecated()
678    {
679        h |= HlMod::Deprecated;
680    }
681
682    h
683}
684
685fn highlight_method_call_by_name_ref(
686    sema: &Semantics<'_, RootDatabase>,
687    krate: Option<hir::Crate>,
688    name_ref: &ast::NameRef,
689    is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
690) -> Option<Highlight> {
691    let mc = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?;
692    highlight_method_call(sema, krate, &mc, is_unsafe_node)
693}
694
695fn highlight_method_call(
696    sema: &Semantics<'_, RootDatabase>,
697    krate: Option<hir::Crate>,
698    method_call: &ast::MethodCallExpr,
699    is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
700) -> Option<Highlight> {
701    let func = sema.resolve_method_call(method_call)?;
702
703    let mut h = SymbolKind::Method.into();
704
705    let is_unsafe = is_unsafe_node(AstPtr::new(method_call).upcast::<ast::Expr>().wrap_left());
706    if is_unsafe {
707        h |= HlMod::Unsafe;
708    }
709    if func.is_async(sema.db) {
710        h |= HlMod::Async;
711    }
712    if func.is_const(sema.db) {
713        h |= HlMod::Const;
714    }
715    if func
716        .as_assoc_item(sema.db)
717        .and_then(|it| it.container_or_implemented_trait(sema.db))
718        .is_some()
719    {
720        h |= HlMod::Trait;
721    }
722
723    let def_crate = func.module(sema.db).krate(sema.db);
724    let is_from_other_crate = krate.as_ref().map_or(false, |krate| def_crate != *krate);
725    let is_from_builtin_crate = def_crate.is_builtin(sema.db);
726    let is_public = func.visibility(sema.db) == hir::Visibility::Public;
727    let is_deprecated = func.attrs(sema.db).is_deprecated();
728
729    if is_from_other_crate {
730        h |= HlMod::Library;
731    } else if is_public {
732        h |= HlMod::Public;
733    }
734
735    if is_from_builtin_crate {
736        h |= HlMod::DefaultLibrary;
737    }
738
739    if is_deprecated {
740        h |= HlMod::Deprecated;
741    }
742
743    if let Some(self_param) = func.self_param(sema.db) {
744        match self_param.access(sema.db) {
745            hir::Access::Shared => h |= HlMod::Reference,
746            hir::Access::Exclusive => {
747                h |= HlMod::Mutable;
748                h |= HlMod::Reference;
749            }
750            hir::Access::Owned => {
751                if let Some(receiver_ty) =
752                    method_call.receiver().and_then(|it| sema.type_of_expr(&it))
753                    && !receiver_ty.adjusted().is_copy(sema.db)
754                {
755                    h |= HlMod::Consuming
756                }
757            }
758        }
759    }
760    Some(h)
761}
762
763fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
764    let default = HlTag::UnresolvedReference;
765
766    let parent = match name.syntax().parent() {
767        Some(it) => it,
768        _ => return default.into(),
769    };
770
771    let tag = match parent.kind() {
772        STRUCT => SymbolKind::Struct,
773        ENUM => SymbolKind::Enum,
774        VARIANT => SymbolKind::Variant,
775        UNION => SymbolKind::Union,
776        TRAIT => SymbolKind::Trait,
777        TYPE_ALIAS => SymbolKind::TypeAlias,
778        TYPE_PARAM => SymbolKind::TypeParam,
779        RECORD_FIELD => SymbolKind::Field,
780        MODULE => SymbolKind::Module,
781        EXTERN_CRATE => SymbolKind::CrateRoot,
782        FN => SymbolKind::Function,
783        CONST => SymbolKind::Const,
784        STATIC => SymbolKind::Static,
785        IDENT_PAT => SymbolKind::Local,
786        FORMAT_ARGS_ARG => SymbolKind::Local,
787        RENAME => SymbolKind::Local,
788        MACRO_RULES => SymbolKind::Macro,
789        CONST_PARAM => SymbolKind::ConstParam,
790        SELF_PARAM => SymbolKind::SelfParam,
791        ASM_OPERAND_NAMED => SymbolKind::Local,
792        _ => return default.into(),
793    };
794
795    tag.into()
796}
797
798fn highlight_name_ref_by_syntax(
799    name: ast::NameRef,
800    sema: &Semantics<'_, RootDatabase>,
801    krate: Option<hir::Crate>,
802    is_unsafe_node: &impl Fn(AstPtr<Either<ast::Expr, ast::Pat>>) -> bool,
803) -> Highlight {
804    let default = HlTag::UnresolvedReference;
805
806    let parent = match name.syntax().parent() {
807        Some(it) => it,
808        _ => return default.into(),
809    };
810
811    match parent.kind() {
812        EXTERN_CRATE => HlTag::Symbol(SymbolKind::CrateRoot).into(),
813        METHOD_CALL_EXPR => ast::MethodCallExpr::cast(parent)
814            .and_then(|it| highlight_method_call(sema, krate, &it, is_unsafe_node))
815            .unwrap_or_else(|| SymbolKind::Method.into()),
816        FIELD_EXPR => {
817            let h = HlTag::Symbol(SymbolKind::Field);
818            let is_unsafe = ast::Expr::cast(parent)
819                .is_some_and(|it| is_unsafe_node(AstPtr::new(&it).wrap_left()));
820            if is_unsafe { h | HlMod::Unsafe } else { h.into() }
821        }
822        RECORD_EXPR_FIELD | RECORD_PAT_FIELD => HlTag::Symbol(SymbolKind::Field).into(),
823        PATH_SEGMENT => {
824            let name_based_fallback = || {
825                if name.text().chars().next().unwrap_or_default().is_uppercase() {
826                    SymbolKind::Struct.into()
827                } else {
828                    SymbolKind::Module.into()
829                }
830            };
831            let path = match parent.parent().and_then(ast::Path::cast) {
832                Some(it) => it,
833                _ => return name_based_fallback(),
834            };
835            let expr = match path.syntax().parent() {
836                Some(parent) => match_ast! {
837                    match parent {
838                        ast::PathExpr(path) => path,
839                        ast::MacroCall(_) => return SymbolKind::Macro.into(),
840                        _ => return name_based_fallback(),
841                    }
842                },
843                // within path, decide whether it is module or adt by checking for uppercase name
844                None => return name_based_fallback(),
845            };
846            let parent = match expr.syntax().parent() {
847                Some(it) => it,
848                None => return default.into(),
849            };
850
851            match parent.kind() {
852                CALL_EXPR => SymbolKind::Function.into(),
853                _ => if name.text().chars().next().unwrap_or_default().is_uppercase() {
854                    SymbolKind::Struct
855                } else {
856                    SymbolKind::Const
857                }
858                .into(),
859            }
860        }
861        ASSOC_TYPE_ARG => SymbolKind::TypeAlias.into(),
862        USE_BOUND_GENERIC_ARGS => SymbolKind::TypeParam.into(),
863        _ => default.into(),
864    }
865}
866
867fn is_consumed_lvalue(node: &SyntaxNode, local: &hir::Local, db: &RootDatabase) -> bool {
868    // When lvalues are passed as arguments and they're not Copy, then mark them as Consuming.
869    parents_match(node.clone().into(), &[PATH_SEGMENT, PATH, PATH_EXPR, ARG_LIST])
870        && !local.ty(db).is_copy(db)
871}
872
873/// Returns true if the parent nodes of `node` all match the `SyntaxKind`s in `kinds` exactly.
874fn parents_match(mut node: NodeOrToken<SyntaxNode, SyntaxToken>, mut kinds: &[SyntaxKind]) -> bool {
875    while let (Some(parent), [kind, rest @ ..]) = (node.parent(), kinds) {
876        if parent.kind() != *kind {
877            return false;
878        }
879
880        node = parent.into();
881        kinds = rest;
882    }
883
884    // Only true if we matched all expected kinds
885    kinds.is_empty()
886}
887
888fn parent_matches<N: AstNode>(token: &SyntaxToken) -> bool {
889    token.parent().is_some_and(|it| N::can_cast(it.kind()))
890}