Merge pull request #1607 from rtfeldman/editor-let-value

file loading/saving/running, headers, top level defs
This commit is contained in:
Richard Feldman 2021-10-03 15:45:08 -05:00 committed by GitHub
commit e3a8d436cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
54 changed files with 6193 additions and 2959 deletions

View file

@ -551,7 +551,53 @@ impl IdentIds {
}
}
/// Generates a unique, new name that's just a stringified integer
// necessary when the name of a value is changed in the editor
pub fn update_key(
&mut self,
old_ident_name: &str,
new_ident_name: &str,
) -> Result<IdentId, String> {
let old_ident: Ident = old_ident_name.into();
let ident_id_ref_opt = self.by_ident.get(&old_ident);
match ident_id_ref_opt {
Some(ident_id_ref) => {
let ident_id = *ident_id_ref;
self.by_ident.remove(&old_ident);
self.by_ident.insert(new_ident_name.into(), ident_id);
let by_id = &mut self.by_id;
let key_index_opt = by_id.iter().position(|x| *x == old_ident);
if let Some(key_index) = key_index_opt {
if let Some(vec_elt) = by_id.get_mut(key_index) {
*vec_elt = new_ident_name.into();
} else {
// we get the index from by_id
unreachable!()
}
Ok(ident_id)
} else {
Err(
format!(
"Tried to find position of key {:?} in IdentIds.by_id but I could not find the key. IdentIds.by_id: {:?}",
old_ident_name,
self.by_id
)
)
}
}
None => Err(format!(
"Tried to update key in IdentIds ({:?}) but I could not find the key ({}).",
self.by_ident, old_ident_name
)),
}
}
/// Generates a unique, new name that's just a strigified integer
/// (e.g. "1" or "5"), using an internal counter. Since valid Roc variable
/// names cannot begin with a number, this has no chance of colliding
/// with actual user-defined variables.