3427: Rename NameDefinition -> Definition r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-03-03 17:41:25 +00:00 committed by GitHub
commit 074474fe00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 104 additions and 106 deletions

View file

@ -13,7 +13,7 @@ use ra_syntax::{
use crate::{
// expand::original_range,
references::NameDefinition,
references::Definition,
FileSymbol,
};
@ -189,15 +189,15 @@ impl ToNav for FileSymbol {
}
}
impl TryToNav for NameDefinition {
impl TryToNav for Definition {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
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)),
Definition::Macro(it) => Some(it.to_nav(db)),
Definition::StructField(it) => Some(it.to_nav(db)),
Definition::ModuleDef(it) => it.try_to_nav(db),
Definition::SelfType(it) => Some(it.to_nav(db)),
Definition::Local(it) => Some(it.to_nav(db)),
Definition::TypeParam(it) => Some(it.to_nav(db)),
}
}
}

View file

@ -2,7 +2,7 @@
use hir::{Adt, HasSource, HirDisplay, Semantics};
use ra_ide_db::{
defs::{classify_name, NameDefinition},
defs::{classify_name, Definition},
RootDatabase,
};
use ra_syntax::{
@ -92,20 +92,20 @@ fn hover_text(docs: Option<String>, desc: Option<String>) -> Option<String> {
}
}
fn hover_text_from_name_kind(db: &RootDatabase, def: NameDefinition) -> Option<String> {
fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<String> {
return match def {
NameDefinition::Macro(it) => {
Definition::Macro(it) => {
let src = it.source(db);
hover_text(src.value.doc_comment_text(), Some(macro_label(&src.value)))
}
NameDefinition::StructField(it) => {
Definition::StructField(it) => {
let src = it.source(db);
match src.value {
hir::FieldSource::Named(it) => hover_text(it.doc_comment_text(), it.short_label()),
_ => None,
}
}
NameDefinition::ModuleDef(it) => match it {
Definition::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,10 +123,10 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: NameDefinition) -> Option<S
hir::ModuleDef::TypeAlias(it) => from_def_source(db, it),
hir::ModuleDef::BuiltinType(it) => Some(it.to_string()),
},
NameDefinition::Local(it) => {
Definition::Local(it) => {
Some(rust_code_markup(it.ty(db).display_truncated(db, None).to_string()))
}
NameDefinition::TypeParam(_) | NameDefinition::SelfType(_) => {
Definition::TypeParam(_) | Definition::SelfType(_) => {
// FIXME: Hover for generic param
None
}

View file

@ -31,7 +31,7 @@ pub(crate) use self::{
classify::{classify_name_ref, NameRefClass},
rename::rename,
};
pub(crate) use ra_ide_db::defs::{classify_name, NameDefinition};
pub(crate) use ra_ide_db::defs::{classify_name, Definition};
pub use self::search_scope::SearchScope;
@ -145,7 +145,7 @@ pub(crate) fn find_all_refs(
pub(crate) fn find_refs_to_def(
db: &RootDatabase,
def: &NameDefinition,
def: &Definition,
search_scope: Option<SearchScope>,
) -> Vec<Reference> {
let search_scope = {
@ -169,7 +169,7 @@ fn find_name(
syntax: &SyntaxNode,
position: FilePosition,
opt_name: Option<ast::Name>,
) -> Option<RangeInfo<NameDefinition>> {
) -> Option<RangeInfo<Definition>> {
if let Some(name) = opt_name {
let def = classify_name(sema, &name)?.definition();
let range = name.syntax().text_range();
@ -183,7 +183,7 @@ fn find_name(
fn process_definition(
db: &RootDatabase,
def: &NameDefinition,
def: &Definition,
name: String,
scope: SearchScope,
) -> Vec<Reference> {
@ -250,13 +250,9 @@ fn process_definition(
refs
}
fn decl_access(
def: &NameDefinition,
syntax: &SyntaxNode,
range: TextRange,
) -> Option<ReferenceAccess> {
fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Option<ReferenceAccess> {
match def {
NameDefinition::Local(_) | NameDefinition::StructField(_) => {}
Definition::Local(_) | Definition::StructField(_) => {}
_ => return None,
};
@ -273,10 +269,10 @@ fn decl_access(
None
}
fn reference_access(def: &NameDefinition, name_ref: &ast::NameRef) -> Option<ReferenceAccess> {
fn reference_access(def: &Definition, name_ref: &ast::NameRef) -> Option<ReferenceAccess> {
// Only Locals and Fields have accesses for now.
match def {
NameDefinition::Local(_) | NameDefinition::StructField(_) => {}
Definition::Local(_) | Definition::StructField(_) => {}
_ => return None,
};

View file

@ -1,22 +1,22 @@
//! Functions that are used to classify an element from its definition or reference.
use hir::{Local, PathResolution, Semantics};
use ra_ide_db::defs::NameDefinition;
use ra_ide_db::defs::Definition;
use ra_ide_db::RootDatabase;
use ra_prof::profile;
use ra_syntax::{ast, AstNode};
use test_utils::tested_by;
pub enum NameRefClass {
NameDefinition(NameDefinition),
FieldShorthand { local: Local, field: NameDefinition },
Definition(Definition),
FieldShorthand { local: Local, field: Definition },
}
impl NameRefClass {
pub fn definition(self) -> NameDefinition {
pub fn definition(self) -> Definition {
match self {
NameRefClass::NameDefinition(def) => def,
NameRefClass::FieldShorthand { local, field: _ } => NameDefinition::Local(local),
NameRefClass::Definition(def) => def,
NameRefClass::FieldShorthand { local, field: _ } => Definition::Local(local),
}
}
}
@ -32,14 +32,14 @@ pub(crate) fn classify_name_ref(
if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
tested_by!(goto_def_for_methods);
if let Some(func) = sema.resolve_method_call(&method_call) {
return Some(NameRefClass::NameDefinition(NameDefinition::ModuleDef(func.into())));
return Some(NameRefClass::Definition(Definition::ModuleDef(func.into())));
}
}
if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) {
tested_by!(goto_def_for_fields);
if let Some(field) = sema.resolve_field(&field_expr) {
return Some(NameRefClass::NameDefinition(NameDefinition::StructField(field)));
return Some(NameRefClass::Definition(Definition::StructField(field)));
}
}
@ -47,9 +47,9 @@ pub(crate) fn classify_name_ref(
tested_by!(goto_def_for_record_fields);
tested_by!(goto_def_for_field_init_shorthand);
if let Some((field, local)) = sema.resolve_record_field(&record_field) {
let field = NameDefinition::StructField(field);
let field = Definition::StructField(field);
let res = match local {
None => NameRefClass::NameDefinition(field),
None => NameRefClass::Definition(field),
Some(local) => NameRefClass::FieldShorthand { field, local },
};
return Some(res);
@ -59,26 +59,26 @@ pub(crate) fn classify_name_ref(
if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
tested_by!(goto_def_for_macros);
if let Some(macro_def) = sema.resolve_macro_call(&macro_call) {
return Some(NameRefClass::NameDefinition(NameDefinition::Macro(macro_def)));
return Some(NameRefClass::Definition(Definition::Macro(macro_def)));
}
}
let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
let resolved = sema.resolve_path(&path)?;
let res = match resolved {
PathResolution::Def(def) => NameDefinition::ModuleDef(def),
PathResolution::Def(def) => Definition::ModuleDef(def),
PathResolution::AssocItem(item) => {
let def = match item {
hir::AssocItem::Function(it) => it.into(),
hir::AssocItem::Const(it) => it.into(),
hir::AssocItem::TypeAlias(it) => it.into(),
};
NameDefinition::ModuleDef(def)
Definition::ModuleDef(def)
}
PathResolution::Local(local) => NameDefinition::Local(local),
PathResolution::TypeParam(par) => NameDefinition::TypeParam(par),
PathResolution::Macro(def) => NameDefinition::Macro(def),
PathResolution::SelfType(impl_def) => NameDefinition::SelfType(impl_def),
PathResolution::Local(local) => Definition::Local(local),
PathResolution::TypeParam(par) => Definition::TypeParam(par),
PathResolution::Macro(def) => Definition::Macro(def),
PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def),
};
Some(NameRefClass::NameDefinition(res))
Some(NameRefClass::Definition(res))
}

View file

@ -12,7 +12,7 @@ use rustc_hash::FxHashMap;
use ra_ide_db::RootDatabase;
use super::NameDefinition;
use super::Definition;
pub struct SearchScope {
entries: FxHashMap<FileId, Option<TextRange>>,
@ -23,7 +23,7 @@ impl SearchScope {
SearchScope { entries: FxHashMap::default() }
}
pub(crate) fn for_def(def: &NameDefinition, db: &RootDatabase) -> SearchScope {
pub(crate) fn for_def(def: &Definition, db: &RootDatabase) -> SearchScope {
let _p = profile("search_scope");
let module = match def.module(db) {
Some(it) => it,
@ -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 NameDefinition::Local(var) = def {
if let Definition::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(),

View file

@ -7,7 +7,7 @@ mod tests;
use hir::{Name, Semantics};
use ra_ide_db::{
defs::{classify_name, NameClass, NameDefinition},
defs::{classify_name, Definition, NameClass},
RootDatabase,
};
use ra_prof::profile;
@ -172,7 +172,7 @@ fn highlight_element(
let name = element.into_node().and_then(ast::Name::cast).unwrap();
let name_kind = classify_name(sema, &name);
if let Some(NameClass::NameDefinition(NameDefinition::Local(local))) = &name_kind {
if let Some(NameClass::Definition(Definition::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;
@ -181,7 +181,7 @@ fn highlight_element(
};
match name_kind {
Some(NameClass::NameDefinition(def)) => {
Some(NameClass::Definition(def)) => {
highlight_name(db, def) | HighlightModifier::Definition
}
Some(NameClass::ConstReference(def)) => highlight_name(db, def),
@ -196,8 +196,8 @@ fn highlight_element(
let name_kind = classify_name_ref(sema, &name_ref)?;
match name_kind {
NameRefClass::NameDefinition(def) => {
if let NameDefinition::Local(local) = &def {
NameRefClass::Definition(def) => {
if let Definition::Local(local) = &def {
if let Some(name) = local.name(db) {
let shadow_count =
bindings_shadow_count.entry(name.clone()).or_default();
@ -260,11 +260,11 @@ fn highlight_element(
}
}
fn highlight_name(db: &RootDatabase, def: NameDefinition) -> Highlight {
fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight {
match def {
NameDefinition::Macro(_) => HighlightTag::Macro,
NameDefinition::StructField(_) => HighlightTag::Field,
NameDefinition::ModuleDef(def) => match def {
Definition::Macro(_) => HighlightTag::Macro,
Definition::StructField(_) => HighlightTag::Field,
Definition::ModuleDef(def) => match def {
hir::ModuleDef::Module(_) => HighlightTag::Module,
hir::ModuleDef::Function(_) => HighlightTag::Function,
hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Struct,
@ -277,10 +277,10 @@ fn highlight_name(db: &RootDatabase, def: NameDefinition) -> Highlight {
hir::ModuleDef::TypeAlias(_) => HighlightTag::TypeAlias,
hir::ModuleDef::BuiltinType(_) => HighlightTag::BuiltinType,
},
NameDefinition::SelfType(_) => HighlightTag::SelfType,
NameDefinition::TypeParam(_) => HighlightTag::TypeParam,
Definition::SelfType(_) => HighlightTag::SelfType,
Definition::TypeParam(_) => HighlightTag::TypeParam,
// FIXME: distinguish between locals and parameters
NameDefinition::Local(local) => {
Definition::Local(local) => {
let mut h = Highlight::new(HighlightTag::Local);
if local.is_mut(db) || local.ty(db).is_mutable_reference() {
h |= HighlightModifier::Mutable;

View file

@ -17,8 +17,9 @@ use ra_syntax::{
use crate::RootDatabase;
// FIXME: a more precise name would probably be `Symbol`?
#[derive(Debug, PartialEq, Eq)]
pub enum NameDefinition {
pub enum Definition {
Macro(MacroDef),
StructField(StructField),
ModuleDef(ModuleDef),
@ -27,26 +28,26 @@ pub enum NameDefinition {
TypeParam(TypeParam),
}
impl NameDefinition {
impl Definition {
pub fn module(&self, db: &RootDatabase) -> Option<Module> {
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)),
Definition::Macro(it) => it.module(db),
Definition::StructField(it) => Some(it.parent_def(db).module(db)),
Definition::ModuleDef(it) => it.module(db),
Definition::SelfType(it) => Some(it.module(db)),
Definition::Local(it) => Some(it.module(db)),
Definition::TypeParam(it) => Some(it.module(db)),
}
}
pub fn visibility(&self, db: &RootDatabase) -> Option<ast::Visibility> {
match self {
NameDefinition::Macro(_) => None,
NameDefinition::StructField(sf) => match sf.source(db).value {
Definition::Macro(_) => None,
Definition::StructField(sf) => match sf.source(db).value {
FieldSource::Named(it) => it.visibility(),
FieldSource::Pos(it) => it.visibility(),
},
NameDefinition::ModuleDef(def) => match def {
Definition::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 {
@ -61,17 +62,17 @@ impl NameDefinition {
ModuleDef::EnumVariant(_) => None,
ModuleDef::BuiltinType(_) => None,
},
NameDefinition::SelfType(_) => None,
NameDefinition::Local(_) => None,
NameDefinition::TypeParam(_) => None,
Definition::SelfType(_) => None,
Definition::Local(_) => None,
Definition::TypeParam(_) => None,
}
}
pub fn name(&self, db: &RootDatabase) -> Option<Name> {
let name = match self {
NameDefinition::Macro(it) => it.name(db)?,
NameDefinition::StructField(it) => it.name(db),
NameDefinition::ModuleDef(def) => match def {
Definition::Macro(it) => it.name(db)?,
Definition::StructField(it) => it.name(db),
Definition::ModuleDef(def) => match def {
hir::ModuleDef::Module(it) => it.name(db)?,
hir::ModuleDef::Function(it) => it.name(db),
hir::ModuleDef::Adt(def) => match def {
@ -86,31 +87,31 @@ impl NameDefinition {
hir::ModuleDef::TypeAlias(it) => it.name(db),
hir::ModuleDef::BuiltinType(_) => return None,
},
NameDefinition::SelfType(_) => return None,
NameDefinition::Local(it) => it.name(db)?,
NameDefinition::TypeParam(it) => it.name(db),
Definition::SelfType(_) => return None,
Definition::Local(it) => it.name(db)?,
Definition::TypeParam(it) => it.name(db),
};
Some(name)
}
}
pub enum NameClass {
NameDefinition(NameDefinition),
Definition(Definition),
/// `None` in `if let None = Some(82) {}`
ConstReference(NameDefinition),
ConstReference(Definition),
}
impl NameClass {
pub fn into_definition(self) -> Option<NameDefinition> {
pub fn into_definition(self) -> Option<Definition> {
match self {
NameClass::NameDefinition(it) => Some(it),
NameClass::Definition(it) => Some(it),
NameClass::ConstReference(_) => None,
}
}
pub fn definition(self) -> NameDefinition {
pub fn definition(self) -> Definition {
match self {
NameClass::NameDefinition(it) | NameClass::ConstReference(it) => it,
NameClass::Definition(it) | NameClass::ConstReference(it) => it,
}
}
}
@ -118,14 +119,14 @@ impl NameClass {
pub fn classify_name(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<NameClass> {
if let Some(bind_pat) = name.syntax().parent().and_then(ast::BindPat::cast) {
if let Some(def) = sema.resolve_bind_pat_to_const(&bind_pat) {
return Some(NameClass::ConstReference(NameDefinition::ModuleDef(def)));
return Some(NameClass::ConstReference(Definition::ModuleDef(def)));
}
}
classify_name_inner(sema, name).map(NameClass::NameDefinition)
classify_name_inner(sema, name).map(NameClass::Definition)
}
fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<NameDefinition> {
fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<Definition> {
let _p = profile("classify_name");
let parent = name.syntax().parent()?;
@ -133,59 +134,59 @@ fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Opti
match parent {
ast::BindPat(it) => {
let local = sema.to_def(&it)?;
Some(NameDefinition::Local(local))
Some(Definition::Local(local))
},
ast::RecordFieldDef(it) => {
let field: hir::StructField = sema.to_def(&it)?;
Some(NameDefinition::StructField(field))
Some(Definition::StructField(field))
},
ast::Module(it) => {
let def = sema.to_def(&it)?;
Some(NameDefinition::ModuleDef(def.into()))
Some(Definition::ModuleDef(def.into()))
},
ast::StructDef(it) => {
let def: hir::Struct = sema.to_def(&it)?;
Some(NameDefinition::ModuleDef(def.into()))
Some(Definition::ModuleDef(def.into()))
},
ast::UnionDef(it) => {
let def: hir::Union = sema.to_def(&it)?;
Some(NameDefinition::ModuleDef(def.into()))
Some(Definition::ModuleDef(def.into()))
},
ast::EnumDef(it) => {
let def: hir::Enum = sema.to_def(&it)?;
Some(NameDefinition::ModuleDef(def.into()))
Some(Definition::ModuleDef(def.into()))
},
ast::TraitDef(it) => {
let def: hir::Trait = sema.to_def(&it)?;
Some(NameDefinition::ModuleDef(def.into()))
Some(Definition::ModuleDef(def.into()))
},
ast::StaticDef(it) => {
let def: hir::Static = sema.to_def(&it)?;
Some(NameDefinition::ModuleDef(def.into()))
Some(Definition::ModuleDef(def.into()))
},
ast::EnumVariant(it) => {
let def: hir::EnumVariant = sema.to_def(&it)?;
Some(NameDefinition::ModuleDef(def.into()))
Some(Definition::ModuleDef(def.into()))
},
ast::FnDef(it) => {
let def: hir::Function = sema.to_def(&it)?;
Some(NameDefinition::ModuleDef(def.into()))
Some(Definition::ModuleDef(def.into()))
},
ast::ConstDef(it) => {
let def: hir::Const = sema.to_def(&it)?;
Some(NameDefinition::ModuleDef(def.into()))
Some(Definition::ModuleDef(def.into()))
},
ast::TypeAliasDef(it) => {
let def: hir::TypeAlias = sema.to_def(&it)?;
Some(NameDefinition::ModuleDef(def.into()))
Some(Definition::ModuleDef(def.into()))
},
ast::MacroCall(it) => {
let def = sema.to_def(&it)?;
Some(NameDefinition::Macro(def))
Some(Definition::Macro(def))
},
ast::TypeParam(it) => {
let def = sema.to_def(&it)?;
Some(NameDefinition::TypeParam(def))
Some(Definition::TypeParam(def))
},
_ => None,
}

View file

@ -6,7 +6,7 @@ use ra_prof::profile;
use ra_syntax::{ast, AstNode, SyntaxKind::NAME};
use crate::{
defs::{classify_name, NameDefinition},
defs::{classify_name, Definition},
symbol_index::{self, FileSymbol, Query},
RootDatabase,
};
@ -43,13 +43,13 @@ impl<'a> ImportsLocator<'a> {
.chain(lib_results.into_iter())
.filter_map(|import_candidate| self.get_name_definition(&import_candidate))
.filter_map(|name_definition_to_import| match name_definition_to_import {
NameDefinition::ModuleDef(module_def) => Some(module_def),
Definition::ModuleDef(module_def) => Some(module_def),
_ => None,
})
.collect()
}
fn get_name_definition(&mut self, import_candidate: &FileSymbol) -> Option<NameDefinition> {
fn get_name_definition(&mut self, import_candidate: &FileSymbol) -> Option<Definition> {
let _p = profile("get_name_definition");
let file_id = import_candidate.file_id;

View file

@ -3,3 +3,4 @@
!package.json
!package-lock.json
!icon.png
!README.md