Skip to content

Commit 5cffef5

Browse files
Consider crate declaration names
1 parent 32f5276 commit 5cffef5

File tree

11 files changed

+135
-86
lines changed

11 files changed

+135
-86
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: 78 additions & 21 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,7 +97,7 @@ 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
}
@@ -107,6 +107,7 @@ impl CrateName {
107107
struct CrateData {
108108
file_id: FileId,
109109
edition: Edition,
110+
declaration_name: Option<String>,
110111
cfg_options: CfgOptions,
111112
env: Env,
112113
dependencies: Vec<Dependency>,
@@ -134,10 +135,11 @@ impl CrateGraph {
134135
&mut self,
135136
file_id: FileId,
136137
edition: Edition,
138+
declaration_name: Option<String>,
137139
cfg_options: CfgOptions,
138140
env: Env,
139141
) -> CrateId {
140-
let data = CrateData::new(file_id, edition, cfg_options, env);
142+
let data = CrateData::new(file_id, edition, declaration_name, cfg_options, env);
141143
let crate_id = CrateId(self.arena.len() as u32);
142144
let prev = self.arena.insert(crate_id, data);
143145
assert!(prev.is_none());
@@ -177,6 +179,15 @@ impl CrateGraph {
177179
self.arena[&crate_id].edition
178180
}
179181

182+
/// Returns a name of a crate, declared in the root project.
183+
/// May be missing for some cases, such as when the crate definition was created for a code snippet.
184+
///
185+
/// This should not be considered as a normal crate name, since the actual name can be different in
186+
/// a particular dependent crate, where it is specified.
187+
pub fn declaration_name(&self, crate_id: &CrateId) -> Option<&String> {
188+
self.arena[crate_id].declaration_name.as_ref()
189+
}
190+
180191
// FIXME: this only finds one crate with the given root; we could have multiple
181192
pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
182193
let (&crate_id, _) = self.arena.iter().find(|(_crate_id, data)| data.file_id == file_id)?;
@@ -230,8 +241,14 @@ impl CrateId {
230241
}
231242

232243
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 }
244+
fn new(
245+
file_id: FileId,
246+
edition: Edition,
247+
declaration_name: Option<String>,
248+
cfg_options: CfgOptions,
249+
env: Env,
250+
) -> CrateData {
251+
CrateData { file_id, edition, declaration_name, dependencies: Vec::new(), cfg_options, env }
235252
}
236253

237254
fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
@@ -290,12 +307,27 @@ mod tests {
290307
#[test]
291308
fn it_should_panic_because_of_cycle_dependencies() {
292309
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());
310+
let crate1 = graph.add_crate_root(
311+
FileId(1u32),
312+
Edition2018,
313+
None,
314+
CfgOptions::default(),
315+
Env::default(),
316+
);
317+
let crate2 = graph.add_crate_root(
318+
FileId(2u32),
319+
Edition2018,
320+
None,
321+
CfgOptions::default(),
322+
Env::default(),
323+
);
324+
let crate3 = graph.add_crate_root(
325+
FileId(3u32),
326+
Edition2018,
327+
None,
328+
CfgOptions::default(),
329+
Env::default(),
330+
);
299331
assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
300332
assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
301333
assert!(graph.add_dep(crate3, CrateName::new("crate1").unwrap(), crate1).is_err());
@@ -304,23 +336,48 @@ mod tests {
304336
#[test]
305337
fn it_works() {
306338
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());
339+
let crate1 = graph.add_crate_root(
340+
FileId(1u32),
341+
Edition2018,
342+
None,
343+
CfgOptions::default(),
344+
Env::default(),
345+
);
346+
let crate2 = graph.add_crate_root(
347+
FileId(2u32),
348+
Edition2018,
349+
None,
350+
CfgOptions::default(),
351+
Env::default(),
352+
);
353+
let crate3 = graph.add_crate_root(
354+
FileId(3u32),
355+
Edition2018,
356+
None,
357+
CfgOptions::default(),
358+
Env::default(),
359+
);
313360
assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok());
314361
assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok());
315362
}
316363

317364
#[test]
318365
fn dashes_are_normalized() {
319366
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());
367+
let crate1 = graph.add_crate_root(
368+
FileId(1u32),
369+
Edition2018,
370+
None,
371+
CfgOptions::default(),
372+
Env::default(),
373+
);
374+
let crate2 = graph.add_crate_root(
375+
FileId(2u32),
376+
Edition2018,
377+
None,
378+
CfgOptions::default(),
379+
Env::default(),
380+
);
324381
assert!(graph
325382
.add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)
326383
.is_ok());

crates/ra_ide/src/hover.rs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! FIXME: write short doc here
22
33
use hir::{
4-
Adt, AsAssocItem, AssocItemContainer, HasSource, HirDisplay, ModuleDef, ModuleSource, Semantics,
4+
Adt, AsAssocItem, AssocItemContainer, FieldSource, HasSource, HirDisplay, ModuleDef,
5+
ModuleSource, Semantics,
56
};
7+
use ra_db::SourceDatabase;
68
use ra_ide_db::{
79
defs::{classify_name, classify_name_ref, Definition},
810
RootDatabase,
@@ -119,7 +121,7 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String>
119121

120122
fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
121123
let mod_path = def.module(db).map(|module| {
122-
once(db.get_crate_original_name(&module.krate().into()))
124+
once(db.crate_graph().declaration_name(&module.krate().into()).cloned())
123125
.chain(
124126
module
125127
.path_to_root(db)
@@ -144,7 +146,7 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
144146
Definition::StructField(it) => {
145147
let src = it.source(db);
146148
match src.value {
147-
hir::FieldSource::Named(it) => {
149+
FieldSource::Named(it) => {
148150
hover_text(it.doc_comment_text(), it.short_label(), mod_path)
149151
}
150152
_ => None,
@@ -576,21 +578,23 @@ fn func(foo: i32) { if true { <|>foo; }; }
576578
fn test_hover_infer_associated_method_exact() {
577579
let (analysis, position) = single_file_with_position(
578580
"
579-
struct Thing { x: u32 }
581+
mod wrapper {
582+
struct Thing { x: u32 }
580583
581-
impl Thing {
582-
fn new() -> Thing {
583-
Thing { x: 0 }
584+
impl Thing {
585+
fn new() -> Thing {
586+
Thing { x: 0 }
587+
}
584588
}
585589
}
586590
587591
fn main() {
588-
let foo_test = Thing::new<|>();
592+
let foo_test = wrapper::Thing::new<|>();
589593
}
590594
",
591595
);
592596
let hover = analysis.hover(position).unwrap().unwrap();
593-
assert_eq!(trim_markup_opt(hover.info.first()), Some("fn new() -> Thing"));
597+
assert_eq!(trim_markup_opt(hover.info.first()), Some("wrapper::Thing\nfn new() -> Thing"));
594598
assert_eq!(hover.info.is_exact(), true);
595599
}
596600

@@ -863,25 +867,4 @@ fn func(foo: i32) { if true { <|>foo; }; }
863867
&["fn foo()\n```\n\n<- `\u{3000}` here"],
864868
);
865869
}
866-
867-
#[test]
868-
fn zzz() {
869-
check_hover_result(
870-
"
871-
//- /main.rs
872-
mod vvv {
873-
pub struct Test;
874-
875-
impl Test {
876-
pub fn whatever() {}
877-
}
878-
}
879-
880-
fn main() {
881-
vvv::Test::what<|>ever();
882-
}
883-
",
884-
&["vvv::Test\npub fn whatever()"],
885-
);
886-
}
887870
}

crates/ra_ide/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,13 @@ impl Analysis {
211211
// Default to enable test for single file.
212212
let mut cfg_options = CfgOptions::default();
213213
cfg_options.insert_atom("test".into());
214-
crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options, Env::default());
214+
crate_graph.add_crate_root(
215+
file_id,
216+
Edition::Edition2018,
217+
None,
218+
cfg_options,
219+
Env::default(),
220+
);
215221
change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text));
216222
change.set_crate_graph(crate_graph);
217223
host.apply_change(change);

crates/ra_ide/src/mock_analysis.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,19 @@ impl MockAnalysis {
9999
root_crate = Some(crate_graph.add_crate_root(
100100
file_id,
101101
Edition2018,
102+
None,
102103
cfg_options,
103104
Env::default(),
104105
));
105106
} else if path.ends_with("/lib.rs") {
106-
let other_crate =
107-
crate_graph.add_crate_root(file_id, Edition2018, cfg_options, Env::default());
108107
let crate_name = path.parent().unwrap().file_name().unwrap();
108+
let other_crate = crate_graph.add_crate_root(
109+
file_id,
110+
Edition2018,
111+
Some(crate_name.to_owned()),
112+
cfg_options,
113+
Env::default(),
114+
);
109115
if let Some(root_crate) = root_crate {
110116
crate_graph
111117
.add_dep(root_crate, CrateName::new(crate_name).unwrap(), other_crate)

crates/ra_ide/src/parent_module.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ mod tests {
133133
let crate_id = crate_graph.add_crate_root(
134134
root_file,
135135
Edition2018,
136+
None,
136137
CfgOptions::default(),
137138
Env::default(),
138139
);

crates/ra_ide_db/src/change.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{fmt, sync::Arc, time};
55

66
use ra_db::{
77
salsa::{Database, Durability, SweepStrategy},
8-
CrateGraph, CrateId, FileId, RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot,
8+
CrateGraph, FileId, RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot,
99
SourceRootId,
1010
};
1111
use ra_prof::{memory_usage, profile, Bytes};
@@ -88,10 +88,6 @@ impl AnalysisChange {
8888
self.crate_graph = Some(graph);
8989
}
9090

91-
pub fn set_debug_crate_name(&mut self, crate_id: CrateId, name: String) {
92-
self.debug_data.crate_names.insert(crate_id, name);
93-
}
94-
9591
pub fn set_debug_root_path(&mut self, source_root_id: SourceRootId, path: String) {
9692
self.debug_data.root_paths.insert(source_root_id, path);
9793
}

crates/ra_ide_db/src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ impl RootDatabase {
104104
db.query_mut(hir::db::MacroExpandQuery).set_lru_capacity(lru_capacity);
105105
db
106106
}
107-
108-
pub fn get_crate_original_name(&self, crate_id: &CrateId) -> Option<String> {
109-
self.debug_data.crate_names.get(crate_id).cloned()
110-
}
111107
}
112108

113109
impl salsa::ParallelDatabase for RootDatabase {
@@ -135,12 +131,10 @@ fn line_index(db: &impl LineIndexDatabase, file_id: FileId) -> Arc<LineIndex> {
135131
#[derive(Debug, Default, Clone)]
136132
pub(crate) struct DebugData {
137133
pub(crate) root_paths: FxHashMap<SourceRootId, String>,
138-
pub(crate) crate_names: FxHashMap<CrateId, String>,
139134
}
140135

141136
impl DebugData {
142137
pub(crate) fn merge(&mut self, other: DebugData) {
143138
self.root_paths.extend(other.root_paths.into_iter());
144-
self.crate_names.extend(other.crate_names.into_iter());
145139
}
146140
}

0 commit comments

Comments
 (0)