mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Rename NameDefinition -> Definition
This commit is contained in:
parent
13b25d73b5
commit
d49a4d1863
8 changed files with 103 additions and 106 deletions
|
@ -13,7 +13,7 @@ use ra_syntax::{
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
// expand::original_range,
|
// expand::original_range,
|
||||||
references::NameDefinition,
|
references::Definition,
|
||||||
FileSymbol,
|
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> {
|
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
|
||||||
match self {
|
match self {
|
||||||
NameDefinition::Macro(it) => Some(it.to_nav(db)),
|
Definition::Macro(it) => Some(it.to_nav(db)),
|
||||||
NameDefinition::StructField(it) => Some(it.to_nav(db)),
|
Definition::StructField(it) => Some(it.to_nav(db)),
|
||||||
NameDefinition::ModuleDef(it) => it.try_to_nav(db),
|
Definition::ModuleDef(it) => it.try_to_nav(db),
|
||||||
NameDefinition::SelfType(it) => Some(it.to_nav(db)),
|
Definition::SelfType(it) => Some(it.to_nav(db)),
|
||||||
NameDefinition::Local(it) => Some(it.to_nav(db)),
|
Definition::Local(it) => Some(it.to_nav(db)),
|
||||||
NameDefinition::TypeParam(it) => Some(it.to_nav(db)),
|
Definition::TypeParam(it) => Some(it.to_nav(db)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
use hir::{Adt, HasSource, HirDisplay, Semantics};
|
use hir::{Adt, HasSource, HirDisplay, Semantics};
|
||||||
use ra_ide_db::{
|
use ra_ide_db::{
|
||||||
defs::{classify_name, NameDefinition},
|
defs::{classify_name, Definition},
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
use ra_syntax::{
|
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 {
|
return match def {
|
||||||
NameDefinition::Macro(it) => {
|
Definition::Macro(it) => {
|
||||||
let src = it.source(db);
|
let src = it.source(db);
|
||||||
hover_text(src.value.doc_comment_text(), Some(macro_label(&src.value)))
|
hover_text(src.value.doc_comment_text(), Some(macro_label(&src.value)))
|
||||||
}
|
}
|
||||||
NameDefinition::StructField(it) => {
|
Definition::StructField(it) => {
|
||||||
let src = it.source(db);
|
let src = it.source(db);
|
||||||
match src.value {
|
match src.value {
|
||||||
hir::FieldSource::Named(it) => hover_text(it.doc_comment_text(), it.short_label()),
|
hir::FieldSource::Named(it) => hover_text(it.doc_comment_text(), it.short_label()),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NameDefinition::ModuleDef(it) => match it {
|
Definition::ModuleDef(it) => match it {
|
||||||
hir::ModuleDef::Module(it) => match it.definition_source(db).value {
|
hir::ModuleDef::Module(it) => match it.definition_source(db).value {
|
||||||
hir::ModuleSource::Module(it) => {
|
hir::ModuleSource::Module(it) => {
|
||||||
hover_text(it.doc_comment_text(), it.short_label())
|
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::TypeAlias(it) => from_def_source(db, it),
|
||||||
hir::ModuleDef::BuiltinType(it) => Some(it.to_string()),
|
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()))
|
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
|
// FIXME: Hover for generic param
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ pub(crate) use self::{
|
||||||
classify::{classify_name_ref, NameRefClass},
|
classify::{classify_name_ref, NameRefClass},
|
||||||
rename::rename,
|
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;
|
pub use self::search_scope::SearchScope;
|
||||||
|
|
||||||
|
@ -145,7 +145,7 @@ pub(crate) fn find_all_refs(
|
||||||
|
|
||||||
pub(crate) fn find_refs_to_def(
|
pub(crate) fn find_refs_to_def(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
def: &NameDefinition,
|
def: &Definition,
|
||||||
search_scope: Option<SearchScope>,
|
search_scope: Option<SearchScope>,
|
||||||
) -> Vec<Reference> {
|
) -> Vec<Reference> {
|
||||||
let search_scope = {
|
let search_scope = {
|
||||||
|
@ -169,7 +169,7 @@ fn find_name(
|
||||||
syntax: &SyntaxNode,
|
syntax: &SyntaxNode,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
opt_name: Option<ast::Name>,
|
opt_name: Option<ast::Name>,
|
||||||
) -> Option<RangeInfo<NameDefinition>> {
|
) -> Option<RangeInfo<Definition>> {
|
||||||
if let Some(name) = opt_name {
|
if let Some(name) = opt_name {
|
||||||
let def = classify_name(sema, &name)?.definition();
|
let def = classify_name(sema, &name)?.definition();
|
||||||
let range = name.syntax().text_range();
|
let range = name.syntax().text_range();
|
||||||
|
@ -183,7 +183,7 @@ fn find_name(
|
||||||
|
|
||||||
fn process_definition(
|
fn process_definition(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
def: &NameDefinition,
|
def: &Definition,
|
||||||
name: String,
|
name: String,
|
||||||
scope: SearchScope,
|
scope: SearchScope,
|
||||||
) -> Vec<Reference> {
|
) -> Vec<Reference> {
|
||||||
|
@ -250,13 +250,9 @@ fn process_definition(
|
||||||
refs
|
refs
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decl_access(
|
fn decl_access(def: &Definition, syntax: &SyntaxNode, range: TextRange) -> Option<ReferenceAccess> {
|
||||||
def: &NameDefinition,
|
|
||||||
syntax: &SyntaxNode,
|
|
||||||
range: TextRange,
|
|
||||||
) -> Option<ReferenceAccess> {
|
|
||||||
match def {
|
match def {
|
||||||
NameDefinition::Local(_) | NameDefinition::StructField(_) => {}
|
Definition::Local(_) | Definition::StructField(_) => {}
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -273,10 +269,10 @@ fn decl_access(
|
||||||
None
|
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.
|
// Only Locals and Fields have accesses for now.
|
||||||
match def {
|
match def {
|
||||||
NameDefinition::Local(_) | NameDefinition::StructField(_) => {}
|
Definition::Local(_) | Definition::StructField(_) => {}
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,22 @@
|
||||||
//! Functions that are used to classify an element from its definition or reference.
|
//! Functions that are used to classify an element from its definition or reference.
|
||||||
|
|
||||||
use hir::{Local, PathResolution, Semantics};
|
use hir::{Local, PathResolution, Semantics};
|
||||||
use ra_ide_db::defs::NameDefinition;
|
use ra_ide_db::defs::Definition;
|
||||||
use ra_ide_db::RootDatabase;
|
use ra_ide_db::RootDatabase;
|
||||||
use ra_prof::profile;
|
use ra_prof::profile;
|
||||||
use ra_syntax::{ast, AstNode};
|
use ra_syntax::{ast, AstNode};
|
||||||
use test_utils::tested_by;
|
use test_utils::tested_by;
|
||||||
|
|
||||||
pub enum NameRefClass {
|
pub enum NameRefClass {
|
||||||
NameDefinition(NameDefinition),
|
Definition(Definition),
|
||||||
FieldShorthand { local: Local, field: NameDefinition },
|
FieldShorthand { local: Local, field: Definition },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NameRefClass {
|
impl NameRefClass {
|
||||||
pub fn definition(self) -> NameDefinition {
|
pub fn definition(self) -> Definition {
|
||||||
match self {
|
match self {
|
||||||
NameRefClass::NameDefinition(def) => def,
|
NameRefClass::Definition(def) => def,
|
||||||
NameRefClass::FieldShorthand { local, field: _ } => NameDefinition::Local(local),
|
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()) {
|
if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
|
||||||
tested_by!(goto_def_for_methods);
|
tested_by!(goto_def_for_methods);
|
||||||
if let Some(func) = sema.resolve_method_call(&method_call) {
|
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()) {
|
if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) {
|
||||||
tested_by!(goto_def_for_fields);
|
tested_by!(goto_def_for_fields);
|
||||||
if let Some(field) = sema.resolve_field(&field_expr) {
|
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_record_fields);
|
||||||
tested_by!(goto_def_for_field_init_shorthand);
|
tested_by!(goto_def_for_field_init_shorthand);
|
||||||
if let Some((field, local)) = sema.resolve_record_field(&record_field) {
|
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 {
|
let res = match local {
|
||||||
None => NameRefClass::NameDefinition(field),
|
None => NameRefClass::Definition(field),
|
||||||
Some(local) => NameRefClass::FieldShorthand { field, local },
|
Some(local) => NameRefClass::FieldShorthand { field, local },
|
||||||
};
|
};
|
||||||
return Some(res);
|
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) {
|
if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
|
||||||
tested_by!(goto_def_for_macros);
|
tested_by!(goto_def_for_macros);
|
||||||
if let Some(macro_def) = sema.resolve_macro_call(¯o_call) {
|
if let Some(macro_def) = sema.resolve_macro_call(¯o_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 path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
|
||||||
let resolved = sema.resolve_path(&path)?;
|
let resolved = sema.resolve_path(&path)?;
|
||||||
let res = match resolved {
|
let res = match resolved {
|
||||||
PathResolution::Def(def) => NameDefinition::ModuleDef(def),
|
PathResolution::Def(def) => Definition::ModuleDef(def),
|
||||||
PathResolution::AssocItem(item) => {
|
PathResolution::AssocItem(item) => {
|
||||||
let def = match item {
|
let def = match item {
|
||||||
hir::AssocItem::Function(it) => it.into(),
|
hir::AssocItem::Function(it) => it.into(),
|
||||||
hir::AssocItem::Const(it) => it.into(),
|
hir::AssocItem::Const(it) => it.into(),
|
||||||
hir::AssocItem::TypeAlias(it) => it.into(),
|
hir::AssocItem::TypeAlias(it) => it.into(),
|
||||||
};
|
};
|
||||||
NameDefinition::ModuleDef(def)
|
Definition::ModuleDef(def)
|
||||||
}
|
}
|
||||||
PathResolution::Local(local) => NameDefinition::Local(local),
|
PathResolution::Local(local) => Definition::Local(local),
|
||||||
PathResolution::TypeParam(par) => NameDefinition::TypeParam(par),
|
PathResolution::TypeParam(par) => Definition::TypeParam(par),
|
||||||
PathResolution::Macro(def) => NameDefinition::Macro(def),
|
PathResolution::Macro(def) => Definition::Macro(def),
|
||||||
PathResolution::SelfType(impl_def) => NameDefinition::SelfType(impl_def),
|
PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def),
|
||||||
};
|
};
|
||||||
Some(NameRefClass::NameDefinition(res))
|
Some(NameRefClass::Definition(res))
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use ra_ide_db::RootDatabase;
|
use ra_ide_db::RootDatabase;
|
||||||
|
|
||||||
use super::NameDefinition;
|
use super::Definition;
|
||||||
|
|
||||||
pub struct SearchScope {
|
pub struct SearchScope {
|
||||||
entries: FxHashMap<FileId, Option<TextRange>>,
|
entries: FxHashMap<FileId, Option<TextRange>>,
|
||||||
|
@ -23,7 +23,7 @@ impl SearchScope {
|
||||||
SearchScope { entries: FxHashMap::default() }
|
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 _p = profile("search_scope");
|
||||||
let module = match def.module(db) {
|
let module = match def.module(db) {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
|
@ -32,7 +32,7 @@ impl SearchScope {
|
||||||
let module_src = module.definition_source(db);
|
let module_src = module.definition_source(db);
|
||||||
let file_id = module_src.file_id.original_file(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) {
|
let range = match var.parent(db) {
|
||||||
DefWithBody::Function(f) => f.source(db).value.syntax().text_range(),
|
DefWithBody::Function(f) => f.source(db).value.syntax().text_range(),
|
||||||
DefWithBody::Const(c) => c.source(db).value.syntax().text_range(),
|
DefWithBody::Const(c) => c.source(db).value.syntax().text_range(),
|
||||||
|
|
|
@ -7,7 +7,7 @@ mod tests;
|
||||||
|
|
||||||
use hir::{Name, Semantics};
|
use hir::{Name, Semantics};
|
||||||
use ra_ide_db::{
|
use ra_ide_db::{
|
||||||
defs::{classify_name, NameClass, NameDefinition},
|
defs::{classify_name, Definition, NameClass},
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
use ra_prof::profile;
|
use ra_prof::profile;
|
||||||
|
@ -172,7 +172,7 @@ fn highlight_element(
|
||||||
let name = element.into_node().and_then(ast::Name::cast).unwrap();
|
let name = element.into_node().and_then(ast::Name::cast).unwrap();
|
||||||
let name_kind = classify_name(sema, &name);
|
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) {
|
if let Some(name) = local.name(db) {
|
||||||
let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
|
let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
|
||||||
*shadow_count += 1;
|
*shadow_count += 1;
|
||||||
|
@ -181,7 +181,7 @@ fn highlight_element(
|
||||||
};
|
};
|
||||||
|
|
||||||
match name_kind {
|
match name_kind {
|
||||||
Some(NameClass::NameDefinition(def)) => {
|
Some(NameClass::Definition(def)) => {
|
||||||
highlight_name(db, def) | HighlightModifier::Definition
|
highlight_name(db, def) | HighlightModifier::Definition
|
||||||
}
|
}
|
||||||
Some(NameClass::ConstReference(def)) => highlight_name(db, def),
|
Some(NameClass::ConstReference(def)) => highlight_name(db, def),
|
||||||
|
@ -196,8 +196,8 @@ fn highlight_element(
|
||||||
let name_kind = classify_name_ref(sema, &name_ref)?;
|
let name_kind = classify_name_ref(sema, &name_ref)?;
|
||||||
|
|
||||||
match name_kind {
|
match name_kind {
|
||||||
NameRefClass::NameDefinition(def) => {
|
NameRefClass::Definition(def) => {
|
||||||
if let NameDefinition::Local(local) = &def {
|
if let Definition::Local(local) = &def {
|
||||||
if let Some(name) = local.name(db) {
|
if let Some(name) = local.name(db) {
|
||||||
let shadow_count =
|
let shadow_count =
|
||||||
bindings_shadow_count.entry(name.clone()).or_default();
|
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 {
|
match def {
|
||||||
NameDefinition::Macro(_) => HighlightTag::Macro,
|
Definition::Macro(_) => HighlightTag::Macro,
|
||||||
NameDefinition::StructField(_) => HighlightTag::Field,
|
Definition::StructField(_) => HighlightTag::Field,
|
||||||
NameDefinition::ModuleDef(def) => match def {
|
Definition::ModuleDef(def) => match def {
|
||||||
hir::ModuleDef::Module(_) => HighlightTag::Module,
|
hir::ModuleDef::Module(_) => HighlightTag::Module,
|
||||||
hir::ModuleDef::Function(_) => HighlightTag::Function,
|
hir::ModuleDef::Function(_) => HighlightTag::Function,
|
||||||
hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Struct,
|
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::TypeAlias(_) => HighlightTag::TypeAlias,
|
||||||
hir::ModuleDef::BuiltinType(_) => HighlightTag::BuiltinType,
|
hir::ModuleDef::BuiltinType(_) => HighlightTag::BuiltinType,
|
||||||
},
|
},
|
||||||
NameDefinition::SelfType(_) => HighlightTag::SelfType,
|
Definition::SelfType(_) => HighlightTag::SelfType,
|
||||||
NameDefinition::TypeParam(_) => HighlightTag::TypeParam,
|
Definition::TypeParam(_) => HighlightTag::TypeParam,
|
||||||
// FIXME: distinguish between locals and parameters
|
// FIXME: distinguish between locals and parameters
|
||||||
NameDefinition::Local(local) => {
|
Definition::Local(local) => {
|
||||||
let mut h = Highlight::new(HighlightTag::Local);
|
let mut h = Highlight::new(HighlightTag::Local);
|
||||||
if local.is_mut(db) || local.ty(db).is_mutable_reference() {
|
if local.is_mut(db) || local.ty(db).is_mutable_reference() {
|
||||||
h |= HighlightModifier::Mutable;
|
h |= HighlightModifier::Mutable;
|
||||||
|
|
|
@ -17,8 +17,9 @@ use ra_syntax::{
|
||||||
|
|
||||||
use crate::RootDatabase;
|
use crate::RootDatabase;
|
||||||
|
|
||||||
|
// FIXME: a more precise name would probably be `Symbol`?
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum NameDefinition {
|
pub enum Definition {
|
||||||
Macro(MacroDef),
|
Macro(MacroDef),
|
||||||
StructField(StructField),
|
StructField(StructField),
|
||||||
ModuleDef(ModuleDef),
|
ModuleDef(ModuleDef),
|
||||||
|
@ -27,26 +28,26 @@ pub enum NameDefinition {
|
||||||
TypeParam(TypeParam),
|
TypeParam(TypeParam),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NameDefinition {
|
impl Definition {
|
||||||
pub fn module(&self, db: &RootDatabase) -> Option<Module> {
|
pub fn module(&self, db: &RootDatabase) -> Option<Module> {
|
||||||
match self {
|
match self {
|
||||||
NameDefinition::Macro(it) => it.module(db),
|
Definition::Macro(it) => it.module(db),
|
||||||
NameDefinition::StructField(it) => Some(it.parent_def(db).module(db)),
|
Definition::StructField(it) => Some(it.parent_def(db).module(db)),
|
||||||
NameDefinition::ModuleDef(it) => it.module(db),
|
Definition::ModuleDef(it) => it.module(db),
|
||||||
NameDefinition::SelfType(it) => Some(it.module(db)),
|
Definition::SelfType(it) => Some(it.module(db)),
|
||||||
NameDefinition::Local(it) => Some(it.module(db)),
|
Definition::Local(it) => Some(it.module(db)),
|
||||||
NameDefinition::TypeParam(it) => Some(it.module(db)),
|
Definition::TypeParam(it) => Some(it.module(db)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn visibility(&self, db: &RootDatabase) -> Option<ast::Visibility> {
|
pub fn visibility(&self, db: &RootDatabase) -> Option<ast::Visibility> {
|
||||||
match self {
|
match self {
|
||||||
NameDefinition::Macro(_) => None,
|
Definition::Macro(_) => None,
|
||||||
NameDefinition::StructField(sf) => match sf.source(db).value {
|
Definition::StructField(sf) => match sf.source(db).value {
|
||||||
FieldSource::Named(it) => it.visibility(),
|
FieldSource::Named(it) => it.visibility(),
|
||||||
FieldSource::Pos(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::Module(it) => it.declaration_source(db)?.value.visibility(),
|
||||||
ModuleDef::Function(it) => it.source(db).value.visibility(),
|
ModuleDef::Function(it) => it.source(db).value.visibility(),
|
||||||
ModuleDef::Adt(adt) => match adt {
|
ModuleDef::Adt(adt) => match adt {
|
||||||
|
@ -61,17 +62,17 @@ impl NameDefinition {
|
||||||
ModuleDef::EnumVariant(_) => None,
|
ModuleDef::EnumVariant(_) => None,
|
||||||
ModuleDef::BuiltinType(_) => None,
|
ModuleDef::BuiltinType(_) => None,
|
||||||
},
|
},
|
||||||
NameDefinition::SelfType(_) => None,
|
Definition::SelfType(_) => None,
|
||||||
NameDefinition::Local(_) => None,
|
Definition::Local(_) => None,
|
||||||
NameDefinition::TypeParam(_) => None,
|
Definition::TypeParam(_) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self, db: &RootDatabase) -> Option<Name> {
|
pub fn name(&self, db: &RootDatabase) -> Option<Name> {
|
||||||
let name = match self {
|
let name = match self {
|
||||||
NameDefinition::Macro(it) => it.name(db)?,
|
Definition::Macro(it) => it.name(db)?,
|
||||||
NameDefinition::StructField(it) => it.name(db),
|
Definition::StructField(it) => it.name(db),
|
||||||
NameDefinition::ModuleDef(def) => match def {
|
Definition::ModuleDef(def) => match def {
|
||||||
hir::ModuleDef::Module(it) => it.name(db)?,
|
hir::ModuleDef::Module(it) => it.name(db)?,
|
||||||
hir::ModuleDef::Function(it) => it.name(db),
|
hir::ModuleDef::Function(it) => it.name(db),
|
||||||
hir::ModuleDef::Adt(def) => match def {
|
hir::ModuleDef::Adt(def) => match def {
|
||||||
|
@ -86,31 +87,31 @@ impl NameDefinition {
|
||||||
hir::ModuleDef::TypeAlias(it) => it.name(db),
|
hir::ModuleDef::TypeAlias(it) => it.name(db),
|
||||||
hir::ModuleDef::BuiltinType(_) => return None,
|
hir::ModuleDef::BuiltinType(_) => return None,
|
||||||
},
|
},
|
||||||
NameDefinition::SelfType(_) => return None,
|
Definition::SelfType(_) => return None,
|
||||||
NameDefinition::Local(it) => it.name(db)?,
|
Definition::Local(it) => it.name(db)?,
|
||||||
NameDefinition::TypeParam(it) => it.name(db),
|
Definition::TypeParam(it) => it.name(db),
|
||||||
};
|
};
|
||||||
Some(name)
|
Some(name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum NameClass {
|
pub enum NameClass {
|
||||||
NameDefinition(NameDefinition),
|
Definition(Definition),
|
||||||
/// `None` in `if let None = Some(82) {}`
|
/// `None` in `if let None = Some(82) {}`
|
||||||
ConstReference(NameDefinition),
|
ConstReference(Definition),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NameClass {
|
impl NameClass {
|
||||||
pub fn into_definition(self) -> Option<NameDefinition> {
|
pub fn into_definition(self) -> Option<Definition> {
|
||||||
match self {
|
match self {
|
||||||
NameClass::NameDefinition(it) => Some(it),
|
NameClass::Definition(it) => Some(it),
|
||||||
NameClass::ConstReference(_) => None,
|
NameClass::ConstReference(_) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn definition(self) -> NameDefinition {
|
pub fn definition(self) -> Definition {
|
||||||
match self {
|
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> {
|
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(bind_pat) = name.syntax().parent().and_then(ast::BindPat::cast) {
|
||||||
if let Some(def) = sema.resolve_bind_pat_to_const(&bind_pat) {
|
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 _p = profile("classify_name");
|
||||||
let parent = name.syntax().parent()?;
|
let parent = name.syntax().parent()?;
|
||||||
|
|
||||||
|
@ -133,59 +134,59 @@ fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Opti
|
||||||
match parent {
|
match parent {
|
||||||
ast::BindPat(it) => {
|
ast::BindPat(it) => {
|
||||||
let local = sema.to_def(&it)?;
|
let local = sema.to_def(&it)?;
|
||||||
Some(NameDefinition::Local(local))
|
Some(Definition::Local(local))
|
||||||
},
|
},
|
||||||
ast::RecordFieldDef(it) => {
|
ast::RecordFieldDef(it) => {
|
||||||
let field: hir::StructField = sema.to_def(&it)?;
|
let field: hir::StructField = sema.to_def(&it)?;
|
||||||
Some(NameDefinition::StructField(field))
|
Some(Definition::StructField(field))
|
||||||
},
|
},
|
||||||
ast::Module(it) => {
|
ast::Module(it) => {
|
||||||
let def = sema.to_def(&it)?;
|
let def = sema.to_def(&it)?;
|
||||||
Some(NameDefinition::ModuleDef(def.into()))
|
Some(Definition::ModuleDef(def.into()))
|
||||||
},
|
},
|
||||||
ast::StructDef(it) => {
|
ast::StructDef(it) => {
|
||||||
let def: hir::Struct = sema.to_def(&it)?;
|
let def: hir::Struct = sema.to_def(&it)?;
|
||||||
Some(NameDefinition::ModuleDef(def.into()))
|
Some(Definition::ModuleDef(def.into()))
|
||||||
},
|
},
|
||||||
ast::UnionDef(it) => {
|
ast::UnionDef(it) => {
|
||||||
let def: hir::Union = sema.to_def(&it)?;
|
let def: hir::Union = sema.to_def(&it)?;
|
||||||
Some(NameDefinition::ModuleDef(def.into()))
|
Some(Definition::ModuleDef(def.into()))
|
||||||
},
|
},
|
||||||
ast::EnumDef(it) => {
|
ast::EnumDef(it) => {
|
||||||
let def: hir::Enum = sema.to_def(&it)?;
|
let def: hir::Enum = sema.to_def(&it)?;
|
||||||
Some(NameDefinition::ModuleDef(def.into()))
|
Some(Definition::ModuleDef(def.into()))
|
||||||
},
|
},
|
||||||
ast::TraitDef(it) => {
|
ast::TraitDef(it) => {
|
||||||
let def: hir::Trait = sema.to_def(&it)?;
|
let def: hir::Trait = sema.to_def(&it)?;
|
||||||
Some(NameDefinition::ModuleDef(def.into()))
|
Some(Definition::ModuleDef(def.into()))
|
||||||
},
|
},
|
||||||
ast::StaticDef(it) => {
|
ast::StaticDef(it) => {
|
||||||
let def: hir::Static = sema.to_def(&it)?;
|
let def: hir::Static = sema.to_def(&it)?;
|
||||||
Some(NameDefinition::ModuleDef(def.into()))
|
Some(Definition::ModuleDef(def.into()))
|
||||||
},
|
},
|
||||||
ast::EnumVariant(it) => {
|
ast::EnumVariant(it) => {
|
||||||
let def: hir::EnumVariant = sema.to_def(&it)?;
|
let def: hir::EnumVariant = sema.to_def(&it)?;
|
||||||
Some(NameDefinition::ModuleDef(def.into()))
|
Some(Definition::ModuleDef(def.into()))
|
||||||
},
|
},
|
||||||
ast::FnDef(it) => {
|
ast::FnDef(it) => {
|
||||||
let def: hir::Function = sema.to_def(&it)?;
|
let def: hir::Function = sema.to_def(&it)?;
|
||||||
Some(NameDefinition::ModuleDef(def.into()))
|
Some(Definition::ModuleDef(def.into()))
|
||||||
},
|
},
|
||||||
ast::ConstDef(it) => {
|
ast::ConstDef(it) => {
|
||||||
let def: hir::Const = sema.to_def(&it)?;
|
let def: hir::Const = sema.to_def(&it)?;
|
||||||
Some(NameDefinition::ModuleDef(def.into()))
|
Some(Definition::ModuleDef(def.into()))
|
||||||
},
|
},
|
||||||
ast::TypeAliasDef(it) => {
|
ast::TypeAliasDef(it) => {
|
||||||
let def: hir::TypeAlias = sema.to_def(&it)?;
|
let def: hir::TypeAlias = sema.to_def(&it)?;
|
||||||
Some(NameDefinition::ModuleDef(def.into()))
|
Some(Definition::ModuleDef(def.into()))
|
||||||
},
|
},
|
||||||
ast::MacroCall(it) => {
|
ast::MacroCall(it) => {
|
||||||
let def = sema.to_def(&it)?;
|
let def = sema.to_def(&it)?;
|
||||||
Some(NameDefinition::Macro(def))
|
Some(Definition::Macro(def))
|
||||||
},
|
},
|
||||||
ast::TypeParam(it) => {
|
ast::TypeParam(it) => {
|
||||||
let def = sema.to_def(&it)?;
|
let def = sema.to_def(&it)?;
|
||||||
Some(NameDefinition::TypeParam(def))
|
Some(Definition::TypeParam(def))
|
||||||
},
|
},
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ use ra_prof::profile;
|
||||||
use ra_syntax::{ast, AstNode, SyntaxKind::NAME};
|
use ra_syntax::{ast, AstNode, SyntaxKind::NAME};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
defs::{classify_name, NameDefinition},
|
defs::{classify_name, Definition},
|
||||||
symbol_index::{self, FileSymbol, Query},
|
symbol_index::{self, FileSymbol, Query},
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
|
@ -43,13 +43,13 @@ impl<'a> ImportsLocator<'a> {
|
||||||
.chain(lib_results.into_iter())
|
.chain(lib_results.into_iter())
|
||||||
.filter_map(|import_candidate| self.get_name_definition(&import_candidate))
|
.filter_map(|import_candidate| self.get_name_definition(&import_candidate))
|
||||||
.filter_map(|name_definition_to_import| match name_definition_to_import {
|
.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,
|
_ => None,
|
||||||
})
|
})
|
||||||
.collect()
|
.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 _p = profile("get_name_definition");
|
||||||
let file_id = import_candidate.file_id;
|
let file_id = import_candidate.file_id;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue