Skip to content

Commit 822c46c

Browse files
committed
Propagating sysroot down + Refactoring
1 parent cba9617 commit 822c46c

File tree

4 files changed

+66
-35
lines changed

4 files changed

+66
-35
lines changed

crates/ide/src/doc_links.rs

+23-22
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ pub(crate) fn external_docs(
133133
db: &RootDatabase,
134134
position: &FilePosition,
135135
target_dir: Option<&OsStr>,
136+
sysroot: Option<&OsStr>,
136137
) -> Option<DocumentationLinks> {
137138
let sema = &Semantics::new(db);
138139
let file = sema.parse(position.file_id).syntax().clone();
@@ -163,7 +164,7 @@ pub(crate) fn external_docs(
163164
}
164165
};
165166

166-
Some(get_doc_links(db, definition, target_dir))
167+
Some(get_doc_links(db, definition, target_dir, sysroot))
167168
}
168169

169170
/// Extracts all links from a given markdown text returning the definition text range, link-text
@@ -325,14 +326,15 @@ fn get_doc_links(
325326
db: &RootDatabase,
326327
def: Definition,
327328
target_dir: Option<&OsStr>,
329+
sysroot: Option<&OsStr>,
328330
) -> DocumentationLinks {
329331
let join_url = |base_url: Option<Url>, path: &str| -> Option<Url> {
330332
base_url.and_then(|url| url.join(path).ok())
331333
};
332334

333335
let Some((target, file, frag)) = filename_and_frag_for_def(db, def) else { return Default::default(); };
334336

335-
let (mut web_url, mut local_url) = get_doc_base_urls(db, target, target_dir);
337+
let (mut web_url, mut local_url) = get_doc_base_urls(db, target, target_dir, sysroot);
336338

337339
if let Some(path) = mod_path_of_def(db, target) {
338340
web_url = join_url(web_url, &path);
@@ -360,7 +362,7 @@ fn rewrite_intra_doc_link(
360362
let (link, ns) = parse_intra_doc_link(target);
361363

362364
let resolved = resolve_doc_path_for_def(db, def, link, ns)?;
363-
let mut url = get_doc_base_urls(db, resolved, None).0?;
365+
let mut url = get_doc_base_urls(db, resolved, None, None).0?;
364366

365367
let (_, file, frag) = filename_and_frag_for_def(db, resolved)?;
366368
if let Some(path) = mod_path_of_def(db, resolved) {
@@ -379,7 +381,7 @@ fn rewrite_url_link(db: &RootDatabase, def: Definition, target: &str) -> Option<
379381
return None;
380382
}
381383

382-
let mut url = get_doc_base_urls(db, def, None).0?;
384+
let mut url = get_doc_base_urls(db, def, None, None).0?;
383385
let (def, file, frag) = filename_and_frag_for_def(db, def)?;
384386

385387
if let Some(path) = mod_path_of_def(db, def) {
@@ -461,27 +463,29 @@ fn get_doc_base_urls(
461463
db: &RootDatabase,
462464
def: Definition,
463465
target_dir: Option<&OsStr>,
466+
sysroot: Option<&OsStr>,
464467
) -> (Option<Url>, Option<Url>) {
465-
let local_doc_path = target_dir
466-
.and_then(|path: &OsStr| -> Option<Url> {
467-
let mut with_prefix = OsStr::new("file:///").to_os_string();
468-
with_prefix.push(path);
469-
with_prefix.push("/");
470-
with_prefix.to_str().and_then(|s| Url::parse(s).ok())
471-
})
468+
let local_doc = target_dir
469+
.and_then(|path| path.to_str())
470+
.and_then(|path| Url::parse(&format!("file:///{path}/")).ok())
472471
.and_then(|it| it.join("doc/").ok());
472+
let system_doc = sysroot
473+
.and_then(|it| it.to_str())
474+
.map(|sysroot| format!("file:///{sysroot}/share/doc/rust/html/"))
475+
.and_then(|it| Url::parse(&it).ok());
476+
473477
// special case base url of `BuiltinType` to core
474478
// https://github.com/rust-lang/rust-analyzer/issues/12250
475479
if let Definition::BuiltinType(..) = def {
476-
let weblink = Url::parse("https://doc.rust-lang.org/nightly/core/").ok();
477-
return (weblink, None);
480+
let web_link = Url::parse("https://doc.rust-lang.org/nightly/core/").ok();
481+
let system_link = system_doc.and_then(|it| it.join("core/").ok());
482+
return (web_link, system_link);
478483
};
479484

480485
let Some(krate) = def.krate(db) else { return Default::default() };
481486
let Some(display_name) = krate.display_name(db) else { return Default::default() };
482487
let crate_data = &db.crate_graph()[krate.into()];
483488
let channel = crate_data.channel.map_or("nightly", ReleaseChannel::as_str);
484-
let sysroot = "/home/ddystopia/.rustup/toolchains/stable-x86_64-unknown-linux-gnu";
485489

486490
let (web_base, local_base) = match &crate_data.origin {
487491
// std and co do not specify `html_root_url` any longer so we gotta handwrite this ourself.
@@ -493,13 +497,10 @@ fn get_doc_base_urls(
493497
| LangCrateOrigin::Std
494498
| LangCrateOrigin::Test),
495499
) => {
496-
let local_url = format!("file:///{sysroot}/share/doc/rust/html/{origin}/index.html");
497-
let local_url = Url::parse(&local_url).ok();
500+
let system_url = system_doc.and_then(|it| it.join(&format!("{origin}")).ok());
498501
let web_url = format!("https://doc.rust-lang.org/{channel}/{origin}");
499-
println!("local_url: {:?}", local_url.unwrap().to_string());
500-
panic!();
501-
(Some(web_url), local_url)
502-
},
502+
(Some(web_url), system_url)
503+
}
503504
CrateOrigin::Lang(_) => return (None, None),
504505
CrateOrigin::Rustc { name: _ } => {
505506
(Some(format!("https://doc.rust-lang.org/{channel}/nightly-rustc/")), None)
@@ -519,7 +520,7 @@ fn get_doc_base_urls(
519520
version = version.as_deref().unwrap_or("*")
520521
))
521522
});
522-
(weblink, local_doc_path)
523+
(weblink, local_doc)
523524
}
524525
CrateOrigin::Library { repo: _, name } => {
525526
let weblink = krate.get_html_root_url(db).or_else(|| {
@@ -535,7 +536,7 @@ fn get_doc_base_urls(
535536
version = version.as_deref().unwrap_or("*")
536537
))
537538
});
538-
(weblink, local_doc_path)
539+
(weblink, local_doc)
539540
}
540541
};
541542
let web_base = web_base

crates/ide/src/doc_links/tests.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ fn check_external_docs(
2020
target_dir: Option<&OsStr>,
2121
expect_web_url: Option<Expect>,
2222
expect_local_url: Option<Expect>,
23+
sysroot: Option<&OsStr>,
2324
) {
2425
let (analysis, position) = fixture::position(ra_fixture);
25-
let links = analysis.external_docs(position, target_dir).unwrap();
26+
let links = analysis.external_docs(position, target_dir, sysroot).unwrap();
2627

2728
let web_url = links.web_url;
2829
let local_url = links.local_url;
@@ -128,7 +129,8 @@ let x: u3$02 = 0;
128129
"#,
129130
Some(&OsStr::new("/home/user/project")),
130131
Some(expect![[r#"https://doc.rust-lang.org/nightly/core/primitive.u32.html"#]]),
131-
None,
132+
Some(expect![[r#"file:///sysroot/share/doc/rust/html/core/primitive.u32.html"#]]),
133+
Some(&OsStr::new("/sysroot")),
132134
);
133135
}
134136

@@ -144,6 +146,7 @@ pub struct Foo;
144146
Some(&OsStr::new("/home/user/project")),
145147
Some(expect![[r#"https://docs.rs/foo/*/foo/index.html"#]]),
146148
Some(expect![[r#"file:///home/user/project/doc/foo/index.html"#]]),
149+
Some(&OsStr::new("/sysroot")),
147150
);
148151
}
149152

@@ -156,7 +159,8 @@ use self$0;
156159
"#,
157160
Some(&OsStr::new("/home/user/project")),
158161
Some(expect!["https://doc.rust-lang.org/stable/std/index.html"]),
159-
None,
162+
Some(expect!["file:///sysroot/share/doc/rust/html/std/index.html"]),
163+
Some(&OsStr::new("/sysroot")),
160164
);
161165
}
162166

@@ -170,6 +174,7 @@ pub struct Fo$0o;
170174
Some(&OsStr::new("/home/user/project")),
171175
Some(expect![[r#"https://docs.rs/foo/*/foo/struct.Foo.html"#]]),
172176
Some(expect![[r#"file:///home/user/project/doc/foo/struct.Foo.html"#]]),
177+
Some(&OsStr::new("/sysroot")),
173178
);
174179
}
175180

@@ -183,6 +188,7 @@ pub struct Fo$0o;
183188
Some(&OsStr::new(r"C:\Users\user\project")),
184189
Some(expect![[r#"https://docs.rs/foo/*/foo/struct.Foo.html"#]]),
185190
Some(expect![[r#"file:///C:/Users/user/project/doc/foo/struct.Foo.html"#]]),
191+
Some(&OsStr::new("/sysroot")),
186192
);
187193
}
188194

@@ -196,6 +202,7 @@ pub struct Fo$0o;
196202
Some(&OsStr::new(r"C:/Users/user/project")),
197203
Some(expect![[r#"https://docs.rs/foo/*/foo/struct.Foo.html"#]]),
198204
Some(expect![[r#"file:///C:/Users/user/project/doc/foo/struct.Foo.html"#]]),
205+
Some(&OsStr::new("/sysroot")),
199206
);
200207
}
201208

@@ -211,6 +218,7 @@ pub struct Foo {
211218
None,
212219
Some(expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#structfield.field"##]]),
213220
None,
221+
None,
214222
);
215223
}
216224

@@ -224,6 +232,7 @@ pub fn fo$0o() {}
224232
None,
225233
Some(expect![[r#"https://docs.rs/foo/*/foo/fn.foo.html"#]]),
226234
None,
235+
None,
227236
);
228237
}
229238

@@ -240,6 +249,7 @@ impl Foo {
240249
None,
241250
Some(expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#method.method"##]]),
242251
None,
252+
None,
243253
);
244254
check_external_docs(
245255
r#"
@@ -252,6 +262,7 @@ impl Foo {
252262
None,
253263
Some(expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#associatedconstant.CONST"##]]),
254264
None,
265+
None,
255266
);
256267
}
257268

@@ -271,6 +282,7 @@ impl Trait for Foo {
271282
None,
272283
Some(expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#method.method"##]]),
273284
None,
285+
None,
274286
);
275287
check_external_docs(
276288
r#"
@@ -286,6 +298,7 @@ impl Trait for Foo {
286298
None,
287299
Some(expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#associatedconstant.CONST"##]]),
288300
None,
301+
None,
289302
);
290303
check_external_docs(
291304
r#"
@@ -301,6 +314,7 @@ impl Trait for Foo {
301314
None,
302315
Some(expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#associatedtype.Type"##]]),
303316
None,
317+
None,
304318
);
305319
}
306320

@@ -316,6 +330,7 @@ pub trait Foo {
316330
None,
317331
Some(expect![[r##"https://docs.rs/foo/*/foo/trait.Foo.html#tymethod.method"##]]),
318332
None,
333+
None,
319334
);
320335
check_external_docs(
321336
r#"
@@ -327,6 +342,7 @@ pub trait Foo {
327342
None,
328343
Some(expect![[r##"https://docs.rs/foo/*/foo/trait.Foo.html#associatedconstant.CONST"##]]),
329344
None,
345+
None,
330346
);
331347
check_external_docs(
332348
r#"
@@ -338,6 +354,7 @@ pub trait Foo {
338354
None,
339355
Some(expect![[r##"https://docs.rs/foo/*/foo/trait.Foo.html#associatedtype.Type"##]]),
340356
None,
357+
None,
341358
);
342359
}
343360

@@ -351,6 +368,7 @@ trait Trait$0 {}
351368
None,
352369
Some(expect![[r#"https://docs.rs/foo/*/foo/trait.Trait.html"#]]),
353370
None,
371+
None,
354372
)
355373
}
356374

@@ -366,6 +384,7 @@ pub mod foo {
366384
None,
367385
Some(expect![[r#"https://docs.rs/foo/*/foo/foo/bar/index.html"#]]),
368386
None,
387+
None,
369388
)
370389
}
371390

@@ -389,6 +408,7 @@ fn foo() {
389408
None,
390409
Some(expect![[r#"https://docs.rs/foo/*/foo/wrapper/module/struct.Item.html"#]]),
391410
None,
411+
None,
392412
)
393413
}
394414

crates/ide/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,11 @@ impl Analysis {
469469
&self,
470470
position: FilePosition,
471471
target_dir: Option<&OsStr>,
472+
sysroot: Option<&OsStr>,
472473
) -> Cancellable<doc_links::DocumentationLinks> {
473-
self.with_db(|db| doc_links::external_docs(db, &position, target_dir).unwrap_or_default())
474+
self.with_db(|db| {
475+
doc_links::external_docs(db, &position, target_dir, sysroot).unwrap_or_default()
476+
})
474477
}
475478

476479
/// Computes parameter information at the given position.

crates/rust-analyzer/src/handlers/request.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -1530,27 +1530,34 @@ pub(crate) fn handle_semantic_tokens_range(
15301530

15311531
pub(crate) fn handle_open_docs(
15321532
snap: GlobalStateSnapshot,
1533-
params: lsp_types::TextDocumentPositionParams,
1534-
) -> Result<(Option<lsp_types::Url>, Option<lsp_types::Url>)> {
1533+
params: lsp_types::TextDocumentPositionParams,
1534+
) -> Result<(Option<lsp_types::Url>, Option<lsp_types::Url>)> {
15351535
let _p = profile::span("handle_open_docs");
1536-
let file_uri = &params.text_document.uri;
1537-
let file_id = from_proto::file_id(&snap, file_uri)?;
15381536
let position = from_proto::file_position(&snap, params)?;
15391537

1540-
let cargo = match &*snap.analysis.crates_for(file_id)? {
1541-
&[crate_id, ..] => snap.cargo_target_for_crate_root(crate_id).map(|(it, _)| it),
1542-
_ => None,
1543-
};
1538+
let ws_and_sysroot = snap.workspaces.iter().find_map(|ws| match ws {
1539+
ProjectWorkspace::Cargo { cargo, sysroot, .. } => Some((cargo, sysroot.as_ref().ok())),
1540+
ProjectWorkspace::Json { .. } => None,
1541+
ProjectWorkspace::DetachedFiles { .. } => None,
1542+
});
15441543

1544+
let (cargo, sysroot) = match ws_and_sysroot {
1545+
Some((ws, Some(sysroot))) => (Some(ws), Some(sysroot)),
1546+
_ => (None, None),
1547+
};
1548+
1549+
let sysroot = sysroot.map(|p| p.root().as_os_str());
15451550
let target_dir = cargo.map(|cargo| cargo.target_directory()).map(|p| p.as_os_str());
1546-
let Ok(remote_urls) = snap.analysis.external_docs(position, target_dir) else { return Ok((None, None)); };
1551+
1552+
let Ok(remote_urls) = snap.analysis.external_docs(position, target_dir, sysroot) else { return Ok((None, None)); };
15471553

15481554
let web_url = remote_urls.web_url.and_then(|it| Url::parse(&it).ok());
15491555
let local_url = remote_urls.local_url.and_then(|it| Url::parse(&it).ok());
15501556

15511557
Ok((web_url, local_url))
15521558
}
15531559

1560+
15541561
pub(crate) fn handle_open_cargo_toml(
15551562
snap: GlobalStateSnapshot,
15561563
params: lsp_ext::OpenCargoTomlParams,

0 commit comments

Comments
 (0)