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

View file

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

View file

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

View file

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

View file

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

View file

@ -184,6 +184,13 @@ impl AbsPath {
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
// Note that we deliberately don't implement `Deref<Target = Path>` here.
@ -213,13 +220,6 @@ impl AbsPath {
pub fn exists(&self) -> bool {
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
}

View file

@ -766,7 +766,6 @@ fn project_json_to_crate_graph(
proc_macro_dylib_path,
is_proc_macro,
repository,
root_module,
..
},
file_id,
@ -785,7 +784,6 @@ fn project_json_to_crate_graph(
*edition,
display_name.clone(),
version.clone(),
crate_path(display_name.as_ref(), root_module),
target_cfgs.iter().chain(cfg.iter()).cloned().collect(),
None,
env,
@ -834,30 +832,6 @@ fn project_json_to_crate_graph(
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(
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
rustc: Option<&(CargoWorkspace, WorkspaceBuildScripts)>,
@ -1079,7 +1053,6 @@ fn detached_files_to_crate_graph(
Edition::CURRENT,
display_name.clone(),
None,
None,
cfg_options.clone(),
None,
Env::default(),
@ -1276,7 +1249,6 @@ fn add_target_crate_root(
edition,
Some(display_name),
Some(pkg.version.to_string()),
Some(pkg.manifest.parent().to_owned()),
cfg_options,
potential_cfg_options,
env,
@ -1345,27 +1317,24 @@ fn sysroot_to_crate_graph(
.filter_map(|krate| {
let file_id = load(&sysroot[krate].root)?;
let env = Env::default();
let display_name =
CrateDisplayName::from_canonical_name(sysroot[krate].name.clone());
let crate_root_path = sysroot.src_root().join(display_name.canonical_name());
let env = Env::default();
let display_name = CrateDisplayName::from_canonical_name(sysroot[krate].name.clone());
let crate_id = crate_graph.add_crate_root(
file_id,
Edition::CURRENT,
Some(display_name),
None,
Some(crate_root_path),
cfg_options.clone(),
None,
env,
false,
CrateOrigin::Lang(LangCrateOrigin::from(&*sysroot[krate].name)),
target_layout.clone(),
channel,
);
Some((krate, crate_id))
})
.collect(),
cfg_options.clone(),
None,
env,
false,
CrateOrigin::Lang(LangCrateOrigin::from(&*sysroot[krate].name)),
target_layout.clone(),
channel,
);
Some((krate, crate_id))
})
.collect(),
};
for from in sysroot.crates() {
for &to in sysroot[from].deps.iter() {

View file

@ -57,7 +57,35 @@ pub(crate) fn fetch_dependency_list(
Ok(FetchDependencyListResult {
crates: crates
.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(),
})
}
//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)]
#[serde(rename_all = "camelCase")]
pub struct CrateInfoResult {
pub name: String,
pub version: String,
pub name: Option<String>,
pub version: Option<String>,
pub path: String,
}
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();
let text = lsp_extensions_md
.lines()
.find_map(|line| dbg!(line.strip_prefix("lsp_ext.rs hash:")))
.find_map(|line| line.strip_prefix("lsp_ext.rs hash:"))
.unwrap()
.trim();
u64::from_str_radix(text, 16).unwrap()

View file

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

View file

@ -88,7 +88,7 @@ export class RustDependenciesProvider
);
const crates = dependenciesResult.crates;
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[stdlib.dependencyPath.toLowerCase()] = stdlib;
return dep;

View file

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