From 372439dec88f8ce3350f29aa56801c8f30e59abe Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 19 Feb 2020 14:56:22 +0100 Subject: Merge NameDefinition and NameKind --- crates/ra_ide/src/goto_definition.rs | 18 ++++----- crates/ra_ide/src/hover.rs | 24 ++++++------ crates/ra_ide/src/references.rs | 32 ++++++++-------- crates/ra_ide/src/references/classify.rs | 25 +++---------- crates/ra_ide/src/references/search_scope.rs | 4 +- crates/ra_ide/src/syntax_highlighting.rs | 44 +++++++++++----------- crates/ra_ide_db/src/defs.rs | 55 ++++++++++------------------ crates/ra_ide_db/src/imports_locator.rs | 8 ++-- 8 files changed, 91 insertions(+), 119 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index de5551a4c..cce539e56 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use hir::{db::AstDatabase, InFile, SourceBinder}; -use ra_ide_db::{symbol_index, RootDatabase}; +use ra_ide_db::{defs::NameDefinition, symbol_index, RootDatabase}; use ra_syntax::{ ast::{self, DocCommentsOwner}, match_ast, AstNode, @@ -12,7 +12,7 @@ use ra_syntax::{ use crate::{ display::{ShortLabel, ToNav}, expand::descend_into_macros, - references::{classify_name_ref, NameKind::*}, + references::classify_name_ref, FilePosition, NavigationTarget, RangeInfo, }; @@ -73,17 +73,17 @@ pub(crate) fn reference_definition( ) -> ReferenceResult { use self::ReferenceResult::*; - let name_kind = classify_name_ref(sb, name_ref).map(|d| d.kind); + let name_kind = classify_name_ref(sb, name_ref); match name_kind { - Some(Macro(it)) => return Exact(it.to_nav(sb.db)), - Some(StructField(it)) => return Exact(it.to_nav(sb.db)), - Some(TypeParam(it)) => return Exact(it.to_nav(sb.db)), - Some(Local(it)) => return Exact(it.to_nav(sb.db)), - Some(ModuleDef(def)) => match NavigationTarget::from_def(sb.db, def) { + 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(SelfType(imp)) => { + 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 diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index 6d4416c0b..1c6ca36df 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -2,7 +2,7 @@ use hir::{db::AstDatabase, Adt, HasSource, HirDisplay, SourceBinder}; use ra_db::SourceDatabase; -use ra_ide_db::RootDatabase; +use ra_ide_db::{defs::NameDefinition, RootDatabase}; use ra_syntax::{ algo::find_covering_element, ast::{self, DocCommentsOwner}, @@ -14,7 +14,7 @@ use ra_syntax::{ use crate::{ display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel}, expand::{descend_into_macros, original_range}, - references::{classify_name, classify_name_ref, NameKind, NameKind::*}, + references::{classify_name, classify_name_ref}, FilePosition, FileRange, RangeInfo, }; @@ -92,20 +92,20 @@ fn hover_text(docs: Option, desc: Option) -> Option { } } -fn hover_text_from_name_kind(db: &RootDatabase, name_kind: NameKind) -> Option { - return match name_kind { - Macro(it) => { +fn hover_text_from_name_kind(db: &RootDatabase, def: NameDefinition) -> Option { + return match def { + NameDefinition::Macro(it) => { let src = it.source(db); hover_text(src.value.doc_comment_text(), Some(macro_label(&src.value))) } - StructField(it) => { + NameDefinition::StructField(it) => { let src = it.source(db); match src.value { hir::FieldSource::Named(it) => hover_text(it.doc_comment_text(), it.short_label()), _ => None, } } - ModuleDef(it) => match it { + NameDefinition::ModuleDef(it) => match it { hir::ModuleDef::Module(it) => match it.definition_source(db).value { hir::ModuleSource::Module(it) => { hover_text(it.doc_comment_text(), it.short_label()) @@ -123,8 +123,10 @@ fn hover_text_from_name_kind(db: &RootDatabase, name_kind: NameKind) -> Option from_def_source(db, it), hir::ModuleDef::BuiltinType(it) => Some(it.to_string()), }, - Local(it) => Some(rust_code_markup(it.ty(db).display_truncated(db, None).to_string())), - TypeParam(_) | SelfType(_) => { + NameDefinition::Local(it) => { + Some(rust_code_markup(it.ty(db).display_truncated(db, None).to_string())) + } + NameDefinition::TypeParam(_) | NameDefinition::SelfType(_) => { // FIXME: Hover for generic param None } @@ -151,10 +153,10 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option { - classify_name_ref(&mut sb, token.with_value(&name_ref)).map(|d| (name_ref.syntax().clone(), d.kind)) + classify_name_ref(&mut sb, token.with_value(&name_ref)).map(|d| (name_ref.syntax().clone(), d)) }, ast::Name(name) => { - classify_name(&mut sb, token.with_value(&name)).map(|d| (name.syntax().clone(), d.kind)) + classify_name(&mut sb, token.with_value(&name)).map(|d| (name.syntax().clone(), d)) }, _ => None, } diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs index 97c08ade5..7f790a62d 100644 --- a/crates/ra_ide/src/references.rs +++ b/crates/ra_ide/src/references.rs @@ -31,7 +31,7 @@ pub(crate) use self::{ classify::{classify_name, classify_name_ref}, rename::rename, }; -pub(crate) use ra_ide_db::defs::{NameDefinition, NameKind}; +pub(crate) use ra_ide_db::defs::NameDefinition; pub use self::search_scope::SearchScope; @@ -126,13 +126,13 @@ pub(crate) fn find_all_refs( let RangeInfo { range, info: (name, def) } = find_name(db, &syntax, position, opt_name)?; - let declaration = match def.kind { - NameKind::Macro(mac) => mac.to_nav(db), - NameKind::StructField(field) => field.to_nav(db), - NameKind::ModuleDef(def) => NavigationTarget::from_def(db, def)?, - NameKind::SelfType(imp) => imp.to_nav(db), - NameKind::Local(local) => local.to_nav(db), - NameKind::TypeParam(_) => return None, + 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 search_scope = { @@ -148,7 +148,7 @@ pub(crate) fn find_all_refs( let declaration = Declaration { nav: declaration, kind: ReferenceKind::Other, - access: decl_access(&def.kind, &name, &syntax, decl_range), + access: decl_access(&def, &name, &syntax, decl_range), }; let references = process_definition(db, def, name, search_scope) @@ -247,7 +247,7 @@ fn process_definition( refs.push(Reference { file_range: FileRange { file_id, range }, kind, - access: reference_access(&d.kind, &name_ref.value), + access: reference_access(&d, &name_ref.value), }); } } @@ -257,13 +257,13 @@ fn process_definition( } fn decl_access( - kind: &NameKind, + def: &NameDefinition, name: &str, syntax: &SyntaxNode, range: TextRange, ) -> Option { - match kind { - NameKind::Local(_) | NameKind::StructField(_) => {} + match def { + NameDefinition::Local(_) | NameDefinition::StructField(_) => {} _ => return None, }; @@ -280,10 +280,10 @@ fn decl_access( None } -fn reference_access(kind: &NameKind, name_ref: &ast::NameRef) -> Option { +fn reference_access(def: &NameDefinition, name_ref: &ast::NameRef) -> Option { // Only Locals and Fields have accesses for now. - match kind { - NameKind::Local(_) | NameKind::StructField(_) => {} + match def { + NameDefinition::Local(_) | NameDefinition::StructField(_) => {} _ => return None, }; diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs index ca5750521..478e18871 100644 --- a/crates/ra_ide/src/references/classify.rs +++ b/crates/ra_ide/src/references/classify.rs @@ -5,7 +5,7 @@ use ra_prof::profile; use ra_syntax::{ast, AstNode}; use test_utils::tested_by; -use super::{NameDefinition, NameKind}; +use super::NameDefinition; use ra_ide_db::RootDatabase; pub use ra_ide_db::defs::{classify_name, from_module_def, from_struct_field}; @@ -46,8 +46,7 @@ pub(crate) fn classify_name_ref( if let Some(macro_def) = analyzer.resolve_macro_call(sb.db, name_ref.with_value(¯o_call)) { - let kind = NameKind::Macro(macro_def); - return Some(NameDefinition { kind }); + return Some(NameDefinition::Macro(macro_def)); } } @@ -63,22 +62,10 @@ pub(crate) fn classify_name_ref( }; from_module_def(def) } - PathResolution::Local(local) => { - let kind = NameKind::Local(local); - NameDefinition { kind } - } - PathResolution::TypeParam(par) => { - let kind = NameKind::TypeParam(par); - NameDefinition { kind } - } - PathResolution::Macro(def) => { - let kind = NameKind::Macro(def); - NameDefinition { kind } - } - PathResolution::SelfType(impl_block) => { - let kind = NameKind::SelfType(impl_block); - NameDefinition { kind } - } + PathResolution::Local(local) => NameDefinition::Local(local), + PathResolution::TypeParam(par) => NameDefinition::TypeParam(par), + PathResolution::Macro(def) => NameDefinition::Macro(def), + PathResolution::SelfType(impl_block) => NameDefinition::SelfType(impl_block), }; Some(res) } diff --git a/crates/ra_ide/src/references/search_scope.rs b/crates/ra_ide/src/references/search_scope.rs index e5ac12044..27d483233 100644 --- a/crates/ra_ide/src/references/search_scope.rs +++ b/crates/ra_ide/src/references/search_scope.rs @@ -12,7 +12,7 @@ use rustc_hash::FxHashMap; use ra_ide_db::RootDatabase; -use super::{NameDefinition, NameKind}; +use super::NameDefinition; pub struct SearchScope { entries: FxHashMap>, @@ -32,7 +32,7 @@ impl SearchScope { let module_src = module.definition_source(db); let file_id = module_src.file_id.original_file(db); - if let NameKind::Local(var) = def.kind { + if let NameDefinition::Local(var) = def { let range = match var.parent(db) { DefWithBody::Function(f) => f.source(db).value.syntax().text_range(), DefWithBody::Const(c) => c.source(db).value.syntax().text_range(), diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index 8e793e479..d873f153e 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs @@ -2,7 +2,7 @@ use hir::{HirFileId, InFile, Name, SourceAnalyzer, SourceBinder}; use ra_db::SourceDatabase; -use ra_ide_db::RootDatabase; +use ra_ide_db::{defs::NameDefinition, RootDatabase}; use ra_prof::profile; use ra_syntax::{ ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, SyntaxToken, TextRange, @@ -12,7 +12,7 @@ use rustc_hash::FxHashMap; use crate::{ expand::descend_into_macros_with_analyzer, - references::{classify_name, classify_name_ref, NameKind}, + references::{classify_name, classify_name_ref}, FileId, }; @@ -186,10 +186,10 @@ fn highlight_node( NAME_REF if node.value.ancestors().any(|it| it.kind() == ATTR) => return None, NAME_REF => { let name_ref = node.value.as_node().cloned().and_then(ast::NameRef::cast).unwrap(); - let name_kind = classify_name_ref(sb, node.with_value(&name_ref)).map(|d| d.kind); + let name_kind = classify_name_ref(sb, node.with_value(&name_ref)); match name_kind { Some(name_kind) => { - if let NameKind::Local(local) = &name_kind { + if let NameDefinition::Local(local) = &name_kind { if let Some(name) = local.name(db) { let shadow_count = bindings_shadow_count.entry(name.clone()).or_default(); @@ -205,9 +205,9 @@ fn highlight_node( } NAME => { let name = node.value.as_node().cloned().and_then(ast::Name::cast).unwrap(); - let name_kind = classify_name(sb, node.with_value(&name)).map(|d| d.kind); + let name_kind = classify_name(sb, node.with_value(&name)); - if let Some(NameKind::Local(local)) = &name_kind { + if let Some(NameDefinition::Local(local)) = &name_kind { if let Some(name) = local.name(db) { let shadow_count = bindings_shadow_count.entry(name.clone()).or_default(); *shadow_count += 1; @@ -310,22 +310,22 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo buf } -fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { - match name_kind { - NameKind::Macro(_) => tags::MACRO, - NameKind::StructField(_) => tags::FIELD, - NameKind::ModuleDef(hir::ModuleDef::Module(_)) => tags::MODULE, - NameKind::ModuleDef(hir::ModuleDef::Function(_)) => tags::FUNCTION, - NameKind::ModuleDef(hir::ModuleDef::Adt(_)) => tags::TYPE, - NameKind::ModuleDef(hir::ModuleDef::EnumVariant(_)) => tags::CONSTANT, - NameKind::ModuleDef(hir::ModuleDef::Const(_)) => tags::CONSTANT, - NameKind::ModuleDef(hir::ModuleDef::Static(_)) => tags::CONSTANT, - NameKind::ModuleDef(hir::ModuleDef::Trait(_)) => tags::TYPE, - NameKind::ModuleDef(hir::ModuleDef::TypeAlias(_)) => tags::TYPE, - NameKind::ModuleDef(hir::ModuleDef::BuiltinType(_)) => tags::TYPE_BUILTIN, - NameKind::SelfType(_) => tags::TYPE_SELF, - NameKind::TypeParam(_) => tags::TYPE_PARAM, - NameKind::Local(local) => { +fn highlight_name(db: &RootDatabase, def: NameDefinition) -> &'static str { + match def { + NameDefinition::Macro(_) => tags::MACRO, + NameDefinition::StructField(_) => tags::FIELD, + NameDefinition::ModuleDef(hir::ModuleDef::Module(_)) => tags::MODULE, + NameDefinition::ModuleDef(hir::ModuleDef::Function(_)) => tags::FUNCTION, + NameDefinition::ModuleDef(hir::ModuleDef::Adt(_)) => tags::TYPE, + NameDefinition::ModuleDef(hir::ModuleDef::EnumVariant(_)) => tags::CONSTANT, + NameDefinition::ModuleDef(hir::ModuleDef::Const(_)) => tags::CONSTANT, + NameDefinition::ModuleDef(hir::ModuleDef::Static(_)) => tags::CONSTANT, + NameDefinition::ModuleDef(hir::ModuleDef::Trait(_)) => tags::TYPE, + NameDefinition::ModuleDef(hir::ModuleDef::TypeAlias(_)) => tags::TYPE, + NameDefinition::ModuleDef(hir::ModuleDef::BuiltinType(_)) => tags::TYPE_BUILTIN, + NameDefinition::SelfType(_) => tags::TYPE_SELF, + NameDefinition::TypeParam(_) => tags::TYPE_PARAM, + NameDefinition::Local(local) => { if local.is_mut(db) || local.ty(db).is_mutable_reference() { tags::VARIABLE_MUT } else { diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs index aec748abf..04c214624 100644 --- a/crates/ra_ide_db/src/defs.rs +++ b/crates/ra_ide_db/src/defs.rs @@ -18,7 +18,7 @@ use ra_syntax::{ use crate::RootDatabase; #[derive(Debug, PartialEq, Eq)] -pub enum NameKind { +pub enum NameDefinition { Macro(MacroDef), StructField(StructField), ModuleDef(ModuleDef), @@ -27,33 +27,26 @@ pub enum NameKind { TypeParam(TypeParam), } -#[derive(PartialEq, Eq)] -pub struct NameDefinition { - /// FIXME: this doesn't really make sense. For example, builtin types don't - /// really have a module. - pub kind: NameKind, -} - impl NameDefinition { pub fn module(&self, db: &RootDatabase) -> Option { - match self.kind { - NameKind::Macro(it) => it.module(db), - NameKind::StructField(it) => Some(it.parent_def(db).module(db)), - NameKind::ModuleDef(it) => it.module(db), - NameKind::SelfType(it) => Some(it.module(db)), - NameKind::Local(it) => Some(it.module(db)), - NameKind::TypeParam(it) => Some(it.module(db)), + match self { + NameDefinition::Macro(it) => it.module(db), + NameDefinition::StructField(it) => Some(it.parent_def(db).module(db)), + NameDefinition::ModuleDef(it) => it.module(db), + NameDefinition::SelfType(it) => Some(it.module(db)), + NameDefinition::Local(it) => Some(it.module(db)), + NameDefinition::TypeParam(it) => Some(it.module(db)), } } pub fn visibility(&self, db: &RootDatabase) -> Option { - match self.kind { - NameKind::Macro(_) => None, - NameKind::StructField(sf) => match sf.source(db).value { + match self { + NameDefinition::Macro(_) => None, + NameDefinition::StructField(sf) => match sf.source(db).value { FieldSource::Named(it) => it.visibility(), FieldSource::Pos(it) => it.visibility(), }, - NameKind::ModuleDef(def) => match def { + NameDefinition::ModuleDef(def) => match def { ModuleDef::Module(it) => it.declaration_source(db)?.value.visibility(), ModuleDef::Function(it) => it.source(db).value.visibility(), ModuleDef::Adt(adt) => match adt { @@ -68,9 +61,9 @@ impl NameDefinition { ModuleDef::EnumVariant(_) => None, ModuleDef::BuiltinType(_) => None, }, - NameKind::SelfType(_) => None, - NameKind::Local(_) => None, - NameKind::TypeParam(_) => None, + NameDefinition::SelfType(_) => None, + NameDefinition::Local(_) => None, + NameDefinition::TypeParam(_) => None, } } } @@ -87,9 +80,7 @@ pub fn classify_name( ast::BindPat(it) => { let src = name.with_value(it); let local = sb.to_def(src)?; - Some(NameDefinition { - kind: NameKind::Local(local), - }) + Some(NameDefinition::Local(local)) }, ast::RecordFieldDef(it) => { let src = name.with_value(it); @@ -144,16 +135,12 @@ pub fn classify_name( let src = name.with_value(it); let def = sb.to_def(src.clone())?; - Some(NameDefinition { - kind: NameKind::Macro(def), - }) + Some(NameDefinition::Macro(def)) }, ast::TypeParam(it) => { let src = name.with_value(it); let def = sb.to_def(src)?; - Some(NameDefinition { - kind: NameKind::TypeParam(def), - }) + Some(NameDefinition::TypeParam(def)) }, _ => None, } @@ -161,11 +148,9 @@ pub fn classify_name( } pub fn from_struct_field(field: StructField) -> NameDefinition { - let kind = NameKind::StructField(field); - NameDefinition { kind } + NameDefinition::StructField(field) } pub fn from_module_def(def: ModuleDef) -> NameDefinition { - let kind = NameKind::ModuleDef(def); - NameDefinition { kind } + NameDefinition::ModuleDef(def) } diff --git a/crates/ra_ide_db/src/imports_locator.rs b/crates/ra_ide_db/src/imports_locator.rs index 86383bcd0..b8dd358a9 100644 --- a/crates/ra_ide_db/src/imports_locator.rs +++ b/crates/ra_ide_db/src/imports_locator.rs @@ -6,8 +6,7 @@ use ra_prof::profile; use ra_syntax::{ast, AstNode, SyntaxKind::NAME}; use crate::{ - defs::classify_name, - defs::NameKind, + defs::{classify_name, NameDefinition}, symbol_index::{self, FileSymbol, Query}, RootDatabase, }; @@ -44,7 +43,7 @@ impl<'a> ImportsLocator<'a> { .chain(lib_results.into_iter()) .filter_map(|import_candidate| self.get_name_definition(db, &import_candidate)) .filter_map(|name_definition_to_import| match name_definition_to_import { - NameKind::ModuleDef(module_def) => Some(module_def), + NameDefinition::ModuleDef(module_def) => Some(module_def), _ => None, }) .collect() @@ -54,7 +53,7 @@ impl<'a> ImportsLocator<'a> { &mut self, db: &impl HirDatabase, import_candidate: &FileSymbol, - ) -> Option { + ) -> Option { let _p = profile("get_name_definition"); let file_id = import_candidate.file_id.into(); let candidate_node = import_candidate.ptr.to_node(&db.parse_or_expand(file_id)?); @@ -67,6 +66,5 @@ impl<'a> ImportsLocator<'a> { &mut self.source_binder, hir::InFile { file_id, value: &ast::Name::cast(candidate_name_node)? }, ) - .map(|it| it.kind) } } -- cgit v1.2.3