10639: fix: make `goto_declaration` multi-token mapping aware r=Veykril a=spookyvision



10640: assume valid identifier r=Veykril a=spookyvision

improve https://github.com/rust-analyzer/rust-analyzer/pull/10637/ by always returning `Some(potentially_empty_vec)` instead of `None` in the empty case

Co-authored-by: Anatol Ulrich <anatol.ulrich@ferrous-systems.com>
This commit is contained in:
bors[bot] 2021-10-26 17:51:33 +00:00 committed by GitHub
commit c48730cb72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 27 deletions

View file

@ -19,7 +19,11 @@ pub(crate) fn goto_declaration(
let original_token = file let original_token = file
.token_at_offset(position.offset) .token_at_offset(position.offset)
.find(|it| matches!(it.kind(), IDENT | T![self] | T![super] | T![crate]))?; .find(|it| matches!(it.kind(), IDENT | T![self] | T![super] | T![crate]))?;
let token = sema.descend_into_macros_single(original_token.clone()); let range = original_token.text_range();
let info: Vec<NavigationTarget> = sema
.descend_into_macros(original_token)
.iter()
.filter_map(|token| {
let parent = token.parent()?; let parent = token.parent()?;
let def = match_ast! { let def = match_ast! {
match parent { match parent {
@ -31,16 +35,19 @@ pub(crate) fn goto_declaration(
NameClass::Definition(it) => Some(it), NameClass::Definition(it) => Some(it),
_ => None _ => None
}, },
_ => None, _ => None
} }
}; };
match def? { match def? {
Definition::ModuleDef(hir::ModuleDef::Module(module)) => Some(RangeInfo::new( Definition::ModuleDef(hir::ModuleDef::Module(module)) => {
original_token.text_range(), Some(NavigationTarget::from_module_to_decl(db, module))
vec![NavigationTarget::from_module_to_decl(db, module)], }
)),
_ => None, _ => None,
} }
})
.collect();
Some(RangeInfo::new(range, info))
} }
#[cfg(test)] #[cfg(test)]

View file

@ -81,12 +81,8 @@ pub(crate) fn goto_type_definition(
} }
}); });
}); });
if res.is_empty() {
None
} else {
Some(RangeInfo::new(range, res)) Some(RangeInfo::new(range, res))
} }
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {