Use Source<&ast::Name> in classify_name

This commit is contained in:
kjeremy 2019-11-18 10:47:19 -05:00
parent 9cc4d058d2
commit a22cb1daa7
4 changed files with 22 additions and 25 deletions

View file

@ -193,7 +193,9 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
None
}
} else if let Some(name) = find_node_at_offset::<ast::Name>(file.syntax(), position.offset) {
if let Some(name_kind) = classify_name(db, position.file_id, &name).map(|d| d.kind) {
if let Some(name_kind) =
classify_name(db, Source::new(position.file_id.into(), &name)).map(|d| d.kind)
{
let mut _b: bool = true;
res.extend(hover_text_from_name_kind(db, name_kind, &mut _b));
}

View file

@ -110,7 +110,7 @@ fn find_name<'a>(
position: FilePosition,
) -> Option<RangeInfo<(String, NameDefinition)>> {
if let Some(name) = find_node_at_offset::<ast::Name>(&syntax, position.offset) {
let def = classify_name(db, position.file_id, &name)?;
let def = classify_name(db, Source::new(position.file_id.into(), &name))?;
let range = name.syntax().text_range();
return Some(RangeInfo::new(range, (name.text().to_string(), def)));
}

View file

@ -1,7 +1,6 @@
//! Functions that are used to classify an element from its definition or reference.
use hir::{FromSource, Module, ModuleSource, Path, PathResolution, Source, SourceAnalyzer};
use ra_db::FileId;
use ra_prof::profile;
use ra_syntax::{ast, match_ast, AstNode};
use test_utils::tested_by;
@ -12,19 +11,14 @@ use super::{
};
use crate::db::RootDatabase;
pub(crate) fn classify_name(
db: &RootDatabase,
file_id: FileId,
name: &ast::Name,
) -> Option<NameDefinition> {
pub(crate) fn classify_name(db: &RootDatabase, name: Source<&ast::Name>) -> Option<NameDefinition> {
let _p = profile("classify_name");
let parent = name.syntax().parent()?;
let file_id = file_id.into();
let parent = name.ast.syntax().parent()?;
match_ast! {
match parent {
ast::BindPat(it) => {
let src = hir::Source { file_id, ast: it };
let src = name.with_ast(it);
let local = hir::Local::from_source(db, src)?;
Some(NameDefinition {
visibility: None,
@ -34,7 +28,7 @@ pub(crate) fn classify_name(
},
ast::RecordFieldDef(it) => {
let ast = hir::FieldSource::Named(it);
let src = hir::Source { file_id, ast };
let src = name.with_ast(ast);
let field = hir::StructField::from_source(db, src)?;
Some(from_struct_field(db, field))
},
@ -42,42 +36,42 @@ pub(crate) fn classify_name(
let def = {
if !it.has_semi() {
let ast = hir::ModuleSource::Module(it);
let src = hir::Source { file_id, ast };
let src = name.with_ast(ast);
hir::Module::from_definition(db, src)
} else {
let src = hir::Source { file_id, ast: it };
let src = name.with_ast(it);
hir::Module::from_declaration(db, src)
}
}?;
Some(from_module_def(db, def.into(), None))
},
ast::StructDef(it) => {
let src = hir::Source { file_id, ast: it };
let src = name.with_ast(it);
let def = hir::Struct::from_source(db, src)?;
Some(from_module_def(db, def.into(), None))
},
ast::EnumDef(it) => {
let src = hir::Source { file_id, ast: it };
let src = name.with_ast(it);
let def = hir::Enum::from_source(db, src)?;
Some(from_module_def(db, def.into(), None))
},
ast::TraitDef(it) => {
let src = hir::Source { file_id, ast: it };
let src = name.with_ast(it);
let def = hir::Trait::from_source(db, src)?;
Some(from_module_def(db, def.into(), None))
},
ast::StaticDef(it) => {
let src = hir::Source { file_id, ast: it };
let src = name.with_ast(it);
let def = hir::Static::from_source(db, src)?;
Some(from_module_def(db, def.into(), None))
},
ast::EnumVariant(it) => {
let src = hir::Source { file_id, ast: it };
let src = name.with_ast(it);
let def = hir::EnumVariant::from_source(db, src)?;
Some(from_module_def(db, def.into(), None))
},
ast::FnDef(it) => {
let src = hir::Source { file_id, ast: it };
let src = name.with_ast(it);
let def = hir::Function::from_source(db, src)?;
if parent.parent().and_then(ast::ItemList::cast).is_some() {
Some(from_assoc_item(db, def.into()))
@ -86,7 +80,7 @@ pub(crate) fn classify_name(
}
},
ast::ConstDef(it) => {
let src = hir::Source { file_id, ast: it };
let src = name.with_ast(it);
let def = hir::Const::from_source(db, src)?;
if parent.parent().and_then(ast::ItemList::cast).is_some() {
Some(from_assoc_item(db, def.into()))
@ -95,7 +89,7 @@ pub(crate) fn classify_name(
}
},
ast::TypeAliasDef(it) => {
let src = hir::Source { file_id, ast: it };
let src = name.with_ast(it);
let def = hir::TypeAlias::from_source(db, src)?;
if parent.parent().and_then(ast::ItemList::cast).is_some() {
Some(from_assoc_item(db, def.into()))
@ -104,11 +98,11 @@ pub(crate) fn classify_name(
}
},
ast::MacroCall(it) => {
let src = hir::Source { file_id, ast: it};
let src = name.with_ast(it);
let def = hir::MacroDef::from_source(db, src.clone())?;
let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax()));
let module = Module::from_definition(db, Source::new(file_id, module_src))?;
let module = Module::from_definition(db, src.with_ast(module_src))?;
Some(NameDefinition {
visibility: None,

View file

@ -94,7 +94,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
}
NAME => {
let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap();
let name_kind = classify_name(db, file_id, &name).map(|d| d.kind);
let name_kind =
classify_name(db, Source::new(file_id.into(), &name)).map(|d| d.kind);
if let Some(Local(local)) = &name_kind {
if let Some(name) = local.name(db) {