]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide/src/syntax_highlighting/highlight.rs
Explicitly check for reference locals or fields in Name classification
[rust.git] / crates / ide / src / syntax_highlighting / highlight.rs
index 6834fe11ae8fe8b4cbaf75d965281aa730aaf9d8..197a32da069438c00d1ee8839c1687980c478025 100644 (file)
@@ -1,6 +1,6 @@
 //! Computes color for a single element.
 
-use hir::{AsAssocItem, Semantics};
+use hir::{AsAssocItem, HasVisibility, Semantics};
 use ide_db::{
     defs::{Definition, NameClass, NameRefClass},
     RootDatabase, SymbolKind,
@@ -46,7 +46,6 @@ pub(super) fn element(
             };
 
             match name_kind {
-                Some(NameClass::ExternCrate(_)) => SymbolKind::Module.into(),
                 Some(NameClass::Definition(def)) => {
                     let mut h = highlight_def(db, krate, def) | HlMod::Definition;
                     if let Definition::ModuleDef(hir::ModuleDef::Trait(trait_)) = &def {
@@ -59,10 +58,8 @@ pub(super) fn element(
                 Some(NameClass::ConstReference(def)) => highlight_def(db, krate, def),
                 Some(NameClass::PatFieldShorthand { field_ref, .. }) => {
                     let mut h = HlTag::Symbol(SymbolKind::Field).into();
-                    if let Definition::Field(field) = field_ref {
-                        if let hir::VariantDef::Union(_) = field.parent_def(db) {
-                            h |= HlMod::Unsafe;
-                        }
+                    if let hir::VariantDef::Union(_) = field_ref.parent_def(db) {
+                        h |= HlMod::Unsafe;
                     }
                     h
                 }
@@ -71,68 +68,87 @@ pub(super) fn element(
         }
         // Highlight references like the definitions they resolve to
         NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => {
-            // even though we track whether we are in an attribute or not we still need this special case
-            // as otherwise we would emit unresolved references for name refs inside attributes
-            SymbolKind::Function.into()
+            // FIXME: We highlight paths in attributes slightly differently to work around this module
+            // currently not knowing about tool attributes and rustc builtin attributes as
+            // we do not want to resolve those to functions that may be defined in scope.
+            let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
+            match NameRefClass::classify(sema, &name_ref) {
+                Some(name_class) => match name_class {
+                    NameRefClass::Definition(Definition::ModuleDef(hir::ModuleDef::Module(_)))
+                        if name_ref
+                            .syntax()
+                            .ancestors()
+                            .find_map(ast::Path::cast)
+                            .map_or(false, |it| it.parent_path().is_some()) =>
+                    {
+                        HlTag::Symbol(SymbolKind::Module)
+                    }
+                    NameRefClass::Definition(Definition::Macro(m))
+                        if m.kind() == hir::MacroKind::Attr =>
+                    {
+                        HlTag::Symbol(SymbolKind::Macro)
+                    }
+                    _ => HlTag::BuiltinAttr,
+                },
+                None => HlTag::BuiltinAttr,
+            }
+            .into()
         }
         NAME_REF => {
             let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
-            highlight_func_by_name_ref(sema, krate, &name_ref).unwrap_or_else(|| {
-                let is_self = name_ref.self_token().is_some();
-                let h = match NameRefClass::classify(sema, &name_ref) {
-                    Some(name_kind) => match name_kind {
-                        NameRefClass::ExternCrate(_) => SymbolKind::Module.into(),
-                        NameRefClass::Definition(def) => {
-                            if let Definition::Local(local) = &def {
-                                if let Some(name) = local.name(db) {
-                                    let shadow_count =
-                                        bindings_shadow_count.entry(name.clone()).or_default();
-                                    binding_hash = Some(calc_binding_hash(&name, *shadow_count))
-                                }
-                            };
-
-                            let mut h = highlight_def(db, krate, def);
-
-                            match def {
-                                Definition::Local(local)
-                                    if is_consumed_lvalue(
-                                        name_ref.syntax().clone().into(),
-                                        &local,
-                                        db,
-                                    ) =>
-                                {
-                                    h |= HlMod::Consuming;
-                                }
-                                Definition::ModuleDef(hir::ModuleDef::Trait(trait_))
-                                    if trait_.is_unsafe(db) =>
-                                {
-                                    if ast::Impl::for_trait_name_ref(&name_ref).is_some() {
-                                        h |= HlMod::Unsafe;
-                                    }
+            highlight_method_call_by_name_ref(sema, krate, &name_ref).unwrap_or_else(|| {
+                let name_class = match NameRefClass::classify(sema, &name_ref) {
+                    Some(name_kind) => name_kind,
+                    None => {
+                        return if syntactic_name_ref_highlighting {
+                            highlight_name_ref_by_syntax(name_ref, sema, krate)
+                        } else {
+                            HlTag::UnresolvedReference.into()
+                        }
+                    }
+                };
+                let h = match name_class {
+                    NameRefClass::Definition(def) => {
+                        if let Definition::Local(local) = &def {
+                            if let Some(name) = local.name(db) {
+                                let shadow_count =
+                                    bindings_shadow_count.entry(name.clone()).or_default();
+                                binding_hash = Some(calc_binding_hash(&name, *shadow_count))
+                            }
+                        };
+
+                        let mut h = highlight_def(db, krate, def);
+
+                        match def {
+                            Definition::Local(local)
+                                if is_consumed_lvalue(name_ref.syntax(), &local, db) =>
+                            {
+                                h |= HlMod::Consuming;
+                            }
+                            Definition::ModuleDef(hir::ModuleDef::Trait(trait_))
+                                if trait_.is_unsafe(db) =>
+                            {
+                                if ast::Impl::for_trait_name_ref(&name_ref).is_some() {
+                                    h |= HlMod::Unsafe;
                                 }
-                                Definition::Field(field) => {
-                                    if let Some(parent) = name_ref.syntax().parent() {
-                                        if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) {
-                                            if let hir::VariantDef::Union(_) = field.parent_def(db)
-                                            {
-                                                h |= HlMod::Unsafe;
-                                            }
+                            }
+                            Definition::Field(field) => {
+                                if let Some(parent) = name_ref.syntax().parent() {
+                                    if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) {
+                                        if let hir::VariantDef::Union(_) = field.parent_def(db) {
+                                            h |= HlMod::Unsafe;
                                         }
                                     }
                                 }
-                                _ => (),
                             }
-
-                            h
+                            _ => (),
                         }
-                        NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(),
-                    },
-                    None if syntactic_name_ref_highlighting => {
-                        highlight_name_ref_by_syntax(name_ref, sema, krate)
+
+                        h
                     }
-                    None => HlTag::UnresolvedReference.into(),
+                    NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(),
                 };
-                if h.tag == HlTag::Symbol(SymbolKind::Module) && is_self {
+                if h.tag == HlTag::Symbol(SymbolKind::Module) && name_ref.self_token().is_some() {
                     SymbolKind::SelfParam.into()
                 } else {
                     h
@@ -172,6 +188,7 @@ pub(super) fn element(
                 _ => Highlight::from(SymbolKind::LifetimeParam) | HlMod::Definition,
             }
         }
+        IDENT if parent_matches::<ast::TokenTree>(&element) => HlTag::None.into(),
         p if p.is_punct() => match p {
             T![&] if parent_matches::<ast::BinExpr>(&element) => HlOperator::Bitwise.into(),
             T![&] => {
@@ -439,15 +456,18 @@ fn highlight_def(db: &RootDatabase, krate: Option<hir::Crate>, def: Definition)
 
     let is_from_other_crate = def.module(db).map(hir::Module::krate) != krate;
     let is_builtin_type = matches!(def, Definition::ModuleDef(hir::ModuleDef::BuiltinType(_)));
+    let is_public = def.visibility(db) == Some(hir::Visibility::Public);
 
-    if is_from_other_crate && !is_builtin_type {
-        h |= HlMod::Library;
+    match (is_from_other_crate, is_builtin_type, is_public) {
+        (true, false, _) => h |= HlMod::Library,
+        (false, _, true) => h |= HlMod::Public,
+        _ => {}
     }
 
     h
 }
 
-fn highlight_func_by_name_ref(
+fn highlight_method_call_by_name_ref(
     sema: &Semantics<RootDatabase>,
     krate: Option<hir::Crate>,
     name_ref: &ast::NameRef,
@@ -475,8 +495,14 @@ fn highlight_method_call(
     if func.as_assoc_item(sema.db).and_then(|it| it.containing_trait(sema.db)).is_some() {
         h |= HlMod::Trait;
     }
-    if Some(func.module(sema.db).krate()) != krate {
+
+    let is_from_other_crate = Some(func.module(sema.db).krate()) != krate;
+    let is_public = func.visibility(sema.db) == hir::Visibility::Public;
+
+    if is_from_other_crate {
         h |= HlMod::Library;
+    } else if is_public {
+        h |= HlMod::Public;
     }
 
     if let Some(self_param) = func.self_param(sema.db) {
@@ -590,13 +616,10 @@ fn highlight_name_ref_by_syntax(
     }
 }
 
-fn is_consumed_lvalue(
-    node: NodeOrToken<SyntaxNode, SyntaxToken>,
-    local: &hir::Local,
-    db: &RootDatabase,
-) -> bool {
+fn is_consumed_lvalue(node: &SyntaxNode, local: &hir::Local, db: &RootDatabase) -> bool {
     // When lvalues are passed as arguments and they're not Copy, then mark them as Consuming.
-    parents_match(node, &[PATH_SEGMENT, PATH, PATH_EXPR, ARG_LIST]) && !local.ty(db).is_copy(db)
+    parents_match(node.clone().into(), &[PATH_SEGMENT, PATH, PATH_EXPR, ARG_LIST])
+        && !local.ty(db).is_copy(db)
 }
 
 /// Returns true if the parent nodes of `node` all match the `SyntaxKind`s in `kinds` exactly.