10667: internal: Expose version string of crates from HIR r=Veykril a=Veykril

Fixes https://github.com/rust-analyzer/rust-analyzer/issues/10664

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-11-03 14:50:39 +00:00 committed by GitHub
commit 0bd2d234a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 111 additions and 1 deletions

View File

@ -136,6 +136,7 @@ pub fn parse(ra_fixture: &str) -> ChangeFixture {
file_id,
meta.edition,
Some(crate_name.clone().into()),
None,
meta.cfg.clone(),
meta.cfg,
meta.env,
@ -168,6 +169,7 @@ pub fn parse(ra_fixture: &str) -> ChangeFixture {
crate_root,
Edition::CURRENT,
Some(CrateName::new("test").unwrap().into()),
None,
default_cfg.clone(),
default_cfg,
Env::default(),
@ -202,6 +204,7 @@ pub fn parse(ra_fixture: &str) -> ChangeFixture {
core_file,
Edition::Edition2021,
Some(CrateDisplayName::from_canonical_name("core".to_string())),
None,
CfgOptions::default(),
CfgOptions::default(),
Env::default(),
@ -235,6 +238,7 @@ pub fn parse(ra_fixture: &str) -> ChangeFixture {
proc_lib_file,
Edition::Edition2021,
Some(CrateDisplayName::from_canonical_name("proc_macros".to_string())),
None,
CfgOptions::default(),
CfgOptions::default(),
Env::default(),

View File

@ -192,6 +192,7 @@ pub struct ProcMacro {
pub struct CrateData {
pub root_file_id: FileId,
pub edition: Edition,
pub version: Option<String>,
/// A name used in the package's project declaration: for Cargo projects,
/// its `[package].name` can be different for other project types or even
/// absent (a dummy crate for the code snippet, for example).
@ -250,6 +251,7 @@ pub fn add_crate_root(
file_id: FileId,
edition: Edition,
display_name: Option<CrateDisplayName>,
version: Option<String>,
cfg_options: CfgOptions,
potential_cfg_options: CfgOptions,
env: Env,
@ -258,6 +260,7 @@ pub fn add_crate_root(
let data = CrateData {
root_file_id: file_id,
edition,
version,
display_name,
cfg_options,
potential_cfg_options,
@ -563,6 +566,7 @@ fn detect_cyclic_dependency_indirect() {
FileId(1u32),
Edition2018,
None,
None,
CfgOptions::default(),
CfgOptions::default(),
Env::default(),
@ -572,6 +576,7 @@ fn detect_cyclic_dependency_indirect() {
FileId(2u32),
Edition2018,
None,
None,
CfgOptions::default(),
CfgOptions::default(),
Env::default(),
@ -581,6 +586,7 @@ fn detect_cyclic_dependency_indirect() {
FileId(3u32),
Edition2018,
None,
None,
CfgOptions::default(),
CfgOptions::default(),
Env::default(),
@ -604,6 +610,7 @@ fn detect_cyclic_dependency_direct() {
FileId(1u32),
Edition2018,
None,
None,
CfgOptions::default(),
CfgOptions::default(),
Env::default(),
@ -613,6 +620,7 @@ fn detect_cyclic_dependency_direct() {
FileId(2u32),
Edition2018,
None,
None,
CfgOptions::default(),
CfgOptions::default(),
Env::default(),
@ -633,6 +641,7 @@ fn it_works() {
FileId(1u32),
Edition2018,
None,
None,
CfgOptions::default(),
CfgOptions::default(),
Env::default(),
@ -642,6 +651,7 @@ fn it_works() {
FileId(2u32),
Edition2018,
None,
None,
CfgOptions::default(),
CfgOptions::default(),
Env::default(),
@ -651,6 +661,7 @@ fn it_works() {
FileId(3u32),
Edition2018,
None,
None,
CfgOptions::default(),
CfgOptions::default(),
Env::default(),
@ -671,6 +682,7 @@ fn dashes_are_normalized() {
FileId(1u32),
Edition2018,
None,
None,
CfgOptions::default(),
CfgOptions::default(),
Env::default(),
@ -680,6 +692,7 @@ fn dashes_are_normalized() {
FileId(2u32),
Edition2018,
None,
None,
CfgOptions::default(),
CfgOptions::default(),
Env::default(),

View File

@ -183,6 +183,10 @@ pub fn edition(self, db: &dyn HirDatabase) -> Edition {
db.crate_graph()[self.id].edition
}
pub fn version(self, db: &dyn HirDatabase) -> Option<String> {
db.crate_graph()[self.id].version.clone()
}
pub fn display_name(self, db: &dyn HirDatabase) -> Option<CrateDisplayName> {
db.crate_graph()[self.id].display_name.clone()
}

View File

@ -473,12 +473,17 @@ fn get_doc_base_url(db: &RootDatabase, krate: &Crate) -> Option<Url> {
}
_ => {
krate.get_html_root_url(db).or_else(|| {
let version = krate.version(db);
// Fallback to docs.rs. This uses `display_name` and can never be
// correct, but that's what fallbacks are about.
//
// FIXME: clicking on the link should just open the file in the editor,
// instead of falling back to external urls.
Some(format!("https://docs.rs/{krate}/*/", krate = display_name))
Some(format!(
"https://docs.rs/{krate}/{version}/",
krate = display_name,
version = version.as_deref().unwrap_or("*")
))
})?
}
};

View File

@ -219,6 +219,7 @@ pub fn from_single_file(text: String) -> (Analysis, FileId) {
file_id,
Edition::CURRENT,
None,
None,
cfg_options.clone(),
cfg_options,
Env::default(),

View File

@ -29,6 +29,7 @@ pub struct Crate {
pub(crate) display_name: Option<CrateDisplayName>,
pub(crate) root_module: AbsPathBuf,
pub(crate) edition: Edition,
pub(crate) version: Option<String>,
pub(crate) deps: Vec<Dependency>,
pub(crate) cfg: Vec<CfgFlag>,
pub(crate) target: Option<String>,
@ -80,6 +81,7 @@ pub fn new(base: &AbsPath, data: ProjectJsonData) -> ProjectJson {
.map(CrateDisplayName::from_canonical_name),
root_module,
edition: crate_data.edition.into(),
version: crate_data.version.as_ref().map(ToString::to_string),
deps: crate_data
.deps
.into_iter()
@ -127,6 +129,8 @@ struct CrateData {
display_name: Option<String>,
root_module: PathBuf,
edition: EditionData,
#[serde(default)]
version: Option<semver::Version>,
deps: Vec<DepData>,
#[serde(default)]
cfg: Vec<CfgFlag>,

View File

@ -121,6 +121,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
1,
),
edition: Edition2018,
version: Some(
"0.1.0",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -178,6 +181,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
6,
),
edition: Edition2015,
version: Some(
"0.2.98",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -244,6 +250,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
3,
),
edition: Edition2018,
version: Some(
"0.1.0",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -310,6 +319,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
5,
),
edition: Edition2015,
version: Some(
"0.2.98",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -366,6 +378,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
2,
),
edition: Edition2018,
version: Some(
"0.1.0",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -432,6 +447,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
7,
),
edition: Edition2015,
version: Some(
"0.2.98",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -488,6 +506,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
4,
),
edition: Edition2018,
version: Some(
"0.1.0",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -576,6 +597,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
1,
),
edition: Edition2018,
version: Some(
"0.1.0",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -635,6 +659,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
6,
),
edition: Edition2015,
version: Some(
"0.2.98",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -701,6 +728,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
3,
),
edition: Edition2018,
version: Some(
"0.1.0",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -769,6 +799,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
5,
),
edition: Edition2015,
version: Some(
"0.2.98",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -825,6 +858,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
2,
),
edition: Edition2018,
version: Some(
"0.1.0",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -893,6 +929,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
7,
),
edition: Edition2015,
version: Some(
"0.2.98",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -949,6 +988,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
4,
),
edition: Edition2018,
version: Some(
"0.1.0",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1030,6 +1072,9 @@ fn cargo_hello_world_project_model() {
1,
),
edition: Edition2018,
version: Some(
"0.1.0",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1089,6 +1134,9 @@ fn cargo_hello_world_project_model() {
6,
),
edition: Edition2015,
version: Some(
"0.2.98",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1157,6 +1205,9 @@ fn cargo_hello_world_project_model() {
3,
),
edition: Edition2018,
version: Some(
"0.1.0",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1225,6 +1276,9 @@ fn cargo_hello_world_project_model() {
5,
),
edition: Edition2015,
version: Some(
"0.2.98",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1283,6 +1337,9 @@ fn cargo_hello_world_project_model() {
2,
),
edition: Edition2018,
version: Some(
"0.1.0",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1351,6 +1408,9 @@ fn cargo_hello_world_project_model() {
7,
),
edition: Edition2015,
version: Some(
"0.2.98",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1409,6 +1469,9 @@ fn cargo_hello_world_project_model() {
4,
),
edition: Edition2018,
version: Some(
"0.1.0",
),
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1490,6 +1553,7 @@ fn rust_project_hello_world_project_model() {
1,
),
edition: Edition2018,
version: None,
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1527,6 +1591,7 @@ fn rust_project_hello_world_project_model() {
11,
),
edition: Edition2018,
version: None,
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1554,6 +1619,7 @@ fn rust_project_hello_world_project_model() {
8,
),
edition: Edition2018,
version: None,
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1581,6 +1647,7 @@ fn rust_project_hello_world_project_model() {
5,
),
edition: Edition2018,
version: None,
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1618,6 +1685,7 @@ fn rust_project_hello_world_project_model() {
2,
),
edition: Edition2018,
version: None,
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1645,6 +1713,7 @@ fn rust_project_hello_world_project_model() {
12,
),
edition: Edition2018,
version: None,
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1709,6 +1778,7 @@ fn rust_project_hello_world_project_model() {
9,
),
edition: Edition2018,
version: None,
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1736,6 +1806,7 @@ fn rust_project_hello_world_project_model() {
6,
),
edition: Edition2018,
version: None,
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1763,6 +1834,7 @@ fn rust_project_hello_world_project_model() {
3,
),
edition: Edition2018,
version: None,
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1790,6 +1862,7 @@ fn rust_project_hello_world_project_model() {
10,
),
edition: Edition2018,
version: None,
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1817,6 +1890,7 @@ fn rust_project_hello_world_project_model() {
7,
),
edition: Edition2018,
version: None,
display_name: Some(
CrateDisplayName {
crate_name: CrateName(
@ -1926,6 +2000,7 @@ fn rust_project_hello_world_project_model() {
4,
),
edition: Edition2018,
version: None,
display_name: Some(
CrateDisplayName {
crate_name: CrateName(

View File

@ -468,6 +468,7 @@ fn project_json_to_crate_graph(
file_id,
krate.edition,
krate.display_name.clone(),
krate.version.clone(),
cfg_options.clone(),
cfg_options,
env,
@ -675,6 +676,7 @@ fn detached_files_to_crate_graph(
file_id,
Edition::CURRENT,
display_name,
None,
cfg_options.clone(),
cfg_options.clone(),
Env::default(),
@ -824,6 +826,7 @@ fn add_target_crate_root(
file_id,
edition,
Some(display_name),
Some(pkg.version.to_string()),
cfg_options,
potential_cfg_options,
env,
@ -866,6 +869,7 @@ fn sysroot_to_crate_graph(
file_id,
Edition::CURRENT,
Some(display_name),
None,
cfg_options.clone(),
cfg_options.clone(),
env,