mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
refactor name_ref_kind.rs
This commit is contained in:
parent
11577288c2
commit
d6ae1b5f0f
@ -10,7 +10,7 @@ use ra_syntax::{
|
||||
use crate::{
|
||||
db::RootDatabase,
|
||||
display::ShortLabel,
|
||||
name_ref_kind::{classify_name_ref, NameKind::*},
|
||||
name_kind::{classify_name_ref, NameKind::*},
|
||||
FilePosition, NavigationTarget, RangeInfo,
|
||||
};
|
||||
|
||||
@ -60,7 +60,6 @@ pub(crate) fn reference_definition(
|
||||
Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)),
|
||||
Some(FieldAccess(field)) => return Exact(NavigationTarget::from_field(db, field)),
|
||||
Some(AssocItem(assoc)) => return Exact(NavigationTarget::from_assoc_item(db, assoc)),
|
||||
Some(Method(func)) => return Exact(NavigationTarget::from_def_source(db, func)),
|
||||
Some(Def(def)) => match NavigationTarget::from_def(db, def) {
|
||||
Some(nav) => return Exact(nav),
|
||||
None => return Approximate(vec![]),
|
||||
|
@ -14,7 +14,7 @@ use crate::{
|
||||
description_from_symbol, docs_from_symbol, macro_label, rust_code_markup,
|
||||
rust_code_markup_with_doc, ShortLabel,
|
||||
},
|
||||
name_ref_kind::{classify_name_ref, NameKind::*},
|
||||
name_kind::{classify_name_ref, NameKind::*},
|
||||
FilePosition, FileRange, RangeInfo,
|
||||
};
|
||||
|
||||
@ -104,7 +104,6 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
|
||||
let mut no_fallback = false;
|
||||
|
||||
match classify_name_ref(db, &analyzer, &name_ref) {
|
||||
Some(Method(it)) => res.extend(from_def_source(db, it)),
|
||||
Some(Macro(it)) => {
|
||||
let src = it.source(db);
|
||||
res.extend(hover_text(src.ast.doc_comment_text(), Some(macro_label(&src.ast))));
|
||||
|
@ -19,7 +19,7 @@ mod feature_flags;
|
||||
mod status;
|
||||
mod completion;
|
||||
mod runnables;
|
||||
mod name_ref_kind;
|
||||
mod name_kind;
|
||||
mod goto_definition;
|
||||
mod goto_type_definition;
|
||||
mod extend_selection;
|
||||
|
@ -1,13 +1,13 @@
|
||||
//! FIXME: write short doc here
|
||||
|
||||
use hir::Either;
|
||||
use hir::{Either, FromSource};
|
||||
use ra_db::FileId;
|
||||
use ra_syntax::{ast, AstNode, AstPtr};
|
||||
use test_utils::tested_by;
|
||||
|
||||
use crate::db::RootDatabase;
|
||||
|
||||
pub enum NameKind {
|
||||
Method(hir::Function),
|
||||
Macro(hir::MacroDef),
|
||||
FieldAccess(hir::StructField),
|
||||
AssocItem(hir::AssocItem),
|
||||
@ -29,7 +29,7 @@ pub(crate) fn classify_name_ref(
|
||||
if let Some(method_call) = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast) {
|
||||
tested_by!(goto_definition_works_for_methods);
|
||||
if let Some(func) = analyzer.resolve_method_call(&method_call) {
|
||||
return Some(Method(func));
|
||||
return Some(AssocItem(func.into()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -59,17 +59,12 @@ pub(crate) fn classify_name_ref(
|
||||
if let Some(field_expr) = name_ref.syntax().parent().and_then(ast::RecordField::cast) {
|
||||
tested_by!(goto_definition_works_for_record_fields);
|
||||
|
||||
let record_lit = field_expr.syntax().ancestors().find_map(ast::RecordLit::cast);
|
||||
|
||||
if let Some(ty) = record_lit.and_then(|lit| analyzer.type_of(db, &lit.into())) {
|
||||
if let Some((hir::Adt::Struct(s), _)) = ty.as_adt() {
|
||||
let hir_path = hir::Path::from_name_ref(name_ref);
|
||||
let hir_name = hir_path.as_ident().unwrap();
|
||||
|
||||
if let Some(field) = s.field(db, hir_name) {
|
||||
return Some(FieldAccess(field));
|
||||
}
|
||||
}
|
||||
if let Some(record_lit) = field_expr.syntax().ancestors().find_map(ast::RecordLit::cast) {
|
||||
let variant_def = analyzer.resolve_record_literal(&record_lit)?;
|
||||
let hir_path = hir::Path::from_name_ref(name_ref);
|
||||
let hir_name = hir_path.as_ident()?;
|
||||
let field = variant_def.field(db, hir_name)?;
|
||||
return Some(FieldAccess(field));
|
||||
}
|
||||
}
|
||||
|
||||
@ -96,3 +91,47 @@ pub(crate) fn classify_name_ref(
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) fn classify_name(
|
||||
db: &RootDatabase,
|
||||
file_id: FileId,
|
||||
name: &ast::Name,
|
||||
) -> Option<NameKind> {
|
||||
use NameKind::*;
|
||||
|
||||
let parent = name.syntax().parent()?;
|
||||
let file_id = file_id.into();
|
||||
|
||||
if let Some(pat) = ast::BindPat::cast(parent.clone()) {
|
||||
return Some(Pat(AstPtr::new(&pat)));
|
||||
}
|
||||
if let Some(var) = ast::EnumVariant::cast(parent.clone()) {
|
||||
let src = hir::Source { file_id, ast: var };
|
||||
let var = hir::EnumVariant::from_source(db, src)?;
|
||||
return Some(Def(var.into()));
|
||||
}
|
||||
if let Some(field) = ast::RecordFieldDef::cast(parent.clone()) {
|
||||
let src = hir::Source { file_id, ast: hir::FieldSource::Named(field) };
|
||||
let field = hir::StructField::from_source(db, src)?;
|
||||
return Some(FieldAccess(field));
|
||||
}
|
||||
if let Some(field) = ast::TupleFieldDef::cast(parent.clone()) {
|
||||
let src = hir::Source { file_id, ast: hir::FieldSource::Pos(field) };
|
||||
let field = hir::StructField::from_source(db, src)?;
|
||||
return Some(FieldAccess(field));
|
||||
}
|
||||
if let Some(_) = parent.parent().and_then(ast::ItemList::cast) {
|
||||
let ast = ast::ImplItem::cast(parent.clone())?;
|
||||
let src = hir::Source { file_id, ast };
|
||||
let item = hir::AssocItem::from_source(db, src)?;
|
||||
return Some(AssocItem(item));
|
||||
}
|
||||
if let Some(item) = ast::ModuleItem::cast(parent.clone()) {
|
||||
let src = hir::Source { file_id, ast: item };
|
||||
let def = hir::ModuleDef::from_source(db, src)?;
|
||||
return Some(Def(def));
|
||||
}
|
||||
// FIXME: TYPE_PARAM, ALIAS, MACRO_CALL; Union
|
||||
|
||||
None
|
||||
}
|
@ -1,14 +1,14 @@
|
||||
//! FIXME: write short doc here
|
||||
|
||||
use hir::{FromSource, ModuleSource};
|
||||
use hir::ModuleSource;
|
||||
use ra_db::{SourceDatabase, SourceDatabaseExt};
|
||||
use ra_syntax::{algo::find_node_at_offset, ast, AstNode, AstPtr, SyntaxNode};
|
||||
use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SyntaxNode};
|
||||
use relative_path::{RelativePath, RelativePathBuf};
|
||||
|
||||
use crate::{
|
||||
db::RootDatabase,
|
||||
name_ref_kind::{
|
||||
classify_name_ref,
|
||||
name_kind::{
|
||||
classify_name, classify_name_ref,
|
||||
NameKind::{self, *},
|
||||
},
|
||||
FileId, FilePosition, FileRange, FileSystemEdit, NavigationTarget, RangeInfo, SourceChange,
|
||||
@ -64,7 +64,6 @@ pub(crate) fn find_all_refs(
|
||||
Macro(mac) => NavigationTarget::from_macro_def(db, mac),
|
||||
FieldAccess(field) => NavigationTarget::from_field(db, field),
|
||||
AssocItem(assoc) => NavigationTarget::from_assoc_item(db, assoc),
|
||||
Method(func) => NavigationTarget::from_def_source(db, func),
|
||||
Def(def) => NavigationTarget::from_def(db, def)?,
|
||||
SelfType(ref ty) => match ty.as_adt() {
|
||||
Some((def_id, _)) => NavigationTarget::from_adt_def(db, def_id),
|
||||
@ -105,44 +104,6 @@ pub(crate) fn find_all_refs(
|
||||
}
|
||||
}
|
||||
|
||||
fn classify_name(db: &RootDatabase, file_id: FileId, name: &ast::Name) -> Option<NameKind> {
|
||||
let parent = name.syntax().parent()?;
|
||||
let file_id = file_id.into();
|
||||
|
||||
if let Some(pat) = ast::BindPat::cast(parent.clone()) {
|
||||
return Some(Pat(AstPtr::new(&pat)));
|
||||
}
|
||||
if let Some(var) = ast::EnumVariant::cast(parent.clone()) {
|
||||
let src = hir::Source { file_id, ast: var };
|
||||
let var = hir::EnumVariant::from_source(db, src)?;
|
||||
return Some(Def(var.into()));
|
||||
}
|
||||
if let Some(field) = ast::RecordFieldDef::cast(parent.clone()) {
|
||||
let src = hir::Source { file_id, ast: hir::FieldSource::Named(field) };
|
||||
let field = hir::StructField::from_source(db, src)?;
|
||||
return Some(FieldAccess(field));
|
||||
}
|
||||
if let Some(field) = ast::TupleFieldDef::cast(parent.clone()) {
|
||||
let src = hir::Source { file_id, ast: hir::FieldSource::Pos(field) };
|
||||
let field = hir::StructField::from_source(db, src)?;
|
||||
return Some(FieldAccess(field));
|
||||
}
|
||||
if let Some(_) = parent.parent().and_then(ast::ItemList::cast) {
|
||||
let ast = ast::ImplItem::cast(parent.clone())?;
|
||||
let src = hir::Source { file_id, ast };
|
||||
let item = hir::AssocItem::from_source(db, src)?;
|
||||
return Some(AssocItem(item));
|
||||
}
|
||||
if let Some(item) = ast::ModuleItem::cast(parent.clone()) {
|
||||
let src = hir::Source { file_id, ast: item };
|
||||
let def = hir::ModuleDef::from_source(db, src)?;
|
||||
return Some(Def(def));
|
||||
}
|
||||
// FIXME: TYPE_PARAM, ALIAS, MACRO_CALL; Union
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) fn rename(
|
||||
db: &RootDatabase,
|
||||
position: FilePosition,
|
||||
|
@ -14,7 +14,7 @@ use ra_syntax::{
|
||||
|
||||
use crate::{
|
||||
db::RootDatabase,
|
||||
name_ref_kind::{classify_name_ref, NameKind::*},
|
||||
name_kind::{classify_name_ref, NameKind::*},
|
||||
FileId,
|
||||
};
|
||||
|
||||
@ -104,7 +104,6 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
|
||||
// FIXME: try to reuse the SourceAnalyzers
|
||||
let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
|
||||
match classify_name_ref(db, &analyzer, &name_ref) {
|
||||
Some(Method(_)) => "function",
|
||||
Some(Macro(_)) => "macro",
|
||||
Some(FieldAccess(_)) => "field",
|
||||
Some(AssocItem(hir::AssocItem::Function(_))) => "function",
|
||||
|
Loading…
x
Reference in New Issue
Block a user