mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 15:15:24 +00:00
Less abstract CrateData api
This commit is contained in:
parent
5cffef56e2
commit
e1aa96f2c5
9 changed files with 48 additions and 51 deletions
|
@ -104,13 +104,16 @@ impl CrateName {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct CrateData {
|
||||
file_id: FileId,
|
||||
edition: Edition,
|
||||
declaration_name: Option<String>,
|
||||
pub struct CrateData {
|
||||
pub root_file_id: FileId,
|
||||
pub edition: Edition,
|
||||
/// 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>,
|
||||
cfg_options: CfgOptions,
|
||||
env: Env,
|
||||
dependencies: Vec<Dependency>,
|
||||
pub dependencies: Vec<Dependency>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
|
@ -135,11 +138,11 @@ impl CrateGraph {
|
|||
&mut self,
|
||||
file_id: FileId,
|
||||
edition: Edition,
|
||||
declaration_name: Option<String>,
|
||||
display_name: Option<String>,
|
||||
cfg_options: CfgOptions,
|
||||
env: Env,
|
||||
) -> CrateId {
|
||||
let data = CrateData::new(file_id, edition, declaration_name, cfg_options, env);
|
||||
let data = CrateData::new(file_id, edition, display_name, cfg_options, env);
|
||||
let crate_id = CrateId(self.arena.len() as u32);
|
||||
let prev = self.arena.insert(crate_id, data);
|
||||
assert!(prev.is_none());
|
||||
|
@ -171,33 +174,17 @@ impl CrateGraph {
|
|||
self.arena.keys().copied()
|
||||
}
|
||||
|
||||
pub fn crate_root(&self, crate_id: CrateId) -> FileId {
|
||||
self.arena[&crate_id].file_id
|
||||
}
|
||||
|
||||
pub fn edition(&self, crate_id: CrateId) -> Edition {
|
||||
self.arena[&crate_id].edition
|
||||
}
|
||||
|
||||
/// Returns a name of a crate, declared in the root project.
|
||||
/// May be missing for some cases, such as when the crate definition was created for a code snippet.
|
||||
///
|
||||
/// This should not be considered as a normal crate name, since the actual name can be different in
|
||||
/// a particular dependent crate, where it is specified.
|
||||
pub fn declaration_name(&self, crate_id: &CrateId) -> Option<&String> {
|
||||
self.arena[crate_id].declaration_name.as_ref()
|
||||
pub fn crate_data(&self, crate_id: &CrateId) -> &CrateData {
|
||||
&self.arena[crate_id]
|
||||
}
|
||||
|
||||
// FIXME: this only finds one crate with the given root; we could have multiple
|
||||
pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
|
||||
let (&crate_id, _) = self.arena.iter().find(|(_crate_id, data)| data.file_id == file_id)?;
|
||||
let (&crate_id, _) =
|
||||
self.arena.iter().find(|(_crate_id, data)| data.root_file_id == file_id)?;
|
||||
Some(crate_id)
|
||||
}
|
||||
|
||||
pub fn dependencies(&self, crate_id: CrateId) -> impl Iterator<Item = &Dependency> {
|
||||
self.arena[&crate_id].dependencies.iter()
|
||||
}
|
||||
|
||||
/// Extends this crate graph by adding a complete disjoint second crate
|
||||
/// graph.
|
||||
///
|
||||
|
@ -220,7 +207,7 @@ impl CrateGraph {
|
|||
return false;
|
||||
}
|
||||
|
||||
for dep in self.dependencies(from) {
|
||||
for dep in &self.crate_data(&from).dependencies {
|
||||
let crate_id = dep.crate_id();
|
||||
if crate_id == target {
|
||||
return true;
|
||||
|
@ -242,13 +229,20 @@ impl CrateId {
|
|||
|
||||
impl CrateData {
|
||||
fn new(
|
||||
file_id: FileId,
|
||||
root_file_id: FileId,
|
||||
edition: Edition,
|
||||
declaration_name: Option<String>,
|
||||
display_name: Option<String>,
|
||||
cfg_options: CfgOptions,
|
||||
env: Env,
|
||||
) -> CrateData {
|
||||
CrateData { file_id, edition, declaration_name, dependencies: Vec::new(), cfg_options, env }
|
||||
CrateData {
|
||||
root_file_id,
|
||||
edition,
|
||||
display_name,
|
||||
dependencies: Vec::new(),
|
||||
cfg_options,
|
||||
env,
|
||||
}
|
||||
}
|
||||
|
||||
fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
|
||||
|
@ -382,8 +376,8 @@ mod tests {
|
|||
.add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)
|
||||
.is_ok());
|
||||
assert_eq!(
|
||||
graph.dependencies(crate1).collect::<Vec<_>>(),
|
||||
vec![&Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }]
|
||||
graph.crate_data(&crate1).dependencies,
|
||||
vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue