bynk_ide/doc_link_urls.rs
1//! #848/#2455: the Markdown link targets of intra-doc links, and the rewrite of
2//! hover Markdown that uses them. Shared by the language server and `bynk mcp`
3//! (#2520), so both render the same hover.
4
5use std::collections::HashMap;
6use std::path::{Path, PathBuf};
7
8use url::Url;
9
10use crate::index_queries::DocLinkContext;
11
12/// The `file://` URL of an absolute path. The `url` crate has no file-path
13/// conversion on wasm32 — there is no filesystem to link to — so the playground
14/// build yields no link, and a hover there renders the unlinked text.
15pub fn file_url(path: &Path) -> Option<Url> {
16 #[cfg(not(target_arch = "wasm32"))]
17 {
18 Url::from_file_path(path).ok()
19 }
20 #[cfg(target_arch = "wasm32")]
21 {
22 let _ = path;
23 None
24 }
25}
26
27/// #848: the `file://` target a resolved intra-doc link opens — the defining
28/// file, with a `#L<line>,<column>` fragment (1-based, the form VS Code's
29/// opener reads) placing the cursor on the declaration. Without the fragment
30/// a click only opens the file, which for a same-file link (the common case)
31/// does nothing visible. `snapshots` is the round's project-relative path →
32/// analysed text map, against which `def.span` is measured; a target missing
33/// from it still links, to the file alone.
34pub fn doc_link_target(
35 project_root: &Path,
36 def: &bynk_check::index::SiteRef,
37 snapshots: &HashMap<PathBuf, String>,
38) -> Option<Url> {
39 let mut url = file_url(&project_root.join(&def.path))?;
40 if let Some(text) = snapshots.get(&def.path) {
41 let (line, character) =
42 bynk_syntax::span::LineIndex::new(text).utf16_line_col(text, def.span.start);
43 url.set_fragment(Some(&format!("L{},{}", line + 1, character + 1)));
44 }
45 Some(url)
46}
47
48/// #2455, #2531: the target of one doc-link candidate — the project scope first
49/// (`crate::index_queries::resolve_doc_link_in`, a `file://` URL), then, only if
50/// that finds nothing, the first-party Book entry (`first_party_docs`, an
51/// `https://` URL). `ctx` (the declaration the doc comment is attached to) lets
52/// its own fields and parameters link. Shared by `document_link` and
53/// `linkify_doc_links`.
54pub fn doc_link_url(
55 index: &bynk_check::index::ProjectIndex,
56 doc_scope: &HashMap<String, Vec<String>>,
57 project_root: &Path,
58 snapshots: &HashMap<PathBuf, String>,
59 owner_unit: &str,
60 name: &str,
61 ctx: Option<&DocLinkContext<'_>>,
62) -> Option<Url> {
63 if let Some(resolved) =
64 crate::index_queries::resolve_doc_link_in(index, doc_scope, owner_unit, name, ctx)
65 .resolved()
66 {
67 return doc_link_target(project_root, &resolved.def, snapshots);
68 }
69 // A project declaration of the same spelling shadows the first-party name,
70 // including when `resolve_doc_link_in` declined it as ambiguous.
71 let owner = name.split('.').next().unwrap_or(name);
72 if crate::index_queries::doc_link_owner_declared(index, doc_scope, owner_unit, owner) {
73 return None;
74 }
75 let external =
76 crate::first_party_docs::resolve_first_party_doc_link(doc_scope, owner_unit, name)?;
77 Url::parse(&external).ok()
78}
79
80/// #848: rewrite every resolvable intra-doc-link candidate in `content`
81/// (hover Markdown — a ```bynk fenced signature, optionally followed by the
82/// declaration's doc-comment prose) into a Markdown link, resolved against
83/// `owner_unit`'s doc-link scope order (`crate::index_queries::resolve_doc_link_in`).
84/// The fenced signature block (and any author example fence) is never
85/// touched — `scan_doc_link_candidates` skips fenced regions itself. An
86/// unresolved candidate is left exactly as authored — no diagnostic,
87/// matching the render-only decision. `ctx` (the declaration the prose
88/// documents) lets its own fields and parameters link (#2454).
89pub fn linkify_doc_links(
90 content: &str,
91 index: &bynk_check::index::ProjectIndex,
92 doc_scope: &HashMap<String, Vec<String>>,
93 project_root: &Path,
94 snapshots: &HashMap<PathBuf, String>,
95 owner_unit: &str,
96 ctx: Option<&DocLinkContext<'_>>,
97) -> String {
98 let candidates = crate::symbols::scan_doc_link_candidates(content);
99 if candidates.is_empty() {
100 return content.to_string();
101 }
102 let mut out = String::with_capacity(content.len());
103 let mut last = 0;
104 for cand in candidates {
105 let Some(url) = doc_link_url(
106 index,
107 doc_scope,
108 project_root,
109 snapshots,
110 owner_unit,
111 &cand.name,
112 ctx,
113 ) else {
114 continue;
115 };
116 out.push_str(&content[last..cand.span.start]);
117 out.push_str(&format!("[{}]({url})", cand.display));
118 last = cand.span.end;
119 }
120 out.push_str(&content[last..]);
121 out
122}