Skip to content

Commit 0235ff8

Browse files
committed
extract function: doc_attributes to find def from inner doc
Signed-off-by: Hayashi Mikihiro <[email protected]>
1 parent cc0e3ac commit 0235ff8

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

crates/ide/src/doc_links.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use pulldown_cmark_to_cmark::{Options as CMarkOptions, cmark_resume_with_options
1212
use stdx::format_to;
1313
use url::Url;
1414

15-
use hir::{Adt, AsAssocItem, AssocItem, AssocItemContainer, HasAttrs, db::HirDatabase, sym};
15+
use hir::{
16+
Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrsWithOwner, HasAttrs, db::HirDatabase, sym,
17+
};
1618
use ide_db::{
1719
RootDatabase,
1820
base_db::{CrateOrigin, LangCrateOrigin, ReleaseChannel, RootQueryDb},
@@ -322,13 +324,7 @@ impl DocCommentToken {
322324
};
323325
let token_start = t.text_range().start();
324326
let abs_in_expansion_offset = token_start + relative_comment_offset + descended_prefix_len;
325-
326-
let (attributes, def) = if is_inner && node.kind() != SOURCE_FILE {
327-
let parent = node.parent()?;
328-
doc_attributes(sema, &parent).unwrap_or(doc_attributes(sema, &parent.parent()?)?)
329-
}else {
330-
doc_attributes(sema, &node)?
331-
};
327+
let (attributes, def) = Self::doc_attributes(sema, &node, is_inner)?;
332328
let (docs, doc_mapping) = docs_with_rangemap(sema.db, &attributes)?;
333329
let (in_expansion_range, link, ns, is_inner) =
334330
extract_definitions_from_docs(&docs).into_iter().find_map(|(range, link, ns)| {
@@ -343,6 +339,28 @@ impl DocCommentToken {
343339
cb(def, node, absolute_range)
344340
})
345341
}
342+
343+
/// When we hover a inner doc item, this find a attached definition.
344+
/// ```
345+
/// // node == ITEM_LIST
346+
/// // node.parent == EXPR_BLOCK
347+
/// // node.parent().parent() == FN
348+
/// fn f() {
349+
/// //! [`S$0`]
350+
/// }
351+
/// ```
352+
fn doc_attributes(
353+
sema: &Semantics<'_, RootDatabase>,
354+
node: &SyntaxNode,
355+
is_inner_doc: bool,
356+
) -> Option<(AttrsWithOwner, Definition)> {
357+
if is_inner_doc && node.kind() != SOURCE_FILE {
358+
let parent = node.parent()?;
359+
doc_attributes(sema, &parent).or(doc_attributes(sema, &parent.parent()?))
360+
} else {
361+
doc_attributes(sema, node)
362+
}
363+
}
346364
}
347365

348366
fn broken_link_clone_cb(link: BrokenLink<'_>) -> Option<(CowStr<'_>, CowStr<'_>)> {

0 commit comments

Comments
 (0)