Propagating sysroot
down + Refactoring
This commit is contained in:
parent
67e58c5ba6
commit
4ac39f0c98
@ -133,6 +133,7 @@ pub(crate) fn external_docs(
|
||||
db: &RootDatabase,
|
||||
position: &FilePosition,
|
||||
target_dir: Option<&OsStr>,
|
||||
sysroot: Option<&OsStr>,
|
||||
) -> Option<DocumentationLinks> {
|
||||
let sema = &Semantics::new(db);
|
||||
let file = sema.parse(position.file_id).syntax().clone();
|
||||
@ -163,7 +164,7 @@ pub(crate) fn external_docs(
|
||||
}
|
||||
};
|
||||
|
||||
Some(get_doc_links(db, definition, target_dir))
|
||||
Some(get_doc_links(db, definition, target_dir, sysroot))
|
||||
}
|
||||
|
||||
/// Extracts all links from a given markdown text returning the definition text range, link-text
|
||||
@ -325,6 +326,7 @@ fn get_doc_links(
|
||||
db: &RootDatabase,
|
||||
def: Definition,
|
||||
target_dir: Option<&OsStr>,
|
||||
sysroot: Option<&OsStr>,
|
||||
) -> DocumentationLinks {
|
||||
let join_url = |base_url: Option<Url>, path: &str| -> Option<Url> {
|
||||
base_url.and_then(|url| url.join(path).ok())
|
||||
@ -332,7 +334,7 @@ fn get_doc_links(
|
||||
|
||||
let Some((target, file, frag)) = filename_and_frag_for_def(db, def) else { return Default::default(); };
|
||||
|
||||
let (mut web_url, mut local_url) = get_doc_base_urls(db, target, target_dir);
|
||||
let (mut web_url, mut local_url) = get_doc_base_urls(db, target, target_dir, sysroot);
|
||||
|
||||
if let Some(path) = mod_path_of_def(db, target) {
|
||||
web_url = join_url(web_url, &path);
|
||||
@ -360,7 +362,7 @@ fn rewrite_intra_doc_link(
|
||||
let (link, ns) = parse_intra_doc_link(target);
|
||||
|
||||
let resolved = resolve_doc_path_for_def(db, def, link, ns)?;
|
||||
let mut url = get_doc_base_urls(db, resolved, None).0?;
|
||||
let mut url = get_doc_base_urls(db, resolved, None, None).0?;
|
||||
|
||||
let (_, file, frag) = filename_and_frag_for_def(db, resolved)?;
|
||||
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<
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut url = get_doc_base_urls(db, def, None).0?;
|
||||
let mut url = get_doc_base_urls(db, def, None, None).0?;
|
||||
let (def, file, frag) = filename_and_frag_for_def(db, def)?;
|
||||
|
||||
if let Some(path) = mod_path_of_def(db, def) {
|
||||
@ -461,27 +463,29 @@ fn get_doc_base_urls(
|
||||
db: &RootDatabase,
|
||||
def: Definition,
|
||||
target_dir: Option<&OsStr>,
|
||||
sysroot: Option<&OsStr>,
|
||||
) -> (Option<Url>, Option<Url>) {
|
||||
let local_doc_path = target_dir
|
||||
.and_then(|path: &OsStr| -> Option<Url> {
|
||||
let mut with_prefix = OsStr::new("file:///").to_os_string();
|
||||
with_prefix.push(path);
|
||||
with_prefix.push("/");
|
||||
with_prefix.to_str().and_then(|s| Url::parse(s).ok())
|
||||
})
|
||||
let local_doc = target_dir
|
||||
.and_then(|path| path.to_str())
|
||||
.and_then(|path| Url::parse(&format!("file:///{path}/")).ok())
|
||||
.and_then(|it| it.join("doc/").ok());
|
||||
let system_doc = sysroot
|
||||
.and_then(|it| it.to_str())
|
||||
.map(|sysroot| format!("file:///{sysroot}/share/doc/rust/html/"))
|
||||
.and_then(|it| Url::parse(&it).ok());
|
||||
|
||||
// special case base url of `BuiltinType` to core
|
||||
// https://github.com/rust-lang/rust-analyzer/issues/12250
|
||||
if let Definition::BuiltinType(..) = def {
|
||||
let weblink = Url::parse("https://doc.rust-lang.org/nightly/core/").ok();
|
||||
return (weblink, None);
|
||||
let web_link = Url::parse("https://doc.rust-lang.org/nightly/core/").ok();
|
||||
let system_link = system_doc.and_then(|it| it.join("core/").ok());
|
||||
return (web_link, system_link);
|
||||
};
|
||||
|
||||
let Some(krate) = def.krate(db) else { return Default::default() };
|
||||
let Some(display_name) = krate.display_name(db) else { return Default::default() };
|
||||
let crate_data = &db.crate_graph()[krate.into()];
|
||||
let channel = crate_data.channel.map_or("nightly", ReleaseChannel::as_str);
|
||||
let sysroot = "/home/ddystopia/.rustup/toolchains/stable-x86_64-unknown-linux-gnu";
|
||||
|
||||
let (web_base, local_base) = match &crate_data.origin {
|
||||
// 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(
|
||||
| LangCrateOrigin::Std
|
||||
| LangCrateOrigin::Test),
|
||||
) => {
|
||||
let local_url = format!("file:///{sysroot}/share/doc/rust/html/{origin}/index.html");
|
||||
let local_url = Url::parse(&local_url).ok();
|
||||
let system_url = system_doc.and_then(|it| it.join(&format!("{origin}")).ok());
|
||||
let web_url = format!("https://doc.rust-lang.org/{channel}/{origin}");
|
||||
println!("local_url: {:?}", local_url.unwrap().to_string());
|
||||
panic!();
|
||||
(Some(web_url), local_url)
|
||||
},
|
||||
(Some(web_url), system_url)
|
||||
}
|
||||
CrateOrigin::Lang(_) => return (None, None),
|
||||
CrateOrigin::Rustc { name: _ } => {
|
||||
(Some(format!("https://doc.rust-lang.org/{channel}/nightly-rustc/")), None)
|
||||
@ -519,7 +520,7 @@ fn get_doc_base_urls(
|
||||
version = version.as_deref().unwrap_or("*")
|
||||
))
|
||||
});
|
||||
(weblink, local_doc_path)
|
||||
(weblink, local_doc)
|
||||
}
|
||||
CrateOrigin::Library { repo: _, name } => {
|
||||
let weblink = krate.get_html_root_url(db).or_else(|| {
|
||||
@ -535,7 +536,7 @@ fn get_doc_base_urls(
|
||||
version = version.as_deref().unwrap_or("*")
|
||||
))
|
||||
});
|
||||
(weblink, local_doc_path)
|
||||
(weblink, local_doc)
|
||||
}
|
||||
};
|
||||
let web_base = web_base
|
||||
|
@ -20,9 +20,10 @@ fn check_external_docs(
|
||||
target_dir: Option<&OsStr>,
|
||||
expect_web_url: Option<Expect>,
|
||||
expect_local_url: Option<Expect>,
|
||||
sysroot: Option<&OsStr>,
|
||||
) {
|
||||
let (analysis, position) = fixture::position(ra_fixture);
|
||||
let links = analysis.external_docs(position, target_dir).unwrap();
|
||||
let links = analysis.external_docs(position, target_dir, sysroot).unwrap();
|
||||
|
||||
let web_url = links.web_url;
|
||||
let local_url = links.local_url;
|
||||
@ -128,7 +129,8 @@ fn external_docs_doc_builtin_type() {
|
||||
"#,
|
||||
Some(&OsStr::new("/home/user/project")),
|
||||
Some(expect![[r#"https://doc.rust-lang.org/nightly/core/primitive.u32.html"#]]),
|
||||
None,
|
||||
Some(expect![[r#"file:///sysroot/share/doc/rust/html/core/primitive.u32.html"#]]),
|
||||
Some(&OsStr::new("/sysroot")),
|
||||
);
|
||||
}
|
||||
|
||||
@ -144,6 +146,7 @@ fn external_docs_doc_url_crate() {
|
||||
Some(&OsStr::new("/home/user/project")),
|
||||
Some(expect![[r#"https://docs.rs/foo/*/foo/index.html"#]]),
|
||||
Some(expect![[r#"file:///home/user/project/doc/foo/index.html"#]]),
|
||||
Some(&OsStr::new("/sysroot")),
|
||||
);
|
||||
}
|
||||
|
||||
@ -156,7 +159,8 @@ fn external_docs_doc_url_std_crate() {
|
||||
"#,
|
||||
Some(&OsStr::new("/home/user/project")),
|
||||
Some(expect!["https://doc.rust-lang.org/stable/std/index.html"]),
|
||||
None,
|
||||
Some(expect!["file:///sysroot/share/doc/rust/html/std/index.html"]),
|
||||
Some(&OsStr::new("/sysroot")),
|
||||
);
|
||||
}
|
||||
|
||||
@ -170,6 +174,7 @@ fn external_docs_doc_url_struct() {
|
||||
Some(&OsStr::new("/home/user/project")),
|
||||
Some(expect![[r#"https://docs.rs/foo/*/foo/struct.Foo.html"#]]),
|
||||
Some(expect![[r#"file:///home/user/project/doc/foo/struct.Foo.html"#]]),
|
||||
Some(&OsStr::new("/sysroot")),
|
||||
);
|
||||
}
|
||||
|
||||
@ -183,6 +188,7 @@ fn external_docs_doc_url_windows_backslash_path() {
|
||||
Some(&OsStr::new(r"C:\Users\user\project")),
|
||||
Some(expect![[r#"https://docs.rs/foo/*/foo/struct.Foo.html"#]]),
|
||||
Some(expect![[r#"file:///C:/Users/user/project/doc/foo/struct.Foo.html"#]]),
|
||||
Some(&OsStr::new("/sysroot")),
|
||||
);
|
||||
}
|
||||
|
||||
@ -196,6 +202,7 @@ fn external_docs_doc_url_windows_slash_path() {
|
||||
Some(&OsStr::new(r"C:/Users/user/project")),
|
||||
Some(expect![[r#"https://docs.rs/foo/*/foo/struct.Foo.html"#]]),
|
||||
Some(expect![[r#"file:///C:/Users/user/project/doc/foo/struct.Foo.html"#]]),
|
||||
Some(&OsStr::new("/sysroot")),
|
||||
);
|
||||
}
|
||||
|
||||
@ -211,6 +218,7 @@ pub struct Foo {
|
||||
None,
|
||||
Some(expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#structfield.field"##]]),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
@ -224,6 +232,7 @@ pub fn fo$0o() {}
|
||||
None,
|
||||
Some(expect![[r#"https://docs.rs/foo/*/foo/fn.foo.html"#]]),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
@ -240,6 +249,7 @@ pub fn method$0() {}
|
||||
None,
|
||||
Some(expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#method.method"##]]),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
check_external_docs(
|
||||
r#"
|
||||
@ -252,6 +262,7 @@ impl Foo {
|
||||
None,
|
||||
Some(expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#associatedconstant.CONST"##]]),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
@ -271,6 +282,7 @@ pub fn method$0() {}
|
||||
None,
|
||||
Some(expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#method.method"##]]),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
check_external_docs(
|
||||
r#"
|
||||
@ -286,6 +298,7 @@ impl Trait for Foo {
|
||||
None,
|
||||
Some(expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#associatedconstant.CONST"##]]),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
check_external_docs(
|
||||
r#"
|
||||
@ -301,6 +314,7 @@ impl Trait for Foo {
|
||||
None,
|
||||
Some(expect![[r##"https://docs.rs/foo/*/foo/struct.Foo.html#associatedtype.Type"##]]),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
@ -316,6 +330,7 @@ pub trait Foo {
|
||||
None,
|
||||
Some(expect![[r##"https://docs.rs/foo/*/foo/trait.Foo.html#tymethod.method"##]]),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
check_external_docs(
|
||||
r#"
|
||||
@ -327,6 +342,7 @@ pub trait Foo {
|
||||
None,
|
||||
Some(expect![[r##"https://docs.rs/foo/*/foo/trait.Foo.html#associatedconstant.CONST"##]]),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
check_external_docs(
|
||||
r#"
|
||||
@ -338,6 +354,7 @@ pub trait Foo {
|
||||
None,
|
||||
Some(expect![[r##"https://docs.rs/foo/*/foo/trait.Foo.html#associatedtype.Type"##]]),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
@ -351,6 +368,7 @@ trait Trait$0 {}
|
||||
None,
|
||||
Some(expect![[r#"https://docs.rs/foo/*/foo/trait.Trait.html"#]]),
|
||||
None,
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
@ -366,6 +384,7 @@ pub mod ba$0r {}
|
||||
None,
|
||||
Some(expect![[r#"https://docs.rs/foo/*/foo/foo/bar/index.html"#]]),
|
||||
None,
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
@ -389,6 +408,7 @@ fn foo() {
|
||||
None,
|
||||
Some(expect![[r#"https://docs.rs/foo/*/foo/wrapper/module/struct.Item.html"#]]),
|
||||
None,
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -475,8 +475,11 @@ pub fn external_docs(
|
||||
&self,
|
||||
position: FilePosition,
|
||||
target_dir: Option<&OsStr>,
|
||||
sysroot: Option<&OsStr>,
|
||||
) -> Cancellable<doc_links::DocumentationLinks> {
|
||||
self.with_db(|db| doc_links::external_docs(db, &position, target_dir).unwrap_or_default())
|
||||
self.with_db(|db| {
|
||||
doc_links::external_docs(db, &position, target_dir, sysroot).unwrap_or_default()
|
||||
})
|
||||
}
|
||||
|
||||
/// Computes parameter information at the given position.
|
||||
|
@ -1534,20 +1534,26 @@ pub(crate) fn handle_semantic_tokens_range(
|
||||
|
||||
pub(crate) fn handle_open_docs(
|
||||
snap: GlobalStateSnapshot,
|
||||
params: lsp_types::TextDocumentPositionParams,
|
||||
) -> Result<(Option<lsp_types::Url>, Option<lsp_types::Url>)> {
|
||||
params: lsp_types::TextDocumentPositionParams,
|
||||
) -> Result<(Option<lsp_types::Url>, Option<lsp_types::Url>)> {
|
||||
let _p = profile::span("handle_open_docs");
|
||||
let file_uri = ¶ms.text_document.uri;
|
||||
let file_id = from_proto::file_id(&snap, file_uri)?;
|
||||
let position = from_proto::file_position(&snap, params)?;
|
||||
|
||||
let cargo = match &*snap.analysis.crates_for(file_id)? {
|
||||
&[crate_id, ..] => snap.cargo_target_for_crate_root(crate_id).map(|(it, _)| it),
|
||||
_ => None,
|
||||
};
|
||||
let ws_and_sysroot = snap.workspaces.iter().find_map(|ws| match ws {
|
||||
ProjectWorkspace::Cargo { cargo, sysroot, .. } => Some((cargo, sysroot.as_ref().ok())),
|
||||
ProjectWorkspace::Json { .. } => None,
|
||||
ProjectWorkspace::DetachedFiles { .. } => None,
|
||||
});
|
||||
|
||||
let (cargo, sysroot) = match ws_and_sysroot {
|
||||
Some((ws, Some(sysroot))) => (Some(ws), Some(sysroot)),
|
||||
_ => (None, None),
|
||||
};
|
||||
|
||||
let sysroot = sysroot.map(|p| p.root().as_os_str());
|
||||
let target_dir = cargo.map(|cargo| cargo.target_directory()).map(|p| p.as_os_str());
|
||||
let Ok(remote_urls) = snap.analysis.external_docs(position, target_dir) else { return Ok((None, None)); };
|
||||
|
||||
let Ok(remote_urls) = snap.analysis.external_docs(position, target_dir, sysroot) else { return Ok((None, None)); };
|
||||
|
||||
let web_url = remote_urls.web_url.and_then(|it| Url::parse(&it).ok());
|
||||
let local_url = remote_urls.local_url.and_then(|it| Url::parse(&it).ok());
|
||||
@ -1555,6 +1561,7 @@ pub(crate) fn handle_open_docs(
|
||||
Ok((web_url, local_url))
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn handle_open_cargo_toml(
|
||||
snap: GlobalStateSnapshot,
|
||||
params: lsp_ext::OpenCargoTomlParams,
|
||||
|
Loading…
Reference in New Issue
Block a user