diff options
Diffstat (limited to 'crates/ide/src/doc_links.rs')
-rw-r--r-- | crates/ide/src/doc_links.rs | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs index d9dc63b33..250f10f9f 100644 --- a/crates/ide/src/doc_links.rs +++ b/crates/ide/src/doc_links.rs | |||
@@ -130,9 +130,10 @@ fn get_doc_link(db: &RootDatabase, definition: Definition) -> Option<String> { | |||
130 | let module = definition.module(db)?; | 130 | let module = definition.module(db)?; |
131 | let krate = module.krate(); | 131 | let krate = module.krate(); |
132 | let import_map = db.import_map(krate.into()); | 132 | let import_map = db.import_map(krate.into()); |
133 | let base = once(krate.declaration_name(db)?.to_string()) | 133 | let base = once(krate.display_name(db)?.to_string()) |
134 | .chain(import_map.path_of(ns)?.segments.iter().map(|name| name.to_string())) | 134 | .chain(import_map.path_of(ns)?.segments.iter().map(|name| name.to_string())) |
135 | .join("/"); | 135 | .join("/") |
136 | + "/"; | ||
136 | 137 | ||
137 | let filename = get_symbol_filename(db, &target_def); | 138 | let filename = get_symbol_filename(db, &target_def); |
138 | let fragment = match definition { | 139 | let fragment = match definition { |
@@ -152,9 +153,16 @@ fn get_doc_link(db: &RootDatabase, definition: Definition) -> Option<String> { | |||
152 | _ => None, | 153 | _ => None, |
153 | }; | 154 | }; |
154 | 155 | ||
155 | get_doc_url(db, &krate) | 156 | get_doc_url(db, &krate)? |
156 | .and_then(|url| url.join(&base).ok()) | 157 | .join(&base) |
157 | .and_then(|url| filename.as_deref().and_then(|f| url.join(f).ok())) | 158 | .ok() |
159 | .and_then(|mut url| { | ||
160 | if !matches!(definition, Definition::ModuleDef(ModuleDef::Module(..))) { | ||
161 | url.path_segments_mut().ok()?.pop(); | ||
162 | }; | ||
163 | Some(url) | ||
164 | }) | ||
165 | .and_then(|url| url.join(filename.as_deref()?).ok()) | ||
158 | .and_then( | 166 | .and_then( |
159 | |url| if let Some(fragment) = fragment { url.join(&fragment).ok() } else { Some(url) }, | 167 | |url| if let Some(fragment) = fragment { url.join(&fragment).ok() } else { Some(url) }, |
160 | ) | 168 | ) |
@@ -188,7 +196,7 @@ fn rewrite_intra_doc_link( | |||
188 | let krate = resolved.module(db)?.krate(); | 196 | let krate = resolved.module(db)?.krate(); |
189 | let canonical_path = resolved.canonical_path(db)?; | 197 | let canonical_path = resolved.canonical_path(db)?; |
190 | let new_target = get_doc_url(db, &krate)? | 198 | let new_target = get_doc_url(db, &krate)? |
191 | .join(&format!("{}/", krate.declaration_name(db)?)) | 199 | .join(&format!("{}/", krate.display_name(db)?)) |
192 | .ok()? | 200 | .ok()? |
193 | .join(&canonical_path.replace("::", "/")) | 201 | .join(&canonical_path.replace("::", "/")) |
194 | .ok()? | 202 | .ok()? |
@@ -208,7 +216,7 @@ fn rewrite_url_link(db: &RootDatabase, def: ModuleDef, target: &str) -> Option<S | |||
208 | let module = def.module(db)?; | 216 | let module = def.module(db)?; |
209 | let krate = module.krate(); | 217 | let krate = module.krate(); |
210 | let canonical_path = def.canonical_path(db)?; | 218 | let canonical_path = def.canonical_path(db)?; |
211 | let base = format!("{}/{}", krate.declaration_name(db)?, canonical_path.replace("::", "/")); | 219 | let base = format!("{}/{}", krate.display_name(db)?, canonical_path.replace("::", "/")); |
212 | 220 | ||
213 | get_doc_url(db, &krate) | 221 | get_doc_url(db, &krate) |
214 | .and_then(|url| url.join(&base).ok()) | 222 | .and_then(|url| url.join(&base).ok()) |
@@ -357,7 +365,7 @@ fn get_doc_url(db: &RootDatabase, krate: &Crate) -> Option<Url> { | |||
357 | // | 365 | // |
358 | // FIXME: clicking on the link should just open the file in the editor, | 366 | // FIXME: clicking on the link should just open the file in the editor, |
359 | // instead of falling back to external urls. | 367 | // instead of falling back to external urls. |
360 | Some(format!("https://docs.rs/{}/*/", krate.declaration_name(db)?)) | 368 | Some(format!("https://docs.rs/{}/*/", krate.display_name(db)?)) |
361 | }) | 369 | }) |
362 | .and_then(|s| Url::parse(&s).ok()) | 370 | .and_then(|s| Url::parse(&s).ok()) |
363 | } | 371 | } |
@@ -522,6 +530,18 @@ pub struct Foo { | |||
522 | ); | 530 | ); |
523 | } | 531 | } |
524 | 532 | ||
533 | #[test] | ||
534 | fn test_module() { | ||
535 | check( | ||
536 | r#" | ||
537 | pub mod foo { | ||
538 | pub mod ba<|>r {} | ||
539 | } | ||
540 | "#, | ||
541 | expect![[r#"https://docs.rs/test/*/test/foo/bar/index.html"#]], | ||
542 | ) | ||
543 | } | ||
544 | |||
525 | // FIXME: ImportMap will return re-export paths instead of public module | 545 | // FIXME: ImportMap will return re-export paths instead of public module |
526 | // paths. The correct path to documentation will never be a re-export. | 546 | // paths. The correct path to documentation will never be a re-export. |
527 | // This problem stops us from resolving stdlib items included in the prelude | 547 | // This problem stops us from resolving stdlib items included in the prelude |