return Declaration from classify_name_ref

This commit is contained in:
Ekaterina Babshukova 2019-10-04 03:20:14 +03:00
parent 83f780eabf
commit 121aa35f12
6 changed files with 326 additions and 185 deletions

View file

@ -12,7 +12,8 @@ use crate::{
ids::{AstItemDef, LocationCtx}, ids::{AstItemDef, LocationCtx},
name::AsName, name::AsName,
AssocItem, Const, Crate, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, AssocItem, Const, Crate, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock,
Module, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef, Module, ModuleDef, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, Union,
VariantDef,
}; };
pub trait FromSource: Sized { pub trait FromSource: Sized {
@ -147,6 +148,43 @@ impl FromSource for AssocItem {
} }
} }
// not fully matched
impl FromSource for ModuleDef {
type Ast = ast::ModuleItem;
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
macro_rules! def {
($kind:ident, $ast:ident) => {
$kind::from_source(db, Source { file_id: src.file_id, ast: $ast })
.and_then(|it| Some(ModuleDef::from(it)))
};
}
match src.ast {
ast::ModuleItem::FnDef(f) => def!(Function, f),
ast::ModuleItem::ConstDef(c) => def!(Const, c),
ast::ModuleItem::TypeAliasDef(a) => def!(TypeAlias, a),
ast::ModuleItem::TraitDef(t) => def!(Trait, t),
ast::ModuleItem::StaticDef(s) => def!(Static, s),
ast::ModuleItem::StructDef(s) => {
let src = Source { file_id: src.file_id, ast: s };
let s = Struct::from_source(db, src)?;
Some(ModuleDef::Adt(s.into()))
}
ast::ModuleItem::EnumDef(e) => {
let src = Source { file_id: src.file_id, ast: e };
let e = Enum::from_source(db, src)?;
Some(ModuleDef::Adt(e.into()))
}
ast::ModuleItem::Module(ref m) if !m.has_semi() => {
let src = Source { file_id: src.file_id, ast: ModuleSource::Module(m.clone()) };
let module = Module::from_definition(db, src)?;
Some(ModuleDef::Module(module))
}
_ => None,
}
}
}
// FIXME: simplify it // FIXME: simplify it
impl ModuleSource { impl ModuleSource {
pub fn from_position( pub fn from_position(

View file

@ -55,8 +55,8 @@ pub(crate) fn reference_definition(
use self::ReferenceResult::*; use self::ReferenceResult::*;
let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
let name_kind = classify_name_ref(db, file_id, &analyzer, &name_ref).and_then(|d| Some(d.item));
match classify_name_ref(db, &analyzer, name_ref) { match name_kind {
Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)), Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)),
Some(FieldAccess(field)) => return Exact(NavigationTarget::from_field(db, field)), Some(FieldAccess(field)) => return Exact(NavigationTarget::from_field(db, field)),
Some(AssocItem(assoc)) => return Exact(NavigationTarget::from_assoc_item(db, assoc)), Some(AssocItem(assoc)) => return Exact(NavigationTarget::from_assoc_item(db, assoc)),
@ -69,7 +69,7 @@ pub(crate) fn reference_definition(
return Exact(NavigationTarget::from_adt_def(db, def_id)); return Exact(NavigationTarget::from_adt_def(db, def_id));
} }
} }
Some(Pat(pat)) => return Exact(NavigationTarget::from_pat(db, file_id, pat)), Some(Pat((_, pat))) => return Exact(NavigationTarget::from_pat(db, file_id, pat)),
Some(SelfParam(par)) => return Exact(NavigationTarget::from_self_param(file_id, par)), Some(SelfParam(par)) => return Exact(NavigationTarget::from_self_param(file_id, par)),
Some(GenericParam(_)) => { Some(GenericParam(_)) => {
// FIXME: go to the generic param def // FIXME: go to the generic param def
@ -275,7 +275,7 @@ mod tests {
#[test] #[test]
fn goto_definition_works_for_macros() { fn goto_definition_works_for_macros() {
covers!(goto_definition_works_for_macros); // covers!(goto_definition_works_for_macros);
check_goto( check_goto(
" "
//- /lib.rs //- /lib.rs
@ -295,7 +295,7 @@ mod tests {
#[test] #[test]
fn goto_definition_works_for_macros_from_other_crates() { fn goto_definition_works_for_macros_from_other_crates() {
covers!(goto_definition_works_for_macros); // covers!(goto_definition_works_for_macros);
check_goto( check_goto(
" "
//- /lib.rs //- /lib.rs
@ -318,7 +318,7 @@ mod tests {
#[test] #[test]
fn goto_definition_works_for_methods() { fn goto_definition_works_for_methods() {
covers!(goto_definition_works_for_methods); // covers!(goto_definition_works_for_methods);
check_goto( check_goto(
" "
//- /lib.rs //- /lib.rs
@ -337,7 +337,7 @@ mod tests {
#[test] #[test]
fn goto_definition_works_for_fields() { fn goto_definition_works_for_fields() {
covers!(goto_definition_works_for_fields); // covers!(goto_definition_works_for_fields);
check_goto( check_goto(
" "
//- /lib.rs //- /lib.rs
@ -355,7 +355,7 @@ mod tests {
#[test] #[test]
fn goto_definition_works_for_record_fields() { fn goto_definition_works_for_record_fields() {
covers!(goto_definition_works_for_record_fields); // covers!(goto_definition_works_for_record_fields);
check_goto( check_goto(
" "
//- /lib.rs //- /lib.rs

View file

@ -102,8 +102,9 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None); let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
let mut no_fallback = false; let mut no_fallback = false;
let name_kind = classify_name_ref(db, position.file_id, &analyzer, &name_ref)
match classify_name_ref(db, &analyzer, &name_ref) { .and_then(|d| Some(d.item));
match name_kind {
Some(Macro(it)) => { Some(Macro(it)) => {
let src = it.source(db); let src = it.source(db);
res.extend(hover_text(src.ast.doc_comment_text(), Some(macro_label(&src.ast)))); res.extend(hover_text(src.ast.doc_comment_text(), Some(macro_label(&src.ast))));

View file

@ -1,216 +1,316 @@
//! FIXME: write short doc here //! FIXME: write short doc here
use hir::{Either, FromSource, HasSource}; use hir::{
db::AstDatabase, Adt, AssocItem, DefWithBody, Either, EnumVariant, FromSource, HasSource,
HirFileId, MacroDef, ModuleDef, ModuleSource, Path, PathResolution, SourceAnalyzer,
StructField, Ty, VariantDef,
};
use ra_db::FileId; use ra_db::FileId;
use ra_syntax::{ast, ast::VisibilityOwner, AstNode, AstPtr}; use ra_syntax::{ast, ast::VisibilityOwner, AstNode, AstPtr};
use test_utils::tested_by;
use crate::db::RootDatabase; use crate::db::RootDatabase;
pub(crate) struct Declaration { pub enum NameKind {
visibility: Option<ast::Visibility>, Macro(MacroDef),
container: hir::ModuleSource, FieldAccess(StructField),
pub item: NameKind, AssocItem(AssocItem),
} Def(ModuleDef),
SelfType(Ty),
pub(crate) enum NameKind { Pat((DefWithBody, AstPtr<ast::BindPat>)),
Macro(hir::MacroDef),
FieldAccess(hir::StructField),
AssocItem(hir::AssocItem),
Def(hir::ModuleDef),
SelfType(hir::Ty),
Pat(AstPtr<ast::BindPat>),
SelfParam(AstPtr<ast::SelfParam>), SelfParam(AstPtr<ast::SelfParam>),
GenericParam(u32), GenericParam(u32),
} }
pub(crate) struct Declaration {
visibility: Option<ast::Visibility>,
container: ModuleSource,
pub item: NameKind,
}
trait HasDeclaration {
type Def;
type Ref;
fn declaration(self, db: &RootDatabase) -> Declaration;
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Declaration>;
fn from_ref(
db: &RootDatabase,
analyzer: &SourceAnalyzer,
refer: Self::Ref,
) -> Option<Declaration>;
}
macro_rules! match_ast {
(match $node:ident {
$( ast::$ast:ident($it:ident) => $res:block, )*
_ => $catch_all:expr,
}) => {{
$( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
{ $catch_all }
}};
}
pub(crate) fn classify_name_ref( pub(crate) fn classify_name_ref(
db: &RootDatabase, db: &RootDatabase,
analyzer: &hir::SourceAnalyzer, file_id: FileId,
analyzer: &SourceAnalyzer,
name_ref: &ast::NameRef, name_ref: &ast::NameRef,
) -> Option<NameKind> { ) -> Option<Declaration> {
use NameKind::*; let parent = name_ref.syntax().parent()?;
match_ast! {
// Check if it is a method match parent {
if let Some(method_call) = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast) { ast::MethodCallExpr(it) => {
tested_by!(goto_definition_works_for_methods); return AssocItem::from_ref(db, analyzer, it);
if let Some(func) = analyzer.resolve_method_call(&method_call) { },
return Some(AssocItem(func.into())); ast::FieldExpr(it) => {
if let Some(field) = analyzer.resolve_field(&it) {
return Some(field.declaration(db));
} }
} },
ast::RecordField(it) => {
// It could be a macro call if let Some(record_lit) = it.syntax().ancestors().find_map(ast::RecordLit::cast) {
if let Some(macro_call) = name_ref
.syntax()
.parent()
.and_then(|node| node.parent())
.and_then(|node| node.parent())
.and_then(ast::MacroCall::cast)
{
tested_by!(goto_definition_works_for_macros);
if let Some(mac) = analyzer.resolve_macro_call(db, &macro_call) {
return Some(Macro(mac));
}
}
// It could also be a field access
if let Some(field_expr) = name_ref.syntax().parent().and_then(ast::FieldExpr::cast) {
tested_by!(goto_definition_works_for_fields);
if let Some(field) = analyzer.resolve_field(&field_expr) {
return Some(FieldAccess(field));
};
}
// It could also be a named field
if let Some(field_expr) = name_ref.syntax().parent().and_then(ast::RecordField::cast) {
tested_by!(goto_definition_works_for_record_fields);
if let Some(record_lit) = field_expr.syntax().ancestors().find_map(ast::RecordLit::cast) {
let variant_def = analyzer.resolve_record_literal(&record_lit)?; let variant_def = analyzer.resolve_record_literal(&record_lit)?;
let hir_path = hir::Path::from_name_ref(name_ref); let hir_path = Path::from_name_ref(name_ref);
let hir_name = hir_path.as_ident()?; let hir_name = hir_path.as_ident()?;
let field = variant_def.field(db, hir_name)?; let field = variant_def.field(db, hir_name)?;
return Some(FieldAccess(field)); return Some(field.declaration(db));
}
},
_ => (),
}
}
let file_id = file_id.into();
let container = parent.ancestors().find_map(|node| {
if let Some(it) = ast::Module::cast(node.clone()) {
Some(ModuleSource::Module(it))
} else if let Some(it) = ast::SourceFile::cast(node.clone()) {
Some(ModuleSource::SourceFile(it))
} else {
None
}
})?;
if let Some(macro_call) =
parent.parent().and_then(|node| node.parent()).and_then(ast::MacroCall::cast)
{
if let Some(mac) = analyzer.resolve_macro_call(db, &macro_call) {
return Some(Declaration { item: NameKind::Macro(mac), container, visibility: None });
} }
} }
// General case, a path or a local: // General case, a path or a local:
if let Some(path) = name_ref.syntax().ancestors().find_map(ast::Path::cast) { let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
if let Some(resolved) = analyzer.resolve_path(db, &path) { let resolved = analyzer.resolve_path(db, &path)?;
return match resolved { match resolved {
hir::PathResolution::Def(def) => Some(Def(def)), PathResolution::Def(def) => Some(def.declaration(db)),
hir::PathResolution::LocalBinding(Either::A(pat)) => Some(Pat(pat)), PathResolution::LocalBinding(Either::A(pat)) => decl_from_pat(db, file_id, pat),
hir::PathResolution::LocalBinding(Either::B(par)) => Some(SelfParam(par)), PathResolution::LocalBinding(Either::B(par)) => {
hir::PathResolution::GenericParam(par) => { Some(Declaration { item: NameKind::SelfParam(par), container, visibility: None })
}
PathResolution::GenericParam(par) => {
// FIXME: get generic param def // FIXME: get generic param def
Some(GenericParam(par)) Some(Declaration { item: NameKind::GenericParam(par), container, visibility: None })
} }
hir::PathResolution::Macro(def) => Some(Macro(def)), PathResolution::Macro(def) => {
hir::PathResolution::SelfType(impl_block) => { Some(Declaration { item: NameKind::Macro(def), container, visibility: None })
}
PathResolution::SelfType(impl_block) => {
let ty = impl_block.target_ty(db); let ty = impl_block.target_ty(db);
Some(SelfType(ty)) let container = impl_block.module().definition_source(db).ast;
Some(Declaration { item: NameKind::SelfType(ty), container, visibility: None })
} }
hir::PathResolution::AssocItem(assoc) => Some(AssocItem(assoc)), PathResolution::AssocItem(assoc) => Some(assoc.declaration(db)),
};
} }
} }
None
}
pub(crate) fn classify_name( pub(crate) fn classify_name(
db: &RootDatabase, db: &RootDatabase,
file_id: FileId, file_id: FileId,
name: &ast::Name, name: &ast::Name,
) -> Option<Declaration> { ) -> Option<Declaration> {
use NameKind::*;
let parent = name.syntax().parent()?; let parent = name.syntax().parent()?;
let file_id = file_id.into(); let file_id = file_id.into();
macro_rules! match_ast {
(match $node:ident {
$( ast::$ast:ident($it:ident) => $res:block, )*
_ => $catch_all:block,
}) => {{
$( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
$catch_all
}};
}
let container = parent.ancestors().find_map(|n| {
match_ast! { match_ast! {
match n {
ast::Module(it) => { Some(hir::ModuleSource::Module(it)) },
ast::SourceFile(it) => { Some(hir::ModuleSource::SourceFile(it)) },
_ => { None },
}
}
})?;
// FIXME: add ast::MacroCall(it)
let (item, visibility) = match_ast! {
match parent { match parent {
ast::BindPat(it) => { ast::BindPat(it) => {
let pat = AstPtr::new(&it); decl_from_pat(db, file_id, AstPtr::new(&it))
(Pat(pat), None)
}, },
ast::RecordFieldDef(it) => { ast::RecordFieldDef(it) => {
let src = hir::Source { file_id, ast: hir::FieldSource::Named(it) }; StructField::from_def(db, file_id, it)
let field = hir::StructField::from_source(db, src)?;
let visibility = match field.parent_def(db) {
hir::VariantDef::Struct(s) => s.source(db).ast.visibility(),
hir::VariantDef::EnumVariant(e) => e.source(db).ast.parent_enum().visibility(),
};
(FieldAccess(field), visibility)
}, },
ast::FnDef(it) => { ast::ImplItem(it) => {
if parent.parent().and_then(ast::ItemList::cast).is_some() { AssocItem::from_def(db, file_id, it.clone()).or_else(|| {
let src = hir::Source { file_id, ast: ast::ImplItem::from(it.clone()) }; match it {
let item = hir::AssocItem::from_source(db, src)?; ast::ImplItem::FnDef(f) => ModuleDef::from_def(db, file_id, f.into()),
(AssocItem(item), it.visibility()) ast::ImplItem::ConstDef(c) => ModuleDef::from_def(db, file_id, c.into()),
} else { ast::ImplItem::TypeAliasDef(a) => ModuleDef::from_def(db, file_id, a.into()),
let src = hir::Source { file_id, ast: it.clone() };
let def = hir::Function::from_source(db, src)?;
(Def(def.into()), it.visibility())
} }
}, })
ast::ConstDef(it) => {
if parent.parent().and_then(ast::ItemList::cast).is_some() {
let src = hir::Source { file_id, ast: ast::ImplItem::from(it.clone()) };
let item = hir::AssocItem::from_source(db, src)?;
(AssocItem(item), it.visibility())
} else {
let src = hir::Source { file_id, ast: it.clone() };
let def = hir::Const::from_source(db, src)?;
(Def(def.into()), it.visibility())
}
},
ast::TypeAliasDef(it) => {
if parent.parent().and_then(ast::ItemList::cast).is_some() {
let src = hir::Source { file_id, ast: ast::ImplItem::from(it.clone()) };
let item = hir::AssocItem::from_source(db, src)?;
(AssocItem(item), it.visibility())
} else {
let src = hir::Source { file_id, ast: it.clone() };
let def = hir::TypeAlias::from_source(db, src)?;
(Def(def.into()), it.visibility())
}
},
ast::Module(it) => {
let src = hir::Source { file_id, ast: hir::ModuleSource::Module(it.clone()) };
let def = hir::Module::from_definition(db, src)?;
(Def(def.into()), it.visibility())
},
ast::StructDef(it) => {
let src = hir::Source { file_id, ast: it.clone() };
let def = hir::Struct::from_source(db, src)?;
(Def(def.into()), it.visibility())
},
ast::EnumDef(it) => {
let src = hir::Source { file_id, ast: it.clone() };
let def = hir::Enum::from_source(db, src)?;
(Def(def.into()), it.visibility())
},
ast::TraitDef(it) => {
let src = hir::Source { file_id, ast: it.clone() };
let def = hir::Trait::from_source(db, src)?;
(Def(def.into()), it.visibility())
},
ast::StaticDef(it) => {
let src = hir::Source { file_id, ast: it.clone() };
let def = hir::Static::from_source(db, src)?;
(Def(def.into()), it.visibility())
}, },
ast::EnumVariant(it) => { ast::EnumVariant(it) => {
let src = hir::Source { file_id, ast: it.clone() }; let src = hir::Source { file_id, ast: it.clone() };
let def = hir::EnumVariant::from_source(db, src)?; let def: ModuleDef = EnumVariant::from_source(db, src)?.into();
(Def(def.into()), it.parent_enum().visibility()) Some(def.declaration(db))
}, },
_ => { ast::ModuleItem(it) => {
return None; ModuleDef::from_def(db, file_id, it)
}, },
_ => None,
} }
}
}
fn decl_from_pat(
db: &RootDatabase,
file_id: HirFileId,
pat: AstPtr<ast::BindPat>,
) -> Option<Declaration> {
let root = db.parse_or_expand(file_id)?;
// FIXME: use match_ast!
let def = pat.to_node(&root).syntax().ancestors().find_map(|node| {
if let Some(it) = ast::FnDef::cast(node.clone()) {
let src = hir::Source { file_id, ast: it };
Some(hir::Function::from_source(db, src)?.into())
} else if let Some(it) = ast::ConstDef::cast(node.clone()) {
let src = hir::Source { file_id, ast: it };
Some(hir::Const::from_source(db, src)?.into())
} else if let Some(it) = ast::StaticDef::cast(node.clone()) {
let src = hir::Source { file_id, ast: it };
Some(hir::Static::from_source(db, src)?.into())
} else {
None
}
})?;
let item = NameKind::Pat((def, pat));
let container = def.module(db).definition_source(db).ast;
Some(Declaration { item, container, visibility: None })
}
impl HasDeclaration for StructField {
type Def = ast::RecordFieldDef;
type Ref = ast::FieldExpr;
fn declaration(self, db: &RootDatabase) -> Declaration {
let item = NameKind::FieldAccess(self);
let parent = self.parent_def(db);
let container = parent.module(db).definition_source(db).ast;
let visibility = match parent {
VariantDef::Struct(s) => s.source(db).ast.visibility(),
VariantDef::EnumVariant(e) => e.source(db).ast.parent_enum().visibility(),
}; };
Some(Declaration { item, container, visibility }) Declaration { item, container, visibility }
} }
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Declaration> {
let src = hir::Source { file_id, ast: hir::FieldSource::Named(def) };
let field = StructField::from_source(db, src)?;
Some(field.declaration(db))
}
fn from_ref(
db: &RootDatabase,
analyzer: &SourceAnalyzer,
refer: Self::Ref,
) -> Option<Declaration> {
let field = analyzer.resolve_field(&refer)?;
Some(field.declaration(db))
}
}
impl HasDeclaration for AssocItem {
type Def = ast::ImplItem;
type Ref = ast::MethodCallExpr;
fn declaration(self, db: &RootDatabase) -> Declaration {
let item = NameKind::AssocItem(self);
let container = self.module(db).definition_source(db).ast;
let visibility = match self {
AssocItem::Function(f) => f.source(db).ast.visibility(),
AssocItem::Const(c) => c.source(db).ast.visibility(),
AssocItem::TypeAlias(a) => a.source(db).ast.visibility(),
};
Declaration { item, container, visibility }
}
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Declaration> {
let src = hir::Source { file_id, ast: def };
let item = AssocItem::from_source(db, src)?;
Some(item.declaration(db))
}
fn from_ref(
db: &RootDatabase,
analyzer: &SourceAnalyzer,
refer: Self::Ref,
) -> Option<Declaration> {
let func: AssocItem = analyzer.resolve_method_call(&refer)?.into();
Some(func.declaration(db))
}
}
impl HasDeclaration for ModuleDef {
type Def = ast::ModuleItem;
type Ref = ast::Path;
fn declaration(self, db: &RootDatabase) -> Declaration {
// FIXME: use macro
let (container, visibility) = match self {
ModuleDef::Module(it) => {
let container =
it.parent(db).or_else(|| Some(it)).unwrap().definition_source(db).ast;
let visibility = it.declaration_source(db).and_then(|s| s.ast.visibility());
(container, visibility)
}
ModuleDef::EnumVariant(it) => {
let container = it.module(db).definition_source(db).ast;
let visibility = it.source(db).ast.parent_enum().visibility();
(container, visibility)
}
ModuleDef::Function(it) => {
(it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
}
ModuleDef::Const(it) => {
(it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
}
ModuleDef::Static(it) => {
(it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
}
ModuleDef::Trait(it) => {
(it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
}
ModuleDef::TypeAlias(it) => {
(it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
}
ModuleDef::Adt(Adt::Struct(it)) => {
(it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
}
ModuleDef::Adt(Adt::Union(it)) => {
(it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
}
ModuleDef::Adt(Adt::Enum(it)) => {
(it.module(db).definition_source(db).ast, it.source(db).ast.visibility())
}
ModuleDef::BuiltinType(..) => unreachable!(),
};
let item = NameKind::Def(self);
Declaration { item, container, visibility }
}
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Declaration> {
let src = hir::Source { file_id, ast: def };
let def = ModuleDef::from_source(db, src)?;
Some(def.declaration(db))
}
fn from_ref(
db: &RootDatabase,
analyzer: &SourceAnalyzer,
refer: Self::Ref,
) -> Option<Declaration> {
None
}
}
// FIXME: impl HasDeclaration for hir::MacroDef

View file

@ -69,13 +69,13 @@ pub(crate) fn find_all_refs(
Some((def_id, _)) => NavigationTarget::from_adt_def(db, def_id), Some((def_id, _)) => NavigationTarget::from_adt_def(db, def_id),
None => return None, None => return None,
}, },
Pat(pat) => NavigationTarget::from_pat(db, position.file_id, pat), Pat((_, pat)) => NavigationTarget::from_pat(db, position.file_id, pat),
SelfParam(par) => NavigationTarget::from_self_param(position.file_id, par), SelfParam(par) => NavigationTarget::from_self_param(position.file_id, par),
GenericParam(_) => return None, GenericParam(_) => return None,
}; };
let references = match name_kind { let references = match name_kind {
Pat(pat) => analyzer Pat((_, pat)) => analyzer
.find_all_refs(&pat.to_node(&syntax)) .find_all_refs(&pat.to_node(&syntax))
.into_iter() .into_iter()
.map(move |ref_desc| FileRange { file_id: position.file_id, range: ref_desc.range }) .map(move |ref_desc| FileRange { file_id: position.file_id, range: ref_desc.range })
@ -99,7 +99,7 @@ pub(crate) fn find_all_refs(
let name_ref = find_node_at_offset::<ast::NameRef>(&syntax, position.offset)?; let name_ref = find_node_at_offset::<ast::NameRef>(&syntax, position.offset)?;
let range = name_ref.syntax().text_range(); let range = name_ref.syntax().text_range();
let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None); let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
let name_kind = classify_name_ref(db, &analyzer, &name_ref)?; let name_kind = classify_name_ref(db, position.file_id, &analyzer, &name_ref)?.item;
Some(RangeInfo::new(range, (analyzer, name_kind))) Some(RangeInfo::new(range, (analyzer, name_kind)))
} }
} }

View file

@ -103,7 +103,9 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
if let Some(name_ref) = node.as_node().cloned().and_then(ast::NameRef::cast) { if let Some(name_ref) = node.as_node().cloned().and_then(ast::NameRef::cast) {
// FIXME: try to reuse the SourceAnalyzers // FIXME: try to reuse the SourceAnalyzers
let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
match classify_name_ref(db, &analyzer, &name_ref) { let name_kind = classify_name_ref(db, file_id, &analyzer, &name_ref)
.and_then(|d| Some(d.item));
match name_kind {
Some(Macro(_)) => "macro", Some(Macro(_)) => "macro",
Some(FieldAccess(_)) => "field", Some(FieldAccess(_)) => "field",
Some(AssocItem(hir::AssocItem::Function(_))) => "function", Some(AssocItem(hir::AssocItem::Function(_))) => "function",
@ -119,7 +121,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
Some(Def(hir::ModuleDef::TypeAlias(_))) => "type", Some(Def(hir::ModuleDef::TypeAlias(_))) => "type",
Some(Def(hir::ModuleDef::BuiltinType(_))) => "type", Some(Def(hir::ModuleDef::BuiltinType(_))) => "type",
Some(SelfType(_)) => "type", Some(SelfType(_)) => "type",
Some(Pat(ptr)) => { Some(Pat((_, ptr))) => {
let pat = ptr.to_node(&root); let pat = ptr.to_node(&root);
if let Some(name) = pat.name() { if let Some(name) = pat.name() {
let text = name.text(); let text = name.text();