ide/
doc_links.rs

1//! Extracts, resolves and rewrites links and intra-doc links in markdown documentation.
2
3#[cfg(test)]
4mod tests;
5
6mod intra_doc_links;
7
8use std::ops::Range;
9
10use pulldown_cmark::{BrokenLink, CowStr, Event, InlineStr, LinkType, Options, Parser, Tag};
11use pulldown_cmark_to_cmark::{Options as CMarkOptions, cmark_with_options};
12use stdx::format_to;
13use url::Url;
14
15use hir::{
16    Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrsWithOwner, HasAttrs, db::HirDatabase,
17};
18use ide_db::{
19    RootDatabase,
20    base_db::{CrateOrigin, LangCrateOrigin, ReleaseChannel, RootQueryDb},
21    defs::{Definition, NameClass, NameRefClass},
22    documentation::{Documentation, HasDocs},
23    helpers::pick_best_token,
24};
25use syntax::{
26    AstNode, AstToken,
27    SyntaxKind::*,
28    SyntaxNode, SyntaxToken, T, TextRange, TextSize,
29    ast::{self, IsString},
30    match_ast,
31};
32
33use crate::{
34    FilePosition, Semantics,
35    doc_links::intra_doc_links::{parse_intra_doc_link, strip_prefixes_suffixes},
36};
37
38/// Web and local links to an item's documentation.
39#[derive(Default, Debug, Clone, PartialEq, Eq)]
40pub struct DocumentationLinks {
41    /// The URL to the documentation on docs.rs.
42    /// May not lead anywhere.
43    pub web_url: Option<String>,
44    /// The URL to the documentation in the local file system.
45    /// May not lead anywhere.
46    pub local_url: Option<String>,
47}
48
49const MARKDOWN_OPTIONS: Options =
50    Options::ENABLE_FOOTNOTES.union(Options::ENABLE_TABLES).union(Options::ENABLE_TASKLISTS);
51
52/// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs)
53pub(crate) fn rewrite_links(
54    db: &RootDatabase,
55    markdown: &str,
56    definition: Definition,
57    range_map: Option<&hir::Docs>,
58) -> String {
59    let mut cb = broken_link_clone_cb;
60    let doc = Parser::new_with_broken_link_callback(markdown, MARKDOWN_OPTIONS, Some(&mut cb))
61        .into_offset_iter();
62
63    let doc = map_links(doc, |target, title, range, link_type| {
64        // This check is imperfect, there's some overlap between valid intra-doc links
65        // and valid URLs so we choose to be too eager to try to resolve what might be
66        // a URL.
67        if target.contains("://") {
68            (Some(LinkType::Inline), target.to_owned(), title.to_owned())
69        } else {
70            // Two possibilities:
71            // * path-based links: `../../module/struct.MyStruct.html`
72            // * module-based links (AKA intra-doc links): `super::super::module::MyStruct`
73            let text_range =
74                TextRange::new(range.start.try_into().unwrap(), range.end.try_into().unwrap());
75            let is_inner_doc = range_map
76                .as_ref()
77                .and_then(|range_map| range_map.find_ast_range(text_range))
78                .map(|(_, is_inner)| is_inner)
79                .unwrap_or(hir::IsInnerDoc::No);
80            if let Some((target, title)) =
81                rewrite_intra_doc_link(db, definition, target, title, is_inner_doc, link_type)
82            {
83                (None, target, title)
84            } else if let Some(target) = rewrite_url_link(db, definition, target) {
85                (Some(LinkType::Inline), target, title.to_owned())
86            } else {
87                (None, target.to_owned(), title.to_owned())
88            }
89        }
90    });
91    let mut out = String::new();
92    cmark_with_options(
93        doc,
94        &mut out,
95        CMarkOptions { code_block_token_count: 3, ..Default::default() },
96    )
97    .ok();
98    out
99}
100
101/// Remove all links in markdown documentation.
102pub(crate) fn remove_links(markdown: &str) -> String {
103    let mut drop_link = false;
104
105    let mut cb = |_: BrokenLink<'_>| {
106        let empty = InlineStr::try_from("").unwrap();
107        Some((CowStr::Inlined(empty), CowStr::Inlined(empty)))
108    };
109    let doc = Parser::new_with_broken_link_callback(markdown, MARKDOWN_OPTIONS, Some(&mut cb));
110    let doc = doc.filter_map(move |evt| match evt {
111        Event::Start(Tag::Link(link_type, target, title)) => {
112            if link_type == LinkType::Inline && target.contains("://") {
113                Some(Event::Start(Tag::Link(link_type, target, title)))
114            } else {
115                drop_link = true;
116                None
117            }
118        }
119        Event::End(_) if drop_link => {
120            drop_link = false;
121            None
122        }
123        _ => Some(evt),
124    });
125
126    let mut out = String::new();
127    cmark_with_options(
128        doc,
129        &mut out,
130        CMarkOptions { code_block_token_count: 3, ..Default::default() },
131    )
132    .ok();
133    out
134}
135
136// Feature: Open Docs
137//
138// Retrieve a links to documentation for the given symbol.
139//
140// The simplest way to use this feature is via the context menu. Right-click on
141// the selected item. The context menu opens. Select **Open Docs**.
142//
143// | Editor  | Action Name |
144// |---------|-------------|
145// | VS Code | **rust-analyzer: Open Docs** |
146pub(crate) fn external_docs(
147    db: &RootDatabase,
148    FilePosition { file_id, offset }: FilePosition,
149    target_dir: Option<&str>,
150    sysroot: Option<&str>,
151) -> Option<DocumentationLinks> {
152    let sema = &Semantics::new(db);
153    let file = sema.parse_guess_edition(file_id).syntax().clone();
154    let token = pick_best_token(file.token_at_offset(offset), |kind| match kind {
155        IDENT | INT_NUMBER | T![self] => 3,
156        T!['('] | T![')'] => 2,
157        kind if kind.is_trivia() => 0,
158        _ => 1,
159    })?;
160    let token = sema.descend_into_macros_single_exact(token);
161
162    let node = token.parent()?;
163    let definition = match_ast! {
164        match node {
165            ast::NameRef(name_ref) => match NameRefClass::classify(sema, &name_ref)? {
166                NameRefClass::Definition(def, _) => def,
167                NameRefClass::FieldShorthand { local_ref: _, field_ref, adt_subst: _ } => {
168                    Definition::Field(field_ref)
169                }
170                NameRefClass::ExternCrateShorthand { decl, .. } => {
171                    Definition::ExternCrateDecl(decl)
172                }
173            },
174            ast::Name(name) => match NameClass::classify(sema, &name)? {
175                NameClass::Definition(it) | NameClass::ConstReference(it) => it,
176                NameClass::PatFieldShorthand { local_def: _, field_ref, adt_subst: _ } => Definition::Field(field_ref),
177            },
178            _ => return None
179        }
180    };
181
182    Some(get_doc_links(db, definition, target_dir, sysroot))
183}
184
185/// Extracts all links from a given markdown text returning the definition text range, link-text
186/// and the namespace if known.
187pub(crate) fn extract_definitions_from_docs(
188    docs: &Documentation<'_>,
189) -> Vec<(TextRange, String, Option<hir::Namespace>)> {
190    Parser::new_with_broken_link_callback(
191        docs.as_str(),
192        MARKDOWN_OPTIONS,
193        Some(&mut broken_link_clone_cb),
194    )
195    .into_offset_iter()
196    .filter_map(|(event, range)| match event {
197        Event::Start(Tag::Link(_, target, _)) => {
198            let (link, ns) = parse_intra_doc_link(&target);
199            Some((
200                TextRange::new(range.start.try_into().ok()?, range.end.try_into().ok()?),
201                link.to_owned(),
202                ns,
203            ))
204        }
205        _ => None,
206    })
207    .collect()
208}
209
210pub(crate) fn resolve_doc_path_for_def(
211    db: &dyn HirDatabase,
212    def: Definition,
213    link: &str,
214    ns: Option<hir::Namespace>,
215    is_inner_doc: hir::IsInnerDoc,
216) -> Option<Definition> {
217    match def {
218        Definition::Module(it) => it.resolve_doc_path(db, link, ns, is_inner_doc),
219        Definition::Crate(it) => it.resolve_doc_path(db, link, ns, is_inner_doc),
220        Definition::Function(it) => it.resolve_doc_path(db, link, ns, is_inner_doc),
221        Definition::Adt(it) => it.resolve_doc_path(db, link, ns, is_inner_doc),
222        Definition::Variant(it) => it.resolve_doc_path(db, link, ns, is_inner_doc),
223        Definition::Const(it) => it.resolve_doc_path(db, link, ns, is_inner_doc),
224        Definition::Static(it) => it.resolve_doc_path(db, link, ns, is_inner_doc),
225        Definition::Trait(it) => it.resolve_doc_path(db, link, ns, is_inner_doc),
226        Definition::TypeAlias(it) => it.resolve_doc_path(db, link, ns, is_inner_doc),
227        Definition::Macro(it) => it.resolve_doc_path(db, link, ns, is_inner_doc),
228        Definition::Field(it) => it.resolve_doc_path(db, link, ns, is_inner_doc),
229        Definition::SelfType(it) => it.resolve_doc_path(db, link, ns, is_inner_doc),
230        Definition::ExternCrateDecl(it) => it.resolve_doc_path(db, link, ns, is_inner_doc),
231        Definition::BuiltinAttr(_)
232        | Definition::BuiltinType(_)
233        | Definition::BuiltinLifetime(_)
234        | Definition::ToolModule(_)
235        | Definition::TupleField(_)
236        | Definition::Local(_)
237        | Definition::GenericParam(_)
238        | Definition::Label(_)
239        | Definition::DeriveHelper(_)
240        | Definition::InlineAsmRegOrRegClass(_)
241        | Definition::InlineAsmOperand(_) => None,
242    }
243    .map(Definition::from)
244}
245
246pub(crate) fn doc_attributes(
247    sema: &Semantics<'_, RootDatabase>,
248    node: &SyntaxNode,
249) -> Option<(hir::AttrsWithOwner, Definition)> {
250    match_ast! {
251        match node {
252            ast::SourceFile(it)  => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
253            ast::Module(it)      => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
254            ast::Fn(it)          => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
255            ast::Struct(it)      => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(hir::Adt::Struct(def)))),
256            ast::Union(it)       => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(hir::Adt::Union(def)))),
257            ast::Enum(it)        => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(hir::Adt::Enum(def)))),
258            ast::Variant(it)     => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
259            ast::Trait(it)       => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
260            ast::Static(it)      => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
261            ast::Const(it)       => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
262            ast::TypeAlias(it)   => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
263            ast::Impl(it)        => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
264            ast::RecordField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
265            ast::TupleField(it)  => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
266            ast::Macro(it)       => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
267            ast::ExternCrate(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
268            // ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
269            _ => None
270        }
271    }
272}
273
274pub(crate) struct DocCommentToken {
275    doc_token: SyntaxToken,
276    prefix_len: TextSize,
277}
278
279pub(crate) fn token_as_doc_comment(doc_token: &SyntaxToken) -> Option<DocCommentToken> {
280    (match_ast! {
281        match doc_token {
282            ast::Comment(comment) => TextSize::try_from(comment.prefix().len()).ok(),
283            ast::String(string) => {
284                doc_token.parent_ancestors().find_map(ast::Attr::cast).filter(|attr| attr.simple_name().as_deref() == Some("doc"))?;
285                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() {
286                    return None;
287                }
288                string.open_quote_text_range().map(|it| it.len())
289            },
290            _ => None,
291        }
292    }).map(|prefix_len| DocCommentToken { prefix_len, doc_token: doc_token.clone() })
293}
294
295impl DocCommentToken {
296    pub(crate) fn get_definition_with_descend_at<T>(
297        self,
298        sema: &Semantics<'_, RootDatabase>,
299        offset: TextSize,
300        // Definition, CommentOwner, range of intra doc link in original file
301        mut cb: impl FnMut(Definition, SyntaxNode, TextRange) -> Option<T>,
302    ) -> Option<T> {
303        let DocCommentToken { prefix_len, doc_token } = self;
304        // offset relative to the comments contents
305        let original_start = doc_token.text_range().start();
306        let relative_comment_offset = offset - original_start - prefix_len;
307
308        sema.descend_into_macros(doc_token).into_iter().find_map(|t| {
309            let (node, descended_prefix_len, is_inner) = match_ast!{
310                match t {
311                    ast::Comment(comment) => {
312                        (t.parent()?, TextSize::try_from(comment.prefix().len()).ok()?, comment.is_inner())
313                    },
314                    ast::String(string) => {
315                        let attr = t.parent_ancestors().find_map(ast::Attr::cast)?;
316                        let attr_is_inner = attr.excl_token().map(|excl| excl.kind() == BANG).unwrap_or(false);
317                        (attr.syntax().parent()?, string.open_quote_text_range()?.len(), attr_is_inner)
318                    },
319                    _ => return None,
320                }
321            };
322            let token_start = t.text_range().start();
323            let abs_in_expansion_offset = token_start + relative_comment_offset + descended_prefix_len;
324            let (attributes, def) = Self::doc_attributes(sema, &node, is_inner)?;
325            let doc_mapping = attributes.hir_docs(sema.db)?;
326            let (in_expansion_range, link, ns, is_inner) =
327                extract_definitions_from_docs(&Documentation::new_borrowed(doc_mapping.docs())).into_iter().find_map(|(range, link, ns)| {
328                    let (mapped, is_inner) = doc_mapping.find_ast_range(range)?;
329                    (mapped.value.contains(abs_in_expansion_offset)).then_some((mapped.value, link, ns, is_inner))
330                })?;
331            // get the relative range to the doc/attribute in the expansion
332            let in_expansion_relative_range = in_expansion_range - descended_prefix_len - token_start;
333            // Apply relative range to the original input comment
334            let absolute_range = in_expansion_relative_range + original_start + prefix_len;
335            let def = resolve_doc_path_for_def(sema.db, def, &link, ns, is_inner)?;
336            cb(def, node, absolute_range)
337        })
338    }
339
340    /// When we hover a inner doc item, this find a attached definition.
341    /// ```
342    /// // node == ITEM_LIST
343    /// // node.parent == EXPR_BLOCK
344    /// // node.parent().parent() == FN
345    /// fn f() {
346    ///    //! [`S$0`]
347    /// }
348    /// ```
349    fn doc_attributes(
350        sema: &Semantics<'_, RootDatabase>,
351        node: &SyntaxNode,
352        is_inner_doc: bool,
353    ) -> Option<(AttrsWithOwner, Definition)> {
354        if is_inner_doc && node.kind() != SOURCE_FILE {
355            let parent = node.parent()?;
356            doc_attributes(sema, &parent).or(doc_attributes(sema, &parent.parent()?))
357        } else {
358            doc_attributes(sema, node)
359        }
360    }
361}
362
363fn broken_link_clone_cb(link: BrokenLink<'_>) -> Option<(CowStr<'_>, CowStr<'_>)> {
364    Some((/*url*/ link.reference.clone(), /*title*/ link.reference))
365}
366
367// FIXME:
368// BUG: For Option::Some
369// Returns https://doc.rust-lang.org/nightly/core/prelude/v1/enum.Option.html#variant.Some
370// Instead of https://doc.rust-lang.org/nightly/core/option/enum.Option.html
371//
372// This should cease to be a problem if RFC2988 (Stable Rustdoc URLs) is implemented
373// https://github.com/rust-lang/rfcs/pull/2988
374fn get_doc_links(
375    db: &RootDatabase,
376    def: Definition,
377    target_dir: Option<&str>,
378    sysroot: Option<&str>,
379) -> DocumentationLinks {
380    let join_url = |base_url: Option<Url>, path: &str| -> Option<Url> {
381        base_url.and_then(|url| url.join(path).ok())
382    };
383
384    let Some((target, file, frag)) = filename_and_frag_for_def(db, def) else {
385        return Default::default();
386    };
387
388    let (mut web_url, mut local_url) = get_doc_base_urls(db, target, target_dir, sysroot);
389
390    let append_mod = !matches!(def, Definition::Macro(m) if m.is_macro_export(db));
391    if append_mod && let Some(path) = mod_path_of_def(db, target) {
392        web_url = join_url(web_url, &path);
393        local_url = join_url(local_url, &path);
394    }
395
396    web_url = join_url(web_url, &file);
397    local_url = join_url(local_url, &file);
398
399    if let Some(url) = web_url.as_mut() {
400        url.set_fragment(frag.as_deref())
401    }
402    if let Some(url) = local_url.as_mut() {
403        url.set_fragment(frag.as_deref())
404    }
405
406    DocumentationLinks {
407        web_url: web_url.map(|it| it.into()),
408        local_url: local_url.map(|it| it.into()),
409    }
410}
411
412fn rewrite_intra_doc_link(
413    db: &RootDatabase,
414    def: Definition,
415    target: &str,
416    title: &str,
417    is_inner_doc: hir::IsInnerDoc,
418    link_type: LinkType,
419) -> Option<(String, String)> {
420    let (link, ns) = parse_intra_doc_link(target);
421
422    let (link, anchor) = match link.split_once('#') {
423        Some((new_link, anchor)) => (new_link, Some(anchor)),
424        None => (link, None),
425    };
426
427    let resolved = resolve_doc_path_for_def(db, def, link, ns, is_inner_doc)?;
428    let mut url = get_doc_base_urls(db, resolved, None, None).0?;
429
430    let (_, file, frag) = filename_and_frag_for_def(db, resolved)?;
431    if let Some(path) = mod_path_of_def(db, resolved) {
432        url = url.join(&path).ok()?;
433    }
434
435    let frag = anchor.or(frag.as_deref());
436
437    url = url.join(&file).ok()?;
438    url.set_fragment(frag);
439
440    // We want to strip the keyword prefix from the title, but only if the target is implicitly the same
441    // as the title.
442    let title = match link_type {
443        LinkType::Email
444        | LinkType::Autolink
445        | LinkType::Shortcut
446        | LinkType::Collapsed
447        | LinkType::Reference
448        | LinkType::Inline => title.to_owned(),
449        LinkType::ShortcutUnknown | LinkType::CollapsedUnknown | LinkType::ReferenceUnknown => {
450            strip_prefixes_suffixes(title).to_owned()
451        }
452    };
453
454    Some((url.into(), title))
455}
456
457/// Try to resolve path to local documentation via path-based links (i.e. `../gateway/struct.Shard.html`).
458fn rewrite_url_link(db: &RootDatabase, def: Definition, target: &str) -> Option<String> {
459    if !(target.contains('#') || target.contains(".html")) {
460        return None;
461    }
462
463    let mut url = get_doc_base_urls(db, def, None, None).0?;
464    let (def, file, frag) = filename_and_frag_for_def(db, def)?;
465
466    if let Some(path) = mod_path_of_def(db, def) {
467        url = url.join(&path).ok()?;
468    }
469
470    url = url.join(&file).ok()?;
471    url.set_fragment(frag.as_deref());
472    url.join(target).ok().map(Into::into)
473}
474
475fn mod_path_of_def(db: &RootDatabase, def: Definition) -> Option<String> {
476    def.canonical_module_path(db).map(|it| {
477        let mut path = String::new();
478        it.flat_map(|it| it.name(db)).for_each(|name| format_to!(path, "{}/", name.as_str()));
479        path
480    })
481}
482
483/// Rewrites a markdown document, applying 'callback' to each link.
484fn map_links<'e>(
485    events: impl Iterator<Item = (Event<'e>, Range<usize>)>,
486    callback: impl Fn(&str, &str, Range<usize>, LinkType) -> (Option<LinkType>, String, String),
487) -> impl Iterator<Item = Event<'e>> {
488    let mut in_link = false;
489    // holds the origin link target on start event and the rewritten one on end event
490    let mut end_link_target: Option<CowStr<'_>> = None;
491    // normally link's type is determined by the type of link tag in the end event,
492    // however in some cases we want to change the link type, for example,
493    // `Shortcut` type parsed from Start/End tags doesn't make sense for url links
494    let mut end_link_type: Option<LinkType> = None;
495
496    events.map(move |(evt, range)| match evt {
497        Event::Start(Tag::Link(link_type, ref target, _)) => {
498            in_link = true;
499            end_link_target = Some(target.clone());
500            end_link_type = Some(link_type);
501            evt
502        }
503        Event::End(Tag::Link(link_type, target, _)) => {
504            in_link = false;
505            Event::End(Tag::Link(
506                end_link_type.take().unwrap_or(link_type),
507                end_link_target.take().unwrap_or(target),
508                CowStr::Borrowed(""),
509            ))
510        }
511        Event::Text(s) if in_link => {
512            let (link_type, link_target_s, link_name) =
513                callback(&end_link_target.take().unwrap(), &s, range, end_link_type.unwrap());
514            end_link_target = Some(CowStr::Boxed(link_target_s.into()));
515            if !matches!(end_link_type, Some(LinkType::Autolink)) && link_type.is_some() {
516                end_link_type = link_type;
517            }
518            Event::Text(CowStr::Boxed(link_name.into()))
519        }
520        Event::Code(s) if in_link => {
521            let (link_type, link_target_s, link_name) =
522                callback(&end_link_target.take().unwrap(), &s, range, end_link_type.unwrap());
523            end_link_target = Some(CowStr::Boxed(link_target_s.into()));
524            if !matches!(end_link_type, Some(LinkType::Autolink)) && link_type.is_some() {
525                end_link_type = link_type;
526            }
527            Event::Code(CowStr::Boxed(link_name.into()))
528        }
529        _ => evt,
530    })
531}
532
533/// Get the root URL for the documentation of a definition.
534///
535/// ```ignore
536/// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
537/// ^^^^^^^^^^^^^^^^^^^^^^^^^^
538/// file:///project/root/target/doc/std/iter/trait.Iterator.html#tymethod.next
539/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
540/// ```
541fn get_doc_base_urls(
542    db: &RootDatabase,
543    def: Definition,
544    target_dir: Option<&str>,
545    sysroot: Option<&str>,
546) -> (Option<Url>, Option<Url>) {
547    let local_doc = target_dir
548        .and_then(|path| Url::parse(&format!("file:///{path}/")).ok())
549        .and_then(|it| it.join("doc/").ok());
550    let system_doc = sysroot
551        .map(|sysroot| format!("file:///{sysroot}/share/doc/rust/html/"))
552        .and_then(|it| Url::parse(&it).ok());
553    let krate = def.krate(db);
554    let channel = krate
555        .and_then(|krate| db.toolchain_channel(krate.into()))
556        .unwrap_or(ReleaseChannel::Nightly)
557        .as_str();
558
559    // special case base url of `BuiltinType` to core
560    // https://github.com/rust-lang/rust-analyzer/issues/12250
561    if let Definition::BuiltinType(..) = def {
562        let web_link = Url::parse(&format!("https://doc.rust-lang.org/{channel}/core/")).ok();
563        let system_link = system_doc.and_then(|it| it.join("core/").ok());
564        return (web_link, system_link);
565    };
566
567    let Some(krate) = krate else { return Default::default() };
568    let Some(display_name) = krate.display_name(db) else { return Default::default() };
569    let (web_base, local_base) = match krate.origin(db) {
570        // std and co do not specify `html_root_url` any longer so we gotta handwrite this ourself.
571        // FIXME: Use the toolchains channel instead of nightly
572        CrateOrigin::Lang(
573            origin @ (LangCrateOrigin::Alloc
574            | LangCrateOrigin::Core
575            | LangCrateOrigin::ProcMacro
576            | LangCrateOrigin::Std
577            | LangCrateOrigin::Test),
578        ) => {
579            let system_url = system_doc.and_then(|it| it.join(&format!("{origin}")).ok());
580            let web_url = format!("https://doc.rust-lang.org/{channel}/{origin}");
581            (Some(web_url), system_url)
582        }
583        CrateOrigin::Lang(_) => return (None, None),
584        CrateOrigin::Rustc { name: _ } => {
585            (Some(format!("https://doc.rust-lang.org/{channel}/nightly-rustc/")), None)
586        }
587        CrateOrigin::Local { repo: _, name: _ } => {
588            // FIXME: These should not attempt to link to docs.rs!
589            let weblink = krate.get_html_root_url(db).or_else(|| {
590                let version = krate.version(db);
591                // Fallback to docs.rs. This uses `display_name` and can never be
592                // correct, but that's what fallbacks are about.
593                //
594                // FIXME: clicking on the link should just open the file in the editor,
595                // instead of falling back to external urls.
596                Some(format!(
597                    "https://docs.rs/{krate}/{version}/",
598                    krate = display_name,
599                    version = version.as_deref().unwrap_or("*")
600                ))
601            });
602            (weblink, local_doc)
603        }
604        CrateOrigin::Library { repo: _, name } => {
605            let weblink = krate.get_html_root_url(db).or_else(|| {
606                let version = krate.version(db);
607                // Fallback to docs.rs. This uses `display_name` and can never be
608                // correct, but that's what fallbacks are about.
609                //
610                // FIXME: clicking on the link should just open the file in the editor,
611                // instead of falling back to external urls.
612                Some(format!(
613                    "https://docs.rs/{krate}/{version}/",
614                    krate = name,
615                    version = version.as_deref().unwrap_or("*")
616                ))
617            });
618            (weblink, local_doc)
619        }
620    };
621    let web_base = web_base
622        .and_then(|it| Url::parse(&it).ok())
623        .and_then(|it| it.join(&format!("{display_name}/")).ok());
624    let local_base = local_base.and_then(|it| it.join(&format!("{display_name}/")).ok());
625
626    (web_base, local_base)
627}
628
629/// Get the filename and extension generated for a symbol by rustdoc.
630///
631/// ```ignore
632/// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
633///                                    ^^^^^^^^^^^^^^^^^^^
634/// ```
635fn filename_and_frag_for_def(
636    db: &dyn HirDatabase,
637    def: Definition,
638) -> Option<(Definition, String, Option<String>)> {
639    if let Some(assoc_item) = def.as_assoc_item(db) {
640        let def = match assoc_item.container(db) {
641            AssocItemContainer::Trait(t) => t.into(),
642            AssocItemContainer::Impl(i) => i.self_ty(db).as_adt()?.into(),
643        };
644        let (_, file, _) = filename_and_frag_for_def(db, def)?;
645        let frag = get_assoc_item_fragment(db, assoc_item)?;
646        return Some((def, file, Some(frag)));
647    }
648
649    let res = match def {
650        Definition::Adt(adt) => match adt {
651            Adt::Struct(s) => {
652                format!("struct.{}.html", s.name(db).as_str())
653            }
654            Adt::Enum(e) => format!("enum.{}.html", e.name(db).as_str()),
655            Adt::Union(u) => format!("union.{}.html", u.name(db).as_str()),
656        },
657        Definition::Crate(_) => String::from("index.html"),
658        Definition::Module(m) => match m.name(db) {
659            // `#[doc(keyword = "...")]` is internal used only by rust compiler
660            Some(name) => match m.doc_keyword(db) {
661                Some(kw) => {
662                    format!("keyword.{kw}.html")
663                }
664                None => format!("{}/index.html", name.as_str()),
665            },
666            None => String::from("index.html"),
667        },
668        Definition::Trait(t) => {
669            // FIXME(trait-alias): url should be traitalias. for aliases
670            format!("trait.{}.html", t.name(db).as_str())
671        }
672        Definition::TypeAlias(t) => {
673            format!("type.{}.html", t.name(db).as_str())
674        }
675        Definition::BuiltinType(t) => {
676            format!("primitive.{}.html", t.name().as_str())
677        }
678        Definition::Function(f) => {
679            format!("fn.{}.html", f.name(db).as_str())
680        }
681        Definition::Variant(ev) => {
682            let def = Definition::Adt(ev.parent_enum(db).into());
683            let (_, file, _) = filename_and_frag_for_def(db, def)?;
684            return Some((def, file, Some(format!("variant.{}", ev.name(db).as_str()))));
685        }
686        Definition::Const(c) => {
687            format!("constant.{}.html", c.name(db)?.as_str())
688        }
689        Definition::Static(s) => {
690            format!("static.{}.html", s.name(db).as_str())
691        }
692        Definition::Macro(mac) => match mac.kind(db) {
693            hir::MacroKind::Declarative
694            | hir::MacroKind::AttrBuiltIn
695            | hir::MacroKind::DeclarativeBuiltIn
696            | hir::MacroKind::Attr
697            | hir::MacroKind::ProcMacro => {
698                format!("macro.{}.html", mac.name(db).as_str())
699            }
700            hir::MacroKind::Derive | hir::MacroKind::DeriveBuiltIn => {
701                format!("derive.{}.html", mac.name(db).as_str())
702            }
703        },
704        Definition::Field(field) => {
705            let def = match field.parent_def(db) {
706                hir::VariantDef::Struct(it) => Definition::Adt(it.into()),
707                hir::VariantDef::Union(it) => Definition::Adt(it.into()),
708                hir::VariantDef::Variant(it) => Definition::Variant(it),
709            };
710            let (_, file, _) = filename_and_frag_for_def(db, def)?;
711            return Some((def, file, Some(format!("structfield.{}", field.name(db).as_str()))));
712        }
713        Definition::SelfType(impl_) => {
714            let adt = impl_.self_ty(db).as_adt()?.into();
715            let (_, file, _) = filename_and_frag_for_def(db, adt)?;
716            // FIXME fragment numbering
717            return Some((adt, file, Some(String::from("impl"))));
718        }
719        Definition::ExternCrateDecl(it) => {
720            format!("{}/index.html", it.name(db).as_str())
721        }
722        Definition::Local(_)
723        | Definition::GenericParam(_)
724        | Definition::TupleField(_)
725        | Definition::Label(_)
726        | Definition::BuiltinAttr(_)
727        | Definition::BuiltinLifetime(_)
728        | Definition::ToolModule(_)
729        | Definition::DeriveHelper(_)
730        | Definition::InlineAsmRegOrRegClass(_)
731        | Definition::InlineAsmOperand(_) => return None,
732    };
733
734    Some((def, res, None))
735}
736
737/// Get the fragment required to link to a specific field, method, associated type, or associated constant.
738///
739/// ```ignore
740/// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
741///                                                       ^^^^^^^^^^^^^^
742/// ```
743fn get_assoc_item_fragment(db: &dyn HirDatabase, assoc_item: hir::AssocItem) -> Option<String> {
744    Some(match assoc_item {
745        AssocItem::Function(function) => {
746            let is_trait_method =
747                function.as_assoc_item(db).and_then(|assoc| assoc.container_trait(db)).is_some();
748            // This distinction may get more complicated when specialization is available.
749            // Rustdoc makes this decision based on whether a method 'has defaultness'.
750            // Currently this is only the case for provided trait methods.
751            if is_trait_method && !function.has_body(db) {
752                format!("tymethod.{}", function.name(db).as_str())
753            } else {
754                format!("method.{}", function.name(db).as_str())
755            }
756        }
757        AssocItem::Const(constant) => {
758            format!("associatedconstant.{}", constant.name(db)?.as_str())
759        }
760        AssocItem::TypeAlias(ty) => {
761            format!("associatedtype.{}", ty.name(db).as_str())
762        }
763    })
764}