From 3b7cf9226d9cbadb6a45bc2a95617e35db11e742 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 16 Nov 2019 13:50:04 +0300 Subject: Source-ify name_definition --- crates/ra_ide_api/src/goto_definition.rs | 39 ++++++++++++++++---------------- 1 file changed, 19 insertions(+), 20 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 5b7fb3c15..821796e5f 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use hir::Source; -use ra_db::{FileId, SourceDatabase}; +use ra_db::SourceDatabase; use ra_syntax::{ algo::find_node_at_offset, ast::{self, DocCommentsOwner}, @@ -27,7 +27,7 @@ pub(crate) fn goto_definition( return Some(RangeInfo::new(name_ref.syntax().text_range(), navs.to_vec())); } if let Some(name) = find_node_at_offset::(&syntax, position.offset) { - let navs = name_definition(db, position.file_id, &name)?; + let navs = name_definition(db, Source::new(position.file_id.into(), &name))?; return Some(RangeInfo::new(name.syntax().text_range(), navs)); } None @@ -86,14 +86,13 @@ pub(crate) fn reference_definition( pub(crate) fn name_definition( db: &RootDatabase, - file_id: FileId, - name: &ast::Name, + name: Source<&ast::Name>, ) -> Option> { - let parent = name.syntax().parent()?; + let parent = name.ast.syntax().parent()?; if let Some(module) = ast::Module::cast(parent.clone()) { if module.has_semi() { - let src = hir::Source { file_id: file_id.into(), ast: module }; + let src = name.with_ast(module); if let Some(child_module) = hir::Module::from_declaration(db, src) { let nav = child_module.to_nav(db); return Some(vec![nav]); @@ -101,20 +100,20 @@ pub(crate) fn name_definition( } } - if let Some(nav) = named_target(db, file_id, &parent) { + if let Some(nav) = named_target(db, name.with_ast(&parent)) { return Some(vec![nav]); } None } -fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option { +fn named_target(db: &RootDatabase, node: Source<&SyntaxNode>) -> Option { match_ast! { - match node { + match (node.ast) { ast::StructDef(it) => { Some(NavigationTarget::from_named( db, - file_id.into(), + node.file_id, &it, it.doc_comment_text(), it.short_label(), @@ -123,7 +122,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option ast::EnumDef(it) => { Some(NavigationTarget::from_named( db, - file_id.into(), + node.file_id, &it, it.doc_comment_text(), it.short_label(), @@ -132,7 +131,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option ast::EnumVariant(it) => { Some(NavigationTarget::from_named( db, - file_id.into(), + node.file_id, &it, it.doc_comment_text(), it.short_label(), @@ -141,7 +140,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option ast::FnDef(it) => { Some(NavigationTarget::from_named( db, - file_id.into(), + node.file_id, &it, it.doc_comment_text(), it.short_label(), @@ -150,7 +149,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option ast::TypeAliasDef(it) => { Some(NavigationTarget::from_named( db, - file_id.into(), + node.file_id, &it, it.doc_comment_text(), it.short_label(), @@ -159,7 +158,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option ast::ConstDef(it) => { Some(NavigationTarget::from_named( db, - file_id.into(), + node.file_id, &it, it.doc_comment_text(), it.short_label(), @@ -168,7 +167,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option ast::StaticDef(it) => { Some(NavigationTarget::from_named( db, - file_id.into(), + node.file_id, &it, it.doc_comment_text(), it.short_label(), @@ -177,7 +176,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option ast::TraitDef(it) => { Some(NavigationTarget::from_named( db, - file_id.into(), + node.file_id, &it, it.doc_comment_text(), it.short_label(), @@ -186,7 +185,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option ast::RecordFieldDef(it) => { Some(NavigationTarget::from_named( db, - file_id.into(), + node.file_id, &it, it.doc_comment_text(), it.short_label(), @@ -195,7 +194,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option ast::Module(it) => { Some(NavigationTarget::from_named( db, - file_id.into(), + node.file_id, &it, it.doc_comment_text(), it.short_label(), @@ -204,7 +203,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option ast::MacroCall(it) => { Some(NavigationTarget::from_named( db, - file_id.into(), + node.file_id, &it, it.doc_comment_text(), None, -- cgit v1.2.3