1
1
mirror of https://github.com/tweag/nickel.git synced 2024-10-06 08:07:37 +03:00

unwrap -> pattern matching

This commit is contained in:
Oghenevwogaga Ebresafe 2022-10-26 11:44:37 +01:00
parent 79a79ad742
commit 740efbad0e

View File

@ -113,7 +113,7 @@ fn get_completion_identifiers(
source: &str,
trigger: Option<&str>,
linearization: &Completed,
item: &LinearizationItem<Types>,
item: &LinearizationItem<Types>
) -> Result<Vec<CompletionItem>, ResponseError> {
let lin_env = &linearization.lin_env;
let in_scope = match trigger {
@ -124,10 +124,9 @@ fn get_completion_identifiers(
.iter()
.find(|(ident, _)| ident.label() == name)
.map(|(_, id)| *id);
// unsafe unwrap here
let name_id = name_id.unwrap();
linearization
match name_id {
Some(name_id) => linearization
.get_in_scope(item)
.iter()
.filter_map(|i| {
@ -147,7 +146,10 @@ fn get_completion_identifiers(
// Get record fields from contract metadata
TermKind::Declaration(_, _, ValueState::Known(body_id))
if name_id == i.id
&& find_record_contract(&linearization.linearization, body_id)
&& find_record_contract(
&linearization.linearization,
body_id,
)
.is_some() =>
{
// unwrapping is safe here because of the pattern condition.
@ -173,7 +175,9 @@ fn get_completion_identifiers(
_ => None,
}
})
.collect()
.collect(),
None => return Ok(Vec::new())
}
}
// variable name completion
@ -220,6 +224,8 @@ pub fn handle_completion(
)
.unwrap();
// -1 because at the current cursor position, there is no linearization item there,
// so we try to get the item just before the cursor.
let locator = (file_id, ByteIndex((start - 1) as u32));
let linearization = server.lin_cache_get(&file_id)?;
// Cloning to avoid mutable borrow and shared borrow at the same time,