From 4deba88c33c470f084c531fa979fe5684d37f757 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 11 Nov 2019 11:15:19 +0300 Subject: Introduce ToNav trait --- crates/ra_ide_api/src/display.rs | 2 +- crates/ra_ide_api/src/display/navigation_target.rs | 328 +++++++++++---------- crates/ra_ide_api/src/goto_definition.rs | 16 +- crates/ra_ide_api/src/goto_type_definition.rs | 4 +- crates/ra_ide_api/src/impls.rs | 11 +- crates/ra_ide_api/src/lib.rs | 4 +- crates/ra_ide_api/src/references.rs | 12 +- 7 files changed, 200 insertions(+), 177 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs index a980c56bc..30617412a 100644 --- a/crates/ra_ide_api/src/display.rs +++ b/crates/ra_ide_api/src/display.rs @@ -15,7 +15,7 @@ pub use function_signature::FunctionSignature; pub use navigation_target::NavigationTarget; pub use structure::{file_structure, StructureNode}; -pub(crate) use navigation_target::{description_from_symbol, docs_from_symbol}; +pub(crate) use navigation_target::{description_from_symbol, docs_from_symbol, ToNav}; pub(crate) use short_label::ShortLabel; pub(crate) fn function_label(node: &ast::FnDef) -> String { diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 1bf81e7d5..41d467564 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -29,19 +29,8 @@ pub struct NavigationTarget { docs: Option, } -fn find_range_from_node( - db: &RootDatabase, - src: hir::HirFileId, - node: &SyntaxNode, -) -> (FileId, TextRange) { - let text_range = node.text_range(); - let (file_id, text_range) = src - .expansion_info(db) - .and_then(|expansion_info| expansion_info.find_range(text_range)) - .unwrap_or((src, text_range)); - - // FIXME: handle recursive macro generated macro - (file_id.original_file(db), text_range) +pub(crate) trait ToNav { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget; } impl NavigationTarget { @@ -95,19 +84,6 @@ impl NavigationTarget { NavigationTarget::from_named(db, file_id.into(), pat, None, None) } - pub(crate) fn from_symbol(db: &RootDatabase, symbol: FileSymbol) -> NavigationTarget { - NavigationTarget { - file_id: symbol.file_id, - name: symbol.name.clone(), - kind: symbol.ptr.kind(), - full_range: symbol.ptr.range(), - focus_range: symbol.name_range, - container_name: symbol.container_name.clone(), - description: description_from_symbol(db, &symbol), - docs: docs_from_symbol(db, &symbol), - } - } - pub(crate) fn from_pat( db: &RootDatabase, file_id: FileId, @@ -136,39 +112,6 @@ impl NavigationTarget { } } - pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget { - let src = module.definition_source(db); - let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); - match src.ast { - ModuleSource::SourceFile(node) => { - let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax()); - - NavigationTarget::from_syntax( - file_id, - name, - None, - text_range, - node.syntax(), - None, - None, - ) - } - ModuleSource::Module(node) => { - let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax()); - - NavigationTarget::from_syntax( - file_id, - name, - None, - text_range, - node.syntax(), - node.doc_comment_text(), - node.short_label(), - ) - } - } - } - pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget { let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); if let Some(src) = module.declaration_source(db) { @@ -183,55 +126,7 @@ impl NavigationTarget { src.ast.short_label(), ); } - NavigationTarget::from_module(db, module) - } - - pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget { - let src = field.source(db); - match src.ast { - FieldSource::Named(it) => NavigationTarget::from_named( - db, - src.file_id, - &it, - it.doc_comment_text(), - it.short_label(), - ), - FieldSource::Pos(it) => { - let (file_id, text_range) = find_range_from_node(db, src.file_id, it.syntax()); - NavigationTarget::from_syntax( - file_id, - "".into(), - None, - text_range, - it.syntax(), - None, - None, - ) - } - } - } - - pub(crate) fn from_def_source(db: &RootDatabase, def: D) -> NavigationTarget - where - D: HasSource, - A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel, - { - let src = def.source(db); - NavigationTarget::from_named( - db, - src.file_id, - &src.ast, - src.ast.doc_comment_text(), - src.ast.short_label(), - ) - } - - pub(crate) fn from_adt_def(db: &RootDatabase, adt_def: hir::Adt) -> NavigationTarget { - match adt_def { - hir::Adt::Struct(it) => NavigationTarget::from_def_source(db, it), - hir::Adt::Union(it) => NavigationTarget::from_def_source(db, it), - hir::Adt::Enum(it) => NavigationTarget::from_def_source(db, it), - } + module.to_nav(db) } pub(crate) fn from_def( @@ -239,14 +134,14 @@ impl NavigationTarget { module_def: hir::ModuleDef, ) -> Option { let nav = match module_def { - hir::ModuleDef::Module(module) => NavigationTarget::from_module(db, module), - hir::ModuleDef::Function(func) => NavigationTarget::from_def_source(db, func), - hir::ModuleDef::Adt(it) => NavigationTarget::from_adt_def(db, it), - hir::ModuleDef::Const(it) => NavigationTarget::from_def_source(db, it), - hir::ModuleDef::Static(it) => NavigationTarget::from_def_source(db, it), - hir::ModuleDef::EnumVariant(it) => NavigationTarget::from_def_source(db, it), - hir::ModuleDef::Trait(it) => NavigationTarget::from_def_source(db, it), - hir::ModuleDef::TypeAlias(it) => NavigationTarget::from_def_source(db, it), + hir::ModuleDef::Module(module) => module.to_nav(db), + hir::ModuleDef::Function(it) => it.to_nav(db), + hir::ModuleDef::Adt(it) => it.to_nav(db), + hir::ModuleDef::Const(it) => it.to_nav(db), + hir::ModuleDef::Static(it) => it.to_nav(db), + hir::ModuleDef::EnumVariant(it) => it.to_nav(db), + hir::ModuleDef::Trait(it) => it.to_nav(db), + hir::ModuleDef::TypeAlias(it) => it.to_nav(db), hir::ModuleDef::BuiltinType(..) => { return None; } @@ -254,41 +149,6 @@ impl NavigationTarget { Some(nav) } - pub(crate) fn from_impl_block( - db: &RootDatabase, - impl_block: hir::ImplBlock, - ) -> NavigationTarget { - let src = impl_block.source(db); - let (file_id, text_range) = find_range_from_node(db, src.file_id, src.ast.syntax()); - - NavigationTarget::from_syntax( - file_id, - "impl".into(), - None, - text_range, - src.ast.syntax(), - None, - None, - ) - } - - pub(crate) fn from_assoc_item( - db: &RootDatabase, - assoc_item: hir::AssocItem, - ) -> NavigationTarget { - match assoc_item { - AssocItem::Function(it) => NavigationTarget::from_def_source(db, it), - AssocItem::Const(it) => NavigationTarget::from_def_source(db, it), - AssocItem::TypeAlias(it) => NavigationTarget::from_def_source(db, it), - } - } - - pub(crate) fn from_macro_def(db: &RootDatabase, macro_call: hir::MacroDef) -> NavigationTarget { - let src = macro_call.source(db); - log::debug!("nav target {:#?}", src.ast.syntax()); - NavigationTarget::from_named(db, src.file_id, &src.ast, src.ast.doc_comment_text(), None) - } - #[cfg(test)] pub(crate) fn assert_match(&self, expected: &str) { let actual = self.debug_render(); @@ -359,6 +219,172 @@ impl NavigationTarget { } } +impl ToNav for FileSymbol { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + NavigationTarget { + file_id: self.file_id, + name: self.name.clone(), + kind: self.ptr.kind(), + full_range: self.ptr.range(), + focus_range: self.name_range, + container_name: self.container_name.clone(), + description: description_from_symbol(db, self), + docs: docs_from_symbol(db, self), + } + } +} + +pub(crate) trait ToNavFromAst {} +impl ToNavFromAst for hir::Function {} +impl ToNavFromAst for hir::Const {} +impl ToNavFromAst for hir::Static {} +impl ToNavFromAst for hir::Struct {} +impl ToNavFromAst for hir::Enum {} +impl ToNavFromAst for hir::EnumVariant {} +impl ToNavFromAst for hir::Union {} +impl ToNavFromAst for hir::TypeAlias {} +impl ToNavFromAst for hir::Trait {} + +impl ToNav for D +where + D: HasSource + ToNavFromAst + Copy, + D::Ast: ast::DocCommentsOwner + ast::NameOwner + ShortLabel, +{ + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + let src = self.source(db); + NavigationTarget::from_named( + db, + src.file_id, + &src.ast, + src.ast.doc_comment_text(), + src.ast.short_label(), + ) + } +} + +impl ToNav for hir::Module { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + let src = self.definition_source(db); + let name = self.name(db).map(|it| it.to_string().into()).unwrap_or_default(); + match src.ast { + ModuleSource::SourceFile(node) => { + let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax()); + + NavigationTarget::from_syntax( + file_id, + name, + None, + text_range, + node.syntax(), + None, + None, + ) + } + ModuleSource::Module(node) => { + let (file_id, text_range) = find_range_from_node(db, src.file_id, node.syntax()); + + NavigationTarget::from_syntax( + file_id, + name, + None, + text_range, + node.syntax(), + node.doc_comment_text(), + node.short_label(), + ) + } + } + } +} + +impl ToNav for hir::ImplBlock { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + let src = self.source(db); + let (file_id, text_range) = find_range_from_node(db, src.file_id, src.ast.syntax()); + + NavigationTarget::from_syntax( + file_id, + "impl".into(), + None, + text_range, + src.ast.syntax(), + None, + None, + ) + } +} + +impl ToNav for hir::StructField { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + let src = self.source(db); + + match src.ast { + FieldSource::Named(it) => NavigationTarget::from_named( + db, + src.file_id, + &it, + it.doc_comment_text(), + it.short_label(), + ), + FieldSource::Pos(it) => { + let (file_id, text_range) = find_range_from_node(db, src.file_id, it.syntax()); + NavigationTarget::from_syntax( + file_id, + "".into(), + None, + text_range, + it.syntax(), + None, + None, + ) + } + } + } +} + +impl ToNav for hir::MacroDef { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + let src = self.source(db); + log::debug!("nav target {:#?}", src.ast.syntax()); + NavigationTarget::from_named(db, src.file_id, &src.ast, src.ast.doc_comment_text(), None) + } +} + +impl ToNav for hir::Adt { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + match self { + hir::Adt::Struct(it) => it.to_nav(db), + hir::Adt::Union(it) => it.to_nav(db), + hir::Adt::Enum(it) => it.to_nav(db), + } + } +} + +impl ToNav for hir::AssocItem { + fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { + match self { + AssocItem::Function(it) => it.to_nav(db), + AssocItem::Const(it) => it.to_nav(db), + AssocItem::TypeAlias(it) => it.to_nav(db), + } + } +} + +fn find_range_from_node( + db: &RootDatabase, + src: hir::HirFileId, + node: &SyntaxNode, +) -> (FileId, TextRange) { + let text_range = node.text_range(); + let (file_id, text_range) = src + .expansion_info(db) + .and_then(|expansion_info| expansion_info.find_range(text_range)) + .unwrap_or((src, text_range)); + + // FIXME: handle recursive macro generated macro + (file_id.original_file(db), text_range) +} + pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option { let parse = db.parse(symbol.file_id); let node = symbol.ptr.to_node(parse.tree().syntax()); diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index afa59cbe3..713b61d5e 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -9,7 +9,7 @@ use ra_syntax::{ use crate::{ db::RootDatabase, - display::ShortLabel, + display::{ShortLabel, ToNav}, references::{classify_name_ref, NameKind::*}, FilePosition, NavigationTarget, RangeInfo, }; @@ -56,16 +56,16 @@ pub(crate) fn reference_definition( let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind); match name_kind { - Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)), - Some(Field(field)) => return Exact(NavigationTarget::from_field(db, field)), - Some(AssocItem(assoc)) => return Exact(NavigationTarget::from_assoc_item(db, assoc)), + Some(Macro(mac)) => return Exact(mac.to_nav(db)), + Some(Field(field)) => return Exact(field.to_nav(db)), + Some(AssocItem(assoc)) => return Exact(assoc.to_nav(db)), Some(Def(def)) => match NavigationTarget::from_def(db, def) { Some(nav) => return Exact(nav), None => return Approximate(vec![]), }, Some(SelfType(ty)) => { - if let Some((def_id, _)) = ty.as_adt() { - return Exact(NavigationTarget::from_adt_def(db, def_id)); + if let Some((adt, _)) = ty.as_adt() { + return Exact(adt.to_nav(db)); } } Some(Pat((_, pat))) => return Exact(NavigationTarget::from_pat(db, file_id, pat)), @@ -79,7 +79,7 @@ pub(crate) fn reference_definition( // Fallback index based approach: let navs = crate::symbol_index::index_resolve(db, name_ref) .into_iter() - .map(|s| NavigationTarget::from_symbol(db, s)) + .map(|s| s.to_nav(db)) .collect(); Approximate(navs) } @@ -95,7 +95,7 @@ pub(crate) fn name_definition( if module.has_semi() { let src = hir::Source { file_id: file_id.into(), ast: module }; if let Some(child_module) = hir::Module::from_declaration(db, src) { - let nav = NavigationTarget::from_module(db, child_module); + let nav = child_module.to_nav(db); return Some(vec![nav]); } } diff --git a/crates/ra_ide_api/src/goto_type_definition.rs b/crates/ra_ide_api/src/goto_type_definition.rs index 059d80524..71146591d 100644 --- a/crates/ra_ide_api/src/goto_type_definition.rs +++ b/crates/ra_ide_api/src/goto_type_definition.rs @@ -3,7 +3,7 @@ use ra_db::SourceDatabase; use ra_syntax::{ast, AstNode}; -use crate::{db::RootDatabase, FilePosition, NavigationTarget, RangeInfo}; +use crate::{db::RootDatabase, display::ToNav, FilePosition, NavigationTarget, RangeInfo}; pub(crate) fn goto_type_definition( db: &RootDatabase, @@ -33,7 +33,7 @@ pub(crate) fn goto_type_definition( let adt_def = analyzer.autoderef(db, ty).find_map(|ty| ty.as_adt().map(|adt| adt.0))?; - let nav = NavigationTarget::from_adt_def(db, adt_def); + let nav = adt_def.to_nav(db); Some(RangeInfo::new(node.text_range(), vec![nav])) } diff --git a/crates/ra_ide_api/src/impls.rs b/crates/ra_ide_api/src/impls.rs index b899ed3a5..bc9b66550 100644 --- a/crates/ra_ide_api/src/impls.rs +++ b/crates/ra_ide_api/src/impls.rs @@ -4,7 +4,7 @@ use hir::{db::HirDatabase, ApplicationTy, FromSource, Ty, TypeCtor}; use ra_db::SourceDatabase; use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; -use crate::{db::RootDatabase, FilePosition, NavigationTarget, RangeInfo}; +use crate::{db::RootDatabase, display::ToNav, FilePosition, NavigationTarget, RangeInfo}; pub(crate) fn goto_implementation( db: &RootDatabase, @@ -58,7 +58,7 @@ fn impls_for_def( impls .all_impls() .filter(|impl_block| is_equal_for_find_impls(&ty, &impl_block.target_ty(db))) - .map(|imp| NavigationTarget::from_impl_block(db, imp)) + .map(|imp| imp.to_nav(db)) .collect(), ) } @@ -75,12 +75,7 @@ fn impls_for_trait( let krate = module.krate(); let impls = db.impls_in_crate(krate); - Some( - impls - .lookup_impl_blocks_for_trait(tr) - .map(|imp| NavigationTarget::from_impl_block(db, imp)) - .collect(), - ) + Some(impls.lookup_impl_blocks_for_trait(tr).map(|imp| imp.to_nav(db)).collect()) } fn is_equal_for_find_impls(original_ty: &Ty, impl_ty: &Ty) -> bool { diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index d0188da44..484fbcc82 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -56,7 +56,7 @@ use ra_db::{ }; use ra_syntax::{SourceFile, TextRange, TextUnit}; -use crate::{db::LineIndexDatabase, symbol_index::FileSymbol}; +use crate::{db::LineIndexDatabase, display::ToNav, symbol_index::FileSymbol}; pub use crate::{ assists::{Assist, AssistId}, @@ -351,7 +351,7 @@ impl Analysis { self.with_db(|db| { symbol_index::world_symbols(db, query) .into_iter() - .map(|s| NavigationTarget::from_symbol(db, s)) + .map(|s| s.to_nav(db)) .collect::>() }) } diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index b5b1c9a16..0a76bf484 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs @@ -19,7 +19,9 @@ use ra_db::{SourceDatabase, SourceDatabaseExt}; use ra_prof::profile; use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, SyntaxNode, TextUnit}; -use crate::{db::RootDatabase, FilePosition, FileRange, NavigationTarget, RangeInfo}; +use crate::{ + db::RootDatabase, display::ToNav, FilePosition, FileRange, NavigationTarget, RangeInfo, +}; pub(crate) use self::{ classify::{classify_name, classify_name_ref}, @@ -76,12 +78,12 @@ pub(crate) fn find_all_refs( let RangeInfo { range, info: (name, def) } = find_name(db, &syntax, position)?; let declaration = match def.kind { - NameKind::Macro(mac) => NavigationTarget::from_macro_def(db, mac), - NameKind::Field(field) => NavigationTarget::from_field(db, field), - NameKind::AssocItem(assoc) => NavigationTarget::from_assoc_item(db, assoc), + NameKind::Macro(mac) => mac.to_nav(db), + NameKind::Field(field) => field.to_nav(db), + NameKind::AssocItem(assoc) => assoc.to_nav(db), NameKind::Def(def) => NavigationTarget::from_def(db, def)?, NameKind::SelfType(ref ty) => match ty.as_adt() { - Some((def_id, _)) => NavigationTarget::from_adt_def(db, def_id), + Some((adt, _)) => adt.to_nav(db), None => return None, }, NameKind::Pat((_, pat)) => NavigationTarget::from_pat(db, position.file_id, pat), -- cgit v1.2.3