Fix crate display name dashes

This commit is contained in:
Kirill Bulatov 2020-03-16 11:47:52 +02:00
parent 6bc226fa19
commit 059ed25a3e
6 changed files with 40 additions and 20 deletions

View file

@ -64,7 +64,9 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
crate_graph.add_crate_root(
file_id,
meta.edition,
meta.krate,
meta.krate.map(|name| {
CrateName::new(&name).expect("Fixture crate name should not contain dashes")
}),
meta.cfg,
meta.env,
Default::default(),
@ -124,7 +126,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
let crate_id = crate_graph.add_crate_root(
file_id,
meta.edition,
Some(krate.clone()),
Some(CrateName::new(&krate).unwrap()),
meta.cfg,
meta.env,
Default::default(),

View file

@ -83,6 +83,7 @@ pub struct CrateGraph {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CrateId(pub u32);
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CrateName(SmolStr);
impl CrateName {
@ -101,6 +102,10 @@ impl CrateName {
pub fn normalize_dashes(name: &str) -> CrateName {
Self(SmolStr::new(name.replace('-', "_")))
}
pub fn get_name(&self) -> String {
self.0.to_string()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
@ -110,7 +115,7 @@ pub struct CrateData {
/// The name to display to the end user.
/// This actual crate name can be different in a particular dependent crate
/// or may even be missing for some cases, such as a dummy crate for the code snippet.
pub display_name: Option<String>,
pub display_name: Option<CrateName>,
pub cfg_options: CfgOptions,
pub env: Env,
pub extern_source: ExternSource,
@ -150,7 +155,7 @@ impl CrateGraph {
&mut self,
file_id: FileId,
edition: Edition,
display_name: Option<String>,
display_name: Option<CrateName>,
cfg_options: CfgOptions,
env: Env,
extern_source: ExternSource,

View file

@ -177,8 +177,13 @@ pub struct ModuleData {
impl CrateDefMap {
pub(crate) fn crate_def_map_query(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> {
let _p = profile("crate_def_map_query")
.detail(|| db.crate_graph()[krate].display_name.clone().unwrap_or_default());
let _p = profile("crate_def_map_query").detail(|| {
db.crate_graph()[krate]
.display_name
.as_ref()
.map(|name| name.get_name())
.unwrap_or_default()
});
let def_map = {
let edition = db.crate_graph()[krate].edition;
let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default();

View file

@ -94,17 +94,22 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String>
fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
let mod_path = def.module(db).map(|module| {
once(db.crate_graph()[module.krate().into()].display_name.clone())
.chain(
module
.path_to_root(db)
.into_iter()
.rev()
.map(|it| it.name(db).map(|name| name.to_string())),
)
.chain(once(definition_owner_name(db, def)))
.flatten()
.join("::")
once(
db.crate_graph()[module.krate().into()]
.display_name
.as_ref()
.map(|name| name.get_name()),
)
.chain(
module
.path_to_root(db)
.into_iter()
.rev()
.map(|it| it.name(db).map(|name| name.to_string())),
)
.chain(once(definition_owner_name(db, def)))
.flatten()
.join("::")
});
mod_path // FIXME: replace dashes with underscores in crate display name
}

View file

@ -109,7 +109,7 @@ impl MockAnalysis {
let other_crate = crate_graph.add_crate_root(
file_id,
Edition2018,
Some(crate_name.to_owned()),
Some(CrateName::new(crate_name).unwrap()),
cfg_options,
Env::default(),
Default::default(),

View file

@ -245,7 +245,10 @@ impl ProjectWorkspace {
let crate_id = crate_graph.add_crate_root(
file_id,
Edition::Edition2018,
Some(krate.name(&sysroot).to_string()),
Some(
CrateName::new(krate.name(&sysroot))
.expect("Sysroot crate names should not contain dashes"),
),
cfg_options,
env,
extern_source,
@ -296,7 +299,7 @@ impl ProjectWorkspace {
let crate_id = crate_graph.add_crate_root(
file_id,
edition,
Some(pkg.name(&cargo).to_string()),
Some(CrateName::normalize_dashes(pkg.name(&cargo))),
cfg_options,
env,
extern_source,