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-12-14 05:01:15 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2021-07-19 12:57:55 -05:00
|
|
|
mod intra_doc_links;
|
|
|
|
|
2023-04-25 12:49:49 -05:00
|
|
|
use std::ffi::OsStr;
|
|
|
|
|
2020-10-13 12:31:23 -05:00
|
|
|
use pulldown_cmark::{BrokenLink, CowStr, Event, InlineStr, LinkType, Options, Parser, Tag};
|
2022-02-06 02:04:06 -06:00
|
|
|
use pulldown_cmark_to_cmark::{cmark_resume_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
|
|
|
|
2023-12-03 13:20:38 -06:00
|
|
|
use hir::{
|
|
|
|
db::HirDatabase, Adt, AsAssocItem, AssocItem, AssocItemContainer, DescendPreference, HasAttrs,
|
|
|
|
};
|
2020-08-30 03:02:29 -05:00
|
|
|
use ide_db::{
|
2023-03-31 02:10:18 -05:00
|
|
|
base_db::{CrateOrigin, LangCrateOrigin, ReleaseChannel, SourceDatabase},
|
2020-10-15 10:27:50 -05:00
|
|
|
defs::{Definition, NameClass, NameRefClass},
|
2023-09-02 10:12:57 -05:00
|
|
|
documentation::{docs_with_rangemap, Documentation, HasDocs},
|
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
|
|
|
|
2023-04-25 08:14:35 -05:00
|
|
|
/// Web and local links to an item's documentation.
|
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct DocumentationLinks {
|
|
|
|
/// The URL to the documentation on docs.rs.
|
2023-04-25 13:16:28 -05:00
|
|
|
/// May not lead anywhere.
|
2023-04-25 08:14:35 -05:00
|
|
|
pub web_url: Option<String>,
|
|
|
|
/// The URL to the documentation in the local file system.
|
2023-04-25 13:16:28 -05:00
|
|
|
/// May not lead anywhere.
|
2023-04-25 08:14:35 -05:00
|
|
|
pub local_url: Option<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("://") {
|
2022-05-04 10:41:29 -05:00
|
|
|
(Some(LinkType::Inline), target.to_string(), title.to_string())
|
2020-07-31 19:55:04 -05:00
|
|
|
} 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`
|
2022-05-04 10:41:29 -05:00
|
|
|
if let Some((target, title)) = rewrite_intra_doc_link(db, definition, target, title) {
|
|
|
|
return (None, target, title);
|
2020-08-26 11:56:41 -05:00
|
|
|
}
|
2021-11-10 15:02:50 -06:00
|
|
|
if let Some(target) = rewrite_url_link(db, definition, target) {
|
2022-05-04 10:41:29 -05:00
|
|
|
return (Some(LinkType::Inline), target, title.to_string());
|
2020-07-31 19:55:04 -05:00
|
|
|
}
|
2020-08-26 11:56:41 -05:00
|
|
|
|
2022-05-04 10:41:29 -05:00
|
|
|
(None, target.to_string(), title.to_string())
|
2020-07-31 19:55:04 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
let mut out = String::new();
|
2022-02-06 02:04:06 -06:00
|
|
|
cmark_resume_with_options(
|
2021-07-20 10:03:39 -05:00
|
|
|
doc,
|
|
|
|
&mut out,
|
|
|
|
None,
|
2021-12-04 07:21:04 -06:00
|
|
|
CMarkOptions { code_block_token_count: 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;
|
|
|
|
|
2022-07-20 08:02:08 -05:00
|
|
|
let mut cb = |_: BrokenLink<'_>| {
|
2020-10-13 12:31:23 -05:00
|
|
|
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();
|
2022-02-06 02:04:06 -06:00
|
|
|
cmark_resume_with_options(
|
2021-07-20 10:03:39 -05:00
|
|
|
doc,
|
|
|
|
&mut out,
|
|
|
|
None,
|
2021-12-04 07:21:04 -06:00
|
|
|
CMarkOptions { code_block_token_count: 3, ..Default::default() },
|
2021-07-20 10:03:39 -05:00
|
|
|
)
|
|
|
|
.ok();
|
2020-09-26 00:02:09 -05:00
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2023-02-20 10:38:33 -06:00
|
|
|
// Feature: Open Docs
|
|
|
|
//
|
2023-04-25 13:16:28 -05:00
|
|
|
// Retrieve a links to documentation for the given symbol.
|
2023-02-20 10:38:33 -06:00
|
|
|
//
|
|
|
|
// The simplest way to use this feature is via the context menu. Right-click on
|
|
|
|
// the selected item. The context menu opens. Select **Open Docs**.
|
|
|
|
//
|
|
|
|
// |===
|
|
|
|
// | Editor | Action Name
|
|
|
|
//
|
|
|
|
// | VS Code | **rust-analyzer: Open Docs**
|
|
|
|
// |===
|
2023-04-25 12:49:49 -05:00
|
|
|
pub(crate) fn external_docs(
|
|
|
|
db: &RootDatabase,
|
2023-08-16 03:07:18 -05:00
|
|
|
FilePosition { file_id, offset }: FilePosition,
|
2023-04-25 12:49:49 -05:00
|
|
|
target_dir: Option<&OsStr>,
|
2023-04-27 11:05:04 -05:00
|
|
|
sysroot: Option<&OsStr>,
|
2023-04-27 06:54:00 -05:00
|
|
|
) -> Option<DocumentationLinks> {
|
2021-07-20 10:03:39 -05:00
|
|
|
let sema = &Semantics::new(db);
|
2023-08-16 03:07:18 -05:00
|
|
|
let file = sema.parse(file_id).syntax().clone();
|
|
|
|
let token = pick_best_token(file.token_at_offset(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,
|
2023-04-27 06:54:00 -05:00
|
|
|
})?;
|
2023-12-05 08:42:39 -06:00
|
|
|
let token = sema.descend_into_macros_single(DescendPreference::None, token);
|
2021-03-29 13:11:28 -05:00
|
|
|
|
2023-04-27 06:54:00 -05:00
|
|
|
let node = token.parent()?;
|
2021-03-29 13:11:28 -05:00
|
|
|
let definition = match_ast! {
|
|
|
|
match node {
|
2023-05-01 03:16:36 -05:00
|
|
|
ast::NameRef(name_ref) => match NameRefClass::classify(sema, &name_ref)? {
|
|
|
|
NameRefClass::Definition(def) => def,
|
|
|
|
NameRefClass::FieldShorthand { local_ref: _, field_ref } => {
|
2021-07-11 08:39:09 -05:00
|
|
|
Definition::Field(field_ref)
|
|
|
|
}
|
2023-08-02 04:52:55 -05:00
|
|
|
NameRefClass::ExternCrateShorthand { decl, .. } => {
|
|
|
|
Definition::ExternCrateDecl(decl)
|
|
|
|
}
|
2021-07-11 08:39:09 -05:00
|
|
|
},
|
2023-05-01 03:16:36 -05:00
|
|
|
ast::Name(name) => match NameClass::classify(sema, &name)? {
|
|
|
|
NameClass::Definition(it) | NameClass::ConstReference(it) => it,
|
|
|
|
NameClass::PatFieldShorthand { local_def: _, field_ref } => Definition::Field(field_ref),
|
2021-07-11 08:39:09 -05:00
|
|
|
},
|
2023-04-27 06:54:00 -05:00
|
|
|
_ => return None
|
2021-03-29 13:11:28 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-04-27 11:05:04 -05:00
|
|
|
Some(get_doc_links(db, definition, target_dir, sysroot))
|
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(
|
2023-09-02 09:26:48 -05:00
|
|
|
docs: &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-11-10 15:02:50 -06:00
|
|
|
) -> Option<Definition> {
|
2022-03-08 16:52:26 -06:00
|
|
|
match def {
|
2021-11-10 15:02:50 -06:00
|
|
|
Definition::Module(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
Definition::Function(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
Definition::Adt(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
Definition::Variant(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
Definition::Const(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
Definition::Static(it) => it.resolve_doc_path(db, link, ns),
|
|
|
|
Definition::Trait(it) => it.resolve_doc_path(db, link, ns),
|
2023-03-03 09:24:07 -06:00
|
|
|
Definition::TraitAlias(it) => it.resolve_doc_path(db, link, ns),
|
2021-11-10 15:02:50 -06:00
|
|
|
Definition::TypeAlias(it) => it.resolve_doc_path(db, link, ns),
|
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),
|
2022-08-23 10:50:45 -05:00
|
|
|
Definition::SelfType(it) => it.resolve_doc_path(db, link, ns),
|
2023-08-02 04:52:55 -05:00
|
|
|
Definition::ExternCrateDecl(it) => it.resolve_doc_path(db, link, ns),
|
2021-12-03 09:32:14 -06:00
|
|
|
Definition::BuiltinAttr(_)
|
2021-12-03 10:15:19 -06:00
|
|
|
| Definition::ToolModule(_)
|
2021-12-03 09:32:14 -06:00
|
|
|
| Definition::BuiltinType(_)
|
2024-01-06 08:04:58 -06:00
|
|
|
| Definition::TupleField(_)
|
2021-03-23 13:19:44 -05:00
|
|
|
| Definition::Local(_)
|
|
|
|
| Definition::GenericParam(_)
|
2022-07-24 07:05:37 -05:00
|
|
|
| Definition::Label(_)
|
|
|
|
| Definition::DeriveHelper(_) => None,
|
2021-03-23 13:19:44 -05:00
|
|
|
}
|
2022-03-08 16:52:26 -06:00
|
|
|
.map(Definition::from)
|
2021-03-23 13:19:44 -05:00
|
|
|
}
|
|
|
|
|
2021-03-30 11:27:16 -05:00
|
|
|
pub(crate) fn doc_attributes(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2021-03-30 11:27:16 -05:00
|
|
|
node: &SyntaxNode,
|
|
|
|
) -> Option<(hir::AttrsWithOwner, Definition)> {
|
|
|
|
match_ast! {
|
|
|
|
match node {
|
2021-11-10 15:02:50 -06:00
|
|
|
ast::SourceFile(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Module(def))),
|
|
|
|
ast::Module(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Module(def))),
|
|
|
|
ast::Fn(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Function(def))),
|
|
|
|
ast::Struct(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Adt(hir::Adt::Struct(def)))),
|
|
|
|
ast::Union(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Adt(hir::Adt::Union(def)))),
|
|
|
|
ast::Enum(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Adt(hir::Adt::Enum(def)))),
|
|
|
|
ast::Variant(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Variant(def))),
|
|
|
|
ast::Trait(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Trait(def))),
|
|
|
|
ast::Static(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Static(def))),
|
|
|
|
ast::Const(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Const(def))),
|
|
|
|
ast::TypeAlias(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::TypeAlias(def))),
|
2021-03-30 11:27:16 -05:00
|
|
|
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(),
|
2022-10-07 04:04:41 -05:00
|
|
|
ast::String(string) => {
|
|
|
|
doc_token.parent_ancestors().find_map(ast::Attr::cast).filter(|attr| attr.simple_name().as_deref() == Some("doc"))?;
|
|
|
|
if doc_token.parent_ancestors().find_map(ast::MacroCall::cast).filter(|mac| mac.path().and_then(|p| p.segment()?.name_ref()).as_ref().map(|n| n.text()).as_deref() == Some("include_str")).is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
string.open_quote_text_range().map(|it| it.len())
|
|
|
|
},
|
2021-09-23 12:17:29 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}).map(|prefix_len| DocCommentToken { prefix_len, doc_token: doc_token.clone() })
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocCommentToken {
|
|
|
|
pub(crate) fn get_definition_with_descend_at<T>(
|
|
|
|
self,
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &Semantics<'_, RootDatabase>,
|
2021-09-23 12:17:29 -05:00
|
|
|
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;
|
|
|
|
|
2023-12-05 08:42:39 -06:00
|
|
|
sema.descend_into_macros(DescendPreference::None, 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()?),
|
2022-06-10 09:30:09 -05:00
|
|
|
ast::String(string) => (t.parent_ancestors().skip_while(|n| n.kind() != ATTR).nth(1)?, string.open_quote_text_range()?.len()),
|
2021-09-23 12:17:29 -05:00
|
|
|
_ => 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)?;
|
2023-09-02 09:26:48 -05:00
|
|
|
let (docs, doc_mapping) = docs_with_rangemap(sema.db, &attributes)?;
|
2021-09-23 12:17:29 -05:00
|
|
|
let (in_expansion_range, link, ns) =
|
|
|
|
extract_definitions_from_docs(&docs).into_iter().find_map(|(range, link, ns)| {
|
|
|
|
let mapped = doc_mapping.map(range)?;
|
2022-12-30 02:30:23 -06:00
|
|
|
(mapped.value.contains(abs_in_expansion_offset)).then_some((mapped.value, link, ns))
|
2021-09-23 12:17:29 -05:00
|
|
|
})?;
|
|
|
|
// 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;
|
2021-11-10 15:02:50 -06:00
|
|
|
let def = resolve_doc_path_for_def(sema.db, def, &link, ns)?;
|
2021-09-23 12:17:29 -05:00
|
|
|
cb(def, node, absolute_range)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 03:00:42 -06:00
|
|
|
fn broken_link_clone_cb(link: BrokenLink<'_>) -> Option<(CowStr<'_>, CowStr<'_>)> {
|
2022-01-21 11:49:57 -06:00
|
|
|
Some((/*url*/ link.reference.clone(), /*title*/ link.reference))
|
2021-03-29 14:23:45 -05:00
|
|
|
}
|
|
|
|
|
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
|
2023-04-25 12:49:49 -05:00
|
|
|
fn get_doc_links(
|
|
|
|
db: &RootDatabase,
|
|
|
|
def: Definition,
|
|
|
|
target_dir: Option<&OsStr>,
|
2023-04-27 11:05:04 -05:00
|
|
|
sysroot: Option<&OsStr>,
|
2023-04-25 12:49:49 -05:00
|
|
|
) -> DocumentationLinks {
|
2023-04-27 06:54:00 -05:00
|
|
|
let join_url = |base_url: Option<Url>, path: &str| -> Option<Url> {
|
|
|
|
base_url.and_then(|url| url.join(path).ok())
|
|
|
|
};
|
|
|
|
|
2023-07-03 13:34:09 -05:00
|
|
|
let Some((target, file, frag)) = filename_and_frag_for_def(db, def) else {
|
|
|
|
return Default::default();
|
|
|
|
};
|
2020-08-31 03:26:55 -05:00
|
|
|
|
2023-04-27 11:05:04 -05:00
|
|
|
let (mut web_url, mut local_url) = get_doc_base_urls(db, target, target_dir, sysroot);
|
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) {
|
2023-04-25 08:14:35 -05:00
|
|
|
web_url = join_url(web_url, &path);
|
|
|
|
local_url = join_url(local_url, &path);
|
2021-04-19 08:50:04 -05:00
|
|
|
}
|
2020-08-30 03:02:29 -05:00
|
|
|
|
2023-04-25 08:14:35 -05:00
|
|
|
web_url = join_url(web_url, &file);
|
|
|
|
local_url = join_url(local_url, &file);
|
|
|
|
|
2023-04-27 06:54:00 -05:00
|
|
|
web_url.as_mut().map(|url| url.set_fragment(frag.as_deref()));
|
|
|
|
local_url.as_mut().map(|url| url.set_fragment(frag.as_deref()));
|
2021-07-20 10:03:39 -05:00
|
|
|
|
2023-05-01 03:16:36 -05:00
|
|
|
DocumentationLinks {
|
2023-04-25 08:14:35 -05:00
|
|
|
web_url: web_url.map(|it| it.into()),
|
|
|
|
local_url: local_url.map(|it| it.into()),
|
2023-05-01 03:16:36 -05:00
|
|
|
}
|
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)?;
|
2023-04-27 11:05:04 -05:00
|
|
|
let mut url = get_doc_base_urls(db, resolved, None, None).0?;
|
2021-07-20 10:41:10 -05:00
|
|
|
|
2021-11-10 15:02:50 -06:00
|
|
|
let (_, file, frag) = filename_and_frag_for_def(db, resolved)?;
|
2021-07-20 10:41:10 -05:00
|
|
|
if let Some(path) = mod_path_of_def(db, resolved) {
|
|
|
|
url = url.join(&path).ok()?;
|
|
|
|
}
|
|
|
|
|
2021-11-10 15:02:50 -06:00
|
|
|
url = url.join(&file).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-11-10 15:02:50 -06:00
|
|
|
fn rewrite_url_link(db: &RootDatabase, def: Definition, 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;
|
|
|
|
}
|
|
|
|
|
2023-04-27 11:05:04 -05:00
|
|
|
let mut url = get_doc_base_urls(db, def, None, None).0?;
|
2021-11-10 15:02:50 -06:00
|
|
|
let (def, file, frag) = filename_and_frag_for_def(db, def)?;
|
2021-07-20 10:41:10 -05:00
|
|
|
|
|
|
|
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-11-10 15:02:50 -06:00
|
|
|
url = url.join(&file).ok()?;
|
|
|
|
url.set_fragment(frag.as_deref());
|
2021-07-20 10:41:10 -05:00
|
|
|
url.join(target).ok().map(Into::into)
|
|
|
|
}
|
|
|
|
|
2021-11-10 15:02:50 -06:00
|
|
|
fn mod_path_of_def(db: &RootDatabase, def: Definition) -> Option<String> {
|
|
|
|
def.canonical_module_path(db).map(|it| {
|
|
|
|
let mut path = String::new();
|
2023-05-24 11:04:29 -05:00
|
|
|
it.flat_map(|it| it.name(db)).for_each(|name| format_to!(path, "{}/", name.display(db)));
|
2021-11-10 15:02:50 -06:00
|
|
|
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>>,
|
2022-05-04 10:41:29 -05:00
|
|
|
callback: impl Fn(&str, &str) -> (Option<LinkType>, String, String),
|
2020-07-31 19:55:04 -05:00
|
|
|
) -> impl Iterator<Item = Event<'e>> {
|
|
|
|
let mut in_link = false;
|
2022-05-04 10:41:29 -05:00
|
|
|
// holds the origin link target on start event and the rewritten one on end event
|
2022-07-20 08:02:08 -05:00
|
|
|
let mut end_link_target: Option<CowStr<'_>> = None;
|
2022-05-04 10:41:29 -05:00
|
|
|
// normally link's type is determined by the type of link tag in the end event,
|
2022-06-20 20:10:45 -05:00
|
|
|
// however in some cases we want to change the link type, for example,
|
2022-06-23 18:02:30 -05:00
|
|
|
// `Shortcut` type parsed from Start/End tags doesn't make sense for url links
|
2022-05-04 10:41:29 -05:00
|
|
|
let mut end_link_type: Option<LinkType> = None;
|
2020-07-31 19:55:04 -05:00
|
|
|
|
|
|
|
events.map(move |evt| match evt {
|
2022-06-20 20:10:45 -05:00
|
|
|
Event::Start(Tag::Link(link_type, ref target, _)) => {
|
2020-07-31 19:55:04 -05:00
|
|
|
in_link = true;
|
2022-05-04 10:41:29 -05:00
|
|
|
end_link_target = Some(target.clone());
|
2022-06-20 20:10:45 -05:00
|
|
|
end_link_type = Some(link_type);
|
2020-07-31 19:55:04 -05:00
|
|
|
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(
|
2022-05-04 10:41:29 -05:00
|
|
|
end_link_type.unwrap_or(link_type),
|
|
|
|
end_link_target.take().unwrap_or(target),
|
2021-07-20 09:01:27 -05:00
|
|
|
CowStr::Borrowed(""),
|
|
|
|
))
|
2020-07-31 19:55:04 -05:00
|
|
|
}
|
|
|
|
Event::Text(s) if in_link => {
|
2022-06-20 22:15:27 -05:00
|
|
|
let (link_type, link_target_s, link_name) =
|
|
|
|
callback(&end_link_target.take().unwrap(), &s);
|
2022-05-04 10:41:29 -05:00
|
|
|
end_link_target = Some(CowStr::Boxed(link_target_s.into()));
|
2022-06-22 21:07:26 -05:00
|
|
|
if !matches!(end_link_type, Some(LinkType::Autolink)) {
|
2022-06-20 22:15:27 -05:00
|
|
|
end_link_type = link_type;
|
|
|
|
}
|
2020-07-31 19:55:04 -05:00
|
|
|
Event::Text(CowStr::Boxed(link_name.into()))
|
|
|
|
}
|
|
|
|
Event::Code(s) if in_link => {
|
2022-06-20 22:15:27 -05:00
|
|
|
let (link_type, link_target_s, link_name) =
|
|
|
|
callback(&end_link_target.take().unwrap(), &s);
|
2022-05-04 10:41:29 -05:00
|
|
|
end_link_target = Some(CowStr::Boxed(link_target_s.into()));
|
2022-06-22 21:07:26 -05:00
|
|
|
if !matches!(end_link_type, Some(LinkType::Autolink)) {
|
2022-06-20 22:15:27 -05:00
|
|
|
end_link_type = link_type;
|
|
|
|
}
|
2020-07-31 19:55:04 -05:00
|
|
|
Event::Code(CowStr::Boxed(link_name.into()))
|
|
|
|
}
|
|
|
|
_ => evt,
|
|
|
|
})
|
|
|
|
}
|
2020-08-26 11:56:41 -05:00
|
|
|
|
2022-05-22 04:50:40 -05:00
|
|
|
/// Get the root URL for the documentation of a definition.
|
2020-08-31 03:26:55 -05:00
|
|
|
///
|
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
|
|
|
|
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
2023-04-25 13:16:28 -05:00
|
|
|
/// file:///project/root/target/doc/std/iter/trait.Iterator.html#tymethod.next
|
|
|
|
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
2020-08-31 03:26:55 -05:00
|
|
|
/// ```
|
2023-04-25 12:49:49 -05:00
|
|
|
fn get_doc_base_urls(
|
|
|
|
db: &RootDatabase,
|
|
|
|
def: Definition,
|
|
|
|
target_dir: Option<&OsStr>,
|
2023-04-27 11:05:04 -05:00
|
|
|
sysroot: Option<&OsStr>,
|
2023-04-25 12:49:49 -05:00
|
|
|
) -> (Option<Url>, Option<Url>) {
|
2023-04-27 11:05:04 -05:00
|
|
|
let local_doc = target_dir
|
|
|
|
.and_then(|path| path.to_str())
|
|
|
|
.and_then(|path| Url::parse(&format!("file:///{path}/")).ok())
|
2023-04-27 06:54:00 -05:00
|
|
|
.and_then(|it| it.join("doc/").ok());
|
2023-04-27 11:05:04 -05:00
|
|
|
let system_doc = sysroot
|
|
|
|
.and_then(|it| it.to_str())
|
|
|
|
.map(|sysroot| format!("file:///{sysroot}/share/doc/rust/html/"))
|
|
|
|
.and_then(|it| Url::parse(&it).ok());
|
|
|
|
|
2022-05-22 04:50:40 -05:00
|
|
|
// special case base url of `BuiltinType` to core
|
|
|
|
// https://github.com/rust-lang/rust-analyzer/issues/12250
|
|
|
|
if let Definition::BuiltinType(..) = def {
|
2023-04-27 11:05:04 -05:00
|
|
|
let web_link = Url::parse("https://doc.rust-lang.org/nightly/core/").ok();
|
|
|
|
let system_link = system_doc.and_then(|it| it.join("core/").ok());
|
|
|
|
return (web_link, system_link);
|
2022-05-22 04:50:40 -05:00
|
|
|
};
|
|
|
|
|
2023-04-26 07:50:13 -05:00
|
|
|
let Some(krate) = def.krate(db) else { return Default::default() };
|
|
|
|
let Some(display_name) = krate.display_name(db) else { return Default::default() };
|
2023-03-31 02:10:18 -05:00
|
|
|
let crate_data = &db.crate_graph()[krate.into()];
|
2023-12-19 05:53:10 -06:00
|
|
|
let channel = crate_data.channel().unwrap_or(ReleaseChannel::Nightly).as_str();
|
2023-04-27 08:03:13 -05:00
|
|
|
|
2023-04-25 08:14:35 -05:00
|
|
|
let (web_base, local_base) = match &crate_data.origin {
|
2021-10-20 09:21:33 -05:00
|
|
|
// 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
|
2022-04-10 05:42:16 -05:00
|
|
|
CrateOrigin::Lang(
|
|
|
|
origin @ (LangCrateOrigin::Alloc
|
|
|
|
| LangCrateOrigin::Core
|
|
|
|
| LangCrateOrigin::ProcMacro
|
|
|
|
| LangCrateOrigin::Std
|
|
|
|
| LangCrateOrigin::Test),
|
2023-04-27 08:03:13 -05:00
|
|
|
) => {
|
2023-04-27 11:05:04 -05:00
|
|
|
let system_url = system_doc.and_then(|it| it.join(&format!("{origin}")).ok());
|
2023-04-27 08:03:13 -05:00
|
|
|
let web_url = format!("https://doc.rust-lang.org/{channel}/{origin}");
|
2023-04-27 11:05:04 -05:00
|
|
|
(Some(web_url), system_url)
|
|
|
|
}
|
2023-04-25 08:14:35 -05:00
|
|
|
CrateOrigin::Lang(_) => return (None, None),
|
2023-03-31 02:10:18 -05:00
|
|
|
CrateOrigin::Rustc { name: _ } => {
|
2023-04-25 08:14:35 -05:00
|
|
|
(Some(format!("https://doc.rust-lang.org/{channel}/nightly-rustc/")), None)
|
2021-10-20 09:21:33 -05:00
|
|
|
}
|
2023-03-31 02:10:18 -05:00
|
|
|
CrateOrigin::Local { repo: _, name: _ } => {
|
|
|
|
// FIXME: These should not attempt to link to docs.rs!
|
2023-04-25 08:14:35 -05:00
|
|
|
let weblink = 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("*")
|
|
|
|
))
|
2023-04-25 08:14:35 -05:00
|
|
|
});
|
2023-04-27 11:05:04 -05:00
|
|
|
(weblink, local_doc)
|
2021-10-20 09:21:33 -05:00
|
|
|
}
|
2023-03-31 02:10:18 -05:00
|
|
|
CrateOrigin::Library { repo: _, name } => {
|
2023-04-25 08:14:35 -05:00
|
|
|
let weblink = krate.get_html_root_url(db).or_else(|| {
|
2023-03-31 02:10:18 -05:00
|
|
|
let version = krate.version(db);
|
|
|
|
// 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.
|
|
|
|
Some(format!(
|
|
|
|
"https://docs.rs/{krate}/{version}/",
|
|
|
|
krate = name,
|
|
|
|
version = version.as_deref().unwrap_or("*")
|
|
|
|
))
|
2023-04-25 08:14:35 -05:00
|
|
|
});
|
2023-04-27 11:05:04 -05:00
|
|
|
(weblink, local_doc)
|
2023-03-31 02:10:18 -05:00
|
|
|
}
|
2021-10-20 09:21:33 -05:00
|
|
|
};
|
2023-04-25 08:14:35 -05:00
|
|
|
let web_base = web_base
|
|
|
|
.and_then(|it| Url::parse(&it).ok())
|
|
|
|
.and_then(|it| it.join(&format!("{display_name}/")).ok());
|
2023-04-26 07:50:13 -05:00
|
|
|
let local_base = local_base.and_then(|it| it.join(&format!("{display_name}/")).ok());
|
|
|
|
|
2023-04-27 06:54:00 -05:00
|
|
|
(web_base, local_base)
|
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-11-10 15:02:50 -06:00
|
|
|
fn filename_and_frag_for_def(
|
2021-08-02 07:33:09 -05:00
|
|
|
db: &dyn HirDatabase,
|
2021-11-10 15:02:50 -06:00
|
|
|
def: Definition,
|
|
|
|
) -> Option<(Definition, String, Option<String>)> {
|
|
|
|
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 (_, file, _) = filename_and_frag_for_def(db, def)?;
|
|
|
|
let frag = get_assoc_item_fragment(db, assoc_item)?;
|
|
|
|
return Some((def, file, Some(frag)));
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = match def {
|
|
|
|
Definition::Adt(adt) => match adt {
|
2023-05-24 11:04:29 -05:00
|
|
|
Adt::Struct(s) => format!("struct.{}.html", s.name(db).display(db.upcast())),
|
|
|
|
Adt::Enum(e) => format!("enum.{}.html", e.name(db).display(db.upcast())),
|
|
|
|
Adt::Union(u) => format!("union.{}.html", u.name(db).display(db.upcast())),
|
2021-11-10 15:02:50 -06:00
|
|
|
},
|
|
|
|
Definition::Module(m) => match m.name(db) {
|
2022-05-04 10:41:29 -05:00
|
|
|
// `#[doc(keyword = "...")]` is internal used only by rust compiler
|
|
|
|
Some(name) => match m.attrs(db).by_key("doc").find_string_value_in_tt("keyword") {
|
|
|
|
Some(kw) => {
|
|
|
|
format!("keyword.{}.html", kw.trim_matches('"'))
|
|
|
|
}
|
2023-05-24 11:04:29 -05:00
|
|
|
None => format!("{}/index.html", name.display(db.upcast())),
|
2022-05-04 10:41:29 -05:00
|
|
|
},
|
2021-11-10 15:02:50 -06:00
|
|
|
None => String::from("index.html"),
|
2021-07-20 10:03:39 -05:00
|
|
|
},
|
2023-05-24 11:04:29 -05:00
|
|
|
Definition::Trait(t) => format!("trait.{}.html", t.name(db).display(db.upcast())),
|
|
|
|
Definition::TraitAlias(t) => format!("traitalias.{}.html", t.name(db).display(db.upcast())),
|
|
|
|
Definition::TypeAlias(t) => format!("type.{}.html", t.name(db).display(db.upcast())),
|
|
|
|
Definition::BuiltinType(t) => format!("primitive.{}.html", t.name().display(db.upcast())),
|
|
|
|
Definition::Function(f) => format!("fn.{}.html", f.name(db).display(db.upcast())),
|
2021-11-10 15:02:50 -06:00
|
|
|
Definition::Variant(ev) => {
|
2023-05-24 11:04:29 -05:00
|
|
|
format!(
|
|
|
|
"enum.{}.html#variant.{}",
|
|
|
|
ev.parent_enum(db).name(db).display(db.upcast()),
|
|
|
|
ev.name(db).display(db.upcast())
|
|
|
|
)
|
2021-11-10 15:02:50 -06:00
|
|
|
}
|
2023-05-24 11:04:29 -05:00
|
|
|
Definition::Const(c) => format!("const.{}.html", c.name(db)?.display(db.upcast())),
|
|
|
|
Definition::Static(s) => format!("static.{}.html", s.name(db).display(db.upcast())),
|
2023-11-04 06:33:19 -05:00
|
|
|
Definition::Macro(mac) => match mac.kind(db) {
|
|
|
|
hir::MacroKind::Declarative
|
|
|
|
| hir::MacroKind::BuiltIn
|
|
|
|
| hir::MacroKind::Attr
|
|
|
|
| hir::MacroKind::ProcMacro => {
|
|
|
|
format!("macro.{}.html", mac.name(db).display(db.upcast()))
|
|
|
|
}
|
|
|
|
hir::MacroKind::Derive => {
|
|
|
|
format!("derive.{}.html", mac.name(db).display(db.upcast()))
|
|
|
|
}
|
|
|
|
},
|
2021-11-10 15:02:50 -06:00
|
|
|
Definition::Field(field) => {
|
|
|
|
let def = match field.parent_def(db) {
|
|
|
|
hir::VariantDef::Struct(it) => Definition::Adt(it.into()),
|
|
|
|
hir::VariantDef::Union(it) => Definition::Adt(it.into()),
|
|
|
|
hir::VariantDef::Variant(it) => Definition::Variant(it),
|
|
|
|
};
|
|
|
|
let (_, file, _) = filename_and_frag_for_def(db, def)?;
|
2023-05-24 11:04:29 -05:00
|
|
|
return Some((
|
|
|
|
def,
|
|
|
|
file,
|
|
|
|
Some(format!("structfield.{}", field.name(db).display(db.upcast()))),
|
|
|
|
));
|
2021-11-10 15:02:50 -06:00
|
|
|
}
|
|
|
|
Definition::SelfType(impl_) => {
|
|
|
|
let adt = impl_.self_ty(db).as_adt()?.into();
|
|
|
|
let (_, file, _) = filename_and_frag_for_def(db, adt)?;
|
|
|
|
// FIXME fragment numbering
|
|
|
|
return Some((adt, file, Some(String::from("impl"))));
|
|
|
|
}
|
2023-08-02 04:52:55 -05:00
|
|
|
Definition::ExternCrateDecl(it) => {
|
|
|
|
format!("{}/index.html", it.name(db).display(db.upcast()))
|
|
|
|
}
|
2021-12-03 09:32:14 -06:00
|
|
|
Definition::Local(_)
|
|
|
|
| Definition::GenericParam(_)
|
2024-01-06 08:04:58 -06:00
|
|
|
| Definition::TupleField(_)
|
2021-12-03 09:32:14 -06:00
|
|
|
| Definition::Label(_)
|
|
|
|
| Definition::BuiltinAttr(_)
|
2022-07-24 07:05:37 -05:00
|
|
|
| Definition::ToolModule(_)
|
|
|
|
| Definition::DeriveHelper(_) => return None,
|
2021-08-02 07:33:09 -05:00
|
|
|
};
|
2021-11-10 15:02:50 -06:00
|
|
|
|
|
|
|
Some((def, res, None))
|
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) {
|
2023-05-24 11:04:29 -05:00
|
|
|
format!("tymethod.{}", function.name(db).display(db.upcast()))
|
2021-07-20 10:03:39 -05:00
|
|
|
} else {
|
2023-05-24 11:04:29 -05:00
|
|
|
format!("method.{}", function.name(db).display(db.upcast()))
|
2020-09-03 02:55:24 -05:00
|
|
|
}
|
2021-07-20 10:03:39 -05:00
|
|
|
}
|
2023-05-24 11:04:29 -05:00
|
|
|
AssocItem::Const(constant) => {
|
|
|
|
format!("associatedconstant.{}", constant.name(db)?.display(db.upcast()))
|
|
|
|
}
|
|
|
|
AssocItem::TypeAlias(ty) => format!("associatedtype.{}", ty.name(db).display(db.upcast())),
|
2020-08-31 03:26:55 -05:00
|
|
|
})
|
|
|
|
}
|