accepting review suggestions

This commit is contained in:
Bruno Ortiz 2023-04-13 13:06:43 -03:00
parent c372fb3495
commit 66fe84d936
13 changed files with 67 additions and 97 deletions

View file

@ -165,7 +165,6 @@ impl ChangeFixture {
meta.edition, meta.edition,
Some(crate_name.clone().into()), Some(crate_name.clone().into()),
version, version,
None,
meta.cfg, meta.cfg,
Default::default(), Default::default(),
meta.env, meta.env,
@ -206,7 +205,6 @@ impl ChangeFixture {
Edition::CURRENT, Edition::CURRENT,
Some(CrateName::new("test").unwrap().into()), Some(CrateName::new("test").unwrap().into()),
None, None,
None,
default_cfg, default_cfg,
Default::default(), Default::default(),
Env::default(), Env::default(),
@ -251,7 +249,6 @@ impl ChangeFixture {
Edition::Edition2021, Edition::Edition2021,
Some(CrateDisplayName::from_canonical_name("core".to_string())), Some(CrateDisplayName::from_canonical_name("core".to_string())),
None, None,
None,
Default::default(), Default::default(),
Default::default(), Default::default(),
Env::default(), Env::default(),
@ -291,7 +288,6 @@ impl ChangeFixture {
Edition::Edition2021, Edition::Edition2021,
Some(CrateDisplayName::from_canonical_name("proc_macros".to_string())), Some(CrateDisplayName::from_canonical_name("proc_macros".to_string())),
None, None,
None,
Default::default(), Default::default(),
Default::default(), Default::default(),
Env::default(), Env::default(),

View file

@ -304,7 +304,6 @@ pub struct CrateData {
/// For purposes of analysis, crates are anonymous (only names in /// For purposes of analysis, crates are anonymous (only names in
/// `Dependency` matters), this name should only be used for UI. /// `Dependency` matters), this name should only be used for UI.
pub display_name: Option<CrateDisplayName>, pub display_name: Option<CrateDisplayName>,
pub crate_root_path: Option<AbsPathBuf>,
pub cfg_options: CfgOptions, pub cfg_options: CfgOptions,
/// The cfg options that could be used by the crate /// The cfg options that could be used by the crate
pub potential_cfg_options: Option<CfgOptions>, pub potential_cfg_options: Option<CfgOptions>,
@ -362,7 +361,6 @@ impl CrateGraph {
edition: Edition, edition: Edition,
display_name: Option<CrateDisplayName>, display_name: Option<CrateDisplayName>,
version: Option<String>, version: Option<String>,
crate_root_path: Option<AbsPathBuf>,
cfg_options: CfgOptions, cfg_options: CfgOptions,
potential_cfg_options: Option<CfgOptions>, potential_cfg_options: Option<CfgOptions>,
env: Env, env: Env,
@ -376,7 +374,6 @@ impl CrateGraph {
edition, edition,
version, version,
display_name, display_name,
crate_root_path,
cfg_options, cfg_options,
potential_cfg_options, potential_cfg_options,
env, env,
@ -743,7 +740,6 @@ mod tests {
Edition2018, Edition2018,
None, None,
None, None,
None,
Default::default(), Default::default(),
Default::default(), Default::default(),
Env::default(), Env::default(),
@ -757,7 +753,6 @@ mod tests {
Edition2018, Edition2018,
None, None,
None, None,
None,
Default::default(), Default::default(),
Default::default(), Default::default(),
Env::default(), Env::default(),
@ -771,7 +766,6 @@ mod tests {
Edition2018, Edition2018,
None, None,
None, None,
None,
Default::default(), Default::default(),
Default::default(), Default::default(),
Env::default(), Env::default(),
@ -799,7 +793,6 @@ mod tests {
Edition2018, Edition2018,
None, None,
None, None,
None,
Default::default(), Default::default(),
Default::default(), Default::default(),
Env::default(), Env::default(),
@ -813,7 +806,6 @@ mod tests {
Edition2018, Edition2018,
None, None,
None, None,
None,
Default::default(), Default::default(),
Default::default(), Default::default(),
Env::default(), Env::default(),
@ -838,7 +830,6 @@ mod tests {
Edition2018, Edition2018,
None, None,
None, None,
None,
Default::default(), Default::default(),
Default::default(), Default::default(),
Env::default(), Env::default(),
@ -852,7 +843,6 @@ mod tests {
Edition2018, Edition2018,
None, None,
None, None,
None,
Default::default(), Default::default(),
Default::default(), Default::default(),
Env::default(), Env::default(),
@ -866,7 +856,6 @@ mod tests {
Edition2018, Edition2018,
None, None,
None, None,
None,
Default::default(), Default::default(),
Default::default(), Default::default(),
Env::default(), Env::default(),
@ -891,7 +880,6 @@ mod tests {
Edition2018, Edition2018,
None, None,
None, None,
None,
Default::default(), Default::default(),
Default::default(), Default::default(),
Env::default(), Env::default(),
@ -905,7 +893,6 @@ mod tests {
Edition2018, Edition2018,
None, None,
None, None,
None,
Default::default(), Default::default(),
Default::default(), Default::default(),
Env::default(), Env::default(),

View file

@ -1,13 +1,13 @@
use ide_db::{ use ide_db::{
base_db::{CrateOrigin, SourceDatabase}, base_db::{CrateOrigin, FileId, SourceDatabase},
FxIndexSet, RootDatabase, FxIndexSet, RootDatabase,
}; };
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CrateInfo { pub struct CrateInfo {
pub name: String, pub name: Option<String>,
pub version: String, pub version: Option<String>,
pub path: String, pub root_file_id: FileId,
} }
// Feature: Show Dependency Tree // Feature: Show Dependency Tree
@ -22,24 +22,16 @@ pub(crate) fn fetch_crates(db: &RootDatabase) -> FxIndexSet<CrateInfo> {
.iter() .iter()
.map(|crate_id| &crate_graph[crate_id]) .map(|crate_id| &crate_graph[crate_id])
.filter(|&data| !matches!(data.origin, CrateOrigin::Local { .. })) .filter(|&data| !matches!(data.origin, CrateOrigin::Local { .. }))
.filter_map(|data| crate_info(data)) .map(|data| crate_info(data))
.collect() .collect()
} }
fn crate_info(data: &ide_db::base_db::CrateData) -> Option<CrateInfo> { fn crate_info(data: &ide_db::base_db::CrateData) -> CrateInfo {
let crate_name = crate_name(data); let crate_name = crate_name(data);
let crate_path = data.crate_root_path.as_ref().map(|p| p.display().to_string()); let version = data.version.clone();
if let Some(crate_path) = crate_path { CrateInfo { name: crate_name, version, root_file_id: data.root_file_id }
let version = data.version.clone().unwrap_or_else(|| "".to_owned());
Some(CrateInfo { name: crate_name, version, path: crate_path })
} else {
None
}
} }
fn crate_name(data: &ide_db::base_db::CrateData) -> String { fn crate_name(data: &ide_db::base_db::CrateData) -> Option<String> {
data.display_name data.display_name.as_ref().map(|it| it.canonical_name().to_owned())
.clone()
.map(|it| it.canonical_name().to_owned())
.unwrap_or("unknown".to_string())
} }

View file

@ -239,7 +239,6 @@ impl Analysis {
Edition::CURRENT, Edition::CURRENT,
None, None,
None, None,
None,
cfg_options.clone(), cfg_options.clone(),
None, None,
Env::default(), Env::default(),

View file

@ -34,7 +34,6 @@ pub(crate) fn shuffle_crate_graph(db: &mut RootDatabase) {
data.edition, data.edition,
data.display_name.clone(), data.display_name.clone(),
data.version.clone(), data.version.clone(),
data.crate_root_path.clone(),
data.cfg_options.clone(), data.cfg_options.clone(),
data.potential_cfg_options.clone(), data.potential_cfg_options.clone(),
data.env.clone(), data.env.clone(),

View file

@ -184,6 +184,13 @@ impl AbsPath {
self.0.ends_with(&suffix.0) self.0.ends_with(&suffix.0)
} }
pub fn name_and_extension(&self) -> Option<(&str, Option<&str>)> {
Some((
self.file_stem()?.to_str()?,
self.extension().and_then(|extension| extension.to_str()),
))
}
// region:delegate-methods // region:delegate-methods
// Note that we deliberately don't implement `Deref<Target = Path>` here. // Note that we deliberately don't implement `Deref<Target = Path>` here.
@ -213,13 +220,6 @@ impl AbsPath {
pub fn exists(&self) -> bool { pub fn exists(&self) -> bool {
self.0.exists() self.0.exists()
} }
pub fn name_and_extension(&self) -> Option<(&str, Option<&str>)> {
Some((
self.file_stem()?.to_str()?,
self.extension().and_then(|extension| extension.to_str()),
))
}
// endregion:delegate-methods // endregion:delegate-methods
} }

View file

@ -766,7 +766,6 @@ fn project_json_to_crate_graph(
proc_macro_dylib_path, proc_macro_dylib_path,
is_proc_macro, is_proc_macro,
repository, repository,
root_module,
.. ..
}, },
file_id, file_id,
@ -785,7 +784,6 @@ fn project_json_to_crate_graph(
*edition, *edition,
display_name.clone(), display_name.clone(),
version.clone(), version.clone(),
crate_path(display_name.as_ref(), root_module),
target_cfgs.iter().chain(cfg.iter()).cloned().collect(), target_cfgs.iter().chain(cfg.iter()).cloned().collect(),
None, None,
env, env,
@ -834,30 +832,6 @@ fn project_json_to_crate_graph(
res res
} }
//Thats a best effort to try and find the crate path for a project configured using JsonProject model
fn crate_path(
crate_name: Option<&CrateDisplayName>,
root_module_path: &AbsPathBuf,
) -> Option<AbsPathBuf> {
crate_name.and_then(|crate_name| {
let mut crate_path = None;
let mut root_path = root_module_path.as_path();
while let Some(path) = root_path.parent() {
match path.name_and_extension() {
Some((name, _)) => {
if name.starts_with(crate_name.canonical_name()) {
crate_path = Some(path.to_path_buf());
break;
}
}
None => break,
}
root_path = path;
}
crate_path
})
}
fn cargo_to_crate_graph( fn cargo_to_crate_graph(
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>, load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
rustc: Option<&(CargoWorkspace, WorkspaceBuildScripts)>, rustc: Option<&(CargoWorkspace, WorkspaceBuildScripts)>,
@ -1079,7 +1053,6 @@ fn detached_files_to_crate_graph(
Edition::CURRENT, Edition::CURRENT,
display_name.clone(), display_name.clone(),
None, None,
None,
cfg_options.clone(), cfg_options.clone(),
None, None,
Env::default(), Env::default(),
@ -1276,7 +1249,6 @@ fn add_target_crate_root(
edition, edition,
Some(display_name), Some(display_name),
Some(pkg.version.to_string()), Some(pkg.version.to_string()),
Some(pkg.manifest.parent().to_owned()),
cfg_options, cfg_options,
potential_cfg_options, potential_cfg_options,
env, env,
@ -1345,27 +1317,24 @@ fn sysroot_to_crate_graph(
.filter_map(|krate| { .filter_map(|krate| {
let file_id = load(&sysroot[krate].root)?; let file_id = load(&sysroot[krate].root)?;
let env = Env::default(); let env = Env::default();
let display_name = let display_name = CrateDisplayName::from_canonical_name(sysroot[krate].name.clone());
CrateDisplayName::from_canonical_name(sysroot[krate].name.clone());
let crate_root_path = sysroot.src_root().join(display_name.canonical_name());
let crate_id = crate_graph.add_crate_root( let crate_id = crate_graph.add_crate_root(
file_id, file_id,
Edition::CURRENT, Edition::CURRENT,
Some(display_name), Some(display_name),
None, None,
Some(crate_root_path), cfg_options.clone(),
cfg_options.clone(), None,
None, env,
env, false,
false, CrateOrigin::Lang(LangCrateOrigin::from(&*sysroot[krate].name)),
CrateOrigin::Lang(LangCrateOrigin::from(&*sysroot[krate].name)), target_layout.clone(),
target_layout.clone(), channel,
channel, );
); Some((krate, crate_id))
Some((krate, crate_id)) })
}) .collect(),
.collect(),
}; };
for from in sysroot.crates() { for from in sysroot.crates() {
for &to in sysroot[from].deps.iter() { for &to in sysroot[from].deps.iter() {

View file

@ -57,7 +57,35 @@ pub(crate) fn fetch_dependency_list(
Ok(FetchDependencyListResult { Ok(FetchDependencyListResult {
crates: crates crates: crates
.into_iter() .into_iter()
.map(|it| CrateInfoResult { name: it.name, version: it.version, path: it.path }) .filter_map(|it| {
let root_file_path = state.file_id_to_file_path(it.root_file_id);
crate_path(it.name.as_ref(), root_file_path).map(|crate_path| CrateInfoResult {
name: it.name,
version: it.version,
path: crate_path.to_string(),
})
})
.collect(), .collect(),
}) })
} }
//Thats a best effort to try and find the crate path
fn crate_path(crate_name: Option<&String>, root_file_path: VfsPath) -> Option<VfsPath> {
crate_name.and_then(|crate_name| {
let mut crate_path = None;
let mut root_path = root_file_path;
while let Some(path) = root_path.parent() {
match path.name_and_extension() {
Some((name, _)) => {
if name.starts_with(crate_name.as_str()) {
crate_path = Some(path);
break;
}
}
None => break,
}
root_path = path;
}
crate_path
})
}

View file

@ -30,8 +30,8 @@ pub struct AnalyzerStatusParams {
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct CrateInfoResult { pub struct CrateInfoResult {
pub name: String, pub name: Option<String>,
pub version: String, pub version: Option<String>,
pub path: String, pub path: String,
} }
pub enum FetchDependencyList {} pub enum FetchDependencyList {}

View file

@ -45,7 +45,7 @@ fn check_lsp_extensions_docs() {
sh.read_file(sourcegen::project_root().join("docs/dev/lsp-extensions.md")).unwrap(); sh.read_file(sourcegen::project_root().join("docs/dev/lsp-extensions.md")).unwrap();
let text = lsp_extensions_md let text = lsp_extensions_md
.lines() .lines()
.find_map(|line| dbg!(line.strip_prefix("lsp_ext.rs hash:"))) .find_map(|line| line.strip_prefix("lsp_ext.rs hash:"))
.unwrap() .unwrap()
.trim(); .trim();
u64::from_str_radix(text, 16).unwrap() u64::from_str_radix(text, 16).unwrap()

View file

@ -860,12 +860,12 @@ export interface Diagnostic {
**Request:** **Request:**
```typescript ```typescript
export interface FetchDependencyGraphParams {} export interface FetchDependencyListParams {}
``` ```
**Response:** **Response:**
```typescript ```typescript
export interface FetchDependencyGraphResult { export interface FetchDependencyListResult {
crates: { crates: {
name: string; name: string;
version: string; version: string;

View file

@ -88,7 +88,7 @@ export class RustDependenciesProvider
); );
const crates = dependenciesResult.crates; const crates = dependenciesResult.crates;
const deps = crates.map((crate) => { const deps = crates.map((crate) => {
const dep = this.toDep(crate.name, crate.version, crate.path); const dep = this.toDep(crate.name || "unknown", crate.version || "", crate.path);
this.dependenciesMap[dep.dependencyPath.toLowerCase()] = dep; this.dependenciesMap[dep.dependencyPath.toLowerCase()] = dep;
this.dependenciesMap[stdlib.dependencyPath.toLowerCase()] = stdlib; this.dependenciesMap[stdlib.dependencyPath.toLowerCase()] = stdlib;
return dep; return dep;

View file

@ -74,8 +74,8 @@ export interface FetchDependencyListParams {}
export interface FetchDependencyListResult { export interface FetchDependencyListResult {
crates: { crates: {
name: string; name: string | undefined;
version: string; version: string | undefined;
path: string; path: string;
}[]; }[];
} }