10252: fix: make `goto_definition` multi-token mapping aware r=Veykril a=XFFXFF

Implement #10070 in  [`goto_definition`](https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ide/src/goto_definition.rs)

Co-authored-by: zhoufan <1247714429@qq.com>
This commit is contained in:
bors[bot] 2021-09-18 01:21:43 +00:00 committed by GitHub
commit a435e49d34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -39,9 +39,8 @@ pub(crate) fn goto_definition(
kind if kind.is_trivia() => 0,
_ => 1,
})?;
let token = sema.descend_into_macros(original_token.clone());
let parent = token.parent()?;
if let Some(_) = ast::Comment::cast(token.clone()) {
if let Some(_) = ast::Comment::cast(original_token.clone()) {
let parent = original_token.parent()?;
let (attributes, def) = doc_attributes(&sema, &parent)?;
let (docs, doc_mapping) = attributes.docs_with_rangemap(db)?;
let (_, link, ns) =
@ -54,33 +53,41 @@ pub(crate) fn goto_definition(
return Some(RangeInfo::new(original_token.text_range(), vec![nav]));
}
let navs = match_ast! {
match parent {
ast::NameRef(name_ref) => {
reference_definition(&sema, Either::Right(&name_ref))
},
ast::Name(name) => {
match NameClass::classify(&sema, &name)? {
NameClass::Definition(def) | NameClass::ConstReference(def) => {
try_find_trait_item_definition(sema.db, &def).unwrap_or_else(|| def_to_nav(sema.db, def))
}
NameClass::PatFieldShorthand { local_def, field_ref } => {
local_and_field_to_nav(sema.db, local_def, field_ref)
let navs = sema.descend_into_macros_many(original_token.clone())
.into_iter()
.filter_map(|token| {
let parent = token.parent()?;
let navs = match_ast! {
match parent {
ast::NameRef(name_ref) => {
reference_definition(&sema, Either::Right(&name_ref))
},
}
},
ast::Lifetime(lt) => if let Some(name_class) = NameClass::classify_lifetime(&sema, &lt) {
match name_class {
NameClass::Definition(def) => def_to_nav(sema.db, def),
ast::Name(name) => {
match NameClass::classify(&sema, &name)? {
NameClass::Definition(def) | NameClass::ConstReference(def) => {
try_find_trait_item_definition(sema.db, &def).unwrap_or_else(|| def_to_nav(sema.db, def))
}
NameClass::PatFieldShorthand { local_def, field_ref } => {
local_and_field_to_nav(sema.db, local_def, field_ref)
},
}
},
ast::Lifetime(lt) => if let Some(name_class) = NameClass::classify_lifetime(&sema, &lt) {
match name_class {
NameClass::Definition(def) => def_to_nav(sema.db, def),
_ => return None,
}
} else {
reference_definition(&sema, Either::Left(&lt))
},
ast::TokenTree(tt) => try_lookup_include_path_or_derive(&sema, tt, token, position.file_id)?,
_ => return None,
}
} else {
reference_definition(&sema, Either::Left(&lt))
},
ast::TokenTree(tt) => try_lookup_include_path_or_derive(&sema, tt, token, position.file_id)?,
_ => return None,
}
};
};
Some(navs)
})
.flatten()
.collect::<Vec<NavigationTarget>>();
Some(RangeInfo::new(original_token.text_range(), navs))
}