Skip to content

Commit 9d19e5b

Browse files
Properly name the field
1 parent 99952f3 commit 9d19e5b

File tree

8 files changed

+26
-24
lines changed

8 files changed

+26
-24
lines changed

crates/base_db/src/input.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,11 @@ impl PartialEq for ProcMacro {
127127
pub struct CrateData {
128128
pub root_file_id: FileId,
129129
pub edition: Edition,
130-
/// The name to display to the end user.
131-
/// This actual crate name can be different in a particular dependent crate
132-
/// or may even be missing for some cases, such as a dummy crate for the code snippet.
133-
pub display_name: Option<CrateName>,
130+
/// A name used in the package's project declaration: for Cargo projects, it's [package].name,
131+
/// can be different for other project types or even absent (a dummy crate for the code snippet, for example).
132+
/// NOTE: The crate can be referenced as a dependency under a different name,
133+
/// this one should be used when working with crate hierarchies.
134+
pub declaration_name: Option<CrateName>,
134135
pub cfg_options: CfgOptions,
135136
pub env: Env,
136137
pub dependencies: Vec<Dependency>,
@@ -159,7 +160,7 @@ impl CrateGraph {
159160
&mut self,
160161
file_id: FileId,
161162
edition: Edition,
162-
display_name: Option<CrateName>,
163+
declaration_name: Option<CrateName>,
163164
cfg_options: CfgOptions,
164165
env: Env,
165166
proc_macro: Vec<(SmolStr, Arc<dyn tt::TokenExpander>)>,
@@ -170,7 +171,7 @@ impl CrateGraph {
170171
let data = CrateData {
171172
root_file_id: file_id,
172173
edition,
173-
display_name,
174+
declaration_name,
174175
cfg_options,
175176
env,
176177
proc_macro,

crates/hir/src/code_model.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ impl Crate {
9898
db.crate_graph()[self.id].edition
9999
}
100100

101-
pub fn display_name(self, db: &dyn HirDatabase) -> Option<CrateName> {
102-
db.crate_graph()[self.id].display_name.clone()
101+
pub fn declaration_name(self, db: &dyn HirDatabase) -> Option<CrateName> {
102+
db.crate_graph()[self.id].declaration_name.clone()
103103
}
104104

105105
pub fn query_external_importables(

crates/hir_def/src/import_map.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,14 @@ mod tests {
334334

335335
use super::*;
336336

337-
fn check_search(ra_fixture: &str, krate_name: &str, query: Query, expect: Expect) {
337+
fn check_search(ra_fixture: &str, crate_name: &str, query: Query, expect: Expect) {
338338
let db = TestDB::with_files(ra_fixture);
339339
let crate_graph = db.crate_graph();
340340
let krate = crate_graph
341341
.iter()
342342
.find(|krate| {
343-
crate_graph[*krate].display_name.as_ref().map(|n| n.to_string())
344-
== Some(krate_name.to_string())
343+
crate_graph[*krate].declaration_name.as_ref().map(|n| n.to_string())
344+
== Some(crate_name.to_string())
345345
})
346346
.unwrap();
347347

@@ -359,7 +359,7 @@ mod tests {
359359
let path = map.path_of(item).unwrap();
360360
format!(
361361
"{}::{} ({})\n",
362-
crate_graph[krate].display_name.as_ref().unwrap(),
362+
crate_graph[krate].declaration_name.as_ref().unwrap(),
363363
path,
364364
mark
365365
)
@@ -400,7 +400,7 @@ mod tests {
400400
.iter()
401401
.filter_map(|krate| {
402402
let cdata = &crate_graph[krate];
403-
let name = cdata.display_name.as_ref()?;
403+
let name = cdata.declaration_name.as_ref()?;
404404

405405
let map = db.import_map(krate);
406406

crates/hir_def/src/nameres.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl CrateDefMap {
173173
pub(crate) fn crate_def_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<CrateDefMap> {
174174
let _p = profile::span("crate_def_map_query").detail(|| {
175175
db.crate_graph()[krate]
176-
.display_name
176+
.declaration_name
177177
.as_ref()
178178
.map(ToString::to_string)
179179
.unwrap_or_default()

crates/ide/src/hover.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String>
289289

290290
fn render_path(db: &RootDatabase, module: Module, item_name: Option<String>) -> String {
291291
let crate_name =
292-
db.crate_graph()[module.krate().into()].display_name.as_ref().map(ToString::to_string);
292+
db.crate_graph()[module.krate().into()].declaration_name.as_ref().map(ToString::to_string);
293293
let module_path = module
294294
.path_to_root(db)
295295
.into_iter()

crates/ide/src/link_rewrite.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn rewrite_intra_doc_link(
107107
let krate = resolved.module(db)?.krate();
108108
let canonical_path = resolved.canonical_path(db)?;
109109
let new_target = get_doc_url(db, &krate)?
110-
.join(&format!("{}/", krate.display_name(db)?))
110+
.join(&format!("{}/", krate.declaration_name(db)?))
111111
.ok()?
112112
.join(&canonical_path.replace("::", "/"))
113113
.ok()?
@@ -127,7 +127,7 @@ fn rewrite_url_link(db: &RootDatabase, def: ModuleDef, target: &str) -> Option<S
127127
let module = def.module(db)?;
128128
let krate = module.krate();
129129
let canonical_path = def.canonical_path(db)?;
130-
let base = format!("{}/{}", krate.display_name(db)?, canonical_path.replace("::", "/"));
130+
let base = format!("{}/{}", krate.declaration_name(db)?, canonical_path.replace("::", "/"));
131131

132132
get_doc_url(db, &krate)
133133
.and_then(|url| url.join(&base).ok())
@@ -248,7 +248,7 @@ fn get_doc_url(db: &RootDatabase, krate: &Crate) -> Option<Url> {
248248
//
249249
// FIXME: clicking on the link should just open the file in the editor,
250250
// instead of falling back to external urls.
251-
Some(format!("https://docs.rs/{}/*/", krate.display_name(db)?))
251+
Some(format!("https://docs.rs/{}/*/", krate.declaration_name(db)?))
252252
})
253253
.and_then(|s| Url::parse(&s).ok())
254254
}

crates/ide/src/status.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {
4545
match krate {
4646
Some(krate) => {
4747
let crate_graph = db.crate_graph();
48-
let display_crate = |krate: CrateId| match &crate_graph[krate].display_name {
48+
let display_crate = |krate: CrateId| match &crate_graph[krate].declaration_name {
4949
Some(it) => format!("{}({:?})", it, krate),
5050
None => format!("{:?}", krate),
5151
};

crates/rust-analyzer/src/cli/diagnostics.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ pub fn diagnostics(path: &Path, load_output_dirs: bool, with_proc_macro: bool) -
3636
for module in work {
3737
let file_id = module.definition_source(db).file_id.original_file(db);
3838
if !visited_files.contains(&file_id) {
39-
let crate_name = if let Some(name) = module.krate().display_name(db) {
40-
format!("{}", name)
41-
} else {
42-
String::from("unknown")
43-
};
39+
let crate_name = module
40+
.krate()
41+
.declaration_name(db)
42+
.as_ref()
43+
.map(ToString::to_string)
44+
.unwrap_or_else(|| "unknown".to_string());
4445
println!("processing crate: {}, module: {}", crate_name, _vfs.file_path(file_id));
4546
for diagnostic in analysis.diagnostics(&DiagnosticsConfig::default(), file_id).unwrap()
4647
{

0 commit comments

Comments
 (0)