Skip to content

Commit 9df78ec

Browse files
committed
Properly resolve intra doc links in hover and goto_definition
1 parent 0b68e03 commit 9df78ec

File tree

4 files changed

+103
-39
lines changed

4 files changed

+103
-39
lines changed

crates/ide/src/doc_links.rs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ pub(crate) type DocumentationLink = String;
2626

2727
/// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs)
2828
pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String {
29-
let mut cb = |link: BrokenLink| {
30-
Some((
31-
/*url*/ link.reference.to_owned().into(),
32-
/*title*/ link.reference.to_owned().into(),
33-
))
34-
};
29+
let mut cb = broken_link_clone_cb;
3530
let doc = Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(&mut cb));
3631

3732
let doc = map_links(doc, |target, title: &str| {
@@ -124,45 +119,49 @@ pub(crate) fn external_docs(
124119
pub(crate) fn extract_definitions_from_markdown(
125120
markdown: &str,
126121
) -> Vec<(Range<usize>, String, Option<hir::Namespace>)> {
127-
let mut res = vec![];
128-
let mut cb = |link: BrokenLink| {
129-
// These allocations are actually unnecessary but the lifetimes on BrokenLinkCallback are wrong
130-
// this is fixed in the repo but not on the crates.io release yet
131-
Some((
132-
/*url*/ link.reference.to_owned().into(),
133-
/*title*/ link.reference.to_owned().into(),
134-
))
135-
};
136-
let doc = Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(&mut cb));
137-
for (event, range) in doc.into_offset_iter() {
138-
if let Event::Start(Tag::Link(_, target, title)) = event {
139-
let link = if target.is_empty() { title } else { target };
140-
let (link, ns) = parse_intra_doc_link(&link);
141-
res.push((range, link.to_string(), ns));
142-
}
143-
}
144-
res
122+
extract_definitions_from_markdown_(markdown, &mut broken_link_clone_cb).collect()
123+
}
124+
125+
fn extract_definitions_from_markdown_<'a>(
126+
markdown: &'a str,
127+
cb: &'a mut dyn FnMut(BrokenLink<'_>) -> Option<(CowStr<'a>, CowStr<'a>)>,
128+
) -> impl Iterator<Item = (Range<usize>, String, Option<hir::Namespace>)> + 'a {
129+
Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(cb))
130+
.into_offset_iter()
131+
.filter_map(|(event, range)| {
132+
if let Event::Start(Tag::Link(_, target, title)) = event {
133+
let link = if target.is_empty() { title } else { target };
134+
let (link, ns) = parse_intra_doc_link(&link);
135+
Some((range, link.to_string(), ns))
136+
} else {
137+
None
138+
}
139+
})
145140
}
146141

147142
/// Extracts a link from a comment at the given position returning the spanning range, link and
148143
/// optionally it's namespace.
149144
pub(crate) fn extract_positioned_link_from_comment(
150145
position: TextSize,
151146
comment: &ast::Comment,
147+
docs: hir::Documentation,
152148
) -> Option<(TextRange, String, Option<hir::Namespace>)> {
153-
let doc_comment = comment.doc_comment()?;
149+
let doc_comment = comment.doc_comment()?.to_string() + "\n" + docs.as_str();
154150
let comment_start =
155151
comment.syntax().text_range().start() + TextSize::from(comment.prefix().len() as u32);
156-
let def_links = extract_definitions_from_markdown(doc_comment);
157-
let (range, def_link, ns) =
158-
def_links.into_iter().find_map(|(Range { start, end }, def_link, ns)| {
152+
let len = comment.syntax().text_range().len().into();
153+
let mut cb = broken_link_clone_cb;
154+
// because pulldown_cmarks lifetimes are wrong we gotta dance around a few temporaries here
155+
let res = extract_definitions_from_markdown_(&doc_comment, &mut cb)
156+
.take_while(|&(Range { end, .. }, ..)| end < len)
157+
.find_map(|(Range { start, end }, def_link, ns)| {
159158
let range = TextRange::at(
160159
comment_start + TextSize::from(start as u32),
161160
TextSize::from((end - start) as u32),
162161
);
163162
range.contains(position).then(|| (range, def_link, ns))
164-
})?;
165-
Some((range, def_link, ns))
163+
});
164+
res
166165
}
167166

168167
/// 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(
220219
}
221220
}
222221

222+
fn broken_link_clone_cb<'a, 'b>(link: BrokenLink<'a>) -> Option<(CowStr<'b>, CowStr<'b>)> {
223+
// These allocations are actually unnecessary but the lifetimes on BrokenLinkCallback are wrong
224+
// this is fixed in the repo but not on the crates.io release yet
225+
Some((
226+
/*url*/ link.reference.to_owned().into(),
227+
/*title*/ link.reference.to_owned().into(),
228+
))
229+
}
230+
223231
// FIXME:
224232
// BUG: For Option::Some
225233
// Returns https://doc.rust-lang.org/nightly/core/prelude/v1/enum.Option.html#variant.Some

crates/ide/src/goto_definition.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ pub(crate) fn goto_definition(
3131
let token = sema.descend_into_macros(original_token.clone());
3232
let parent = token.parent()?;
3333
if let Some(comment) = ast::Comment::cast(token) {
34-
let (_, link, ns) = extract_positioned_link_from_comment(position.offset, &comment)?;
34+
let docs = doc_owner_to_def(&sema, &parent)?.docs(db)?;
35+
let (_, link, ns) = extract_positioned_link_from_comment(position.offset, &comment, docs)?;
3536
let def = doc_owner_to_def(&sema, &parent)?;
3637
let nav = resolve_doc_path_for_def(db, def, &link, ns)?.try_to_nav(db)?;
3738
return Some(RangeInfo::new(original_token.text_range(), vec![nav]));
@@ -1158,4 +1159,25 @@ fn fn_macro() {}
11581159
"#,
11591160
)
11601161
}
1162+
1163+
#[test]
1164+
fn goto_intra_doc_links() {
1165+
check(
1166+
r#"
1167+
1168+
pub mod theitem {
1169+
/// This is the item. Cool!
1170+
pub struct TheItem;
1171+
//^^^^^^^
1172+
}
1173+
1174+
/// Gives you a [`TheItem$0`].
1175+
///
1176+
/// [`TheItem`]: theitem::TheItem
1177+
pub fn gimme() -> theitem::TheItem {
1178+
theitem::TheItem
1179+
}
1180+
"#,
1181+
);
1182+
}
11611183
}

crates/ide/src/hover.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,11 @@ pub(crate) fn hover(
115115

116116
_ => ast::Comment::cast(token.clone())
117117
.and_then(|comment| {
118+
let def = doc_owner_to_def(&sema, &node)?;
119+
let docs = def.docs(db)?;
118120
let (idl_range, link, ns) =
119-
extract_positioned_link_from_comment(position.offset, &comment)?;
121+
extract_positioned_link_from_comment(position.offset, &comment, docs)?;
120122
range = Some(idl_range);
121-
let def = doc_owner_to_def(&sema, &node)?;
122123
resolve_doc_path_for_def(db, def, &link, ns)
123124
})
124125
.map(Definition::ModuleDef),
@@ -3812,23 +3813,33 @@ fn main() {
38123813
fn hover_intra_doc_links() {
38133814
check(
38143815
r#"
3815-
/// This is the [`foo`](foo$0) function.
3816-
fn foo() {}
3816+
3817+
pub mod theitem {
3818+
/// This is the item. Cool!
3819+
pub struct TheItem;
3820+
}
3821+
3822+
/// Gives you a [`TheItem$0`].
3823+
///
3824+
/// [`TheItem`]: theitem::TheItem
3825+
pub fn gimme() -> theitem::TheItem {
3826+
theitem::TheItem
3827+
}
38173828
"#,
38183829
expect![[r#"
3819-
*[`foo`](foo)*
3830+
*[`TheItem`]*
38203831
38213832
```rust
3822-
test
3833+
test::theitem
38233834
```
38243835
38253836
```rust
3826-
fn foo()
3837+
pub struct TheItem
38273838
```
38283839
38293840
---
38303841
3831-
This is the [`foo`](https://docs.rs/test/*/test/fn.foo.html) function.
3842+
This is the item. Cool!
38323843
"#]],
38333844
);
38343845
}

crates/ide_db/src/defs.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,29 @@ impl Definition {
7979
};
8080
Some(name)
8181
}
82+
83+
pub fn docs(&self, db: &RootDatabase) -> Option<hir::Documentation> {
84+
match self {
85+
Definition::Macro(it) => it.docs(db),
86+
Definition::Field(it) => it.docs(db),
87+
Definition::ModuleDef(def) => match def {
88+
hir::ModuleDef::Module(it) => it.docs(db),
89+
hir::ModuleDef::Function(it) => it.docs(db),
90+
hir::ModuleDef::Adt(def) => match def {
91+
hir::Adt::Struct(it) => it.docs(db),
92+
hir::Adt::Union(it) => it.docs(db),
93+
hir::Adt::Enum(it) => it.docs(db),
94+
},
95+
hir::ModuleDef::Variant(it) => it.docs(db),
96+
hir::ModuleDef::Const(it) => it.docs(db),
97+
hir::ModuleDef::Static(it) => it.docs(db),
98+
hir::ModuleDef::Trait(it) => it.docs(db),
99+
hir::ModuleDef::TypeAlias(it) => it.docs(db),
100+
hir::ModuleDef::BuiltinType(_) => None,
101+
},
102+
_ => None,
103+
}
104+
}
82105
}
83106

84107
#[derive(Debug)]

0 commit comments

Comments
 (0)