Skip to content

Commit 579d8a7

Browse files
committed
adding doc and simplifying function
1 parent c073e6d commit 579d8a7

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

crates/rust-analyzer/src/handlers.rs

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,18 @@ pub(crate) fn fetch_dependency_list(
134134
_params: lsp_ext::FetchDependencyListParams,
135135
) -> Result<lsp_ext::FetchDependencyListResult> {
136136
let crates = state.analysis.fetch_crates()?;
137-
Ok(FetchDependencyListResult {
138-
crates: crates
139-
.into_iter()
140-
.filter_map(|it| {
141-
let root_file_path = state.file_id_to_file_path(it.root_file_id);
142-
crate_path(it.name.as_ref(), root_file_path).map(|crate_path| CrateInfoResult {
143-
name: it.name,
144-
version: it.version,
145-
path: crate_path.to_string(),
146-
})
137+
let crate_infos = crates
138+
.into_iter()
139+
.filter_map(|it| {
140+
let root_file_path = state.file_id_to_file_path(it.root_file_id);
141+
crate_path(it.name.as_ref(), root_file_path).map(|path| CrateInfoResult {
142+
name: it.name,
143+
version: it.version,
144+
path: path.to_string(),
147145
})
148-
.collect(),
149-
})
146+
})
147+
.collect();
148+
Ok(FetchDependencyListResult { crates: crate_infos })
150149
}
151150

152151
pub(crate) fn handle_shuffle_crate_graph(state: &mut GlobalState, _: ()) -> Result<()> {
@@ -1190,7 +1189,7 @@ pub(crate) fn handle_code_action_resolve(
11901189
"Failed to parse action id string '{}': {e}",
11911190
params.id
11921191
))
1193-
.into())
1192+
.into());
11941193
}
11951194
};
11961195

@@ -1210,14 +1209,14 @@ pub(crate) fn handle_code_action_resolve(
12101209
"Failed to find the assist for index {} provided by the resolve request. Resolve request assist id: {}",
12111210
assist_index, params.id,
12121211
))
1213-
.into())
1212+
.into())
12141213
};
12151214
if assist.id.0 != expected_assist_id || assist.id.1 != expected_kind {
12161215
return Err(invalid_params_error(format!(
12171216
"Mismatching assist at index {} for the resolve parameters given. Resolve request assist id: {}, actual id: {:?}.",
12181217
assist_index, params.id, assist.id
12191218
))
1220-
.into());
1219+
.into());
12211220
}
12221221
let ca = to_proto::code_action(&snap, assist.clone(), None)?;
12231222
code_action.edit = ca.edit;
@@ -1287,7 +1286,7 @@ pub(crate) fn handle_code_lens_resolve(
12871286
snap: GlobalStateSnapshot,
12881287
code_lens: CodeLens,
12891288
) -> Result<CodeLens> {
1290-
let Some(annotation) = from_proto::annotation(&snap, code_lens.clone())? else { return Ok(code_lens) };
1289+
let Some(annotation) = from_proto::annotation(&snap, code_lens.clone())? else { return Ok(code_lens); };
12911290
let annotation = snap.analysis.resolve_annotation(annotation)?;
12921291

12931292
let mut acc = Vec::new();
@@ -1934,23 +1933,33 @@ fn run_rustfmt(
19341933
}
19351934
}
19361935

1937-
//Thats a best effort to try and find the crate path
1936+
/// Searches for the directory of a Rust crate with a given name in the directory tree
1937+
/// of the root file of that crate.
1938+
///
1939+
/// # Arguments
1940+
///
1941+
/// * `crate_name`: The name of the crate to search for. This should be a `Some` value if
1942+
/// a crate name has been specified, or `None` if no crate name has been specified.
1943+
/// * `root_file_path`: The path to the root file of the crate.
1944+
///
1945+
/// # Returns
1946+
///
1947+
/// An `Option` value representing the path to the directory of the crate with the given
1948+
/// name, if such a crate is found. If no crate with the given name is found, this function
1949+
/// returns `None`.
19381950
fn crate_path(crate_name: Option<&String>, root_file_path: VfsPath) -> Option<VfsPath> {
19391951
crate_name.and_then(|crate_name| {
1940-
let mut crate_path = None;
19411952
let mut root_path = root_file_path;
19421953
while let Some(path) = root_path.parent() {
1943-
match path.name_and_extension() {
1944-
Some((name, _)) => {
1945-
if name.starts_with(crate_name.as_str()) {
1946-
crate_path = Some(path);
1947-
break;
1948-
}
1954+
if let Some((name, _)) = path.name_and_extension() {
1955+
if name.starts_with(crate_name.as_str()) {
1956+
return Some(path);
19491957
}
1950-
None => break,
1958+
} else {
1959+
break;
19511960
}
19521961
root_path = path;
19531962
}
1954-
crate_path
1963+
None
19551964
})
19561965
}

0 commit comments

Comments
 (0)