This commit is contained in:
Lukas Wirth 2022-04-10 12:42:16 +02:00
parent e691ae0ab2
commit 7f3572fa0d
7 changed files with 54 additions and 35 deletions

View file

@ -135,6 +135,33 @@ pub enum LangCrateOrigin {
Other,
}
impl From<&str> for LangCrateOrigin {
fn from(s: &str) -> Self {
match s {
"alloc" => LangCrateOrigin::Alloc,
"core" => LangCrateOrigin::Core,
"proc-macro" => LangCrateOrigin::ProcMacro,
"std" => LangCrateOrigin::Std,
"test" => LangCrateOrigin::Test,
_ => LangCrateOrigin::Other,
}
}
}
impl fmt::Display for LangCrateOrigin {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let text = match self {
LangCrateOrigin::Alloc => "alloc",
LangCrateOrigin::Core => "core",
LangCrateOrigin::ProcMacro => "proc_macro",
LangCrateOrigin::Std => "std",
LangCrateOrigin::Test => "test",
LangCrateOrigin::Other => "other",
};
f.write_str(text)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CrateDisplayName {
// The name we use to display various paths (with `_`).

View file

@ -12,6 +12,7 @@ use url::Url;
use hir::{db::HirDatabase, Adt, AsAssocItem, AssocItem, AssocItemContainer, Crate, HasAttrs};
use ide_db::{
base_db::{CrateOrigin, LangCrateOrigin, SourceDatabase},
defs::{Definition, NameClass, NameRefClass},
helpers::pick_best_token,
RootDatabase,
@ -293,7 +294,7 @@ fn get_doc_link(db: &RootDatabase, def: Definition) -> Option<String> {
let (target, file, frag) = filename_and_frag_for_def(db, def)?;
let krate = crate_of_def(db, target)?;
let mut url = get_doc_base_url(db, &krate)?;
let mut url = get_doc_base_url(db, krate)?;
if let Some(path) = mod_path_of_def(db, target) {
url = url.join(&path).ok()?;
@ -315,7 +316,7 @@ fn rewrite_intra_doc_link(
let resolved = resolve_doc_path_for_def(db, def, link, ns)?;
let krate = crate_of_def(db, resolved)?;
let mut url = get_doc_base_url(db, &krate)?;
let mut url = get_doc_base_url(db, krate)?;
let (_, file, frag) = filename_and_frag_for_def(db, resolved)?;
if let Some(path) = mod_path_of_def(db, resolved) {
@ -335,7 +336,7 @@ fn rewrite_url_link(db: &RootDatabase, def: Definition, target: &str) -> Option<
}
let krate = crate_of_def(db, def)?;
let mut url = get_doc_base_url(db, &krate)?;
let mut url = get_doc_base_url(db, krate)?;
let (def, file, frag) = filename_and_frag_for_def(db, def)?;
if let Some(path) = mod_path_of_def(db, def) {
@ -406,13 +407,20 @@ fn map_links<'e>(
/// https://doc.rust-lang.org/std/iter/trait.Iterator.html#tymethod.next
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^
/// ```
fn get_doc_base_url(db: &RootDatabase, krate: &Crate) -> Option<Url> {
fn get_doc_base_url(db: &RootDatabase, krate: Crate) -> Option<Url> {
let display_name = krate.display_name(db)?;
let base = match &**display_name.crate_name() {
let base = match db.crate_graph()[krate.into()].origin {
// std and co do not specify `html_root_url` any longer so we gotta handwrite this ourself.
// FIXME: Use the toolchains channel instead of nightly
name @ ("core" | "std" | "alloc" | "proc_macro" | "test") => {
format!("https://doc.rust-lang.org/nightly/{}", name)
CrateOrigin::Lang(
origin @ (LangCrateOrigin::Alloc
| LangCrateOrigin::Core
| LangCrateOrigin::ProcMacro
| LangCrateOrigin::Std
| LangCrateOrigin::Test),
) => {
format!("https://doc.rust-lang.org/nightly/{origin}")
}
_ => {
krate.get_html_root_url(db).or_else(|| {

View file

@ -4531,7 +4531,7 @@ foo_macro!(
---
Doc comment for [`Foo`](https://doc.rust-lang.org/nightly/test/struct.Foo.html)
Doc comment for [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
"#]],
);
}
@ -4556,7 +4556,7 @@ pub struct Foo;
---
Doc comment for [`Foo`](https://doc.rust-lang.org/nightly/test/struct.Foo.html)
Doc comment for [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
"#]],
);
}

View file

@ -153,17 +153,12 @@ pub(crate) fn def_to_moniker(
CrateOrigin::CratesIo { repo } => (repo?, krate.version(db)?),
CrateOrigin::Lang(lang) => (
"https://github.com/rust-lang/rust/".to_string(),
format!(
"https://github.com/rust-lang/rust/library/{}",
match lang {
LangCrateOrigin::Alloc => "alloc",
LangCrateOrigin::Core => "core",
LangCrateOrigin::ProcMacro => "proc_macro",
LangCrateOrigin::Std => "std",
LangCrateOrigin::Test => "test",
LangCrateOrigin::Other => "",
match lang {
LangCrateOrigin::Other => {
"https://github.com/rust-lang/rust/library/".into()
}
),
lang => format!("https://github.com/rust-lang/rust/library/{lang}",),
},
),
};
PackageInformation { name, repo, version }

View file

@ -158,13 +158,9 @@ impl FamousDefs<'_, '_> {
let mut path = path.split(':');
let trait_ = path.next_back()?;
let lang_crate = path.next()?;
let lang_crate = match lang_crate {
"core" => LangCrateOrigin::Core,
"alloc" => LangCrateOrigin::Alloc,
"test" => LangCrateOrigin::Test,
"proc_macro" => LangCrateOrigin::ProcMacro,
"std" => LangCrateOrigin::Std,
_ => return None,
let lang_crate = match LangCrateOrigin::from(lang_crate) {
LangCrateOrigin::Other => return None,
lang_crate => lang_crate,
};
let std_crate = self.find_lang_crate(lang_crate)?;
let mut module = std_crate.root_module(db);

View file

@ -1368,7 +1368,7 @@ fn rust_project_hello_world_project_model() {
],
proc_macro: [],
origin: Lang(
ProcMacro,
Other,
),
is_proc_macro: false,
},

View file

@ -908,14 +908,7 @@ fn sysroot_to_crate_graph(
env,
proc_macro,
false,
CrateOrigin::Lang(match &*sysroot[krate].name {
"alloc" => LangCrateOrigin::Alloc,
"core" => LangCrateOrigin::Core,
"proc_macro" => LangCrateOrigin::ProcMacro,
"std" => LangCrateOrigin::Std,
"test" => LangCrateOrigin::Test,
_ => LangCrateOrigin::Other,
}),
CrateOrigin::Lang(LangCrateOrigin::from(&*sysroot[krate].name)),
);
Some((krate, crate_id))
})