Skip to content

Commit 0bd2d23

Browse files
bors[bot]Veykril
andauthored
Merge #10667
10667: internal: Expose version string of crates from HIR r=Veykril a=Veykril Fixes #10664 Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 53b5b2f + 52f5af7 commit 0bd2d23

File tree

8 files changed

+111
-1
lines changed

8 files changed

+111
-1
lines changed

crates/base_db/src/fixture.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ impl ChangeFixture {
136136
file_id,
137137
meta.edition,
138138
Some(crate_name.clone().into()),
139+
None,
139140
meta.cfg.clone(),
140141
meta.cfg,
141142
meta.env,
@@ -168,6 +169,7 @@ impl ChangeFixture {
168169
crate_root,
169170
Edition::CURRENT,
170171
Some(CrateName::new("test").unwrap().into()),
172+
None,
171173
default_cfg.clone(),
172174
default_cfg,
173175
Env::default(),
@@ -202,6 +204,7 @@ impl ChangeFixture {
202204
core_file,
203205
Edition::Edition2021,
204206
Some(CrateDisplayName::from_canonical_name("core".to_string())),
207+
None,
205208
CfgOptions::default(),
206209
CfgOptions::default(),
207210
Env::default(),
@@ -235,6 +238,7 @@ impl ChangeFixture {
235238
proc_lib_file,
236239
Edition::Edition2021,
237240
Some(CrateDisplayName::from_canonical_name("proc_macros".to_string())),
241+
None,
238242
CfgOptions::default(),
239243
CfgOptions::default(),
240244
Env::default(),

crates/base_db/src/input.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ pub struct ProcMacro {
192192
pub struct CrateData {
193193
pub root_file_id: FileId,
194194
pub edition: Edition,
195+
pub version: Option<String>,
195196
/// A name used in the package's project declaration: for Cargo projects,
196197
/// its `[package].name` can be different for other project types or even
197198
/// absent (a dummy crate for the code snippet, for example).
@@ -250,6 +251,7 @@ impl CrateGraph {
250251
file_id: FileId,
251252
edition: Edition,
252253
display_name: Option<CrateDisplayName>,
254+
version: Option<String>,
253255
cfg_options: CfgOptions,
254256
potential_cfg_options: CfgOptions,
255257
env: Env,
@@ -258,6 +260,7 @@ impl CrateGraph {
258260
let data = CrateData {
259261
root_file_id: file_id,
260262
edition,
263+
version,
261264
display_name,
262265
cfg_options,
263266
potential_cfg_options,
@@ -563,6 +566,7 @@ mod tests {
563566
FileId(1u32),
564567
Edition2018,
565568
None,
569+
None,
566570
CfgOptions::default(),
567571
CfgOptions::default(),
568572
Env::default(),
@@ -572,6 +576,7 @@ mod tests {
572576
FileId(2u32),
573577
Edition2018,
574578
None,
579+
None,
575580
CfgOptions::default(),
576581
CfgOptions::default(),
577582
Env::default(),
@@ -581,6 +586,7 @@ mod tests {
581586
FileId(3u32),
582587
Edition2018,
583588
None,
589+
None,
584590
CfgOptions::default(),
585591
CfgOptions::default(),
586592
Env::default(),
@@ -604,6 +610,7 @@ mod tests {
604610
FileId(1u32),
605611
Edition2018,
606612
None,
613+
None,
607614
CfgOptions::default(),
608615
CfgOptions::default(),
609616
Env::default(),
@@ -613,6 +620,7 @@ mod tests {
613620
FileId(2u32),
614621
Edition2018,
615622
None,
623+
None,
616624
CfgOptions::default(),
617625
CfgOptions::default(),
618626
Env::default(),
@@ -633,6 +641,7 @@ mod tests {
633641
FileId(1u32),
634642
Edition2018,
635643
None,
644+
None,
636645
CfgOptions::default(),
637646
CfgOptions::default(),
638647
Env::default(),
@@ -642,6 +651,7 @@ mod tests {
642651
FileId(2u32),
643652
Edition2018,
644653
None,
654+
None,
645655
CfgOptions::default(),
646656
CfgOptions::default(),
647657
Env::default(),
@@ -651,6 +661,7 @@ mod tests {
651661
FileId(3u32),
652662
Edition2018,
653663
None,
664+
None,
654665
CfgOptions::default(),
655666
CfgOptions::default(),
656667
Env::default(),
@@ -671,6 +682,7 @@ mod tests {
671682
FileId(1u32),
672683
Edition2018,
673684
None,
685+
None,
674686
CfgOptions::default(),
675687
CfgOptions::default(),
676688
Env::default(),
@@ -680,6 +692,7 @@ mod tests {
680692
FileId(2u32),
681693
Edition2018,
682694
None,
695+
None,
683696
CfgOptions::default(),
684697
CfgOptions::default(),
685698
Env::default(),

crates/hir/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ impl Crate {
183183
db.crate_graph()[self.id].edition
184184
}
185185

186+
pub fn version(self, db: &dyn HirDatabase) -> Option<String> {
187+
db.crate_graph()[self.id].version.clone()
188+
}
189+
186190
pub fn display_name(self, db: &dyn HirDatabase) -> Option<CrateDisplayName> {
187191
db.crate_graph()[self.id].display_name.clone()
188192
}

crates/ide/src/doc_links.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,17 @@ fn get_doc_base_url(db: &RootDatabase, krate: &Crate) -> Option<Url> {
473473
}
474474
_ => {
475475
krate.get_html_root_url(db).or_else(|| {
476+
let version = krate.version(db);
476477
// Fallback to docs.rs. This uses `display_name` and can never be
477478
// correct, but that's what fallbacks are about.
478479
//
479480
// FIXME: clicking on the link should just open the file in the editor,
480481
// instead of falling back to external urls.
481-
Some(format!("https://docs.rs/{krate}/*/", krate = display_name))
482+
Some(format!(
483+
"https://docs.rs/{krate}/{version}/",
484+
krate = display_name,
485+
version = version.as_deref().unwrap_or("*")
486+
))
482487
})?
483488
}
484489
};

crates/ide/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ impl Analysis {
219219
file_id,
220220
Edition::CURRENT,
221221
None,
222+
None,
222223
cfg_options.clone(),
223224
cfg_options,
224225
Env::default(),

crates/project_model/src/project_json.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub struct Crate {
2929
pub(crate) display_name: Option<CrateDisplayName>,
3030
pub(crate) root_module: AbsPathBuf,
3131
pub(crate) edition: Edition,
32+
pub(crate) version: Option<String>,
3233
pub(crate) deps: Vec<Dependency>,
3334
pub(crate) cfg: Vec<CfgFlag>,
3435
pub(crate) target: Option<String>,
@@ -80,6 +81,7 @@ impl ProjectJson {
8081
.map(CrateDisplayName::from_canonical_name),
8182
root_module,
8283
edition: crate_data.edition.into(),
84+
version: crate_data.version.as_ref().map(ToString::to_string),
8385
deps: crate_data
8486
.deps
8587
.into_iter()
@@ -127,6 +129,8 @@ struct CrateData {
127129
display_name: Option<String>,
128130
root_module: PathBuf,
129131
edition: EditionData,
132+
#[serde(default)]
133+
version: Option<semver::Version>,
130134
deps: Vec<DepData>,
131135
#[serde(default)]
132136
cfg: Vec<CfgFlag>,

0 commit comments

Comments
 (0)