Skip to content

Commit 6a1dd67

Browse files
committed
Small improvements and fixes
1 parent 591f2e0 commit 6a1dd67

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

crates/ide/src/doc_links.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,18 +467,17 @@ fn get_doc_base_urls(
467467
def: Definition,
468468
target_dir: Option<&OsStr>,
469469
) -> (Option<Url>, Option<Url>) {
470-
let local_doc_path = target_dir
471-
.and_then(|it| Url::from_directory_path(it).ok())
472-
.and_then(|it| it.join("doc").ok());
470+
let local_doc_path =
471+
target_dir.and_then(create_url_from_os_str).and_then(|it| it.join("doc/").ok());
473472
// special case base url of `BuiltinType` to core
474473
// https://github.com/rust-lang/rust-analyzer/issues/12250
475474
if let Definition::BuiltinType(..) = def {
476475
let weblink = Url::parse("https://doc.rust-lang.org/nightly/core/").ok();
477476
return (weblink, local_doc_path);
478477
};
479478

480-
let Some(krate) = def.krate(db) else { return (None, local_doc_path) };
481-
let Some(display_name) = krate.display_name(db) else { return (None, local_doc_path) };
479+
let Some(krate) = def.krate(db) else { return Default::default() };
480+
let Some(display_name) = krate.display_name(db) else { return Default::default() };
482481
let crate_data = &db.crate_graph()[krate.into()];
483482
let channel = crate_data.channel.map_or("nightly", ReleaseChannel::as_str);
484483
let (web_base, local_base) = match &crate_data.origin {
@@ -532,7 +531,18 @@ fn get_doc_base_urls(
532531
let web_base = web_base
533532
.and_then(|it| Url::parse(&it).ok())
534533
.and_then(|it| it.join(&format!("{display_name}/")).ok());
535-
(web_base, local_base)
534+
let local_base = local_base.and_then(|it| it.join(&format!("{display_name}/")).ok());
535+
536+
return (web_base, local_base);
537+
538+
// On Windows, cargo metadata returns paths without leading slashes, but
539+
// Url::from_directory_path requires them.
540+
// In unix adding another "/" will not make any difference.
541+
fn create_url_from_os_str(path: &OsStr) -> Option<Url> {
542+
let mut with_leading_slash = OsStr::new("/").to_os_string();
543+
with_leading_slash.push(path);
544+
Url::from_directory_path(with_leading_slash.as_os_str()).ok()
545+
}
536546
}
537547

538548
/// Get the filename and extension generated for a symbol by rustdoc.

0 commit comments

Comments
 (0)