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:
Edward Langley
2026-04-09 01:56:34 -07:00
parent 4686f47026
commit 24c59fbf40
2 changed files with 30 additions and 1 deletions

View File

@ -172,6 +172,11 @@ impl Model {
} }
pub fn add_formula(&mut self, formula: Formula) { 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 // Replace if same target within the same category
if let Some(pos) = self.formulas.iter().position(|f| { if let Some(pos) = self.formulas.iter().position(|f| {
f.target == formula.target && f.target_category == formula.target_category 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))); 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] #[test]
fn evaluate_aggregated_no_hidden_delegates_to_evaluate() { fn evaluate_aggregated_no_hidden_delegates_to_evaluate() {
let mut m = Model::new("Test"); let mut m = Model::new("Test");

View File

@ -50,7 +50,7 @@ impl PanelContent for FormulaContent<'_> {
} else { } else {
Style::default().fg(Color::Green) Style::default().fg(Color::Green)
}; };
let text = format!(" {} = {:?}", formula.target, formula.raw); let text = format!(" {}", formula.raw);
let truncated = if text.len() > inner.width as usize { let truncated = if text.len() > inner.width as usize {
format!("{}", &text[..inner.width as usize - 1]) format!("{}", &text[..inner.width as usize - 1])
} else { } else {