mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 21:13:37 +00:00
Merge #2276
2276: Source-ify name_definition r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
786cae520a
5 changed files with 41 additions and 41 deletions
|
@ -1,6 +1,7 @@
|
|||
//! FIXME: write short doc here
|
||||
|
||||
use ra_db::{FileId, SourceDatabase};
|
||||
use hir::Source;
|
||||
use ra_db::SourceDatabase;
|
||||
use ra_syntax::{
|
||||
algo::find_node_at_offset,
|
||||
ast::{self, DocCommentsOwner},
|
||||
|
@ -21,11 +22,12 @@ 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) {
|
||||
let navs = name_definition(db, position.file_id, &name)?;
|
||||
let navs = name_definition(db, Source::new(position.file_id.into(), &name))?;
|
||||
return Some(RangeInfo::new(name.syntax().text_range(), navs));
|
||||
}
|
||||
None
|
||||
|
@ -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();
|
||||
|
@ -85,14 +86,13 @@ pub(crate) fn reference_definition(
|
|||
|
||||
pub(crate) fn name_definition(
|
||||
db: &RootDatabase,
|
||||
file_id: FileId,
|
||||
name: &ast::Name,
|
||||
name: Source<&ast::Name>,
|
||||
) -> Option<Vec<NavigationTarget>> {
|
||||
let parent = name.syntax().parent()?;
|
||||
let parent = name.ast.syntax().parent()?;
|
||||
|
||||
if let Some(module) = ast::Module::cast(parent.clone()) {
|
||||
if module.has_semi() {
|
||||
let src = hir::Source { file_id: file_id.into(), ast: module };
|
||||
let src = name.with_ast(module);
|
||||
if let Some(child_module) = hir::Module::from_declaration(db, src) {
|
||||
let nav = child_module.to_nav(db);
|
||||
return Some(vec![nav]);
|
||||
|
@ -100,20 +100,20 @@ pub(crate) fn name_definition(
|
|||
}
|
||||
}
|
||||
|
||||
if let Some(nav) = named_target(db, file_id, &parent) {
|
||||
if let Some(nav) = named_target(db, name.with_ast(&parent)) {
|
||||
return Some(vec![nav]);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option<NavigationTarget> {
|
||||
fn named_target(db: &RootDatabase, node: Source<&SyntaxNode>) -> Option<NavigationTarget> {
|
||||
match_ast! {
|
||||
match node {
|
||||
match (node.ast) {
|
||||
ast::StructDef(it) => {
|
||||
Some(NavigationTarget::from_named(
|
||||
db,
|
||||
file_id.into(),
|
||||
node.file_id,
|
||||
&it,
|
||||
it.doc_comment_text(),
|
||||
it.short_label(),
|
||||
|
@ -122,7 +122,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
|
|||
ast::EnumDef(it) => {
|
||||
Some(NavigationTarget::from_named(
|
||||
db,
|
||||
file_id.into(),
|
||||
node.file_id,
|
||||
&it,
|
||||
it.doc_comment_text(),
|
||||
it.short_label(),
|
||||
|
@ -131,7 +131,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
|
|||
ast::EnumVariant(it) => {
|
||||
Some(NavigationTarget::from_named(
|
||||
db,
|
||||
file_id.into(),
|
||||
node.file_id,
|
||||
&it,
|
||||
it.doc_comment_text(),
|
||||
it.short_label(),
|
||||
|
@ -140,7 +140,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
|
|||
ast::FnDef(it) => {
|
||||
Some(NavigationTarget::from_named(
|
||||
db,
|
||||
file_id.into(),
|
||||
node.file_id,
|
||||
&it,
|
||||
it.doc_comment_text(),
|
||||
it.short_label(),
|
||||
|
@ -149,7 +149,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
|
|||
ast::TypeAliasDef(it) => {
|
||||
Some(NavigationTarget::from_named(
|
||||
db,
|
||||
file_id.into(),
|
||||
node.file_id,
|
||||
&it,
|
||||
it.doc_comment_text(),
|
||||
it.short_label(),
|
||||
|
@ -158,7 +158,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
|
|||
ast::ConstDef(it) => {
|
||||
Some(NavigationTarget::from_named(
|
||||
db,
|
||||
file_id.into(),
|
||||
node.file_id,
|
||||
&it,
|
||||
it.doc_comment_text(),
|
||||
it.short_label(),
|
||||
|
@ -167,7 +167,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
|
|||
ast::StaticDef(it) => {
|
||||
Some(NavigationTarget::from_named(
|
||||
db,
|
||||
file_id.into(),
|
||||
node.file_id,
|
||||
&it,
|
||||
it.doc_comment_text(),
|
||||
it.short_label(),
|
||||
|
@ -176,7 +176,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
|
|||
ast::TraitDef(it) => {
|
||||
Some(NavigationTarget::from_named(
|
||||
db,
|
||||
file_id.into(),
|
||||
node.file_id,
|
||||
&it,
|
||||
it.doc_comment_text(),
|
||||
it.short_label(),
|
||||
|
@ -185,7 +185,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
|
|||
ast::RecordFieldDef(it) => {
|
||||
Some(NavigationTarget::from_named(
|
||||
db,
|
||||
file_id.into(),
|
||||
node.file_id,
|
||||
&it,
|
||||
it.doc_comment_text(),
|
||||
it.short_label(),
|
||||
|
@ -194,7 +194,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
|
|||
ast::Module(it) => {
|
||||
Some(NavigationTarget::from_named(
|
||||
db,
|
||||
file_id.into(),
|
||||
node.file_id,
|
||||
&it,
|
||||
it.doc_comment_text(),
|
||||
it.short_label(),
|
||||
|
@ -203,7 +203,7 @@ fn named_target(db: &RootDatabase, file_id: FileId, node: &SyntaxNode) -> Option
|
|||
ast::MacroCall(it) => {
|
||||
Some(NavigationTarget::from_named(
|
||||
db,
|
||||
file_id.into(),
|
||||
node.file_id,
|
||||
&it,
|
||||
it.doc_comment_text(),
|
||||
None,
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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 });
|
||||
}
|
||||
|
|
|
@ -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))),
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue