2021-03-23 13:19:44 -05:00
|
|
|
//! Extracts, resolves and rewrites links and intra-doc links in markdown documentation.
|
2020-07-31 19:55:04 -05:00
|
|
|
|
2021-07-19 12:57:55 -05:00
|
|
|
mod intra_doc_links;
|
|
|
|
|
2021-08-02 07:33:09 -05:00
|
|
|
use either::Either;
|
2020-10-13 12:31:23 -05:00
|
|
|
use pulldown_cmark::{BrokenLink, CowStr, Event, InlineStr, LinkType, Options, Parser, Tag};
|
2021-10-28 09:13:37 -05:00
|
|
|
use pulldown_cmark_to_cmark::{cmark_with_options, Options as CMarkOptions};
|
2021-07-20 09:01:27 -05:00
|
|
|
use stdx::format_to;
|
2020-08-26 11:56:41 -05:00
|
|
|
use url::Url;
|
2020-07-31 19:55:04 -05:00
|
|
|
|
2020-09-01 03:26:10 -05:00
|
|
|
use hir::{
|
2021-08-02 07:33:09 -05:00
|
|
|
db::HirDatabase, Adt, AsAssocItem, AssocItem, AssocItemContainer, Crate, HasAttrs, MacroDef,
|
|
|
|
ModuleDef,
|
2020-09-01 03:26:10 -05:00
|
|
|
};
|
2020-08-30 03:02:29 -05:00
|
|
|
use ide_db::{
|
2020-10-15 10:27:50 -05:00
|
|
|
defs::{Definition, NameClass, NameRefClass},
|
2021-06-22 10:28:07 -05:00
|
|
|
helpers::pick_best_token,
|
2020-08-30 03:02:29 -05:00
|
|
|
RootDatabase,
|
|
|
|
};
|
2021-09-23 12:17:29 -05:00
|
|
|
use syntax::{
|
|
|
|
ast::{self, IsString},
|
|
|
|
match_ast, AstNode, AstToken,
|
|
|
|
SyntaxKind::*,
|
|
|
|
SyntaxNode, SyntaxToken, TextRange, TextSize, T,
|
|
|
|
};
|
2020-08-30 03:02:29 -05:00
|
|
|
|
2021-07-19 12:57:55 -05:00
|
|
|
use crate::{
|
|
|
|
doc_links::intra_doc_links::{parse_intra_doc_link, strip_prefixes_suffixes},
|
|
|
|
FilePosition, Semantics,
|
|
|
|
};
|
2020-09-01 03:26:10 -05:00
|
|
|
|
2021-07-26 06:30:41 -05:00
|
|
|
/// Weblink to an item's documentation.
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) type DocumentationLink = String;
|
2020-08-30 03:02:29 -05:00
|
|
|
|
2021-11-03 09:40:41 -05:00
|
|
|
const MARKDOWN_OPTIONS: Options =
|
|
|
|
Options::ENABLE_FOOTNOTES.union(Options::ENABLE_TABLES).union(Options::ENABLE_TASKLISTS);
|
|
|
|
|
2020-07-31 19:55:04 -05:00
|
|
|
/// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs)
|
2021-07-20 09:01:27 -05:00
|
|
|
pub(crate) fn rewrite_links(db: &RootDatabase, markdown: &str, definition: Definition) -> String {
|
2021-03-29 14:23:45 -05:00
|
|
|
let mut cb = broken_link_clone_cb;
|
2021-11-03 09:40:41 -05:00
|
|
|
let doc = Parser::new_with_broken_link_callback(markdown, MARKDOWN_OPTIONS, Some(&mut cb));
|
2020-07-31 19:55:04 -05:00
|
|
|
|
2021-07-20 09:01:27 -05:00
|
|
|
let doc = map_links(doc, |target, title| {
|
2020-07-31 19:55:04 -05:00
|
|
|
// This check is imperfect, there's some overlap between valid intra-doc links
|
|
|
|
// and valid URLs so we choose to be too eager to try to resolve what might be
|
|
|
|
// a URL.
|
|
|
|
if target.contains("://") {
|
|
|
|
(target.to_string(), title.to_string())
|
|
|
|
} else {
|
2021-01-08 08:46:48 -06:00
|
|
|
// Two possibilities:
|
2020-07-31 19:55:04 -05:00
|
|
|
// * path-based links: `../../module/struct.MyStruct.html`
|
|
|
|
// * module-based links (AKA intra-doc links): `super::super::module::MyStruct`
|
2021-07-20 09:01:27 -05:00
|
|
|
if let Some(rewritten) = rewrite_intra_doc_link(db, definition, target, title) {
|
2020-08-26 11:56:41 -05:00
|
|
|
return rewritten;
|
|
|
|
}
|
2021-07-20 09:01:27 -05:00
|
|
|
if let Definition::ModuleDef(def) = definition {
|
2021-08-02 07:33:09 -05:00
|
|
|
if let Some(target) = rewrite_url_link(db, Either::Left(def), target) {
|
2020-08-26 11:56:41 -05:00
|
|
|
return (target, title.to_string());
|
|
|
|
}
|
2020-07-31 19:55:04 -05:00
|
|
|
}
|
2020-08-26 11:56:41 -05:00
|
|
|
|
|
|
|
(target.to_string(), title.to_string())
|
2020-07-31 19:55:04 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
let mut out = String::new();
|
2021-07-20 10:03:39 -05:00
|
|
|
cmark_with_options(
|
|
|
|
doc,
|
|
|
|
&mut out,
|
|
|
|
None,
|
2021-10-28 09:13:37 -05:00
|
|
|
CMarkOptions { code_block_backticks: 3, ..Default::default() },
|
2021-07-20 10:03:39 -05:00
|
|
|
)
|
|
|
|
.ok();
|
2020-07-31 19:55:04 -05:00
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2020-09-26 00:02:09 -05:00
|
|
|
/// Remove all links in markdown documentation.
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) fn remove_links(markdown: &str) -> String {
|
2020-09-26 00:02:09 -05:00
|
|
|
let mut drop_link = false;
|
|
|
|
|
2020-10-13 12:31:23 -05:00
|
|
|
let mut cb = |_: BrokenLink| {
|
|
|
|
let empty = InlineStr::try_from("").unwrap();
|
2021-03-16 19:56:31 -05:00
|
|
|
Some((CowStr::Inlined(empty), CowStr::Inlined(empty)))
|
2020-10-13 12:31:23 -05:00
|
|
|
};
|
2021-11-03 09:40:41 -05:00
|
|
|
let doc = Parser::new_with_broken_link_callback(markdown, MARKDOWN_OPTIONS, Some(&mut cb));
|
2020-09-26 00:02:09 -05:00
|
|
|
let doc = doc.filter_map(move |evt| match evt {
|
2021-07-20 09:01:27 -05:00
|
|
|
Event::Start(Tag::Link(link_type, target, title)) => {
|
2020-09-26 00:02:09 -05:00
|
|
|
if link_type == LinkType::Inline && target.contains("://") {
|
2021-07-20 09:01:27 -05:00
|
|
|
Some(Event::Start(Tag::Link(link_type, target, title)))
|
2020-09-26 00:02:09 -05:00
|
|
|
} else {
|
|
|
|
drop_link = true;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::End(_) if drop_link => {
|
|
|
|
drop_link = false;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
_ => Some(evt),
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut out = String::new();
|
2021-07-20 10:03:39 -05:00
|
|
|
cmark_with_options(
|
|
|
|
doc,
|
|
|
|
&mut out,
|
|
|
|
None,
|
2021-10-28 09:13:37 -05:00
|
|
|
CMarkOptions { code_block_backticks: 3, ..Default::default() },
|
2021-07-20 10:03:39 -05:00
|
|
|
)
|
|
|
|
.ok();
|
2020-09-26 00:02:09 -05:00
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2021-03-29 13:11:28 -05:00
|
|
|
/// Retrieve a link to documentation for the given symbol.
|
|
|
|
pub(crate) fn external_docs(
|
|
|
|
db: &RootDatabase,
|
|
|
|
position: &FilePosition,
|
|
|
|
) -> Option<DocumentationLink> {
|
2021-07-20 10:03:39 -05:00
|
|
|
let sema = &Semantics::new(db);
|
2021-03-29 13:11:28 -05:00
|
|
|
let file = sema.parse(position.file_id).syntax().clone();
|
2021-06-22 10:28:07 -05:00
|
|
|
let token = pick_best_token(file.token_at_offset(position.offset), |kind| match kind {
|
2021-07-20 10:03:39 -05:00
|
|
|
IDENT | INT_NUMBER | T![self] => 3,
|
2021-06-22 10:28:07 -05:00
|
|
|
T!['('] | T![')'] => 2,
|
|
|
|
kind if kind.is_trivia() => 0,
|
|
|
|
_ => 1,
|
|
|
|
})?;
|
2021-10-12 10:52:31 -05:00
|
|
|
let token = sema.descend_into_macros_single(token);
|
2021-03-29 13:11:28 -05:00
|
|
|
|
|
|
|
let node = token.parent()?;
|
|
|
|
let definition = match_ast! {
|
|
|
|
match node {
|
2021-07-20 10:03:39 -05:00
|
|
|
ast::NameRef(name_ref) => match NameRefClass::classify(sema, &name_ref)? {
|
2021-07-11 08:39:09 -05:00
|
|
|
NameRefClass::Definition(def) => def,
|
|
|
|
NameRefClass::FieldShorthand { local_ref: _, field_ref } => {
|
|
|
|
Definition::Field(field_ref)
|
|
|
|
}
|
|
|
|
},
|
2021-07-20 10:03:39 -05:00
|
|
|
ast::Name(name) => match NameClass::classify(sema, &name)? {
|
2021-07-11 08:39:09 -05:00
|
|
|
NameClass::Definition(it) | NameClass::ConstReference(it) => it,
|
|
|
|
NameClass::PatFieldShorthand { local_def: _, field_ref } => Definition::Field(field_ref),
|
|
|
|
},
|
2021-04-19 08:50:04 -05:00
|
|
|
_ => return None,
|
2021-03-29 13:11:28 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-19 08:50:04 -05:00
|
|
|
get_doc_link(db, definition)
|
2021-03-29 13:11:28 -05:00
|
|
|
}
|
|
|
|
|
2021-07-20 10:03:39 -05:00
|
|
|
/// Extracts all links from a given markdown text returning the definition text range, link-text
|
|
|
|
/// and the namespace if known.
|
2021-07-20 09:01:27 -05:00
|
|
|
pub(crate) fn extract_definitions_from_docs(
|
|
|
|
docs: &hir::Documentation,
|
2021-03-30 15:26:03 -05:00
|
|
|
) -> Vec<(TextRange, String, Option<hir::Namespace>)> {
|
2021-03-30 11:27:16 -05:00
|
|
|
Parser::new_with_broken_link_callback(
|
2021-07-20 09:01:27 -05:00
|
|
|
docs.as_str(),
|
2021-11-03 09:40:41 -05:00
|
|
|
MARKDOWN_OPTIONS,
|
2021-03-30 11:27:16 -05:00
|
|
|
Some(&mut broken_link_clone_cb),
|
|
|
|
)
|
|
|
|
.into_offset_iter()
|
2021-07-20 10:03:39 -05:00
|
|
|
.filter_map(|(event, range)| match event {
|
|
|
|
Event::Start(Tag::Link(_, target, _)) => {
|
|
|
|
let (link, ns) = parse_intra_doc_link(&target);
|
2021-03-30 15:26:03 -05:00
|
|
|
Some((
|
|
|
|
TextRange::new(range.start.try_into().ok()?, range.end.try_into().ok()?),
|
|
|
|
link.to_string(),
|
|
|
|
ns,
|
|
|
|
))
|
2021-03-23 13:19:44 -05:00
|
|
|
}
|
2021-07-20 10:03:39 -05:00
|
|
|
_ => None,
|
2021-03-30 11:27:16 -05:00
|
|
|
})
|
|
|
|
.collect()
|
2021-03-23 13:19:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn resolve_doc_path_for_def(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
def: Definition,
|
|
|
|
link: &str,
|
|
|
|
ns: Option<hir::Namespace>,
|
2021-08-02 07:33:09 -05:00
|
|
|
) -> Option<Either<ModuleDef, MacroDef>> {
|
2021-03-23 13:19:44 -05:00
|
|
|
match def {
|
|
|
|
Definition::ModuleDef(def) => match def {
|
2021-06-12 22:54:16 -05:00
|
|
|
hir::ModuleDef::Module(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
hir::ModuleDef::Function(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
hir::ModuleDef::Adt(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
hir::ModuleDef::Variant(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
hir::ModuleDef::Const(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
hir::ModuleDef::Static(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
hir::ModuleDef::Trait(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
hir::ModuleDef::TypeAlias(it) => it.resolve_doc_path(db, link, ns),
|
2021-03-29 13:11:28 -05:00
|
|
|
hir::ModuleDef::BuiltinType(_) => None,
|
2021-03-23 13:19:44 -05:00
|
|
|
},
|
2021-06-12 22:54:16 -05:00
|
|
|
Definition::Macro(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
Definition::Field(it) => it.resolve_doc_path(db, link, ns),
|
2021-03-23 13:19:44 -05:00
|
|
|
Definition::SelfType(_)
|
|
|
|
| Definition::Local(_)
|
|
|
|
| Definition::GenericParam(_)
|
|
|
|
| Definition::Label(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 11:27:16 -05:00
|
|
|
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))),
|
2021-06-12 23:05:29 -05:00
|
|
|
_ => None
|
2021-03-30 11:27:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 12:17:29 -05:00
|
|
|
pub(crate) struct DocCommentToken {
|
|
|
|
doc_token: SyntaxToken,
|
|
|
|
prefix_len: TextSize,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn token_as_doc_comment(doc_token: &SyntaxToken) -> Option<DocCommentToken> {
|
|
|
|
(match_ast! {
|
|
|
|
match doc_token {
|
|
|
|
ast::Comment(comment) => TextSize::try_from(comment.prefix().len()).ok(),
|
|
|
|
ast::String(string) => doc_token.ancestors().find_map(ast::Attr::cast)
|
|
|
|
.filter(|attr| attr.simple_name().as_deref() == Some("doc")).and_then(|_| string.open_quote_text_range().map(|it| it.len())),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}).map(|prefix_len| DocCommentToken { prefix_len, doc_token: doc_token.clone() })
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocCommentToken {
|
|
|
|
pub(crate) fn get_definition_with_descend_at<T>(
|
|
|
|
self,
|
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
offset: TextSize,
|
|
|
|
// Definition, CommentOwner, range of intra doc link in original file
|
|
|
|
mut cb: impl FnMut(Definition, SyntaxNode, TextRange) -> Option<T>,
|
|
|
|
) -> Option<T> {
|
|
|
|
let DocCommentToken { prefix_len, doc_token } = self;
|
|
|
|
// offset relative to the comments contents
|
|
|
|
let original_start = doc_token.text_range().start();
|
|
|
|
let relative_comment_offset = offset - original_start - prefix_len;
|
|
|
|
|
2021-10-12 10:52:31 -05:00
|
|
|
sema.descend_into_macros(doc_token).into_iter().find_map(|t| {
|
2021-09-23 12:17:29 -05:00
|
|
|
let (node, descended_prefix_len) = match_ast! {
|
|
|
|
match t {
|
|
|
|
ast::Comment(comment) => (t.parent()?, TextSize::try_from(comment.prefix().len()).ok()?),
|
|
|
|
ast::String(string) => (t.ancestors().skip_while(|n| n.kind() != ATTR).nth(1)?, string.open_quote_text_range()?.len()),
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let token_start = t.text_range().start();
|
|
|
|
let abs_in_expansion_offset = token_start + relative_comment_offset + descended_prefix_len;
|
|
|
|
|
|
|
|
let (attributes, def) = doc_attributes(sema, &node)?;
|
|
|
|
let (docs, doc_mapping) = attributes.docs_with_rangemap(sema.db)?;
|
|
|
|
let (in_expansion_range, link, ns) =
|
|
|
|
extract_definitions_from_docs(&docs).into_iter().find_map(|(range, link, ns)| {
|
|
|
|
let mapped = doc_mapping.map(range)?;
|
|
|
|
(mapped.value.contains(abs_in_expansion_offset)).then(|| (mapped.value, link, ns))
|
|
|
|
})?;
|
|
|
|
// get the relative range to the doc/attribute in the expansion
|
|
|
|
let in_expansion_relative_range = in_expansion_range - descended_prefix_len - token_start;
|
|
|
|
// Apply relative range to the original input comment
|
|
|
|
let absolute_range = in_expansion_relative_range + original_start + prefix_len;
|
|
|
|
let def = match resolve_doc_path_for_def(sema.db, def, &link, ns)? {
|
|
|
|
Either::Left(it) => Definition::ModuleDef(it),
|
|
|
|
Either::Right(it) => Definition::Macro(it),
|
|
|
|
};
|
|
|
|
cb(def, node, absolute_range)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 14:23:45 -05:00
|
|
|
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(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2020-09-03 03:09:36 -05:00
|
|
|
// FIXME:
|
|
|
|
// BUG: For Option::Some
|
2020-08-30 03:02:29 -05:00
|
|
|
// Returns https://doc.rust-lang.org/nightly/core/prelude/v1/enum.Option.html#variant.Some
|
|
|
|
// Instead of https://doc.rust-lang.org/nightly/core/option/enum.Option.html
|
2020-10-07 21:44:52 -05:00
|
|
|
//
|
|
|
|
// This should cease to be a problem if RFC2988 (Stable Rustdoc URLs) is implemented
|
|
|
|
// https://github.com/rust-lang/rfcs/pull/2988
|
2020-09-01 03:26:10 -05:00
|
|
|
fn get_doc_link(db: &RootDatabase, definition: Definition) -> Option<String> {
|
2021-07-20 10:03:39 -05:00
|
|
|
let (target, frag) = match definition {
|
|
|
|
Definition::ModuleDef(def) => {
|
|
|
|
if let Some(assoc_item) = def.as_assoc_item(db) {
|
|
|
|
let def = match assoc_item.container(db) {
|
|
|
|
AssocItemContainer::Trait(t) => t.into(),
|
|
|
|
AssocItemContainer::Impl(i) => i.self_ty(db).as_adt()?.into(),
|
|
|
|
};
|
|
|
|
let frag = get_assoc_item_fragment(db, assoc_item)?;
|
2021-08-02 07:33:09 -05:00
|
|
|
(Either::Left(def), Some(frag))
|
2021-07-20 10:03:39 -05:00
|
|
|
} else {
|
2021-08-02 07:33:09 -05:00
|
|
|
(Either::Left(def), None)
|
2021-07-20 10:03:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Definition::Field(field) => {
|
|
|
|
let def = match field.parent_def(db) {
|
|
|
|
hir::VariantDef::Struct(it) => it.into(),
|
|
|
|
hir::VariantDef::Union(it) => it.into(),
|
|
|
|
hir::VariantDef::Variant(it) => it.into(),
|
|
|
|
};
|
2021-08-02 07:33:09 -05:00
|
|
|
(Either::Left(def), Some(format!("structfield.{}", field.name(db))))
|
2021-07-20 10:03:39 -05:00
|
|
|
}
|
2021-08-02 07:33:09 -05:00
|
|
|
Definition::Macro(makro) => (Either::Right(makro), None),
|
2021-07-20 10:41:10 -05:00
|
|
|
// FIXME impls
|
|
|
|
Definition::SelfType(_) => return None,
|
2021-07-20 10:03:39 -05:00
|
|
|
Definition::Local(_) | Definition::GenericParam(_) | Definition::Label(_) => return None,
|
2020-08-31 03:26:55 -05:00
|
|
|
};
|
|
|
|
|
2021-07-20 10:41:10 -05:00
|
|
|
let krate = crate_of_def(db, target)?;
|
|
|
|
let mut url = get_doc_base_url(db, &krate)?;
|
2021-04-19 08:50:04 -05:00
|
|
|
|
2021-07-20 10:41:10 -05:00
|
|
|
if let Some(path) = mod_path_of_def(db, target) {
|
|
|
|
url = url.join(&path).ok()?;
|
2021-04-19 08:50:04 -05:00
|
|
|
}
|
2020-08-30 03:02:29 -05:00
|
|
|
|
2021-08-02 07:33:09 -05:00
|
|
|
url = url.join(&get_symbol_filename(db, target)?).ok()?;
|
2021-07-20 10:03:39 -05:00
|
|
|
url.set_fragment(frag.as_deref());
|
|
|
|
|
|
|
|
Some(url.into())
|
2020-08-30 03:02:29 -05:00
|
|
|
}
|
|
|
|
|
2020-08-26 11:56:41 -05:00
|
|
|
fn rewrite_intra_doc_link(
|
|
|
|
db: &RootDatabase,
|
|
|
|
def: Definition,
|
|
|
|
target: &str,
|
|
|
|
title: &str,
|
|
|
|
) -> Option<(String, String)> {
|
2021-07-20 09:01:27 -05:00
|
|
|
let (link, ns) = parse_intra_doc_link(target);
|
2021-07-20 10:41:10 -05:00
|
|
|
|
2021-03-23 14:56:38 -05:00
|
|
|
let resolved = resolve_doc_path_for_def(db, def, link, ns)?;
|
2021-07-20 10:41:10 -05:00
|
|
|
let krate = crate_of_def(db, resolved)?;
|
|
|
|
let mut url = get_doc_base_url(db, &krate)?;
|
|
|
|
|
|
|
|
if let Some(path) = mod_path_of_def(db, resolved) {
|
|
|
|
url = url.join(&path).ok()?;
|
|
|
|
}
|
|
|
|
|
2021-08-02 07:33:09 -05:00
|
|
|
let (resolved, frag) =
|
|
|
|
if let Some(assoc_item) = resolved.left().and_then(|it| it.as_assoc_item(db)) {
|
|
|
|
let resolved = match assoc_item.container(db) {
|
|
|
|
AssocItemContainer::Trait(t) => t.into(),
|
|
|
|
AssocItemContainer::Impl(i) => i.self_ty(db).as_adt()?.into(),
|
|
|
|
};
|
|
|
|
let frag = get_assoc_item_fragment(db, assoc_item)?;
|
|
|
|
(Either::Left(resolved), Some(frag))
|
|
|
|
} else {
|
|
|
|
(resolved, None)
|
2021-01-19 09:43:06 -06:00
|
|
|
};
|
2021-08-02 07:33:09 -05:00
|
|
|
url = url.join(&get_symbol_filename(db, resolved)?).ok()?;
|
2021-07-20 10:41:10 -05:00
|
|
|
url.set_fragment(frag.as_deref());
|
2021-01-19 09:43:06 -06:00
|
|
|
|
2021-07-20 10:41:10 -05:00
|
|
|
Some((url.into(), strip_prefixes_suffixes(title).to_string()))
|
2020-08-26 11:56:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to resolve path to local documentation via path-based links (i.e. `../gateway/struct.Shard.html`).
|
2021-08-02 07:33:09 -05:00
|
|
|
fn rewrite_url_link(
|
|
|
|
db: &RootDatabase,
|
|
|
|
def: Either<ModuleDef, MacroDef>,
|
|
|
|
target: &str,
|
|
|
|
) -> Option<String> {
|
2020-09-30 14:22:49 -05:00
|
|
|
if !(target.contains('#') || target.contains(".html")) {
|
2020-08-26 11:56:41 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-07-20 10:41:10 -05:00
|
|
|
let krate = crate_of_def(db, def)?;
|
|
|
|
let mut url = get_doc_base_url(db, &krate)?;
|
|
|
|
|
|
|
|
if let Some(path) = mod_path_of_def(db, def) {
|
|
|
|
url = url.join(&path).ok()?;
|
2021-07-20 10:03:39 -05:00
|
|
|
}
|
2020-08-26 11:56:41 -05:00
|
|
|
|
2021-08-02 07:33:09 -05:00
|
|
|
url = url.join(&get_symbol_filename(db, def)?).ok()?;
|
2021-07-20 10:41:10 -05:00
|
|
|
url.join(target).ok().map(Into::into)
|
|
|
|
}
|
|
|
|
|
2021-08-02 07:33:09 -05:00
|
|
|
fn crate_of_def(db: &RootDatabase, def: Either<ModuleDef, MacroDef>) -> Option<Crate> {
|
2021-07-20 10:41:10 -05:00
|
|
|
let krate = match def {
|
|
|
|
// Definition::module gives back the parent module, we don't want that as it fails for root modules
|
2021-08-02 07:33:09 -05:00
|
|
|
Either::Left(ModuleDef::Module(module)) => module.krate(),
|
|
|
|
Either::Left(def) => def.module(db)?.krate(),
|
|
|
|
Either::Right(def) => def.module(db)?.krate(),
|
2021-07-20 10:41:10 -05:00
|
|
|
};
|
|
|
|
Some(krate)
|
|
|
|
}
|
|
|
|
|
2021-08-02 07:33:09 -05:00
|
|
|
fn mod_path_of_def(db: &RootDatabase, def: Either<ModuleDef, MacroDef>) -> Option<String> {
|
|
|
|
match def {
|
|
|
|
Either::Left(def) => def.canonical_module_path(db).map(|it| {
|
|
|
|
let mut path = String::new();
|
|
|
|
it.flat_map(|it| it.name(db)).for_each(|name| format_to!(path, "{}/", name));
|
|
|
|
path
|
|
|
|
}),
|
|
|
|
Either::Right(def) => {
|
|
|
|
def.module(db).map(|it| it.path_to_root(db).into_iter().rev()).map(|it| {
|
|
|
|
let mut path = String::new();
|
|
|
|
it.flat_map(|it| it.name(db)).for_each(|name| format_to!(path, "{}/", name));
|
|
|
|
path
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-08-26 11:56:41 -05:00
|
|
|
}
|
|
|
|
|
2020-08-30 03:02:29 -05:00
|
|
|
/// Rewrites a markdown document, applying 'callback' to each link.
|
2020-07-31 19:55:04 -05:00
|
|
|
fn map_links<'e>(
|
|
|
|
events: impl Iterator<Item = Event<'e>>,
|
|
|
|
callback: impl Fn(&str, &str) -> (String, String),
|
|
|
|
) -> impl Iterator<Item = Event<'e>> {
|
|
|
|
let mut in_link = false;
|
|
|
|
let mut link_target: Option<CowStr> = None;
|
|
|
|
|
|
|
|
events.map(move |evt| match evt {
|
2021-07-20 09:01:27 -05:00
|
|
|
Event::Start(Tag::Link(_, ref target, _)) => {
|
2020-07-31 19:55:04 -05:00
|
|
|
in_link = true;
|
|
|
|
link_target = Some(target.clone());
|
|
|
|
evt
|
|
|
|
}
|
2021-07-20 09:01:27 -05:00
|
|
|
Event::End(Tag::Link(link_type, target, _)) => {
|
2020-07-31 19:55:04 -05:00
|
|
|
in_link = false;
|
2021-07-20 09:01:27 -05:00
|
|
|
Event::End(Tag::Link(
|
|
|
|
link_type,
|
|
|
|
link_target.take().unwrap_or(target),
|
|
|
|
CowStr::Borrowed(""),
|
|
|
|
))
|
2020-07-31 19:55:04 -05:00
|
|
|
}
|
|
|
|
Event::Text(s) if in_link => {
|
|
|
|
let (link_target_s, link_name) = callback(&link_target.take().unwrap(), &s);
|
|
|
|
link_target = Some(CowStr::Boxed(link_target_s.into()));
|
|
|
|
Event::Text(CowStr::Boxed(link_name.into()))
|
|
|
|
}
|
|
|
|
Event::Code(s) if in_link => {
|
|
|
|
let (link_target_s, link_name) = callback(&link_target.take().unwrap(), &s);
|
|
|
|
link_target = Some(CowStr::Boxed(link_target_s.into()));
|
|
|
|
Event::Code(CowStr::Boxed(link_name.into()))
|
|
|
|
}
|
|
|
|
_ => evt,
|
|
|
|
})
|
|
|
|
}
|
2020-08-26 11:56:41 -05:00
|
|
|
|
2020-08-31 03:26:55 -05:00
|
|
|
/// Get the root URL for the documentation of a crate.
|
|
|
|
///
|
2021-07-20 10:03:39 -05:00
|
|
|
/// ```ignore
|
2020-08-31 03:26:55 -05:00
|
|
|
/// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
|
|
|
|
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
/// ```
|
2021-07-20 09:01:27 -05:00
|
|
|
fn get_doc_base_url(db: &RootDatabase, krate: &Crate) -> Option<Url> {
|
2021-07-20 10:41:10 -05:00
|
|
|
let display_name = krate.display_name(db)?;
|
2021-10-20 09:21:33 -05:00
|
|
|
let base = match &**display_name.crate_name() {
|
|
|
|
// std and co do not specify `html_root_url` any longer so we gotta handwrite this ourself.
|
|
|
|
// FIXME: Use the toolchains channel instead of nightly
|
|
|
|
name @ ("core" | "std" | "alloc" | "proc_macro" | "test") => {
|
|
|
|
format!("https://doc.rust-lang.org/nightly/{}", name)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
krate.get_html_root_url(db).or_else(|| {
|
2021-10-30 09:17:04 -05:00
|
|
|
let version = krate.version(db);
|
2021-10-20 09:21:33 -05:00
|
|
|
// Fallback to docs.rs. This uses `display_name` and can never be
|
|
|
|
// correct, but that's what fallbacks are about.
|
|
|
|
//
|
|
|
|
// FIXME: clicking on the link should just open the file in the editor,
|
|
|
|
// instead of falling back to external urls.
|
2021-10-30 09:17:04 -05:00
|
|
|
Some(format!(
|
|
|
|
"https://docs.rs/{krate}/{version}/",
|
|
|
|
krate = display_name,
|
|
|
|
version = version.as_deref().unwrap_or("*")
|
|
|
|
))
|
2021-10-20 09:21:33 -05:00
|
|
|
})?
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Url::parse(&base).ok()?.join(&format!("{}/", display_name)).ok()
|
2020-08-26 11:56:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the filename and extension generated for a symbol by rustdoc.
|
|
|
|
///
|
2021-07-20 10:03:39 -05:00
|
|
|
/// ```ignore
|
2020-08-31 03:26:55 -05:00
|
|
|
/// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
|
|
|
|
/// ^^^^^^^^^^^^^^^^^^^
|
|
|
|
/// ```
|
2021-08-02 07:33:09 -05:00
|
|
|
fn get_symbol_filename(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
definition: Either<ModuleDef, MacroDef>,
|
|
|
|
) -> Option<String> {
|
|
|
|
let res = match definition {
|
|
|
|
Either::Left(definition) => match definition {
|
|
|
|
ModuleDef::Adt(adt) => match adt {
|
|
|
|
Adt::Struct(s) => format!("struct.{}.html", s.name(db)),
|
|
|
|
Adt::Enum(e) => format!("enum.{}.html", e.name(db)),
|
|
|
|
Adt::Union(u) => format!("union.{}.html", u.name(db)),
|
|
|
|
},
|
|
|
|
ModuleDef::Module(m) => match m.name(db) {
|
|
|
|
Some(name) => format!("{}/index.html", name),
|
|
|
|
None => String::from("index.html"),
|
|
|
|
},
|
|
|
|
ModuleDef::Trait(t) => format!("trait.{}.html", t.name(db)),
|
|
|
|
ModuleDef::TypeAlias(t) => format!("type.{}.html", t.name(db)),
|
|
|
|
ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t.name()),
|
|
|
|
ModuleDef::Function(f) => format!("fn.{}.html", f.name(db)),
|
|
|
|
ModuleDef::Variant(ev) => {
|
|
|
|
format!("enum.{}.html#variant.{}", ev.parent_enum(db).name(db), ev.name(db))
|
|
|
|
}
|
|
|
|
ModuleDef::Const(c) => format!("const.{}.html", c.name(db)?),
|
|
|
|
ModuleDef::Static(s) => format!("static.{}.html", s.name(db)?),
|
2021-07-20 10:03:39 -05:00
|
|
|
},
|
2021-08-02 07:33:09 -05:00
|
|
|
Either::Right(mac) => format!("macro.{}.html", mac.name(db)?),
|
|
|
|
};
|
|
|
|
Some(res)
|
2020-08-26 11:56:41 -05:00
|
|
|
}
|
2020-08-30 03:02:29 -05:00
|
|
|
|
2020-08-31 03:26:55 -05:00
|
|
|
/// Get the fragment required to link to a specific field, method, associated type, or associated constant.
|
|
|
|
///
|
2021-07-20 10:03:39 -05:00
|
|
|
/// ```ignore
|
2020-08-31 03:26:55 -05:00
|
|
|
/// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
|
|
|
|
/// ^^^^^^^^^^^^^^
|
|
|
|
/// ```
|
2021-07-20 10:03:39 -05:00
|
|
|
fn get_assoc_item_fragment(db: &dyn HirDatabase, assoc_item: hir::AssocItem) -> Option<String> {
|
|
|
|
Some(match assoc_item {
|
|
|
|
AssocItem::Function(function) => {
|
|
|
|
let is_trait_method =
|
|
|
|
function.as_assoc_item(db).and_then(|assoc| assoc.containing_trait(db)).is_some();
|
|
|
|
// This distinction may get more complicated when specialization is available.
|
|
|
|
// Rustdoc makes this decision based on whether a method 'has defaultness'.
|
|
|
|
// Currently this is only the case for provided trait methods.
|
|
|
|
if is_trait_method && !function.has_body(db) {
|
|
|
|
format!("tymethod.{}", function.name(db))
|
|
|
|
} else {
|
|
|
|
format!("method.{}", function.name(db))
|
2020-09-03 02:55:24 -05:00
|
|
|
}
|
2021-07-20 10:03:39 -05:00
|
|
|
}
|
|
|
|
AssocItem::Const(constant) => format!("associatedconstant.{}", constant.name(db)?),
|
|
|
|
AssocItem::TypeAlias(ty) => format!("associatedtype.{}", ty.name(db)),
|
2020-08-31 03:26:55 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-03 04:27:46 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use expect_test::{expect, Expect};
|
2021-07-19 12:57:55 -05:00
|
|
|
use ide_db::base_db::FileRange;
|
2021-07-20 10:03:39 -05:00
|
|
|
use itertools::Itertools;
|
2020-09-03 04:27:46 -05:00
|
|
|
|
2021-07-19 12:57:55 -05:00
|
|
|
use crate::{display::TryToNav, fixture};
|
2020-09-03 04:27:46 -05:00
|
|
|
|
2021-07-19 12:57:55 -05:00
|
|
|
use super::*;
|
|
|
|
|
2021-04-19 08:50:04 -05:00
|
|
|
#[test]
|
2021-07-20 11:05:25 -05:00
|
|
|
fn external_docs_doc_url_crate() {
|
2021-07-19 12:57:55 -05:00
|
|
|
check_external_docs(
|
2021-04-19 08:50:04 -05:00
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:main deps:foo
|
|
|
|
use foo$0::Foo;
|
|
|
|
//- /lib.rs crate:foo
|
2021-04-19 08:50:04 -05:00
|
|
|
pub struct Foo;
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r#"https://docs.rs/foo/*/foo/index.html"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn external_docs_doc_url_std_crate() {
|
|
|
|
check_external_docs(
|
|
|
|
r#"
|
|
|
|
//- /main.rs crate:std
|
|
|
|
use self$0;
|
|
|
|
"#,
|
|
|
|
expect![[r#"https://doc.rust-lang.org/nightly/std/index.html"#]],
|
2021-04-19 08:50:04 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-03 04:27:46 -05:00
|
|
|
#[test]
|
2021-07-20 11:05:25 -05:00
|
|
|
fn external_docs_doc_url_struct() {
|
2021-07-19 12:57:55 -05:00
|
|
|
check_external_docs(
|
2020-09-03 04:27:46 -05:00
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-01-06 14:15:48 -06:00
|
|
|
pub struct Fo$0o;
|
2020-09-03 04:27:46 -05:00
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r#"https://docs.rs/foo/*/foo/struct.Foo.html"#]],
|
2020-09-03 04:27:46 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-20 11:05:25 -05:00
|
|
|
fn external_docs_doc_url_struct_field() {
|
|
|
|
check_external_docs(
|
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-07-20 11:05:25 -05:00
|
|
|
pub struct Foo {
|
|
|
|
field$0: ()
|
|
|
|
}
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#structfield.field"##]],
|
2021-07-20 11:05:25 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn external_docs_doc_url_fn() {
|
2021-07-19 12:57:55 -05:00
|
|
|
check_external_docs(
|
2020-09-03 04:27:46 -05:00
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-01-06 14:15:48 -06:00
|
|
|
pub fn fo$0o() {}
|
2020-09-03 04:27:46 -05:00
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r#"https://docs.rs/foo/*/foo/fn.foo.html"#]],
|
2020-09-03 04:27:46 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-20 11:05:25 -05:00
|
|
|
fn external_docs_doc_url_impl_assoc() {
|
2021-07-19 12:57:55 -05:00
|
|
|
check_external_docs(
|
2020-09-03 04:27:46 -05:00
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2020-09-03 04:27:46 -05:00
|
|
|
pub struct Foo;
|
|
|
|
impl Foo {
|
2021-07-20 11:05:25 -05:00
|
|
|
pub fn method$0() {}
|
2020-09-03 04:27:46 -05:00
|
|
|
}
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#method.method"##]],
|
2020-09-03 04:27:46 -05:00
|
|
|
);
|
2021-07-20 11:05:25 -05:00
|
|
|
check_external_docs(
|
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-07-20 11:05:25 -05:00
|
|
|
pub struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
const CONST$0: () = ();
|
|
|
|
}
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#associatedconstant.CONST"##]],
|
2021-07-20 11:05:25 -05:00
|
|
|
);
|
2020-09-03 04:27:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-20 11:05:25 -05:00
|
|
|
fn external_docs_doc_url_impl_trait_assoc() {
|
2021-07-19 12:57:55 -05:00
|
|
|
check_external_docs(
|
2020-09-03 04:27:46 -05:00
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-07-20 09:01:27 -05:00
|
|
|
pub struct Foo;
|
|
|
|
pub trait Trait {
|
2021-07-20 11:05:25 -05:00
|
|
|
fn method() {}
|
2021-07-20 09:01:27 -05:00
|
|
|
}
|
|
|
|
impl Trait for Foo {
|
2021-07-20 11:05:25 -05:00
|
|
|
pub fn method$0() {}
|
2020-09-03 04:27:46 -05:00
|
|
|
}
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#method.method"##]],
|
2020-09-03 04:27:46 -05:00
|
|
|
);
|
2021-07-20 11:05:25 -05:00
|
|
|
check_external_docs(
|
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-07-20 11:05:25 -05:00
|
|
|
pub struct Foo;
|
|
|
|
pub trait Trait {
|
|
|
|
const CONST: () = ();
|
|
|
|
}
|
|
|
|
impl Trait for Foo {
|
|
|
|
const CONST$0: () = ();
|
|
|
|
}
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#associatedconstant.CONST"##]],
|
2021-07-20 11:05:25 -05:00
|
|
|
);
|
|
|
|
check_external_docs(
|
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-07-20 11:05:25 -05:00
|
|
|
pub struct Foo;
|
|
|
|
pub trait Trait {
|
|
|
|
type Type;
|
|
|
|
}
|
|
|
|
impl Trait for Foo {
|
|
|
|
type Type$0 = ();
|
|
|
|
}
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#associatedtype.Type"##]],
|
2021-07-20 11:05:25 -05:00
|
|
|
);
|
2020-09-03 04:27:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-20 11:05:25 -05:00
|
|
|
fn external_docs_doc_url_trait_assoc() {
|
2021-07-19 12:57:55 -05:00
|
|
|
check_external_docs(
|
2020-09-03 04:27:46 -05:00
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2020-09-03 04:27:46 -05:00
|
|
|
pub trait Foo {
|
2021-07-20 11:05:25 -05:00
|
|
|
fn method$0();
|
2020-09-03 04:27:46 -05:00
|
|
|
}
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r##"https://docs.rs/foo/*/foo/trait.Foo.html#tymethod.method"##]],
|
2020-09-03 04:27:46 -05:00
|
|
|
);
|
2021-07-20 11:05:25 -05:00
|
|
|
check_external_docs(
|
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-07-20 11:05:25 -05:00
|
|
|
pub trait Foo {
|
|
|
|
const CONST$0: ();
|
|
|
|
}
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r##"https://docs.rs/foo/*/foo/trait.Foo.html#associatedconstant.CONST"##]],
|
2021-07-20 11:05:25 -05:00
|
|
|
);
|
|
|
|
check_external_docs(
|
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-07-20 11:05:25 -05:00
|
|
|
pub trait Foo {
|
|
|
|
type Type$0;
|
|
|
|
}
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r##"https://docs.rs/foo/*/foo/trait.Foo.html#associatedtype.Type"##]],
|
2021-07-20 11:05:25 -05:00
|
|
|
);
|
2020-09-03 04:27:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-20 11:05:25 -05:00
|
|
|
fn external_docs_trait() {
|
2021-07-19 12:57:55 -05:00
|
|
|
check_external_docs(
|
2020-09-03 04:27:46 -05:00
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-07-20 11:05:25 -05:00
|
|
|
trait Trait$0 {}
|
2020-09-03 04:27:46 -05:00
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r#"https://docs.rs/foo/*/foo/trait.Trait.html"#]],
|
2021-07-20 11:05:25 -05:00
|
|
|
)
|
2020-09-03 04:27:46 -05:00
|
|
|
}
|
2020-09-05 00:21:52 -05:00
|
|
|
|
2020-10-21 23:31:25 -05:00
|
|
|
#[test]
|
2021-07-20 11:05:25 -05:00
|
|
|
fn external_docs_module() {
|
2021-07-19 12:57:55 -05:00
|
|
|
check_external_docs(
|
2020-10-21 23:31:25 -05:00
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2020-10-21 23:31:25 -05:00
|
|
|
pub mod foo {
|
2021-01-06 14:15:48 -06:00
|
|
|
pub mod ba$0r {}
|
2020-10-21 23:31:25 -05:00
|
|
|
}
|
2021-07-20 11:05:25 -05:00
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r#"https://docs.rs/foo/*/foo/foo/bar/index.html"#]],
|
2020-10-21 23:31:25 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-05 00:21:52 -05:00
|
|
|
#[test]
|
2021-07-20 11:05:25 -05:00
|
|
|
fn external_docs_reexport_order() {
|
2021-07-19 12:57:55 -05:00
|
|
|
check_external_docs(
|
2020-09-05 00:21:52 -05:00
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2020-09-05 00:21:52 -05:00
|
|
|
pub mod wrapper {
|
|
|
|
pub use module::Item;
|
|
|
|
|
|
|
|
pub mod module {
|
|
|
|
pub struct Item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
let bar: wrapper::It$0em;
|
2020-09-05 00:21:52 -05:00
|
|
|
}
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r#"https://docs.rs/foo/*/foo/wrapper/module/struct.Item.html"#]],
|
2020-09-05 00:21:52 -05:00
|
|
|
)
|
|
|
|
}
|
2021-07-19 12:57:55 -05:00
|
|
|
|
2021-07-20 09:01:27 -05:00
|
|
|
#[test]
|
|
|
|
fn test_trait_items() {
|
|
|
|
check_doc_links(
|
|
|
|
r#"
|
|
|
|
/// [`Trait`]
|
|
|
|
/// [`Trait::Type`]
|
|
|
|
/// [`Trait::CONST`]
|
|
|
|
/// [`Trait::func`]
|
|
|
|
trait Trait$0 {
|
|
|
|
// ^^^^^ Trait
|
|
|
|
type Type;
|
|
|
|
// ^^^^ Trait::Type
|
|
|
|
const CONST: usize;
|
|
|
|
// ^^^^^ Trait::CONST
|
|
|
|
fn func();
|
|
|
|
// ^^^^ Trait::func
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-07-20 10:57:47 -05:00
|
|
|
#[test]
|
2021-07-20 11:05:25 -05:00
|
|
|
fn rewrite_html_root_url() {
|
2021-07-20 10:57:47 -05:00
|
|
|
check_rewrite(
|
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-07-20 10:57:47 -05:00
|
|
|
#![doc(arbitrary_attribute = "test", html_root_url = "https:/example.com", arbitrary_attribute2)]
|
|
|
|
|
|
|
|
pub mod foo {
|
|
|
|
pub struct Foo;
|
|
|
|
}
|
|
|
|
/// [Foo](foo::Foo)
|
|
|
|
pub struct B$0ar
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r#"[Foo](https://example.com/foo/foo/struct.Foo.html)"#]],
|
2021-07-20 10:57:47 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-20 11:05:25 -05:00
|
|
|
fn rewrite_on_field() {
|
2021-07-20 10:57:47 -05:00
|
|
|
// FIXME: Should be
|
|
|
|
// [Foo](https://docs.rs/test/*/test/struct.Foo.html)
|
|
|
|
check_rewrite(
|
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-07-20 10:57:47 -05:00
|
|
|
pub struct Foo {
|
|
|
|
/// [Foo](struct.Foo.html)
|
|
|
|
fie$0ld: ()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
expect![[r#"[Foo](struct.Foo.html)"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-20 11:05:25 -05:00
|
|
|
fn rewrite_struct() {
|
2021-07-20 10:57:47 -05:00
|
|
|
check_rewrite(
|
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-07-20 10:57:47 -05:00
|
|
|
/// [Foo]
|
|
|
|
pub struct $0Foo;
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r#"[Foo](https://docs.rs/foo/*/foo/struct.Foo.html)"#]],
|
2021-07-20 10:57:47 -05:00
|
|
|
);
|
|
|
|
check_rewrite(
|
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-07-20 10:57:47 -05:00
|
|
|
/// [`Foo`]
|
|
|
|
pub struct $0Foo;
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r#"[`Foo`](https://docs.rs/foo/*/foo/struct.Foo.html)"#]],
|
2021-07-20 10:57:47 -05:00
|
|
|
);
|
|
|
|
check_rewrite(
|
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-07-20 10:57:47 -05:00
|
|
|
/// [Foo](struct.Foo.html)
|
|
|
|
pub struct $0Foo;
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r#"[Foo](https://docs.rs/foo/*/foo/struct.Foo.html)"#]],
|
2021-07-20 10:57:47 -05:00
|
|
|
);
|
|
|
|
check_rewrite(
|
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-07-20 10:57:47 -05:00
|
|
|
/// [struct Foo](struct.Foo.html)
|
|
|
|
pub struct $0Foo;
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r#"[struct Foo](https://docs.rs/foo/*/foo/struct.Foo.html)"#]],
|
2021-07-20 10:57:47 -05:00
|
|
|
);
|
|
|
|
check_rewrite(
|
|
|
|
r#"
|
2021-10-20 09:21:33 -05:00
|
|
|
//- /main.rs crate:foo
|
2021-07-20 10:57:47 -05:00
|
|
|
/// [my Foo][foo]
|
|
|
|
///
|
|
|
|
/// [foo]: Foo
|
|
|
|
pub struct $0Foo;
|
|
|
|
"#,
|
2021-10-20 09:21:33 -05:00
|
|
|
expect![[r#"[my Foo](https://docs.rs/foo/*/foo/struct.Foo.html)"#]],
|
2021-07-20 10:57:47 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-20 09:01:27 -05:00
|
|
|
fn check_external_docs(ra_fixture: &str, expect: Expect) {
|
|
|
|
let (analysis, position) = fixture::position(ra_fixture);
|
|
|
|
let url = analysis.external_docs(position).unwrap().expect("could not find url for symbol");
|
|
|
|
|
|
|
|
expect.assert_eq(&url)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_rewrite(ra_fixture: &str, expect: Expect) {
|
|
|
|
let (analysis, position) = fixture::position(ra_fixture);
|
|
|
|
let sema = &Semantics::new(&*analysis.db);
|
|
|
|
let (cursor_def, docs) = def_under_cursor(sema, &position);
|
|
|
|
let res = rewrite_links(sema.db, docs.as_str(), cursor_def);
|
|
|
|
expect.assert_eq(&res)
|
|
|
|
}
|
|
|
|
|
2021-07-19 12:57:55 -05:00
|
|
|
fn check_doc_links(ra_fixture: &str) {
|
|
|
|
let key_fn = |&(FileRange { file_id, range }, _): &_| (file_id, range.start());
|
|
|
|
|
|
|
|
let (analysis, position, mut expected) = fixture::annotations(ra_fixture);
|
|
|
|
expected.sort_by_key(key_fn);
|
|
|
|
let sema = &Semantics::new(&*analysis.db);
|
2021-07-20 09:01:27 -05:00
|
|
|
let (cursor_def, docs) = def_under_cursor(sema, &position);
|
|
|
|
let defs = extract_definitions_from_docs(&docs);
|
2021-07-19 12:57:55 -05:00
|
|
|
let actual: Vec<_> = defs
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_, link, ns)| {
|
|
|
|
let def = resolve_doc_path_for_def(sema.db, cursor_def, &link, ns)
|
|
|
|
.unwrap_or_else(|| panic!("Failed to resolve {}", link));
|
|
|
|
let nav_target = def.try_to_nav(sema.db).unwrap();
|
|
|
|
let range = FileRange {
|
|
|
|
file_id: nav_target.file_id,
|
|
|
|
range: nav_target.focus_or_full_range(),
|
|
|
|
};
|
|
|
|
(range, link)
|
|
|
|
})
|
|
|
|
.sorted_by_key(key_fn)
|
|
|
|
.collect();
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
}
|
|
|
|
|
2021-07-20 09:01:27 -05:00
|
|
|
fn def_under_cursor(
|
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
position: &FilePosition,
|
|
|
|
) -> (Definition, hir::Documentation) {
|
|
|
|
let (docs, def) = sema
|
|
|
|
.parse(position.file_id)
|
|
|
|
.syntax()
|
|
|
|
.token_at_offset(position.offset)
|
2021-07-20 12:00:52 -05:00
|
|
|
.left_biased()
|
2021-07-20 09:01:27 -05:00
|
|
|
.unwrap()
|
|
|
|
.ancestors()
|
|
|
|
.find_map(|it| node_to_def(sema, &it))
|
|
|
|
.expect("no def found")
|
|
|
|
.unwrap();
|
|
|
|
let docs = docs.expect("no docs found for cursor def");
|
|
|
|
(def, docs)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn node_to_def(
|
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
node: &SyntaxNode,
|
|
|
|
) -> Option<Option<(Option<hir::Documentation>, Definition)>> {
|
|
|
|
Some(match_ast! {
|
|
|
|
match node {
|
|
|
|
ast::SourceFile(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Module(def)))),
|
|
|
|
ast::Module(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Module(def)))),
|
|
|
|
ast::Fn(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Function(def)))),
|
|
|
|
ast::Struct(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Struct(def))))),
|
|
|
|
ast::Union(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Union(def))))),
|
|
|
|
ast::Enum(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(def))))),
|
|
|
|
ast::Variant(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Variant(def)))),
|
|
|
|
ast::Trait(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Trait(def)))),
|
|
|
|
ast::Static(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Static(def)))),
|
|
|
|
ast::Const(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::Const(def)))),
|
|
|
|
ast::TypeAlias(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::ModuleDef(hir::ModuleDef::TypeAlias(def)))),
|
|
|
|
ast::Impl(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::SelfType(def))),
|
|
|
|
ast::RecordField(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::Field(def))),
|
|
|
|
ast::TupleField(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::Field(def))),
|
|
|
|
ast::Macro(it) => sema.to_def(&it).map(|def| (def.docs(sema.db), Definition::Macro(def))),
|
|
|
|
// ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
})
|
2021-07-19 12:57:55 -05:00
|
|
|
}
|
2020-09-03 04:27:46 -05:00
|
|
|
}
|