Skip to content

Commit 059ed25

Browse files
Fix crate display name dashes
1 parent 6bc226f commit 059ed25

File tree

6 files changed

+40
-20
lines changed

6 files changed

+40
-20
lines changed

crates/ra_db/src/fixture.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
6464
crate_graph.add_crate_root(
6565
file_id,
6666
meta.edition,
67-
meta.krate,
67+
meta.krate.map(|name| {
68+
CrateName::new(&name).expect("Fixture crate name should not contain dashes")
69+
}),
6870
meta.cfg,
6971
meta.env,
7072
Default::default(),
@@ -124,7 +126,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
124126
let crate_id = crate_graph.add_crate_root(
125127
file_id,
126128
meta.edition,
127-
Some(krate.clone()),
129+
Some(CrateName::new(&krate).unwrap()),
128130
meta.cfg,
129131
meta.env,
130132
Default::default(),

crates/ra_db/src/input.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub struct CrateGraph {
8383
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8484
pub struct CrateId(pub u32);
8585

86+
#[derive(Debug, Clone, PartialEq, Eq)]
8687
pub struct CrateName(SmolStr);
8788

8889
impl CrateName {
@@ -101,6 +102,10 @@ impl CrateName {
101102
pub fn normalize_dashes(name: &str) -> CrateName {
102103
Self(SmolStr::new(name.replace('-', "_")))
103104
}
105+
106+
pub fn get_name(&self) -> String {
107+
self.0.to_string()
108+
}
104109
}
105110

106111
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -110,7 +115,7 @@ pub struct CrateData {
110115
/// The name to display to the end user.
111116
/// This actual crate name can be different in a particular dependent crate
112117
/// or may even be missing for some cases, such as a dummy crate for the code snippet.
113-
pub display_name: Option<String>,
118+
pub display_name: Option<CrateName>,
114119
pub cfg_options: CfgOptions,
115120
pub env: Env,
116121
pub extern_source: ExternSource,
@@ -150,7 +155,7 @@ impl CrateGraph {
150155
&mut self,
151156
file_id: FileId,
152157
edition: Edition,
153-
display_name: Option<String>,
158+
display_name: Option<CrateName>,
154159
cfg_options: CfgOptions,
155160
env: Env,
156161
extern_source: ExternSource,

crates/ra_hir_def/src/nameres.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,13 @@ pub struct ModuleData {
177177

178178
impl CrateDefMap {
179179
pub(crate) fn crate_def_map_query(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> {
180-
let _p = profile("crate_def_map_query")
181-
.detail(|| db.crate_graph()[krate].display_name.clone().unwrap_or_default());
180+
let _p = profile("crate_def_map_query").detail(|| {
181+
db.crate_graph()[krate]
182+
.display_name
183+
.as_ref()
184+
.map(|name| name.get_name())
185+
.unwrap_or_default()
186+
});
182187
let def_map = {
183188
let edition = db.crate_graph()[krate].edition;
184189
let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default();

crates/ra_ide/src/hover.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,22 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String>
9494

9595
fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
9696
let mod_path = def.module(db).map(|module| {
97-
once(db.crate_graph()[module.krate().into()].display_name.clone())
98-
.chain(
99-
module
100-
.path_to_root(db)
101-
.into_iter()
102-
.rev()
103-
.map(|it| it.name(db).map(|name| name.to_string())),
104-
)
105-
.chain(once(definition_owner_name(db, def)))
106-
.flatten()
107-
.join("::")
97+
once(
98+
db.crate_graph()[module.krate().into()]
99+
.display_name
100+
.as_ref()
101+
.map(|name| name.get_name()),
102+
)
103+
.chain(
104+
module
105+
.path_to_root(db)
106+
.into_iter()
107+
.rev()
108+
.map(|it| it.name(db).map(|name| name.to_string())),
109+
)
110+
.chain(once(definition_owner_name(db, def)))
111+
.flatten()
112+
.join("::")
108113
});
109114
mod_path // FIXME: replace dashes with underscores in crate display name
110115
}

crates/ra_ide/src/mock_analysis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl MockAnalysis {
109109
let other_crate = crate_graph.add_crate_root(
110110
file_id,
111111
Edition2018,
112-
Some(crate_name.to_owned()),
112+
Some(CrateName::new(crate_name).unwrap()),
113113
cfg_options,
114114
Env::default(),
115115
Default::default(),

crates/ra_project_model/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ impl ProjectWorkspace {
245245
let crate_id = crate_graph.add_crate_root(
246246
file_id,
247247
Edition::Edition2018,
248-
Some(krate.name(&sysroot).to_string()),
248+
Some(
249+
CrateName::new(krate.name(&sysroot))
250+
.expect("Sysroot crate names should not contain dashes"),
251+
),
249252
cfg_options,
250253
env,
251254
extern_source,
@@ -296,7 +299,7 @@ impl ProjectWorkspace {
296299
let crate_id = crate_graph.add_crate_root(
297300
file_id,
298301
edition,
299-
Some(pkg.name(&cargo).to_string()),
302+
Some(CrateName::normalize_dashes(pkg.name(&cargo))),
300303
cfg_options,
301304
env,
302305
extern_source,

0 commit comments

Comments
 (0)