From fda118f4e90fb3341f0aa2a6dbb583acbf39aabd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 22 Feb 2020 16:57:29 +0100 Subject: Simplify --- crates/ra_ide/src/display.rs | 2 +- crates/ra_ide/src/display/navigation_target.rs | 66 ++++++----- crates/ra_ide/src/goto_definition.rs | 149 +++---------------------- crates/ra_ide/src/references.rs | 12 +- 4 files changed, 58 insertions(+), 171 deletions(-) diff --git a/crates/ra_ide/src/display.rs b/crates/ra_ide/src/display.rs index fbe89841b..1c26a8697 100644 --- a/crates/ra_ide/src/display.rs +++ b/crates/ra_ide/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::ToNav; +pub(crate) use navigation_target::{ToNav, TryToNav}; pub(crate) use short_label::ShortLabel; pub(crate) fn function_label(node: &ast::FnDef) -> String { diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs index 096c41c81..b42cb477e 100644 --- a/crates/ra_ide/src/display/navigation_target.rs +++ b/crates/ra_ide/src/display/navigation_target.rs @@ -11,7 +11,7 @@ use ra_syntax::{ TextRange, }; -use crate::{expand::original_range, FileSymbol}; +use crate::{expand::original_range, references::NameDefinition, FileSymbol}; use super::short_label::ShortLabel; @@ -36,6 +36,10 @@ pub(crate) trait ToNav { fn to_nav(&self, db: &RootDatabase) -> NavigationTarget; } +pub(crate) trait TryToNav { + fn try_to_nav(&self, db: &RootDatabase) -> Option; +} + impl NavigationTarget { /// When `focus_range` is specified, returns it. otherwise /// returns `full_range` @@ -96,26 +100,6 @@ impl NavigationTarget { module.to_nav(db) } - pub(crate) fn from_def( - db: &RootDatabase, - module_def: hir::ModuleDef, - ) -> Option { - let nav = match module_def { - 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; - } - }; - Some(nav) - } - #[cfg(test)] pub(crate) fn assert_match(&self, expected: &str) { let actual = self.debug_render(); @@ -201,6 +185,36 @@ impl ToNav for FileSymbol { } } +impl TryToNav for NameDefinition { + fn try_to_nav(&self, db: &RootDatabase) -> Option { + match self { + NameDefinition::Macro(it) => Some(it.to_nav(db)), + NameDefinition::StructField(it) => Some(it.to_nav(db)), + NameDefinition::ModuleDef(it) => it.try_to_nav(db), + NameDefinition::SelfType(it) => Some(it.to_nav(db)), + NameDefinition::Local(it) => Some(it.to_nav(db)), + NameDefinition::TypeParam(it) => Some(it.to_nav(db)), + } + } +} + +impl TryToNav for hir::ModuleDef { + fn try_to_nav(&self, db: &RootDatabase) -> Option { + let res = match self { + hir::ModuleDef::Module(it) => it.to_nav(db), + hir::ModuleDef::Function(it) => it.to_nav(db), + hir::ModuleDef::Adt(it) => it.to_nav(db), + hir::ModuleDef::EnumVariant(it) => it.to_nav(db), + hir::ModuleDef::Const(it) => it.to_nav(db), + hir::ModuleDef::Static(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, + }; + Some(res) + } +} + pub(crate) trait ToNavFromAst {} impl ToNavFromAst for hir::Function {} impl ToNavFromAst for hir::Const {} @@ -232,15 +246,17 @@ 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(); - let syntax = match &src.value { - ModuleSource::SourceFile(node) => node.syntax(), - ModuleSource::Module(node) => node.syntax(), + let (syntax, focus) = match &src.value { + ModuleSource::SourceFile(node) => (node.syntax(), None), + ModuleSource::Module(node) => { + (node.syntax(), node.name().map(|it| it.syntax().text_range())) + } }; let frange = original_range(db, src.with_value(syntax)); NavigationTarget::from_syntax( frange.file_id, name, - None, + focus, frange.range, syntax.kind(), None, diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index cce539e56..feff1ec3f 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -1,18 +1,18 @@ //! FIXME: write short doc here use hir::{db::AstDatabase, InFile, SourceBinder}; -use ra_ide_db::{defs::NameDefinition, symbol_index, RootDatabase}; +use ra_ide_db::{symbol_index, RootDatabase}; use ra_syntax::{ - ast::{self, DocCommentsOwner}, + ast::{self}, match_ast, AstNode, SyntaxKind::*, - SyntaxNode, SyntaxToken, TokenAtOffset, + SyntaxToken, TokenAtOffset, }; use crate::{ - display::{ShortLabel, ToNav}, + display::{ToNav, TryToNav}, expand::descend_into_macros, - references::classify_name_ref, + references::{classify_name, classify_name_ref}, FilePosition, NavigationTarget, RangeInfo, }; @@ -74,23 +74,12 @@ pub(crate) fn reference_definition( use self::ReferenceResult::*; let name_kind = classify_name_ref(sb, name_ref); - match name_kind { - Some(NameDefinition::Macro(it)) => return Exact(it.to_nav(sb.db)), - Some(NameDefinition::StructField(it)) => return Exact(it.to_nav(sb.db)), - Some(NameDefinition::TypeParam(it)) => return Exact(it.to_nav(sb.db)), - Some(NameDefinition::Local(it)) => return Exact(it.to_nav(sb.db)), - Some(NameDefinition::ModuleDef(def)) => match NavigationTarget::from_def(sb.db, def) { - Some(nav) => return Exact(nav), - None => return Approximate(vec![]), - }, - Some(NameDefinition::SelfType(imp)) => { - // FIXME: ideally, this should point to the type in the impl, and - // not at the whole impl. And goto **type** definition should bring - // us to the actual type - return Exact(imp.to_nav(sb.db)); - } - None => {} - }; + if let Some(def) = name_kind { + return match def.try_to_nav(sb.db) { + Some(nav) => ReferenceResult::Exact(nav), + None => ReferenceResult::Approximate(Vec::new()), + }; + } // Fallback index based approach: let navs = symbol_index::index_resolve(sb.db, name_ref.value) @@ -104,119 +93,9 @@ fn name_definition( sb: &mut SourceBinder, name: InFile<&ast::Name>, ) -> Option> { - let parent = name.value.syntax().parent()?; - - if let Some(module) = ast::Module::cast(parent.clone()) { - if module.has_semi() { - let src = name.with_value(module); - if let Some(child_module) = sb.to_def(src) { - let nav = child_module.to_nav(sb.db); - return Some(vec![nav]); - } - } - } - - if let Some(nav) = named_target(sb.db, name.with_value(&parent)) { - return Some(vec![nav]); - } - - None -} - -fn named_target(db: &RootDatabase, node: InFile<&SyntaxNode>) -> Option { - match_ast! { - match (node.value) { - ast::StructDef(it) => { - Some(NavigationTarget::from_named( - db, - node.with_value(&it), - it.doc_comment_text(), - it.short_label(), - )) - }, - ast::EnumDef(it) => { - Some(NavigationTarget::from_named( - db, - node.with_value(&it), - it.doc_comment_text(), - it.short_label(), - )) - }, - ast::EnumVariant(it) => { - Some(NavigationTarget::from_named( - db, - node.with_value(&it), - it.doc_comment_text(), - it.short_label(), - )) - }, - ast::FnDef(it) => { - Some(NavigationTarget::from_named( - db, - node.with_value(&it), - it.doc_comment_text(), - it.short_label(), - )) - }, - ast::TypeAliasDef(it) => { - Some(NavigationTarget::from_named( - db, - node.with_value(&it), - it.doc_comment_text(), - it.short_label(), - )) - }, - ast::ConstDef(it) => { - Some(NavigationTarget::from_named( - db, - node.with_value(&it), - it.doc_comment_text(), - it.short_label(), - )) - }, - ast::StaticDef(it) => { - Some(NavigationTarget::from_named( - db, - node.with_value(&it), - it.doc_comment_text(), - it.short_label(), - )) - }, - ast::TraitDef(it) => { - Some(NavigationTarget::from_named( - db, - node.with_value(&it), - it.doc_comment_text(), - it.short_label(), - )) - }, - ast::RecordFieldDef(it) => { - Some(NavigationTarget::from_named( - db, - node.with_value(&it), - it.doc_comment_text(), - it.short_label(), - )) - }, - ast::Module(it) => { - Some(NavigationTarget::from_named( - db, - node.with_value(&it), - it.doc_comment_text(), - it.short_label(), - )) - }, - ast::MacroCall(it) => { - Some(NavigationTarget::from_named( - db, - node.with_value(&it), - it.doc_comment_text(), - None, - )) - }, - _ => None, - } - } + let def = classify_name(sb, name)?; + let nav = def.try_to_nav(sb.db)?; + Some(vec![nav]) } #[cfg(test)] diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs index 7f790a62d..aadc2dbcb 100644 --- a/crates/ra_ide/src/references.rs +++ b/crates/ra_ide/src/references.rs @@ -25,7 +25,7 @@ use ra_syntax::{ match_ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, TextUnit, TokenAtOffset, }; -use crate::{display::ToNav, FilePosition, FileRange, NavigationTarget, RangeInfo}; +use crate::{display::TryToNav, FilePosition, FileRange, NavigationTarget, RangeInfo}; pub(crate) use self::{ classify::{classify_name, classify_name_ref}, @@ -125,15 +125,7 @@ pub(crate) fn find_all_refs( }; let RangeInfo { range, info: (name, def) } = find_name(db, &syntax, position, opt_name)?; - - let declaration = match def { - NameDefinition::Macro(mac) => mac.to_nav(db), - NameDefinition::StructField(field) => field.to_nav(db), - NameDefinition::ModuleDef(def) => NavigationTarget::from_def(db, def)?, - NameDefinition::SelfType(imp) => imp.to_nav(db), - NameDefinition::Local(local) => local.to_nav(db), - NameDefinition::TypeParam(_) => return None, - }; + let declaration = def.try_to_nav(db)?; let search_scope = { let base = SearchScope::for_def(&def, db); -- cgit v1.2.3