Skip to content

Commit a3081a6

Browse files
committed
Adding crate_root_path to crate_graph
1 parent 8e687f7 commit a3081a6

File tree

10 files changed

+74
-37
lines changed

10 files changed

+74
-37
lines changed

crates/base-db/src/fixture.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ impl ChangeFixture {
165165
meta.edition,
166166
Some(crate_name.clone().into()),
167167
version,
168+
None,
168169
meta.cfg,
169170
Default::default(),
170171
meta.env,
@@ -205,6 +206,7 @@ impl ChangeFixture {
205206
Edition::CURRENT,
206207
Some(CrateName::new("test").unwrap().into()),
207208
None,
209+
None,
208210
default_cfg,
209211
Default::default(),
210212
Env::default(),
@@ -249,6 +251,7 @@ impl ChangeFixture {
249251
Edition::Edition2021,
250252
Some(CrateDisplayName::from_canonical_name("core".to_string())),
251253
None,
254+
None,
252255
Default::default(),
253256
Default::default(),
254257
Env::default(),
@@ -288,6 +291,7 @@ impl ChangeFixture {
288291
Edition::Edition2021,
289292
Some(CrateDisplayName::from_canonical_name("proc_macros".to_string())),
290293
None,
294+
None,
291295
Default::default(),
292296
Default::default(),
293297
Env::default(),

crates/base-db/src/input.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ pub struct CrateData {
304304
/// For purposes of analysis, crates are anonymous (only names in
305305
/// `Dependency` matters), this name should only be used for UI.
306306
pub display_name: Option<CrateDisplayName>,
307+
pub crate_root_path: Option<AbsPathBuf>,
307308
pub cfg_options: CfgOptions,
308309
/// The cfg options that could be used by the crate
309310
pub potential_cfg_options: Option<CfgOptions>,
@@ -361,6 +362,7 @@ impl CrateGraph {
361362
edition: Edition,
362363
display_name: Option<CrateDisplayName>,
363364
version: Option<String>,
365+
crate_root_path: Option<AbsPathBuf>,
364366
cfg_options: CfgOptions,
365367
potential_cfg_options: Option<CfgOptions>,
366368
env: Env,
@@ -374,6 +376,7 @@ impl CrateGraph {
374376
edition,
375377
version,
376378
display_name,
379+
crate_root_path,
377380
cfg_options,
378381
potential_cfg_options,
379382
env,
@@ -740,6 +743,7 @@ mod tests {
740743
Edition2018,
741744
None,
742745
None,
746+
None,
743747
Default::default(),
744748
Default::default(),
745749
Env::default(),
@@ -753,6 +757,7 @@ mod tests {
753757
Edition2018,
754758
None,
755759
None,
760+
None,
756761
Default::default(),
757762
Default::default(),
758763
Env::default(),
@@ -766,6 +771,7 @@ mod tests {
766771
Edition2018,
767772
None,
768773
None,
774+
None,
769775
Default::default(),
770776
Default::default(),
771777
Env::default(),
@@ -793,6 +799,7 @@ mod tests {
793799
Edition2018,
794800
None,
795801
None,
802+
None,
796803
Default::default(),
797804
Default::default(),
798805
Env::default(),
@@ -806,6 +813,7 @@ mod tests {
806813
Edition2018,
807814
None,
808815
None,
816+
None,
809817
Default::default(),
810818
Default::default(),
811819
Env::default(),
@@ -830,6 +838,7 @@ mod tests {
830838
Edition2018,
831839
None,
832840
None,
841+
None,
833842
Default::default(),
834843
Default::default(),
835844
Env::default(),
@@ -843,6 +852,7 @@ mod tests {
843852
Edition2018,
844853
None,
845854
None,
855+
None,
846856
Default::default(),
847857
Default::default(),
848858
Env::default(),
@@ -856,6 +866,7 @@ mod tests {
856866
Edition2018,
857867
None,
858868
None,
869+
None,
859870
Default::default(),
860871
Default::default(),
861872
Env::default(),
@@ -880,6 +891,7 @@ mod tests {
880891
Edition2018,
881892
None,
882893
None,
894+
None,
883895
Default::default(),
884896
Default::default(),
885897
Env::default(),
@@ -893,6 +905,7 @@ mod tests {
893905
Edition2018,
894906
None,
895907
None,
908+
None,
896909
Default::default(),
897910
Default::default(),
898911
Env::default(),

crates/ide/src/fetch_crates.rs

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ide_db::{
2-
base_db::{CrateOrigin, SourceDatabase, SourceDatabaseExt},
2+
base_db::{CrateOrigin, SourceDatabase},
33
FxIndexSet, RootDatabase,
44
};
55

@@ -22,13 +22,13 @@ pub(crate) fn fetch_crates(db: &RootDatabase) -> FxIndexSet<CrateInfo> {
2222
.iter()
2323
.map(|crate_id| &crate_graph[crate_id])
2424
.filter(|&data| !matches!(data.origin, CrateOrigin::Local { .. }))
25-
.filter_map(|data| crate_info(data, db))
25+
.filter_map(|data| crate_info(data))
2626
.collect()
2727
}
2828

29-
fn crate_info(data: &ide_db::base_db::CrateData, db: &RootDatabase) -> Option<CrateInfo> {
29+
fn crate_info(data: &ide_db::base_db::CrateData) -> Option<CrateInfo> {
3030
let crate_name = crate_name(data);
31-
let crate_path = crate_path(db, data, &crate_name);
31+
let crate_path = data.crate_root_path.as_ref().map(|p| p.display().to_string());
3232
if let Some(crate_path) = crate_path {
3333
let version = data.version.clone().unwrap_or_else(|| "".to_owned());
3434
Some(CrateInfo { name: crate_name, version, path: crate_path })
@@ -43,29 +43,3 @@ fn crate_name(data: &ide_db::base_db::CrateData) -> String {
4343
.map(|it| it.canonical_name().to_owned())
4444
.unwrap_or("unknown".to_string())
4545
}
46-
47-
fn crate_path(
48-
db: &RootDatabase,
49-
data: &ide_db::base_db::CrateData,
50-
crate_name: &str,
51-
) -> Option<String> {
52-
let source_root_id = db.file_source_root(data.root_file_id);
53-
let source_root = db.source_root(source_root_id);
54-
let source_root_path = source_root.path_for_file(&data.root_file_id);
55-
source_root_path.cloned().and_then(|mut root_path| {
56-
let mut crate_path = None;
57-
while let Some(vfs_path) = root_path.parent() {
58-
match vfs_path.name_and_extension() {
59-
Some((name, _)) => {
60-
if name.starts_with(crate_name) {
61-
crate_path = Some(vfs_path.to_string());
62-
break;
63-
}
64-
}
65-
None => break,
66-
}
67-
root_path = vfs_path;
68-
}
69-
crate_path
70-
})
71-
}

crates/ide/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ impl Analysis {
239239
Edition::CURRENT,
240240
None,
241241
None,
242+
None,
242243
cfg_options.clone(),
243244
None,
244245
Env::default(),

crates/ide/src/shuffle_crate_graph.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub(crate) fn shuffle_crate_graph(db: &mut RootDatabase) {
3434
data.edition,
3535
data.display_name.clone(),
3636
data.version.clone(),
37+
data.crate_root_path.clone(),
3738
data.cfg_options.clone(),
3839
data.potential_cfg_options.clone(),
3940
data.env.clone(),

crates/paths/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ impl AbsPath {
213213
pub fn exists(&self) -> bool {
214214
self.0.exists()
215215
}
216+
217+
pub fn name_and_extension(&self) -> Option<(&str, Option<&str>)> {
218+
Some((
219+
self.file_stem()?.to_str()?,
220+
self.extension().and_then(|extension| extension.to_str()),
221+
))
222+
}
216223
// endregion:delegate-methods
217224
}
218225

crates/project-model/src/tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ fn replace_root(s: &mut String, direction: bool) {
102102
}
103103
}
104104

105+
fn replace_fake_sys_root(s: &mut String) {
106+
let root = get_test_path("fake-sysroot");
107+
*s = s.replace(root.to_str().expect("expected str"), "$FAKESYSROOT$")
108+
}
109+
105110
fn get_test_path(file: &str) -> PathBuf {
106111
let base = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
107112
base.join("test_data").join(file)
@@ -140,6 +145,7 @@ fn to_crate_graph(project_workspace: ProjectWorkspace) -> (CrateGraph, ProcMacro
140145
fn check_crate_graph(crate_graph: CrateGraph, expect: ExpectFile) {
141146
let mut crate_graph = format!("{crate_graph:#?}");
142147
replace_root(&mut crate_graph, false);
148+
replace_fake_sys_root(&mut crate_graph);
143149
expect.assert_eq(&crate_graph);
144150
}
145151

crates/project-model/src/workspace.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ fn project_json_to_crate_graph(
766766
proc_macro_dylib_path,
767767
is_proc_macro,
768768
repository,
769+
root_module,
769770
..
770771
},
771772
file_id,
@@ -784,6 +785,7 @@ fn project_json_to_crate_graph(
784785
*edition,
785786
display_name.clone(),
786787
version.clone(),
788+
crate_path(display_name.as_ref(), root_module),
787789
target_cfgs.iter().chain(cfg.iter()).cloned().collect(),
788790
None,
789791
env,
@@ -832,6 +834,30 @@ fn project_json_to_crate_graph(
832834
res
833835
}
834836

837+
//Thats a best effort to try and find the crate path for a project configured using JsonProject model
838+
fn crate_path(
839+
crate_name: Option<&CrateDisplayName>,
840+
root_module_path: &AbsPathBuf,
841+
) -> Option<AbsPathBuf> {
842+
crate_name.and_then(|crate_name| {
843+
let mut crate_path = None;
844+
let mut root_path = root_module_path.as_path();
845+
while let Some(path) = root_path.parent() {
846+
match path.name_and_extension() {
847+
Some((name, _)) => {
848+
if name.starts_with(crate_name.canonical_name()) {
849+
crate_path = Some(path.to_path_buf());
850+
break;
851+
}
852+
}
853+
None => break,
854+
}
855+
root_path = path;
856+
}
857+
crate_path
858+
})
859+
}
860+
835861
fn cargo_to_crate_graph(
836862
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
837863
rustc: Option<&(CargoWorkspace, WorkspaceBuildScripts)>,
@@ -1053,6 +1079,7 @@ fn detached_files_to_crate_graph(
10531079
Edition::CURRENT,
10541080
display_name.clone(),
10551081
None,
1082+
None,
10561083
cfg_options.clone(),
10571084
None,
10581085
Env::default(),
@@ -1249,6 +1276,7 @@ fn add_target_crate_root(
12491276
edition,
12501277
Some(display_name),
12511278
Some(pkg.version.to_string()),
1279+
Some(pkg.manifest.parent().to_owned()),
12521280
cfg_options,
12531281
potential_cfg_options,
12541282
env,
@@ -1320,11 +1348,13 @@ fn sysroot_to_crate_graph(
13201348
let env = Env::default();
13211349
let display_name =
13221350
CrateDisplayName::from_canonical_name(sysroot[krate].name.clone());
1323-
let crate_id = crate_graph.add_crate_root(
1351+
let crate_root_path = sysroot.src_root().join(display_name.canonical_name());
1352+
let crate_id = crate_graph.add_crate_root(
13241353
file_id,
13251354
Edition::CURRENT,
13261355
Some(display_name),
13271356
None,
1357+
Some(crate_root_path),
13281358
cfg_options.clone(),
13291359
None,
13301360
env,

crates/vfs/src/vfs_path.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ impl VfsPath {
107107
/// Returns `self`'s base name and file extension.
108108
pub fn name_and_extension(&self) -> Option<(&str, Option<&str>)> {
109109
match &self.0 {
110-
VfsPathRepr::PathBuf(p) => Some((
111-
p.file_stem()?.to_str()?,
112-
p.extension().and_then(|extension| extension.to_str()),
113-
)),
110+
VfsPathRepr::PathBuf(p) => p.name_and_extension(),
114111
VfsPathRepr::VirtualPath(p) => p.name_and_extension(),
115112
}
116113
}

editors/code/src/dependencies_provider.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,13 @@ export class Dependency extends vscode.TreeItem {
110110
) {
111111
super(label, collapsibleState);
112112
this.id = this.dependencyPath.toLowerCase();
113-
this.tooltip = `${this.label}-${this.version}`;
114113
this.description = this.version;
115114
this.resourceUri = vscode.Uri.file(dependencyPath);
115+
if (this.version) {
116+
this.tooltip = `${this.label}-${this.version}`;
117+
} else {
118+
this.tooltip = this.label;
119+
}
116120
}
117121
}
118122

@@ -124,8 +128,8 @@ export class DependencyFile extends vscode.TreeItem {
124128
public readonly collapsibleState: vscode.TreeItemCollapsibleState
125129
) {
126130
super(vscode.Uri.file(dependencyPath), collapsibleState);
127-
const isDir = fs.lstatSync(this.dependencyPath).isDirectory();
128131
this.id = this.dependencyPath.toLowerCase();
132+
const isDir = fs.lstatSync(this.dependencyPath).isDirectory();
129133
if (!isDir) {
130134
this.command = { command: "vscode.open",
131135
title: "Open File",

0 commit comments

Comments
 (0)