fix: Model::add_formula auto-adds target item; fix formula panel display
Model::add_formula now ensures the formula target exists as an item in the target category (same fix as AddFormula effect, but at the model level — covers file loading and headless paths). Formula panel now shows raw formula text instead of debug-formatted string with redundant target name and quotes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@ -172,6 +172,11 @@ impl Model {
|
||||
}
|
||||
|
||||
pub fn add_formula(&mut self, formula: Formula) {
|
||||
// Ensure the formula target exists as an item in the target category
|
||||
// so the grid layout includes cells for it.
|
||||
if let Some(cat) = self.categories.get_mut(&formula.target_category) {
|
||||
cat.add_item(&formula.target);
|
||||
}
|
||||
// Replace if same target within the same category
|
||||
if let Some(pos) = self.formulas.iter().position(|f| {
|
||||
f.target == formula.target && f.target_category == formula.target_category
|
||||
@ -731,6 +736,30 @@ mod model_tests {
|
||||
assert_eq!(result, Some(CellValue::Number(150.0)));
|
||||
}
|
||||
|
||||
/// Model::add_formula should add the formula target as an item to the
|
||||
/// target category so the grid layout includes cells for it.
|
||||
#[test]
|
||||
fn add_formula_adds_target_item() {
|
||||
use crate::formula::parse_formula;
|
||||
|
||||
let mut m = Model::new("Test");
|
||||
m.add_category("Measure").unwrap();
|
||||
m.add_category("Region").unwrap();
|
||||
m.category_mut("Region").unwrap().add_item("East");
|
||||
m.category_mut("Measure").unwrap().add_item("Revenue");
|
||||
m.category_mut("Measure").unwrap().add_item("Cost");
|
||||
|
||||
m.add_formula(parse_formula("Profit = Revenue - Cost", "Measure").unwrap());
|
||||
|
||||
assert!(
|
||||
m.category("Measure")
|
||||
.unwrap()
|
||||
.ordered_item_names()
|
||||
.contains(&"Profit"),
|
||||
"add_formula should add target item to category"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn evaluate_aggregated_no_hidden_delegates_to_evaluate() {
|
||||
let mut m = Model::new("Test");
|
||||
|
||||
Reference in New Issue
Block a user