]> git.lizzy.rs Git - rust.git/commitdiff
Fix syntax highlighting not highlighting derives anymore
authorLukas Wirth <lukastw97@gmail.com>
Mon, 21 Feb 2022 12:21:25 +0000 (13:21 +0100)
committerLukas Wirth <lukastw97@gmail.com>
Tue, 22 Feb 2022 09:20:44 +0000 (10:20 +0100)
crates/hir/src/semantics.rs
crates/hir/src/semantics/source_to_def.rs
crates/hir_def/src/dyn_map.rs
crates/hir_def/src/keys.rs
crates/ide/src/syntax_highlighting.rs

index 1156dfe98e15b18a2972bc51da28afa2b544544b..2e519158162249be65d910ae2a2e4d56d54b3ea8 100644 (file)
@@ -160,6 +160,10 @@ pub fn is_attr_macro_call(&self, item: &ast::Item) -> bool {
         self.imp.is_attr_macro_call(item)
     }
 
+    pub fn is_derive_annotated(&self, item: &ast::Adt) -> bool {
+        self.imp.is_derive_annotated(item)
+    }
+
     pub fn speculative_expand(
         &self,
         actual_macro_call: &ast::MacroCall,
@@ -470,6 +474,12 @@ fn derive_macro_calls(&self, attr: &ast::Attr) -> Option<Vec<Option<MacroCallId>
         })
     }
 
+    fn is_derive_annotated(&self, adt: &ast::Adt) -> bool {
+        let file_id = self.find_file(adt.syntax()).file_id;
+        let adt = InFile::new(file_id, adt);
+        self.with_ctx(|ctx| ctx.has_derives(adt))
+    }
+
     fn is_attr_macro_call(&self, item: &ast::Item) -> bool {
         let file_id = self.find_file(item.syntax()).file_id;
         let src = InFile::new(file_id, item.clone());
index c0d8e69e492c6b0cf9f496279c2fb0c5bc0a5f3a..dddb8e33dcc0b4a8f91021dcbc964bf3dea5ee95 100644 (file)
@@ -255,6 +255,9 @@ pub(super) fn attr_to_derive_macro_call(
             .get(&src.value)
             .map(|&(attr_id, call_id, ref ids)| (attr_id, call_id, &**ids))
     }
+    pub(super) fn has_derives(&mut self, adt: InFile<&ast::Adt>) -> bool {
+        self.dyn_map(adt).as_ref().map_or(false, |map| !map[keys::DERIVE_MACRO_CALL].is_empty())
+    }
 
     fn to_def<Ast: AstNode + 'static, ID: Copy + 'static>(
         &mut self,
index 6f269d7b01fec1645d62e7f79bef846b9f57af6f..166aa04da044ff71dbd7c5c010413fc002b3a865 100644 (file)
@@ -54,6 +54,7 @@ pub trait Policy {
 
     fn insert(map: &mut DynMap, key: Self::K, value: Self::V);
     fn get<'a>(map: &'a DynMap, key: &Self::K) -> Option<&'a Self::V>;
+    fn is_empty(map: &DynMap) -> bool;
 }
 
 impl<K: Hash + Eq + 'static, V: 'static> Policy for (K, V) {
@@ -65,6 +66,9 @@ fn insert(map: &mut DynMap, key: K, value: V) {
     fn get<'a>(map: &'a DynMap, key: &K) -> Option<&'a V> {
         map.map.get::<FxHashMap<K, V>>()?.get(key)
     }
+    fn is_empty(map: &DynMap) -> bool {
+        map.map.get::<FxHashMap<K, V>>().map_or(true, |it| it.is_empty())
+    }
 }
 
 pub struct DynMap {
@@ -90,6 +94,10 @@ pub fn insert(&mut self, key: P::K, value: P::V) {
     pub fn get(&self, key: &P::K) -> Option<&P::V> {
         P::get(&self.map, key)
     }
+
+    pub fn is_empty(&self) -> bool {
+        P::is_empty(&self.map)
+    }
 }
 
 impl<P: Policy> Index<Key<P::K, P::V, P>> for DynMap {
index 1c9d99eb7882472fc5df84aab73e7d3e46d36ad1..8cd2d771721493061b0f6acec96dfa7c9a820dc8 100644 (file)
@@ -61,4 +61,7 @@ fn get<'a>(map: &'a DynMap, key: &AST) -> Option<&'a ID> {
         let key = AstPtr::new(key);
         map.map.get::<FxHashMap<AstPtr<AST>, ID>>()?.get(&key)
     }
+    fn is_empty(map: &DynMap) -> bool {
+        map.map.get::<FxHashMap<AstPtr<AST>, ID>>().map_or(true, |it| it.is_empty())
+    }
 }
index d67f6baff0798003aef2e1c721fd3c8bdcd5674d..7d92c5051b14ff77fa4bb42b547a2b9e285bccaf 100644 (file)
@@ -237,6 +237,20 @@ fn traverse(
                     continue;
                 }
                 Some(item) if sema.is_attr_macro_call(&item) => current_attr_call = Some(item),
+                Some(item) if current_attr_call.is_none() => {
+                    let adt = match item {
+                        ast::Item::Enum(it) => Some(ast::Adt::Enum(it)),
+                        ast::Item::Struct(it) => Some(ast::Adt::Struct(it)),
+                        ast::Item::Union(it) => Some(ast::Adt::Union(it)),
+                        _ => None,
+                    };
+                    match adt {
+                        Some(adt) if sema.is_derive_annotated(&adt) => {
+                            current_attr_call = Some(adt.into());
+                        }
+                        _ => (),
+                    }
+                }
                 None if ast::Attr::can_cast(node.kind()) => inside_attribute = true,
                 _ => (),
             },