mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 14:51:48 +00:00
Properly resolve intra doc links in hover and goto_definition
This commit is contained in:
parent
0b68e03bf5
commit
9df78ec4a4
4 changed files with 104 additions and 40 deletions
|
@ -26,12 +26,7 @@ pub(crate) type DocumentationLink = String;
|
||||||
|
|
||||||
/// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs)
|
/// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs)
|
||||||
pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String {
|
pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String {
|
||||||
let mut cb = |link: BrokenLink| {
|
let mut cb = broken_link_clone_cb;
|
||||||
Some((
|
|
||||||
/*url*/ link.reference.to_owned().into(),
|
|
||||||
/*title*/ link.reference.to_owned().into(),
|
|
||||||
))
|
|
||||||
};
|
|
||||||
let doc = Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(&mut cb));
|
let doc = Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(&mut cb));
|
||||||
|
|
||||||
let doc = map_links(doc, |target, title: &str| {
|
let doc = map_links(doc, |target, title: &str| {
|
||||||
|
@ -124,24 +119,24 @@ pub(crate) fn external_docs(
|
||||||
pub(crate) fn extract_definitions_from_markdown(
|
pub(crate) fn extract_definitions_from_markdown(
|
||||||
markdown: &str,
|
markdown: &str,
|
||||||
) -> Vec<(Range<usize>, String, Option<hir::Namespace>)> {
|
) -> Vec<(Range<usize>, String, Option<hir::Namespace>)> {
|
||||||
let mut res = vec![];
|
extract_definitions_from_markdown_(markdown, &mut broken_link_clone_cb).collect()
|
||||||
let mut cb = |link: BrokenLink| {
|
}
|
||||||
// These allocations are actually unnecessary but the lifetimes on BrokenLinkCallback are wrong
|
|
||||||
// this is fixed in the repo but not on the crates.io release yet
|
fn extract_definitions_from_markdown_<'a>(
|
||||||
Some((
|
markdown: &'a str,
|
||||||
/*url*/ link.reference.to_owned().into(),
|
cb: &'a mut dyn FnMut(BrokenLink<'_>) -> Option<(CowStr<'a>, CowStr<'a>)>,
|
||||||
/*title*/ link.reference.to_owned().into(),
|
) -> impl Iterator<Item = (Range<usize>, String, Option<hir::Namespace>)> + 'a {
|
||||||
))
|
Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(cb))
|
||||||
};
|
.into_offset_iter()
|
||||||
let doc = Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(&mut cb));
|
.filter_map(|(event, range)| {
|
||||||
for (event, range) in doc.into_offset_iter() {
|
if let Event::Start(Tag::Link(_, target, title)) = event {
|
||||||
if let Event::Start(Tag::Link(_, target, title)) = event {
|
let link = if target.is_empty() { title } else { target };
|
||||||
let link = if target.is_empty() { title } else { target };
|
let (link, ns) = parse_intra_doc_link(&link);
|
||||||
let (link, ns) = parse_intra_doc_link(&link);
|
Some((range, link.to_string(), ns))
|
||||||
res.push((range, link.to_string(), ns));
|
} else {
|
||||||
}
|
None
|
||||||
}
|
}
|
||||||
res
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extracts a link from a comment at the given position returning the spanning range, link and
|
/// Extracts a link from a comment at the given position returning the spanning range, link and
|
||||||
|
@ -149,20 +144,24 @@ pub(crate) fn extract_definitions_from_markdown(
|
||||||
pub(crate) fn extract_positioned_link_from_comment(
|
pub(crate) fn extract_positioned_link_from_comment(
|
||||||
position: TextSize,
|
position: TextSize,
|
||||||
comment: &ast::Comment,
|
comment: &ast::Comment,
|
||||||
|
docs: hir::Documentation,
|
||||||
) -> Option<(TextRange, String, Option<hir::Namespace>)> {
|
) -> Option<(TextRange, String, Option<hir::Namespace>)> {
|
||||||
let doc_comment = comment.doc_comment()?;
|
let doc_comment = comment.doc_comment()?.to_string() + "\n" + docs.as_str();
|
||||||
let comment_start =
|
let comment_start =
|
||||||
comment.syntax().text_range().start() + TextSize::from(comment.prefix().len() as u32);
|
comment.syntax().text_range().start() + TextSize::from(comment.prefix().len() as u32);
|
||||||
let def_links = extract_definitions_from_markdown(doc_comment);
|
let len = comment.syntax().text_range().len().into();
|
||||||
let (range, def_link, ns) =
|
let mut cb = broken_link_clone_cb;
|
||||||
def_links.into_iter().find_map(|(Range { start, end }, def_link, ns)| {
|
// because pulldown_cmarks lifetimes are wrong we gotta dance around a few temporaries here
|
||||||
|
let res = extract_definitions_from_markdown_(&doc_comment, &mut cb)
|
||||||
|
.take_while(|&(Range { end, .. }, ..)| end < len)
|
||||||
|
.find_map(|(Range { start, end }, def_link, ns)| {
|
||||||
let range = TextRange::at(
|
let range = TextRange::at(
|
||||||
comment_start + TextSize::from(start as u32),
|
comment_start + TextSize::from(start as u32),
|
||||||
TextSize::from((end - start) as u32),
|
TextSize::from((end - start) as u32),
|
||||||
);
|
);
|
||||||
range.contains(position).then(|| (range, def_link, ns))
|
range.contains(position).then(|| (range, def_link, ns))
|
||||||
})?;
|
});
|
||||||
Some((range, def_link, ns))
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Turns a syntax node into it's [`Definition`] if it can hold docs.
|
/// Turns a syntax node into it's [`Definition`] if it can hold docs.
|
||||||
|
@ -220,6 +219,15 @@ pub(crate) fn resolve_doc_path_for_def(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn broken_link_clone_cb<'a, 'b>(link: BrokenLink<'a>) -> Option<(CowStr<'b>, CowStr<'b>)> {
|
||||||
|
// These allocations are actually unnecessary but the lifetimes on BrokenLinkCallback are wrong
|
||||||
|
// this is fixed in the repo but not on the crates.io release yet
|
||||||
|
Some((
|
||||||
|
/*url*/ link.reference.to_owned().into(),
|
||||||
|
/*title*/ link.reference.to_owned().into(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME:
|
// FIXME:
|
||||||
// BUG: For Option::Some
|
// BUG: For Option::Some
|
||||||
// Returns https://doc.rust-lang.org/nightly/core/prelude/v1/enum.Option.html#variant.Some
|
// Returns https://doc.rust-lang.org/nightly/core/prelude/v1/enum.Option.html#variant.Some
|
||||||
|
|
|
@ -31,7 +31,8 @@ pub(crate) fn goto_definition(
|
||||||
let token = sema.descend_into_macros(original_token.clone());
|
let token = sema.descend_into_macros(original_token.clone());
|
||||||
let parent = token.parent()?;
|
let parent = token.parent()?;
|
||||||
if let Some(comment) = ast::Comment::cast(token) {
|
if let Some(comment) = ast::Comment::cast(token) {
|
||||||
let (_, link, ns) = extract_positioned_link_from_comment(position.offset, &comment)?;
|
let docs = doc_owner_to_def(&sema, &parent)?.docs(db)?;
|
||||||
|
let (_, link, ns) = extract_positioned_link_from_comment(position.offset, &comment, docs)?;
|
||||||
let def = doc_owner_to_def(&sema, &parent)?;
|
let def = doc_owner_to_def(&sema, &parent)?;
|
||||||
let nav = resolve_doc_path_for_def(db, def, &link, ns)?.try_to_nav(db)?;
|
let nav = resolve_doc_path_for_def(db, def, &link, ns)?.try_to_nav(db)?;
|
||||||
return Some(RangeInfo::new(original_token.text_range(), vec![nav]));
|
return Some(RangeInfo::new(original_token.text_range(), vec![nav]));
|
||||||
|
@ -1158,4 +1159,25 @@ fn fn_macro() {}
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn goto_intra_doc_links() {
|
||||||
|
check(
|
||||||
|
r#"
|
||||||
|
|
||||||
|
pub mod theitem {
|
||||||
|
/// This is the item. Cool!
|
||||||
|
pub struct TheItem;
|
||||||
|
//^^^^^^^
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gives you a [`TheItem$0`].
|
||||||
|
///
|
||||||
|
/// [`TheItem`]: theitem::TheItem
|
||||||
|
pub fn gimme() -> theitem::TheItem {
|
||||||
|
theitem::TheItem
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,10 +115,11 @@ pub(crate) fn hover(
|
||||||
|
|
||||||
_ => ast::Comment::cast(token.clone())
|
_ => ast::Comment::cast(token.clone())
|
||||||
.and_then(|comment| {
|
.and_then(|comment| {
|
||||||
let (idl_range, link, ns) =
|
|
||||||
extract_positioned_link_from_comment(position.offset, &comment)?;
|
|
||||||
range = Some(idl_range);
|
|
||||||
let def = doc_owner_to_def(&sema, &node)?;
|
let def = doc_owner_to_def(&sema, &node)?;
|
||||||
|
let docs = def.docs(db)?;
|
||||||
|
let (idl_range, link, ns) =
|
||||||
|
extract_positioned_link_from_comment(position.offset, &comment, docs)?;
|
||||||
|
range = Some(idl_range);
|
||||||
resolve_doc_path_for_def(db, def, &link, ns)
|
resolve_doc_path_for_def(db, def, &link, ns)
|
||||||
})
|
})
|
||||||
.map(Definition::ModuleDef),
|
.map(Definition::ModuleDef),
|
||||||
|
@ -3812,23 +3813,33 @@ fn main() {
|
||||||
fn hover_intra_doc_links() {
|
fn hover_intra_doc_links() {
|
||||||
check(
|
check(
|
||||||
r#"
|
r#"
|
||||||
/// This is the [`foo`](foo$0) function.
|
|
||||||
fn foo() {}
|
pub mod theitem {
|
||||||
|
/// This is the item. Cool!
|
||||||
|
pub struct TheItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gives you a [`TheItem$0`].
|
||||||
|
///
|
||||||
|
/// [`TheItem`]: theitem::TheItem
|
||||||
|
pub fn gimme() -> theitem::TheItem {
|
||||||
|
theitem::TheItem
|
||||||
|
}
|
||||||
"#,
|
"#,
|
||||||
expect*
|
*[`TheItem`]*
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
test
|
test::theitem
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn foo()
|
pub struct TheItem
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
This is the [`foo`](https://docs.rs/test/*/test/fn.foo.html) function.
|
This is the item. Cool!
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,6 +79,29 @@ impl Definition {
|
||||||
};
|
};
|
||||||
Some(name)
|
Some(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn docs(&self, db: &RootDatabase) -> Option<hir::Documentation> {
|
||||||
|
match self {
|
||||||
|
Definition::Macro(it) => it.docs(db),
|
||||||
|
Definition::Field(it) => it.docs(db),
|
||||||
|
Definition::ModuleDef(def) => match def {
|
||||||
|
hir::ModuleDef::Module(it) => it.docs(db),
|
||||||
|
hir::ModuleDef::Function(it) => it.docs(db),
|
||||||
|
hir::ModuleDef::Adt(def) => match def {
|
||||||
|
hir::Adt::Struct(it) => it.docs(db),
|
||||||
|
hir::Adt::Union(it) => it.docs(db),
|
||||||
|
hir::Adt::Enum(it) => it.docs(db),
|
||||||
|
},
|
||||||
|
hir::ModuleDef::Variant(it) => it.docs(db),
|
||||||
|
hir::ModuleDef::Const(it) => it.docs(db),
|
||||||
|
hir::ModuleDef::Static(it) => it.docs(db),
|
||||||
|
hir::ModuleDef::Trait(it) => it.docs(db),
|
||||||
|
hir::ModuleDef::TypeAlias(it) => it.docs(db),
|
||||||
|
hir::ModuleDef::BuiltinType(_) => None,
|
||||||
|
},
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue