diff --git a/crates/base-db/src/fixture.rs b/crates/base-db/src/fixture.rs index cf3be9d07f..1936eabdd9 100644 --- a/crates/base-db/src/fixture.rs +++ b/crates/base-db/src/fixture.rs @@ -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(), diff --git a/crates/base-db/src/input.rs b/crates/base-db/src/input.rs index 466e2eab56..e6d265df67 100644 --- a/crates/base-db/src/input.rs +++ b/crates/base-db/src/input.rs @@ -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, - pub crate_root_path: Option, pub cfg_options: CfgOptions, /// The cfg options that could be used by the crate pub potential_cfg_options: Option, @@ -362,7 +361,6 @@ impl CrateGraph { edition: Edition, display_name: Option, version: Option, - crate_root_path: Option, cfg_options: CfgOptions, potential_cfg_options: Option, 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(), diff --git a/crates/ide/src/fetch_crates.rs b/crates/ide/src/fetch_crates.rs index 916c26855a..d326b7c2cc 100644 --- a/crates/ide/src/fetch_crates.rs +++ b/crates/ide/src/fetch_crates.rs @@ -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, + pub version: Option, + pub root_file_id: FileId, } // Feature: Show Dependency Tree @@ -22,24 +22,16 @@ pub(crate) fn fetch_crates(db: &RootDatabase) -> FxIndexSet { .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 { +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 { + data.display_name.as_ref().map(|it| it.canonical_name().to_owned()) } diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 131c781bee..24e2aed65a 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -239,7 +239,6 @@ impl Analysis { Edition::CURRENT, None, None, - None, cfg_options.clone(), None, Env::default(), diff --git a/crates/ide/src/shuffle_crate_graph.rs b/crates/ide/src/shuffle_crate_graph.rs index 51ecc4001f..d94b15f60c 100644 --- a/crates/ide/src/shuffle_crate_graph.rs +++ b/crates/ide/src/shuffle_crate_graph.rs @@ -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(), diff --git a/crates/paths/src/lib.rs b/crates/paths/src/lib.rs index 6a3c685016..083dfcf43d 100644 --- a/crates/paths/src/lib.rs +++ b/crates/paths/src/lib.rs @@ -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` 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 } diff --git a/crates/project-model/src/workspace.rs b/crates/project-model/src/workspace.rs index ba5a1c4e35..102f862522 100644 --- a/crates/project-model/src/workspace.rs +++ b/crates/project-model/src/workspace.rs @@ -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 { - 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, 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() { diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 5c966f0f68..ae6dd4dd56 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -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 { + 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 + }) +} \ No newline at end of file diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index 6285578a6c..8689fae079 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -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, + pub version: Option, pub path: String, } pub enum FetchDependencyList {} diff --git a/crates/rust-analyzer/tests/slow-tests/tidy.rs b/crates/rust-analyzer/tests/slow-tests/tidy.rs index 91dde1c752..8e3097fce4 100644 --- a/crates/rust-analyzer/tests/slow-tests/tidy.rs +++ b/crates/rust-analyzer/tests/slow-tests/tidy.rs @@ -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() diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md index d73ae3d519..0fb92638e0 100644 --- a/docs/dev/lsp-extensions.md +++ b/docs/dev/lsp-extensions.md @@ -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; diff --git a/editors/code/src/dependencies_provider.ts b/editors/code/src/dependencies_provider.ts index aff5102e01..b1ea365886 100644 --- a/editors/code/src/dependencies_provider.ts +++ b/editors/code/src/dependencies_provider.ts @@ -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; diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index 30de5035d6..b72804e510 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -74,8 +74,8 @@ export interface FetchDependencyListParams {} export interface FetchDependencyListResult { crates: { - name: string; - version: string; + name: string | undefined; + version: string | undefined; path: string; }[]; }