Skip to content

Commit 800b3b6

Browse files
committed
adding doc and simplifying function
1 parent 072f69e commit 800b3b6

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

crates/rust-analyzer/src/handlers.rs

+33-24
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,50 @@ pub(crate) fn publish_diagnostics(
5151

5252
pub(crate) fn fetch_dependency_list(
5353
state: GlobalStateSnapshot,
54-
_params: FetchDependencyListParams,
55-
) -> Result<FetchDependencyListResult> {
54+
_params: lsp_ext::FetchDependencyListParams,
55+
) -> Result<lsp_ext::FetchDependencyListResult> {
5656
let crates = state.analysis.fetch_crates()?;
57-
Ok(FetchDependencyListResult {
58-
crates: crates
59-
.into_iter()
60-
.filter_map(|it| {
61-
let root_file_path = state.file_id_to_file_path(it.root_file_id);
62-
crate_path(it.name.as_ref(), root_file_path).map(|crate_path| CrateInfoResult {
63-
name: it.name,
64-
version: it.version,
65-
path: crate_path.to_string(),
66-
})
57+
let crate_infos = crates
58+
.into_iter()
59+
.filter_map(|it| {
60+
let root_file_path = state.file_id_to_file_path(it.root_file_id);
61+
crate_path(it.name.as_ref(), root_file_path).map(|path| CrateInfoResult {
62+
name: it.name,
63+
version: it.version,
64+
path: path.to_string(),
6765
})
68-
.collect(),
69-
})
66+
})
67+
.collect();
68+
Ok(FetchDependencyListResult { crates: crate_infos })
7069
}
7170

72-
//Thats a best effort to try and find the crate path
71+
/// Searches for the directory of a Rust crate with a given name in the directory tree
72+
/// of the root file of that crate.
73+
///
74+
/// # Arguments
75+
///
76+
/// * `crate_name`: The name of the crate to search for. This should be a `Some` value if
77+
/// a crate name has been specified, or `None` if no crate name has been specified.
78+
/// * `root_file_path`: The path to the root file of the crate.
79+
///
80+
/// # Returns
81+
///
82+
/// An `Option` value representing the path to the directory of the crate with the given
83+
/// name, if such a crate is found. If no crate with the given name is found, this function
84+
/// returns `None`.
7385
fn crate_path(crate_name: Option<&String>, root_file_path: VfsPath) -> Option<VfsPath> {
7486
crate_name.and_then(|crate_name| {
75-
let mut crate_path = None;
7687
let mut root_path = root_file_path;
7788
while let Some(path) = root_path.parent() {
78-
match path.name_and_extension() {
79-
Some((name, _)) => {
80-
if name.starts_with(crate_name.as_str()) {
81-
crate_path = Some(path);
82-
break;
83-
}
89+
if let Some((name, _)) = path.name_and_extension() {
90+
if name.starts_with(crate_name.as_str()) {
91+
return Some(path);
8492
}
85-
None => break,
93+
} else {
94+
break;
8695
}
8796
root_path = path;
8897
}
89-
crate_path
98+
None
9099
})
91100
}

0 commit comments

Comments
 (0)