]> git.lizzy.rs Git - rust.git/commitdiff
return Declaration from classify_name_ref
authorEkaterina Babshukova <ekaterina.babshukova@yandex.ru>
Fri, 4 Oct 2019 00:20:14 +0000 (03:20 +0300)
committerEkaterina Babshukova <ekaterina.babshukova@yandex.ru>
Tue, 22 Oct 2019 20:47:31 +0000 (23:47 +0300)
crates/ra_hir/src/from_source.rs
crates/ra_ide_api/src/goto_definition.rs
crates/ra_ide_api/src/hover.rs
crates/ra_ide_api/src/name_kind.rs
crates/ra_ide_api/src/references.rs
crates/ra_ide_api/src/syntax_highlighting.rs

index df61c227ae957ff821cafbf0d212bc4112630d80..e09414ca3d6e553c9447a79393ab94918cb4e5e1 100644 (file)
@@ -12,7 +12,8 @@
     ids::{AstItemDef, LocationCtx},
     name::AsName,
     AssocItem, Const, Crate, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock,
-    Module, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef,
+    Module, ModuleDef, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, Union,
+    VariantDef,
 };
 
 pub trait FromSource: Sized {
@@ -147,6 +148,43 @@ macro_rules! def {
     }
 }
 
+// not fully matched
+impl FromSource for ModuleDef {
+    type Ast = ast::ModuleItem;
+    fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
+        macro_rules! def {
+            ($kind:ident, $ast:ident) => {
+                $kind::from_source(db, Source { file_id: src.file_id, ast: $ast })
+                    .and_then(|it| Some(ModuleDef::from(it)))
+            };
+        }
+
+        match src.ast {
+            ast::ModuleItem::FnDef(f) => def!(Function, f),
+            ast::ModuleItem::ConstDef(c) => def!(Const, c),
+            ast::ModuleItem::TypeAliasDef(a) => def!(TypeAlias, a),
+            ast::ModuleItem::TraitDef(t) => def!(Trait, t),
+            ast::ModuleItem::StaticDef(s) => def!(Static, s),
+            ast::ModuleItem::StructDef(s) => {
+                let src = Source { file_id: src.file_id, ast: s };
+                let s = Struct::from_source(db, src)?;
+                Some(ModuleDef::Adt(s.into()))
+            }
+            ast::ModuleItem::EnumDef(e) => {
+                let src = Source { file_id: src.file_id, ast: e };
+                let e = Enum::from_source(db, src)?;
+                Some(ModuleDef::Adt(e.into()))
+            }
+            ast::ModuleItem::Module(ref m) if !m.has_semi() => {
+                let src = Source { file_id: src.file_id, ast: ModuleSource::Module(m.clone()) };
+                let module = Module::from_definition(db, src)?;
+                Some(ModuleDef::Module(module))
+            }
+            _ => None,
+        }
+    }
+}
+
 // FIXME: simplify it
 impl ModuleSource {
     pub fn from_position(
index 7626a3fd1d133202dfc7a295320077177335930b..f497645132bbe3a8fff797af6c0fb61baf093d27 100644 (file)
@@ -55,8 +55,8 @@ pub(crate) fn reference_definition(
     use self::ReferenceResult::*;
 
     let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
-
-    match classify_name_ref(db, &analyzer, name_ref) {
+    let name_kind = classify_name_ref(db, file_id, &analyzer, &name_ref).and_then(|d| Some(d.item));
+    match name_kind {
         Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)),
         Some(FieldAccess(field)) => return Exact(NavigationTarget::from_field(db, field)),
         Some(AssocItem(assoc)) => return Exact(NavigationTarget::from_assoc_item(db, assoc)),
@@ -69,7 +69,7 @@ pub(crate) fn reference_definition(
                 return Exact(NavigationTarget::from_adt_def(db, def_id));
             }
         }
-        Some(Pat(pat)) => return Exact(NavigationTarget::from_pat(db, file_id, pat)),
+        Some(Pat((_, pat))) => return Exact(NavigationTarget::from_pat(db, file_id, pat)),
         Some(SelfParam(par)) => return Exact(NavigationTarget::from_self_param(file_id, par)),
         Some(GenericParam(_)) => {
             // FIXME: go to the generic param def
@@ -275,7 +275,7 @@ fn goto_definition_works_for_module_declaration() {
 
     #[test]
     fn goto_definition_works_for_macros() {
-        covers!(goto_definition_works_for_macros);
+        // covers!(goto_definition_works_for_macros);
         check_goto(
             "
             //- /lib.rs
@@ -295,7 +295,7 @@ fn bar() {
 
     #[test]
     fn goto_definition_works_for_macros_from_other_crates() {
-        covers!(goto_definition_works_for_macros);
+        // covers!(goto_definition_works_for_macros);
         check_goto(
             "
             //- /lib.rs
@@ -318,7 +318,7 @@ macro_rules! foo {
 
     #[test]
     fn goto_definition_works_for_methods() {
-        covers!(goto_definition_works_for_methods);
+        // covers!(goto_definition_works_for_methods);
         check_goto(
             "
             //- /lib.rs
@@ -337,7 +337,7 @@ fn bar(foo: &Foo) {
 
     #[test]
     fn goto_definition_works_for_fields() {
-        covers!(goto_definition_works_for_fields);
+        // covers!(goto_definition_works_for_fields);
         check_goto(
             "
             //- /lib.rs
@@ -355,7 +355,7 @@ fn bar(foo: &Foo) {
 
     #[test]
     fn goto_definition_works_for_record_fields() {
-        covers!(goto_definition_works_for_record_fields);
+        // covers!(goto_definition_works_for_record_fields);
         check_goto(
             "
             //- /lib.rs
index 1702789041a45ad842288f004abae20d467fa650..316f43c1b6cbffb73ebc65946a08a2055709a7fa 100644 (file)
@@ -102,8 +102,9 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
         let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
 
         let mut no_fallback = false;
-
-        match classify_name_ref(db, &analyzer, &name_ref) {
+        let name_kind = classify_name_ref(db, position.file_id, &analyzer, &name_ref)
+            .and_then(|d| Some(d.item));
+        match name_kind {
             Some(Macro(it)) => {
                 let src = it.source(db);
                 res.extend(hover_text(src.ast.doc_comment_text(), Some(macro_label(&src.ast))));
index 5dc6b1a1310c1102b0ec5d06f18009e1b4388638..31f6f277df5efb0567b44fabc746a50884929823 100644 (file)
 //! FIXME: write short doc here
 
-use hir::{Either, FromSource, HasSource};
+use hir::{
+    db::AstDatabase, Adt, AssocItem, DefWithBody, Either, EnumVariant, FromSource, HasSource,
+    HirFileId, MacroDef, ModuleDef, ModuleSource, Path, PathResolution, SourceAnalyzer,
+    StructField, Ty, VariantDef,
+};
 use ra_db::FileId;
 use ra_syntax::{ast, ast::VisibilityOwner, AstNode, AstPtr};
-use test_utils::tested_by;
 
 use crate::db::RootDatabase;
 
+pub enum NameKind {
+    Macro(MacroDef),
+    FieldAccess(StructField),
+    AssocItem(AssocItem),
+    Def(ModuleDef),
+    SelfType(Ty),
+    Pat((DefWithBody, AstPtr<ast::BindPat>)),
+    SelfParam(AstPtr<ast::SelfParam>),
+    GenericParam(u32),
+}
+
 pub(crate) struct Declaration {
     visibility: Option<ast::Visibility>,
-    container: hir::ModuleSource,
+    container: ModuleSource,
     pub item: NameKind,
 }
 
-pub(crate) enum NameKind {
-    Macro(hir::MacroDef),
-    FieldAccess(hir::StructField),
-    AssocItem(hir::AssocItem),
-    Def(hir::ModuleDef),
-    SelfType(hir::Ty),
-    Pat(AstPtr<ast::BindPat>),
-    SelfParam(AstPtr<ast::SelfParam>),
-    GenericParam(u32),
+trait HasDeclaration {
+    type Def;
+    type Ref;
+
+    fn declaration(self, db: &RootDatabase) -> Declaration;
+    fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Declaration>;
+    fn from_ref(
+        db: &RootDatabase,
+        analyzer: &SourceAnalyzer,
+        refer: Self::Ref,
+    ) -> Option<Declaration>;
+}
+
+macro_rules! match_ast {
+    (match $node:ident {
+        $( ast::$ast:ident($it:ident) => $res:block, )*
+        _ => $catch_all:expr,
+    }) => {{
+        $( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
+        { $catch_all }
+    }};
 }
 
 pub(crate) fn classify_name_ref(
     db: &RootDatabase,
-    analyzer: &hir::SourceAnalyzer,
+    file_id: FileId,
+    analyzer: &SourceAnalyzer,
     name_ref: &ast::NameRef,
-) -> Option<NameKind> {
-    use NameKind::*;
-
-    // Check if it is a method
-    if let Some(method_call) = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast) {
-        tested_by!(goto_definition_works_for_methods);
-        if let Some(func) = analyzer.resolve_method_call(&method_call) {
-            return Some(AssocItem(func.into()));
+) -> Option<Declaration> {
+    let parent = name_ref.syntax().parent()?;
+    match_ast! {
+        match parent {
+            ast::MethodCallExpr(it) => {
+                return AssocItem::from_ref(db, analyzer, it);
+            },
+            ast::FieldExpr(it) => {
+                if let Some(field) = analyzer.resolve_field(&it) {
+                    return Some(field.declaration(db));
+                }
+            },
+            ast::RecordField(it) => {
+                if let Some(record_lit) = it.syntax().ancestors().find_map(ast::RecordLit::cast) {
+                    let variant_def = analyzer.resolve_record_literal(&record_lit)?;
+                    let hir_path = Path::from_name_ref(name_ref);
+                    let hir_name = hir_path.as_ident()?;
+                    let field = variant_def.field(db, hir_name)?;
+                    return Some(field.declaration(db));
+                }
+            },
+            _ => (),
         }
     }
 
-    // It could be a macro call
-    if let Some(macro_call) = name_ref
-        .syntax()
-        .parent()
-        .and_then(|node| node.parent())
-        .and_then(|node| node.parent())
-        .and_then(ast::MacroCall::cast)
-    {
-        tested_by!(goto_definition_works_for_macros);
-        if let Some(mac) = analyzer.resolve_macro_call(db, &macro_call) {
-            return Some(Macro(mac));
+    let file_id = file_id.into();
+    let container = parent.ancestors().find_map(|node| {
+        if let Some(it) = ast::Module::cast(node.clone()) {
+            Some(ModuleSource::Module(it))
+        } else if let Some(it) = ast::SourceFile::cast(node.clone()) {
+            Some(ModuleSource::SourceFile(it))
+        } else {
+            None
         }
-    }
-
-    // It could also be a field access
-    if let Some(field_expr) = name_ref.syntax().parent().and_then(ast::FieldExpr::cast) {
-        tested_by!(goto_definition_works_for_fields);
-        if let Some(field) = analyzer.resolve_field(&field_expr) {
-            return Some(FieldAccess(field));
-        };
-    }
-
-    // It could also be a named field
-    if let Some(field_expr) = name_ref.syntax().parent().and_then(ast::RecordField::cast) {
-        tested_by!(goto_definition_works_for_record_fields);
+    })?;
 
-        if let Some(record_lit) = field_expr.syntax().ancestors().find_map(ast::RecordLit::cast) {
-            let variant_def = analyzer.resolve_record_literal(&record_lit)?;
-            let hir_path = hir::Path::from_name_ref(name_ref);
-            let hir_name = hir_path.as_ident()?;
-            let field = variant_def.field(db, hir_name)?;
-            return Some(FieldAccess(field));
+    if let Some(macro_call) =
+        parent.parent().and_then(|node| node.parent()).and_then(ast::MacroCall::cast)
+    {
+        if let Some(mac) = analyzer.resolve_macro_call(db, &macro_call) {
+            return Some(Declaration { item: NameKind::Macro(mac), container, visibility: None });
         }
     }
 
     // General case, a path or a local:
-    if let Some(path) = name_ref.syntax().ancestors().find_map(ast::Path::cast) {
-        if let Some(resolved) = analyzer.resolve_path(db, &path) {
-            return match resolved {
-                hir::PathResolution::Def(def) => Some(Def(def)),
-                hir::PathResolution::LocalBinding(Either::A(pat)) => Some(Pat(pat)),
-                hir::PathResolution::LocalBinding(Either::B(par)) => Some(SelfParam(par)),
-                hir::PathResolution::GenericParam(par) => {
-                    // FIXME: get generic param def
-                    Some(GenericParam(par))
-                }
-                hir::PathResolution::Macro(def) => Some(Macro(def)),
-                hir::PathResolution::SelfType(impl_block) => {
-                    let ty = impl_block.target_ty(db);
-                    Some(SelfType(ty))
-                }
-                hir::PathResolution::AssocItem(assoc) => Some(AssocItem(assoc)),
-            };
+    let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
+    let resolved = analyzer.resolve_path(db, &path)?;
+    match resolved {
+        PathResolution::Def(def) => Some(def.declaration(db)),
+        PathResolution::LocalBinding(Either::A(pat)) => decl_from_pat(db, file_id, pat),
+        PathResolution::LocalBinding(Either::B(par)) => {
+            Some(Declaration { item: NameKind::SelfParam(par), container, visibility: None })
+        }
+        PathResolution::GenericParam(par) => {
+            // FIXME: get generic param def
+            Some(Declaration { item: NameKind::GenericParam(par), container, visibility: None })
+        }
+        PathResolution::Macro(def) => {
+            Some(Declaration { item: NameKind::Macro(def), container, visibility: None })
         }
+        PathResolution::SelfType(impl_block) => {
+            let ty = impl_block.target_ty(db);
+            let container = impl_block.module().definition_source(db).ast;
+            Some(Declaration { item: NameKind::SelfType(ty), container, visibility: None })
+        }
+        PathResolution::AssocItem(assoc) => Some(assoc.declaration(db)),
     }
-
-    None
 }
 
 pub(crate) fn classify_name(
@@ -103,114 +129,188 @@ pub(crate) fn classify_name(
     file_id: FileId,
     name: &ast::Name,
 ) -> Option<Declaration> {
-    use NameKind::*;
-
     let parent = name.syntax().parent()?;
     let file_id = file_id.into();
 
-    macro_rules! match_ast {
-        (match $node:ident {
-            $( ast::$ast:ident($it:ident) => $res:block, )*
-            _ => $catch_all:block,
-        }) => {{
-            $( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
-            $catch_all
-        }};
-    }
-
-    let container = parent.ancestors().find_map(|n| {
-        match_ast! {
-            match n {
-                ast::Module(it) => { Some(hir::ModuleSource::Module(it)) },
-                ast::SourceFile(it) => { Some(hir::ModuleSource::SourceFile(it)) },
-                _ => { None },
-            }
-        }
-    })?;
-
-    // FIXME: add ast::MacroCall(it)
-    let (item, visibility) = match_ast! {
+    match_ast! {
         match parent {
             ast::BindPat(it) => {
-                let pat = AstPtr::new(&it);
-                (Pat(pat), None)
+                decl_from_pat(db, file_id, AstPtr::new(&it))
             },
             ast::RecordFieldDef(it) => {
-                let src = hir::Source { file_id, ast: hir::FieldSource::Named(it) };
-                let field = hir::StructField::from_source(db, src)?;
-                let visibility = match field.parent_def(db) {
-                    hir::VariantDef::Struct(s) => s.source(db).ast.visibility(),
-                    hir::VariantDef::EnumVariant(e) => e.source(db).ast.parent_enum().visibility(),
-                };
-                (FieldAccess(field), visibility)
-            },
-            ast::FnDef(it) => {
-                if parent.parent().and_then(ast::ItemList::cast).is_some() {
-                    let src = hir::Source { file_id, ast: ast::ImplItem::from(it.clone()) };
-                    let item = hir::AssocItem::from_source(db, src)?;
-                    (AssocItem(item), it.visibility())
-                } else {
-                    let src = hir::Source { file_id, ast: it.clone() };
-                    let def = hir::Function::from_source(db, src)?;
-                    (Def(def.into()), it.visibility())
-                }
-            },
-            ast::ConstDef(it) => {
-                if parent.parent().and_then(ast::ItemList::cast).is_some() {
-                    let src = hir::Source { file_id, ast: ast::ImplItem::from(it.clone()) };
-                    let item = hir::AssocItem::from_source(db, src)?;
-                    (AssocItem(item), it.visibility())
-                } else {
-                    let src = hir::Source { file_id, ast: it.clone() };
-                    let def = hir::Const::from_source(db, src)?;
-                    (Def(def.into()), it.visibility())
-                }
-            },
-            ast::TypeAliasDef(it) => {
-                if parent.parent().and_then(ast::ItemList::cast).is_some() {
-                    let src = hir::Source { file_id, ast: ast::ImplItem::from(it.clone()) };
-                    let item = hir::AssocItem::from_source(db, src)?;
-                    (AssocItem(item), it.visibility())
-                } else {
-                    let src = hir::Source { file_id, ast: it.clone() };
-                    let def = hir::TypeAlias::from_source(db, src)?;
-                    (Def(def.into()), it.visibility())
-                }
-            },
-            ast::Module(it) => {
-                let src = hir::Source { file_id, ast: hir::ModuleSource::Module(it.clone()) };
-                let def = hir::Module::from_definition(db, src)?;
-                (Def(def.into()), it.visibility())
-            },
-            ast::StructDef(it) => {
-                let src = hir::Source { file_id, ast: it.clone() };
-                let def = hir::Struct::from_source(db, src)?;
-                (Def(def.into()), it.visibility())
+                StructField::from_def(db, file_id, it)
             },
-            ast::EnumDef(it) => {
-                let src = hir::Source { file_id, ast: it.clone() };
-                let def = hir::Enum::from_source(db, src)?;
-                (Def(def.into()), it.visibility())
-            },
-            ast::TraitDef(it) => {
-                let src = hir::Source { file_id, ast: it.clone() };
-                let def = hir::Trait::from_source(db, src)?;
-                (Def(def.into()), it.visibility())
-            },
-            ast::StaticDef(it) => {
-                let src = hir::Source { file_id, ast: it.clone() };
-                let def = hir::Static::from_source(db, src)?;
-                (Def(def.into()), it.visibility())
+            ast::ImplItem(it) => {
+                AssocItem::from_def(db, file_id, it.clone()).or_else(|| {
+                    match it {
+                        ast::ImplItem::FnDef(f) => ModuleDef::from_def(db, file_id, f.into()),
+                        ast::ImplItem::ConstDef(c) => ModuleDef::from_def(db, file_id, c.into()),
+                        ast::ImplItem::TypeAliasDef(a) => ModuleDef::from_def(db, file_id, a.into()),
+                    }
+                })
             },
             ast::EnumVariant(it) => {
                 let src = hir::Source { file_id, ast: it.clone() };
-                let def = hir::EnumVariant::from_source(db, src)?;
-                (Def(def.into()), it.parent_enum().visibility())
+                let def: ModuleDef = EnumVariant::from_source(db, src)?.into();
+                Some(def.declaration(db))
             },
-            _ => {
-                return None;
+            ast::ModuleItem(it) => {
+                ModuleDef::from_def(db, file_id, it)
             },
+            _ => None,
         }
-    };
-    Some(Declaration { item, container, visibility })
+    }
+}
+
+fn decl_from_pat(
+    db: &RootDatabase,
+    file_id: HirFileId,
+    pat: AstPtr<ast::BindPat>,
+) -> Option<Declaration> {
+    let root = db.parse_or_expand(file_id)?;
+    // FIXME: use match_ast!
+    let def = pat.to_node(&root).syntax().ancestors().find_map(|node| {
+        if let Some(it) = ast::FnDef::cast(node.clone()) {
+            let src = hir::Source { file_id, ast: it };
+            Some(hir::Function::from_source(db, src)?.into())
+        } else if let Some(it) = ast::ConstDef::cast(node.clone()) {
+            let src = hir::Source { file_id, ast: it };
+            Some(hir::Const::from_source(db, src)?.into())
+        } else if let Some(it) = ast::StaticDef::cast(node.clone()) {
+            let src = hir::Source { file_id, ast: it };
+            Some(hir::Static::from_source(db, src)?.into())
+        } else {
+            None
+        }
+    })?;
+    let item = NameKind::Pat((def, pat));
+    let container = def.module(db).definition_source(db).ast;
+    Some(Declaration { item, container, visibility: None })
+}
+
+impl HasDeclaration for StructField {
+    type Def = ast::RecordFieldDef;
+    type Ref = ast::FieldExpr;
+
+    fn declaration(self, db: &RootDatabase) -> Declaration {
+        let item = NameKind::FieldAccess(self);
+        let parent = self.parent_def(db);
+        let container = parent.module(db).definition_source(db).ast;
+        let visibility = match parent {
+            VariantDef::Struct(s) => s.source(db).ast.visibility(),
+            VariantDef::EnumVariant(e) => e.source(db).ast.parent_enum().visibility(),
+        };
+        Declaration { item, container, visibility }
+    }
+
+    fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Declaration> {
+        let src = hir::Source { file_id, ast: hir::FieldSource::Named(def) };
+        let field = StructField::from_source(db, src)?;
+        Some(field.declaration(db))
+    }
+
+    fn from_ref(
+        db: &RootDatabase,
+        analyzer: &SourceAnalyzer,
+        refer: Self::Ref,
+    ) -> Option<Declaration> {
+        let field = analyzer.resolve_field(&refer)?;
+        Some(field.declaration(db))
+    }
+}
+
+impl HasDeclaration for AssocItem {
+    type Def = ast::ImplItem;
+    type Ref = ast::MethodCallExpr;
+
+    fn declaration(self, db: &RootDatabase) -> Declaration {
+        let item = NameKind::AssocItem(self);
+        let container = self.module(db).definition_source(db).ast;
+        let visibility = match self {
+            AssocItem::Function(f) => f.source(db).ast.visibility(),
+            AssocItem::Const(c) => c.source(db).ast.visibility(),
+            AssocItem::TypeAlias(a) => a.source(db).ast.visibility(),
+        };
+        Declaration { item, container, visibility }
+    }
+
+    fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Declaration> {
+        let src = hir::Source { file_id, ast: def };
+        let item = AssocItem::from_source(db, src)?;
+        Some(item.declaration(db))
+    }
+
+    fn from_ref(
+        db: &RootDatabase,
+        analyzer: &SourceAnalyzer,
+        refer: Self::Ref,
+    ) -> Option<Declaration> {
+        let func: AssocItem = analyzer.resolve_method_call(&refer)?.into();
+        Some(func.declaration(db))
+    }
 }
+
+impl HasDeclaration for ModuleDef {
+    type Def = ast::ModuleItem;
+    type Ref = ast::Path;
+
+    fn declaration(self, db: &RootDatabase) -> Declaration {
+        // FIXME: use macro
+        let (container, visibility) = match self {
+            ModuleDef::Module(it) => {
+                let container =
+                    it.parent(db).or_else(|| Some(it)).unwrap().definition_source(db).ast;
+                let visibility = it.declaration_source(db).and_then(|s| s.ast.visibility());
+                (container, visibility)
+            }
+            ModuleDef::EnumVariant(it) => {
+                let container = it.module(db).definition_source(db).ast;
+                let visibility = it.source(db).ast.parent_enum().visibility();
+                (container, visibility)
+            }
+            ModuleDef::Function(it) => {
+                (it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
+            }
+            ModuleDef::Const(it) => {
+                (it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
+            }
+            ModuleDef::Static(it) => {
+                (it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
+            }
+            ModuleDef::Trait(it) => {
+                (it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
+            }
+            ModuleDef::TypeAlias(it) => {
+                (it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
+            }
+            ModuleDef::Adt(Adt::Struct(it)) => {
+                (it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
+            }
+            ModuleDef::Adt(Adt::Union(it)) => {
+                (it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
+            }
+            ModuleDef::Adt(Adt::Enum(it)) => {
+                (it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
+            }
+            ModuleDef::BuiltinType(..) => unreachable!(),
+        };
+        let item = NameKind::Def(self);
+        Declaration { item, container, visibility }
+    }
+
+    fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Declaration> {
+        let src = hir::Source { file_id, ast: def };
+        let def = ModuleDef::from_source(db, src)?;
+        Some(def.declaration(db))
+    }
+
+    fn from_ref(
+        db: &RootDatabase,
+        analyzer: &SourceAnalyzer,
+        refer: Self::Ref,
+    ) -> Option<Declaration> {
+        None
+    }
+}
+
+// FIXME: impl HasDeclaration for hir::MacroDef
index 6777aa5f390a9c845c1c6d8c6e1ef4eb77e15b23..24453452bf631b644278197ce235c8bfd2c3e152 100644 (file)
@@ -69,13 +69,13 @@ pub(crate) fn find_all_refs(
             Some((def_id, _)) => NavigationTarget::from_adt_def(db, def_id),
             None => return None,
         },
-        Pat(pat) => NavigationTarget::from_pat(db, position.file_id, pat),
+        Pat((_, pat)) => NavigationTarget::from_pat(db, position.file_id, pat),
         SelfParam(par) => NavigationTarget::from_self_param(position.file_id, par),
         GenericParam(_) => return None,
     };
 
     let references = match name_kind {
-        Pat(pat) => analyzer
+        Pat((_, pat)) => analyzer
             .find_all_refs(&pat.to_node(&syntax))
             .into_iter()
             .map(move |ref_desc| FileRange { file_id: position.file_id, range: ref_desc.range })
@@ -99,7 +99,7 @@ fn find_name<'a>(
         let name_ref = find_node_at_offset::<ast::NameRef>(&syntax, position.offset)?;
         let range = name_ref.syntax().text_range();
         let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
-        let name_kind = classify_name_ref(db, &analyzer, &name_ref)?;
+        let name_kind = classify_name_ref(db, position.file_id, &analyzer, &name_ref)?.item;
         Some(RangeInfo::new(range, (analyzer, name_kind)))
     }
 }
index 98373a49c6d56e24a03a448e1eea1c5f599a9e3e..8be93e27ea221d62f54943e0ed94bbcb72d8c73c 100644 (file)
@@ -103,7 +103,9 @@ fn hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 {
                 if let Some(name_ref) = node.as_node().cloned().and_then(ast::NameRef::cast) {
                     // FIXME: try to reuse the SourceAnalyzers
                     let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
-                    match classify_name_ref(db, &analyzer, &name_ref) {
+                    let name_kind = classify_name_ref(db, file_id, &analyzer, &name_ref)
+                        .and_then(|d| Some(d.item));
+                    match name_kind {
                         Some(Macro(_)) => "macro",
                         Some(FieldAccess(_)) => "field",
                         Some(AssocItem(hir::AssocItem::Function(_))) => "function",
@@ -119,7 +121,7 @@ fn hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 {
                         Some(Def(hir::ModuleDef::TypeAlias(_))) => "type",
                         Some(Def(hir::ModuleDef::BuiltinType(_))) => "type",
                         Some(SelfType(_)) => "type",
-                        Some(Pat(ptr)) => {
+                        Some(Pat((_, ptr))) => {
                             let pat = ptr.to_node(&root);
                             if let Some(name) = pat.name() {
                                 let text = name.text();