This commit is contained in:
Lukas Wirth 2021-08-16 16:12:20 +02:00
parent b641a66078
commit 0c0142f61a
4 changed files with 16 additions and 22 deletions

View file

@ -131,13 +131,8 @@ impl SourceToDefCtx<'_, '_> {
pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> { pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> {
let _p = profile::span("module_to_def"); let _p = profile::span("module_to_def");
let parent_declaration = src let parent_declaration =
.as_ref() src.syntax().cloned().ancestors_with_macros(self.db.upcast()).skip(1).find_map(|it| {
.map(|it| it.syntax())
.cloned()
.ancestors_with_macros(self.db.upcast())
.skip(1)
.find_map(|it| {
let m = ast::Module::cast(it.value.clone())?; let m = ast::Module::cast(it.value.clone())?;
Some(it.with_value(m)) Some(it.with_value(m))
}); });
@ -217,7 +212,7 @@ impl SourceToDefCtx<'_, '_> {
&mut self, &mut self,
src: InFile<ast::IdentPat>, src: InFile<ast::IdentPat>,
) -> Option<(DefWithBodyId, PatId)> { ) -> Option<(DefWithBodyId, PatId)> {
let container = self.find_pat_or_label_container(src.as_ref().map(|it| it.syntax()))?; let container = self.find_pat_or_label_container(src.syntax())?;
let (_body, source_map) = self.db.body_with_source_map(container); let (_body, source_map) = self.db.body_with_source_map(container);
let src = src.map(ast::Pat::from); let src = src.map(ast::Pat::from);
let pat_id = source_map.node_pat(src.as_ref())?; let pat_id = source_map.node_pat(src.as_ref())?;
@ -227,7 +222,7 @@ impl SourceToDefCtx<'_, '_> {
&mut self, &mut self,
src: InFile<ast::SelfParam>, src: InFile<ast::SelfParam>,
) -> Option<(DefWithBodyId, PatId)> { ) -> Option<(DefWithBodyId, PatId)> {
let container = self.find_pat_or_label_container(src.as_ref().map(|it| it.syntax()))?; let container = self.find_pat_or_label_container(src.syntax())?;
let (_body, source_map) = self.db.body_with_source_map(container); let (_body, source_map) = self.db.body_with_source_map(container);
let pat_id = source_map.node_self_param(src.as_ref())?; let pat_id = source_map.node_self_param(src.as_ref())?;
Some((container, pat_id)) Some((container, pat_id))
@ -236,7 +231,7 @@ impl SourceToDefCtx<'_, '_> {
&mut self, &mut self,
src: InFile<ast::Label>, src: InFile<ast::Label>,
) -> Option<(DefWithBodyId, LabelId)> { ) -> Option<(DefWithBodyId, LabelId)> {
let container = self.find_pat_or_label_container(src.as_ref().map(|it| it.syntax()))?; let container = self.find_pat_or_label_container(src.syntax())?;
let (_body, source_map) = self.db.body_with_source_map(container); let (_body, source_map) = self.db.body_with_source_map(container);
let label_id = source_map.node_label(src.as_ref())?; let label_id = source_map.node_label(src.as_ref())?;
Some((container, label_id)) Some((container, label_id))
@ -264,8 +259,7 @@ impl SourceToDefCtx<'_, '_> {
} }
pub(super) fn type_param_to_def(&mut self, src: InFile<ast::TypeParam>) -> Option<TypeParamId> { pub(super) fn type_param_to_def(&mut self, src: InFile<ast::TypeParam>) -> Option<TypeParamId> {
let container: ChildContainer = let container: ChildContainer = self.find_generic_param_container(src.syntax())?.into();
self.find_generic_param_container(src.as_ref().map(|it| it.syntax()))?.into();
let db = self.db; let db = self.db;
let dyn_map = let dyn_map =
&*self.cache.entry(container).or_insert_with(|| container.child_by_source(db)); &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
@ -276,8 +270,7 @@ impl SourceToDefCtx<'_, '_> {
&mut self, &mut self,
src: InFile<ast::LifetimeParam>, src: InFile<ast::LifetimeParam>,
) -> Option<LifetimeParamId> { ) -> Option<LifetimeParamId> {
let container: ChildContainer = let container: ChildContainer = self.find_generic_param_container(src.syntax())?.into();
self.find_generic_param_container(src.as_ref().map(|it| it.syntax()))?.into();
let db = self.db; let db = self.db;
let dyn_map = let dyn_map =
&*self.cache.entry(container).or_insert_with(|| container.child_by_source(db)); &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
@ -288,8 +281,7 @@ impl SourceToDefCtx<'_, '_> {
&mut self, &mut self,
src: InFile<ast::ConstParam>, src: InFile<ast::ConstParam>,
) -> Option<ConstParamId> { ) -> Option<ConstParamId> {
let container: ChildContainer = let container: ChildContainer = self.find_generic_param_container(src.syntax())?.into();
self.find_generic_param_container(src.as_ref().map(|it| it.syntax()))?.into();
let db = self.db; let db = self.db;
let dyn_map = let dyn_map =
&*self.cache.entry(container).or_insert_with(|| container.child_by_source(db)); &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));

View file

@ -376,7 +376,7 @@ impl ExpansionInfo {
db::TokenExpander::MacroRules { def_site_token_map, .. } db::TokenExpander::MacroRules { def_site_token_map, .. }
| db::TokenExpander::MacroDef { def_site_token_map, .. }, | db::TokenExpander::MacroDef { def_site_token_map, .. },
Some(tt), Some(tt),
) => (def_site_token_map, tt.as_ref().map(|tt| tt.syntax().clone())), ) => (def_site_token_map, tt.syntax().cloned()),
_ => panic!("`Origin::Def` used with non-`macro_rules!` macro"), _ => panic!("`Origin::Def` used with non-`macro_rules!` macro"),
}, },
}; };

View file

@ -92,7 +92,7 @@ impl NavigationTarget {
pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget { pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
if let Some(src) = module.declaration_source(db) { if let Some(src) = module.declaration_source(db) {
let node = src.as_ref().map(|it| it.syntax()); let node = src.syntax();
let full_range = node.original_file_range(db); let full_range = node.original_file_range(db);
let focus_range = src let focus_range = src
.value .value
@ -298,7 +298,7 @@ impl TryToNav for hir::Impl {
let frange = if let Some(item) = &derive_attr { let frange = if let Some(item) = &derive_attr {
item.syntax().original_file_range(db) item.syntax().original_file_range(db)
} else { } else {
src.as_ref().map(|it| it.syntax()).original_file_range(db) src.syntax().original_file_range(db)
}; };
let focus_range = if derive_attr.is_some() { let focus_range = if derive_attr.is_some() {
None None

View file

@ -117,8 +117,6 @@ pub(crate) fn hover(
let node = token.parent()?; let node = token.parent()?;
let definition = match_ast! { let definition = match_ast! {
match node { match node {
// We don't use NameClass::referenced_or_defined here as we do not want to resolve
// field pattern shorthands to their definition.
ast::Name(name) => NameClass::classify(&sema, &name).map(|class| match class { ast::Name(name) => NameClass::classify(&sema, &name).map(|class| match class {
NameClass::Definition(it) | NameClass::ConstReference(it) => it, NameClass::Definition(it) | NameClass::ConstReference(it) => it,
NameClass::PatFieldShorthand { local_def, field_ref: _ } => Definition::Local(local_def), NameClass::PatFieldShorthand { local_def, field_ref: _ } => Definition::Local(local_def),
@ -139,6 +137,7 @@ pub(crate) fn hover(
NameClass::defined, NameClass::defined,
), ),
_ => { _ => {
// intra-doc links
if ast::Comment::cast(token.clone()).is_some() { if ast::Comment::cast(token.clone()).is_some() {
cov_mark::hit!(no_highlight_on_comment_hover); cov_mark::hit!(no_highlight_on_comment_hover);
let (attributes, def) = doc_attributes(&sema, &node)?; let (attributes, def) = doc_attributes(&sema, &node)?;
@ -153,9 +152,12 @@ pub(crate) fn hover(
Either::Left(it) => Definition::ModuleDef(it), Either::Left(it) => Definition::ModuleDef(it),
Either::Right(it) => Definition::Macro(it), Either::Right(it) => Definition::Macro(it),
}) })
// attributes, require special machinery as they are mere ident tokens
} else if let Some(attr) = token.ancestors().find_map(ast::Attr::cast) { } else if let Some(attr) = token.ancestors().find_map(ast::Attr::cast) {
// lints
if let res@Some(_) = try_hover_for_lint(&attr, &token) { if let res@Some(_) = try_hover_for_lint(&attr, &token) {
return res; return res;
// derives
} else { } else {
range_override = Some(token.text_range()); range_override = Some(token.text_range());
try_resolve_derive_input_at(&sema, &attr, &token).map(Definition::Macro) try_resolve_derive_input_at(&sema, &attr, &token).map(Definition::Macro)
@ -276,7 +278,7 @@ fn hover_type_info(
"```text\nType: {:>apad$}\nCoerced to: {:>opad$}\n```\n", "```text\nType: {:>apad$}\nCoerced to: {:>opad$}\n```\n",
uncoerced = original, uncoerced = original,
coerced = adjusted, coerced = adjusted,
// 6 base padding for static text prefix of each line // 6 base padding for difference of length of the two text prefixes
apad = 6 + adjusted.len().max(original.len()), apad = 6 + adjusted.len().max(original.len()),
opad = original.len(), opad = original.len(),
) )