Skip to content

Commit 57c27f9

Browse files
Merge #3519
3519: Show mod path on hover r=matklad a=SomeoneToIgnore Closes #1064 Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
2 parents 58ab084 + e1aa96f commit 57c27f9

File tree

18 files changed

+270
-121
lines changed

18 files changed

+270
-121
lines changed

crates/ra_db/src/fixture.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, text: &str) -> FileId {
5656
crate_graph.add_crate_root(
5757
file_id,
5858
Edition::Edition2018,
59+
None,
5960
CfgOptions::default(),
6061
Env::default(),
6162
);
@@ -98,8 +99,13 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
9899
assert!(meta.path.starts_with(&source_root_prefix));
99100

100101
if let Some(krate) = meta.krate {
101-
let crate_id =
102-
crate_graph.add_crate_root(file_id, meta.edition, meta.cfg, Env::default());
102+
let crate_id = crate_graph.add_crate_root(
103+
file_id,
104+
meta.edition,
105+
Some(krate.clone()),
106+
meta.cfg,
107+
Env::default(),
108+
);
103109
let prev = crates.insert(krate.clone(), crate_id);
104110
assert!(prev.is_none());
105111
for dep in meta.deps {
@@ -132,6 +138,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
132138
crate_graph.add_crate_root(
133139
crate_root,
134140
Edition::Edition2018,
141+
None,
135142
CfgOptions::default(),
136143
Env::default(),
137144
);

crates/ra_db/src/input.rs

Lines changed: 90 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub struct CrateId(pub u32);
8686
pub struct CrateName(SmolStr);
8787

8888
impl CrateName {
89-
/// Crates a crate name, checking for dashes in the string provided.
89+
/// Creates a crate name, checking for dashes in the string provided.
9090
/// Dashes are not allowed in the crate names,
9191
/// hence the input string is returned as `Err` for those cases.
9292
pub fn new(name: &str) -> Result<CrateName, &str> {
@@ -97,19 +97,23 @@ impl CrateName {
9797
}
9898
}
9999

100-
/// Crates a crate name, unconditionally replacing the dashes with underscores.
100+
/// Creates a crate name, unconditionally replacing the dashes with underscores.
101101
pub fn normalize_dashes(name: &str) -> CrateName {
102102
Self(SmolStr::new(name.replace('-', "_")))
103103
}
104104
}
105105

106106
#[derive(Debug, Clone, PartialEq, Eq)]
107-
struct CrateData {
108-
file_id: FileId,
109-
edition: Edition,
107+
pub struct CrateData {
108+
pub root_file_id: FileId,
109+
pub edition: Edition,
110+
/// The name to display to the end user.
111+
/// This actual crate name can be different in a particular dependent crate
112+
/// or may even be missing for some cases, such as a dummy crate for the code snippet.
113+
pub display_name: Option<String>,
110114
cfg_options: CfgOptions,
111115
env: Env,
112-
dependencies: Vec<Dependency>,
116+
pub dependencies: Vec<Dependency>,
113117
}
114118

115119
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -134,10 +138,11 @@ impl CrateGraph {
134138
&mut self,
135139
file_id: FileId,
136140
edition: Edition,
141+
display_name: Option<String>,
137142
cfg_options: CfgOptions,
138143
env: Env,
139144
) -> CrateId {
140-
let data = CrateData::new(file_id, edition, cfg_options, env);
145+
let data = CrateData::new(file_id, edition, display_name, cfg_options, env);
141146
let crate_id = CrateId(self.arena.len() as u32);
142147
let prev = self.arena.insert(crate_id, data);
143148
assert!(prev.is_none());
@@ -169,24 +174,17 @@ impl CrateGraph {
169174
self.arena.keys().copied()
170175
}
171176

172-
pub fn crate_root(&self, crate_id: CrateId) -> FileId {
173-
self.arena[&crate_id].file_id
174-
}
175-
176-
pub fn edition(&self, crate_id: CrateId) -> Edition {
177-
self.arena[&crate_id].edition
177+
pub fn crate_data(&self, crate_id: &CrateId) -> &CrateData {
178+
&self.arena[crate_id]
178179
}
179180

180181
// FIXME: this only finds one crate with the given root; we could have multiple
181182
pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
182-
let (&crate_id, _) = self.arena.iter().find(|(_crate_id, data)| data.file_id == file_id)?;
183+
let (&crate_id, _) =
184+
self.arena.iter().find(|(_crate_id, data)| data.root_file_id == file_id)?;
183185
Some(crate_id)
184186
}
185187

186-
pub fn dependencies(&self, crate_id: CrateId) -> impl Iterator<Item = &Dependency> {
187-
self.arena[&crate_id].dependencies.iter()
188-
}
189-
190188
/// Extends this crate graph by adding a complete disjoint second crate
191189
/// graph.
192190
///
@@ -209,7 +207,7 @@ impl CrateGraph {
209207
return false;
210208
}
211209

212-
for dep in self.dependencies(from) {
210+
for dep in &self.crate_data(&from).dependencies {
213211
let crate_id = dep.crate_id();
214212
if crate_id == target {
215213
return true;
@@ -230,8 +228,21 @@ impl CrateId {
230228
}
231229

232230
impl CrateData {
233-
fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions, env: Env) -> CrateData {
234-
CrateData { file_id, edition, dependencies: Vec::new(), cfg_options, env }
231+
fn new(
232+
root_file_id: FileId,
233+
edition: Edition,
234+
display_name: Option<String>,
235+
cfg_options: CfgOptions,
236+
env: Env,
237+
) -> CrateData {
238+
CrateData {
239+
root_file_id,
240+
edition,
241+
display_name,
242+
dependencies: Vec::new(),
243+
cfg_options,
244+
env,
245+
}
235246
}
236247

237248
fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
@@ -290,12 +301,27 @@ mod tests {
290301
#[test]
291302
fn it_should_panic_because_of_cycle_dependencies() {
292303
let mut graph = CrateGraph::default();
293-
let crate1 =
294-
graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default());
295-
let crate2 =
296-
graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default());
297-
let crate3 =
298-
graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default());
304+
let crate1 = graph.add_crate_root(
305+
FileId(1u32),
306+
Edition2018,
307+
None,
308+
CfgOptions::default(),
309+
Env::default(),
310+
);
311+
let crate2 = graph.add_crate_root(
312+
FileId(2u32),
313+
Edition2018,
314+
None,
315+
CfgOptions::default(),
316+
Env::default(),
317+
);
318+
let crate3 = graph.add_crate_root(
319+
FileId(3u32),
320+
Edition2018,
321+
None,
322+
CfgOptions::default(),
323+
Env::default(),
324+
);
299325
assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
300326
assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
301327
assert!(graph.add_dep(crate3, CrateName::new("crate1").unwrap(), crate1).is_err());
@@ -304,29 +330,54 @@ mod tests {
304330
#[test]
305331
fn it_works() {
306332
let mut graph = CrateGraph::default();
307-
let crate1 =
308-
graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default());
309-
let crate2 =
310-
graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default());
311-
let crate3 =
312-
graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default());
333+
let crate1 = graph.add_crate_root(
334+
FileId(1u32),
335+
Edition2018,
336+
None,
337+
CfgOptions::default(),
338+
Env::default(),
339+
);
340+
let crate2 = graph.add_crate_root(
341+
FileId(2u32),
342+
Edition2018,
343+
None,
344+
CfgOptions::default(),
345+
Env::default(),
346+
);
347+
let crate3 = graph.add_crate_root(
348+
FileId(3u32),
349+
Edition2018,
350+
None,
351+
CfgOptions::default(),
352+
Env::default(),
353+
);
313354
assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
314355
assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
315356
}
316357

317358
#[test]
318359
fn dashes_are_normalized() {
319360
let mut graph = CrateGraph::default();
320-
let crate1 =
321-
graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default());
322-
let crate2 =
323-
graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default());
361+
let crate1 = graph.add_crate_root(
362+
FileId(1u32),
363+
Edition2018,
364+
None,
365+
CfgOptions::default(),
366+
Env::default(),
367+
);
368+
let crate2 = graph.add_crate_root(
369+
FileId(2u32),
370+
Edition2018,
371+
None,
372+
CfgOptions::default(),
373+
Env::default(),
374+
);
324375
assert!(graph
325376
.add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)
326377
.is_ok());
327378
assert_eq!(
328-
graph.dependencies(crate1).collect::<Vec<_>>(),
329-
vec![&Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }]
379+
graph.crate_data(&crate1).dependencies,
380+
vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }]
330381
);
331382
}
332383
}

crates/ra_hir/src/code_model.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ pub struct CrateDependency {
5555
impl Crate {
5656
pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> {
5757
db.crate_graph()
58-
.dependencies(self.id)
58+
.crate_data(&self.id)
59+
.dependencies
60+
.iter()
5961
.map(|dep| {
6062
let krate = Crate { id: dep.crate_id() };
6163
let name = dep.as_name();
@@ -69,7 +71,9 @@ impl Crate {
6971
let crate_graph = db.crate_graph();
7072
crate_graph
7173
.iter()
72-
.filter(|&krate| crate_graph.dependencies(krate).any(|it| it.crate_id == self.id))
74+
.filter(|&krate| {
75+
crate_graph.crate_data(&krate).dependencies.iter().any(|it| it.crate_id == self.id)
76+
})
7377
.map(|id| Crate { id })
7478
.collect()
7579
}
@@ -80,12 +84,11 @@ impl Crate {
8084
}
8185

8286
pub fn root_file(self, db: &impl DefDatabase) -> FileId {
83-
db.crate_graph().crate_root(self.id)
87+
db.crate_graph().crate_data(&self.id).root_file_id
8488
}
8589

8690
pub fn edition(self, db: &impl DefDatabase) -> Edition {
87-
let crate_graph = db.crate_graph();
88-
crate_graph.edition(self.id)
91+
db.crate_graph().crate_data(&self.id).edition
8992
}
9093

9194
pub fn all(db: &impl DefDatabase) -> Vec<Crate> {
@@ -496,6 +499,14 @@ impl Adt {
496499
pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
497500
Some(self.module(db).krate())
498501
}
502+
503+
pub fn name(&self, db: &impl HirDatabase) -> Name {
504+
match self {
505+
Adt::Struct(s) => s.name(db),
506+
Adt::Union(u) => u.name(db),
507+
Adt::Enum(e) => e.name(db),
508+
}
509+
}
499510
}
500511

501512
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -523,6 +534,14 @@ impl VariantDef {
523534
}
524535
}
525536

537+
pub fn name(&self, db: &impl HirDatabase) -> Name {
538+
match self {
539+
VariantDef::Struct(s) => s.name(db),
540+
VariantDef::Union(u) => u.name(db),
541+
VariantDef::EnumVariant(e) => e.name(db),
542+
}
543+
}
544+
526545
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
527546
match self {
528547
VariantDef::Struct(it) => it.variant_data(db),
@@ -550,6 +569,14 @@ impl DefWithBody {
550569
DefWithBody::Static(s) => s.module(db),
551570
}
552571
}
572+
573+
pub fn name(self, db: &impl HirDatabase) -> Option<Name> {
574+
match self {
575+
DefWithBody::Function(f) => Some(f.name(db)),
576+
DefWithBody::Static(s) => s.name(db),
577+
DefWithBody::Const(c) => c.name(db),
578+
}
579+
}
553580
}
554581

555582
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

crates/ra_hir_def/src/find_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ fn find_importable_locations(
176176
// directly (only through reexports in direct dependencies).
177177
for krate in Some(from.krate)
178178
.into_iter()
179-
.chain(crate_graph.dependencies(from.krate).map(|dep| dep.crate_id))
179+
.chain(crate_graph.crate_data(&from.krate).dependencies.iter().map(|dep| dep.crate_id))
180180
{
181181
result.extend(
182182
importable_locations_in_crate(db, item, krate)

crates/ra_hir_def/src/lang_item.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ impl LangItems {
117117
return Some(*target);
118118
}
119119
db.crate_graph()
120-
.dependencies(start_crate)
120+
.crate_data(&start_crate)
121+
.dependencies
122+
.iter()
121123
.find_map(|dep| db.lang_item(dep.crate_id, item.clone()))
122124
}
123125

crates/ra_hir_def/src/nameres.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ impl CrateDefMap {
179179
pub(crate) fn crate_def_map_query(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> {
180180
let _p = profile("crate_def_map_query");
181181
let def_map = {
182-
let crate_graph = db.crate_graph();
183-
let edition = crate_graph.edition(krate);
182+
let edition = db.crate_graph().crate_data(&krate).edition;
184183
let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default();
185184
let root = modules.alloc(ModuleData::default());
186185
CrateDefMap {

crates/ra_hir_def/src/nameres/collector.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> C
3434
let crate_graph = db.crate_graph();
3535

3636
// populate external prelude
37-
for dep in crate_graph.dependencies(def_map.krate) {
37+
for dep in &crate_graph.crate_data(&def_map.krate).dependencies {
3838
let dep_def_map = db.crate_def_map(dep.crate_id);
3939
log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id);
4040
def_map.extern_prelude.insert(
@@ -128,8 +128,7 @@ where
128128
DB: DefDatabase,
129129
{
130130
fn collect(&mut self) {
131-
let crate_graph = self.db.crate_graph();
132-
let file_id = crate_graph.crate_root(self.def_map.krate);
131+
let file_id = self.db.crate_graph().crate_data(&self.def_map.krate).root_file_id;
133132
let raw_items = self.db.raw_items(file_id.into());
134133
let module_id = self.def_map.root;
135134
self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id };
@@ -955,7 +954,7 @@ mod tests {
955954
let krate = db.test_crate();
956955

957956
let def_map = {
958-
let edition = db.crate_graph().edition(krate);
957+
let edition = db.crate_graph().crate_data(&krate).edition;
959958
let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default();
960959
let root = modules.alloc(ModuleData::default());
961960
CrateDefMap {

crates/ra_hir_ty/src/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub(crate) fn impls_for_trait_query(
4747
// will only ever get called for a few crates near the root of the tree (the
4848
// ones the user is editing), so this may actually be a waste of memory. I'm
4949
// doing it like this mainly for simplicity for now.
50-
for dep in db.crate_graph().dependencies(krate) {
50+
for dep in &db.crate_graph().crate_data(&krate).dependencies {
5151
impls.extend(db.impls_for_trait(dep.crate_id, trait_).iter());
5252
}
5353
let crate_impl_defs = db.impls_in_crate(krate);

0 commit comments

Comments
 (0)