8245: Properly resolve intra doc links in hover and goto_definition r=matklad a=Veykril

Unfortunately involves a bit of weird workarounds due to pulldown_cmark's incorrect lifetimes on `BrokenLinkCallback`... I should probably open an issue there asking for the fixes to be pushed to a release since they already exist in the repo for quite some time it seems.

Fixes #8258, Fixes #8238

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-04-05 12:30:20 +00:00 committed by GitHub
commit c2be91dcd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 260 additions and 157 deletions

View file

@ -1,6 +1,9 @@
//! Extracts, resolves and rewrites links and intra-doc links in markdown documentation.
use std::{convert::TryFrom, iter::once, ops::Range};
use std::{
convert::{TryFrom, TryInto},
iter::once,
};
use itertools::Itertools;
use pulldown_cmark::{BrokenLink, CowStr, Event, InlineStr, LinkType, Options, Parser, Tag};
@ -16,8 +19,7 @@ use ide_db::{
RootDatabase,
};
use syntax::{
ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxNode, SyntaxToken, TextRange, TextSize,
TokenAtOffset, T,
ast, match_ast, AstNode, SyntaxKind::*, SyntaxNode, SyntaxToken, TextRange, TokenAtOffset, T,
};
use crate::{FilePosition, Semantics};
@ -26,12 +28,7 @@ pub(crate) type DocumentationLink = String;
/// 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 {
let mut cb = |link: BrokenLink| {
Some((
/*url*/ link.reference.to_owned().into(),
/*title*/ link.reference.to_owned().into(),
))
};
let mut cb = broken_link_clone_cb;
let doc = Parser::new_with_broken_link_callback(markdown, Options::empty(), Some(&mut cb));
let doc = map_links(doc, |target, title: &str| {
@ -123,74 +120,27 @@ pub(crate) fn external_docs(
/// Extracts all links from a given markdown text.
pub(crate) fn extract_definitions_from_markdown(
markdown: &str,
) -> Vec<(Range<usize>, String, Option<hir::Namespace>)> {
let mut res = vec![];
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
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));
for (event, range) in doc.into_offset_iter() {
) -> Vec<(TextRange, String, Option<hir::Namespace>)> {
Parser::new_with_broken_link_callback(
markdown,
Options::empty(),
Some(&mut broken_link_clone_cb),
)
.into_offset_iter()
.filter_map(|(event, range)| {
if let Event::Start(Tag::Link(_, target, title)) = event {
let link = if target.is_empty() { title } else { target };
let (link, ns) = parse_intra_doc_link(&link);
res.push((range, link.to_string(), ns));
Some((
TextRange::new(range.start.try_into().ok()?, range.end.try_into().ok()?),
link.to_string(),
ns,
))
} else {
None
}
}
res
}
/// Extracts a link from a comment at the given position returning the spanning range, link and
/// optionally it's namespace.
pub(crate) fn extract_positioned_link_from_comment(
position: TextSize,
comment: &ast::Comment,
) -> Option<(TextRange, String, Option<hir::Namespace>)> {
let doc_comment = comment.doc_comment()?;
let comment_start =
comment.syntax().text_range().start() + TextSize::from(comment.prefix().len() as u32);
let def_links = extract_definitions_from_markdown(doc_comment);
let (range, def_link, ns) =
def_links.into_iter().find_map(|(Range { start, end }, def_link, ns)| {
let range = TextRange::at(
comment_start + TextSize::from(start as u32),
TextSize::from((end - start) as u32),
);
range.contains(position).then(|| (range, def_link, ns))
})?;
Some((range, def_link, ns))
}
/// Turns a syntax node into it's [`Definition`] if it can hold docs.
pub(crate) fn doc_owner_to_def(
sema: &Semantics<RootDatabase>,
item: &SyntaxNode,
) -> Option<Definition> {
let res: hir::ModuleDef = match_ast! {
match item {
ast::SourceFile(_it) => sema.scope(item).module()?.into(),
ast::Fn(it) => sema.to_def(&it)?.into(),
ast::Struct(it) => sema.to_def(&it)?.into(),
ast::Enum(it) => sema.to_def(&it)?.into(),
ast::Union(it) => sema.to_def(&it)?.into(),
ast::Trait(it) => sema.to_def(&it)?.into(),
ast::Const(it) => sema.to_def(&it)?.into(),
ast::Static(it) => sema.to_def(&it)?.into(),
ast::TypeAlias(it) => sema.to_def(&it)?.into(),
ast::Variant(it) => sema.to_def(&it)?.into(),
ast::Trait(it) => sema.to_def(&it)?.into(),
ast::Impl(it) => return sema.to_def(&it).map(Definition::SelfType),
ast::Macro(it) => return sema.to_def(&it).map(Definition::Macro),
ast::TupleField(it) => return sema.to_def(&it).map(Definition::Field),
ast::RecordField(it) => return sema.to_def(&it).map(Definition::Field),
_ => return None,
}
};
Some(Definition::ModuleDef(res))
})
.collect()
}
pub(crate) fn resolve_doc_path_for_def(
@ -220,6 +170,42 @@ pub(crate) fn resolve_doc_path_for_def(
}
}
pub(crate) fn doc_attributes(
sema: &Semantics<RootDatabase>,
node: &SyntaxNode,
) -> Option<(hir::AttrsWithOwner, Definition)> {
match_ast! {
match node {
ast::SourceFile(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Module(def)))),
ast::Module(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Module(def)))),
ast::Fn(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Function(def)))),
ast::Struct(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Struct(def))))),
ast::Union(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Union(def))))),
ast::Enum(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(def))))),
ast::Variant(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Variant(def)))),
ast::Trait(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Trait(def)))),
ast::Static(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Static(def)))),
ast::Const(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::Const(def)))),
ast::TypeAlias(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::ModuleDef(hir::ModuleDef::TypeAlias(def)))),
ast::Impl(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::SelfType(def))),
ast::RecordField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
ast::TupleField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
ast::Macro(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Macro(def))),
// ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
_ => return None
}
}
}
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:
// BUG: For Option::Some
// Returns https://doc.rust-lang.org/nightly/core/prelude/v1/enum.Option.html#variant.Some