Variables: Import Data Sets and Load Variable Library record their step through edit_variables

This commit is contained in:
bflatastic
2026-11-02 12:03:49 +07:00
parent a6ce22da5c
commit 78014a1b43
2 changed files with 21 additions and 29 deletions
+14 -18
View File
@@ -324,26 +324,22 @@ pub(super) fn dataset_import(s: &mut Session, p: &Value) -> Result<Value> {
}
let names: Vec<String> = parsed.iter().map(|d| d.name.clone()).collect();
let new_variables: Vec<String> = defined.iter().map(|v| v.name.clone()).collect();
let mut vars = s.doc()?.doc.variables.clone();
vars.variables.extend(defined);
let (mut imported, mut updated) = (0usize, 0usize);
for row in parsed {
if let Some(existing) = vars.datasets.iter_mut().find(|x| x.name == row.name) {
*existing = row;
updated += 1;
} else {
vars.datasets.push(row);
imported += 1;
}
}
// The import is one undo step, and an import that leaves the variables and data sets as they
// were adds none.
if vars != s.doc()?.doc.variables {
s.edit("Import Data Sets", |d, _| {
d.variables = vars;
Ok(())
})?;
}
let (imported, updated) = super::edit_variables(s, "Import Data Sets", |vars| {
vars.variables.extend(defined);
let (mut imported, mut updated) = (0usize, 0usize);
for row in parsed {
if let Some(existing) = vars.datasets.iter_mut().find(|x| x.name == row.name) {
*existing = row;
updated += 1;
} else {
vars.datasets.push(row);
imported += 1;
}
}
Ok((imported, updated))
})?;
Ok(json!({"imported": imported, "updated": updated, "names": names, "defined": new_variables}))
}
+7 -11
View File
@@ -269,18 +269,14 @@ pub(super) fn load_library(s: &mut Session, p: &Value) -> Result<Value> {
}
let added: Vec<Variable> = lib.text.iter().map(|n| Variable { name: n.clone(), kind: VariableKind::Text }).collect();
let names: Vec<String> = lib.datasets.iter().map(|d| d.name.clone()).collect();
let mut vars = s.doc()?.doc.variables.clone();
vars.prune(&deleted);
vars.variables.extend(added);
vars.datasets = lib.datasets;
vars.active_dataset = None;
// A load that leaves the variables and data sets as they were adds no undo step.
if vars != s.doc()?.doc.variables {
s.edit("Load Variable Library", |d, _| {
d.variables = vars;
Ok(())
})?;
}
super::edit_variables(s, "Load Variable Library", |vars| {
vars.prune(&deleted);
vars.variables.extend(added);
vars.datasets = lib.datasets;
vars.active_dataset = None;
Ok(())
})?;
// A highlight of a variable that is gone highlights nothing.
let st = s.doc_mut()?;
st.drop_stale_variables_highlight();