Sourcify classify_name_ref

This commit is contained in:
Aleksey Kladov 2019-11-16 13:33:25 +03:00
parent d9d99369b2
commit 272af56a5c
5 changed files with 22 additions and 21 deletions

View file

@ -1,5 +1,6 @@
//! FIXME: write short doc here
use hir::Source;
use ra_db::{FileId, SourceDatabase};
use ra_syntax::{
algo::find_node_at_offset,
@ -21,7 +22,8 @@ pub(crate) fn goto_definition(
let parse = db.parse(position.file_id);
let syntax = parse.tree().syntax().clone();
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(&syntax, position.offset) {
let navs = reference_definition(db, position.file_id, &name_ref).to_vec();
let navs =
reference_definition(db, Source::new(position.file_id.into(), &name_ref)).to_vec();
return Some(RangeInfo::new(name_ref.syntax().text_range(), navs.to_vec()));
}
if let Some(name) = find_node_at_offset::<ast::Name>(&syntax, position.offset) {
@ -49,12 +51,11 @@ impl ReferenceResult {
pub(crate) fn reference_definition(
db: &RootDatabase,
file_id: FileId,
name_ref: &ast::NameRef,
name_ref: Source<&ast::NameRef>,
) -> ReferenceResult {
use self::ReferenceResult::*;
let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind);
let name_kind = classify_name_ref(db, name_ref).map(|d| d.kind);
match name_kind {
Some(Macro(mac)) => return Exact(mac.to_nav(db)),
Some(Field(field)) => return Exact(field.to_nav(db)),
@ -76,7 +77,7 @@ pub(crate) fn reference_definition(
};
// Fallback index based approach:
let navs = crate::symbol_index::index_resolve(db, name_ref)
let navs = crate::symbol_index::index_resolve(db, name_ref.ast)
.into_iter()
.map(|s| s.to_nav(db))
.collect();

View file

@ -1,6 +1,6 @@
//! FIXME: write short doc here
use hir::{Adt, HasSource, HirDisplay};
use hir::{Adt, HasSource, HirDisplay, Source};
use ra_db::SourceDatabase;
use ra_syntax::{
algo::{ancestors_at_offset, find_covering_element, find_node_at_offset},
@ -171,7 +171,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset)
{
let mut no_fallback = false;
if let Some(name_kind) = classify_name_ref(db, position.file_id, &name_ref).map(|d| d.kind)
if let Some(name_kind) =
classify_name_ref(db, Source::new(position.file_id.into(), &name_ref)).map(|d| d.kind)
{
res.extend(hover_text_from_name_kind(db, name_kind, &mut no_fallback))
}

View file

@ -14,6 +14,7 @@ mod name_definition;
mod rename;
mod search_scope;
use hir::Source;
use once_cell::unsync::Lazy;
use ra_db::{SourceDatabase, SourceDatabaseExt};
use ra_prof::profile;
@ -114,7 +115,7 @@ fn find_name<'a>(
return Some(RangeInfo::new(range, (name.text().to_string(), def)));
}
let name_ref = find_node_at_offset::<ast::NameRef>(&syntax, position.offset)?;
let def = classify_name_ref(db, position.file_id, &name_ref)?;
let def = classify_name_ref(db, Source::new(position.file_id.into(), &name_ref))?;
let range = name_ref.syntax().text_range();
Some(RangeInfo::new(range, (name_ref.text().to_string(), def)))
}
@ -146,7 +147,7 @@ fn process_definition(
continue;
}
}
if let Some(d) = classify_name_ref(db, file_id, &name_ref) {
if let Some(d) = classify_name_ref(db, Source::new(file_id.into(), &name_ref)) {
if d == def {
refs.push(FileRange { file_id, range });
}

View file

@ -123,14 +123,12 @@ pub(crate) fn classify_name(
pub(crate) fn classify_name_ref(
db: &RootDatabase,
file_id: FileId,
name_ref: &ast::NameRef,
name_ref: Source<&ast::NameRef>,
) -> Option<NameDefinition> {
let _p = profile("classify_name_ref");
let parent = name_ref.syntax().parent()?;
let analyzer =
SourceAnalyzer::new(db, hir::Source::new(file_id.into(), name_ref.syntax()), None);
let parent = name_ref.ast.syntax().parent()?;
let analyzer = SourceAnalyzer::new(db, name_ref.map(|it| it.syntax()), None);
if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
tested_by!(goto_definition_works_for_methods);
@ -150,17 +148,16 @@ pub(crate) fn classify_name_ref(
tested_by!(goto_definition_works_for_record_fields);
if let Some(record_lit) = record_field.syntax().ancestors().find_map(ast::RecordLit::cast) {
let variant_def = analyzer.resolve_record_literal(&record_lit)?;
let hir_path = Path::from_name_ref(name_ref);
let hir_path = Path::from_name_ref(name_ref.ast);
let hir_name = hir_path.as_ident()?;
let field = variant_def.field(db, hir_name)?;
return Some(from_struct_field(db, field));
}
}
let file_id = file_id.into();
let ast = ModuleSource::from_child_node(db, Source::new(file_id, &parent));
let ast = ModuleSource::from_child_node(db, name_ref.with_ast(&parent));
// FIXME: find correct container and visibility for each case
let container = Module::from_definition(db, Source { file_id, ast })?;
let container = Module::from_definition(db, name_ref.with_ast(ast))?;
let visibility = None;
if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
@ -171,7 +168,7 @@ pub(crate) fn classify_name_ref(
}
}
let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
let path = name_ref.ast.syntax().ancestors().find_map(ast::Path::cast)?;
let resolved = analyzer.resolve_path(db, &path)?;
match resolved {
PathResolution::Def(def) => Some(from_module_def(db, def, Some(container))),

View file

@ -2,7 +2,7 @@
use rustc_hash::{FxHashMap, FxHashSet};
use hir::{Mutability, Name};
use hir::{Mutability, Name, Source};
use ra_db::SourceDatabase;
use ra_prof::profile;
use ra_syntax::{ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, TextRange, T};
@ -80,7 +80,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
}
let name_ref = node.as_node().cloned().and_then(ast::NameRef::cast).unwrap();
let name_kind = classify_name_ref(db, file_id, &name_ref).map(|d| d.kind);
let name_kind =
classify_name_ref(db, Source::new(file_id.into(), &name_ref)).map(|d| d.kind);
if let Some(Local(local)) = &name_kind {
if let Some(name) = local.name(db) {