From f3996da2ecd9d4f5cdab241d853aab781bcf4a5c Mon Sep 17 00:00:00 2001 From: Edward Langley Date: Tue, 14 Apr 2026 01:03:25 -0700 Subject: [PATCH] refactor(cmd): simplify commit and navigation logic Simplify the return value of CommitFormula::execute to use a vec! macro and move the page_cat_data helper function in navigation.rs for better organization. Co-Authored-By: fiddlerwoaroof/git-smart-commit (gemma-4-31B-it-UD-Q4_K_XL.gguf) --- src/command/cmd/commit.rs | 19 +++++----- src/command/cmd/navigation.rs | 70 +++++++++++++++++------------------ 2 files changed, 45 insertions(+), 44 deletions(-) diff --git a/src/command/cmd/commit.rs b/src/command/cmd/commit.rs index c522e40..fa78d4c 100644 --- a/src/command/cmd/commit.rs +++ b/src/command/cmd/commit.rs @@ -222,22 +222,23 @@ impl Cmd for CommitFormula { } fn execute(&self, ctx: &CmdContext) -> Vec> { let buf = ctx.buffers.get("formula").cloned().unwrap_or_default(); - let mut effects: Vec> = Vec::new(); // Default formula target to _Measure (the virtual measure category). // _Measure dynamically includes all formula targets. - effects.push(Box::new(effect::AddFormula { - raw: buf, - target_category: "_Measure".to_string(), - })); - effects.push(effect::mark_dirty()); - effects.push(effect::set_status("Formula added")); - effects.push(effect::change_mode(AppMode::FormulaPanel)); - effects + vec![ + Box::new(effect::AddFormula { + raw: buf, + target_category: "_Measure".to_string(), + }), + effect::mark_dirty(), + effect::set_status("Formula added"), + effect::change_mode(AppMode::FormulaPanel), + ] } } /// Shared helper: read a buffer, trim it, and if non-empty, produce add + dirty /// + status effects. If empty, return to CategoryPanel. +/// /// Buffer clearing is handled by the keymap (Enter → [commit, clear-buffer]). fn commit_add_from_buffer( ctx: &CmdContext, diff --git a/src/command/cmd/navigation.rs b/src/command/cmd/navigation.rs index e007b14..d9a69b4 100644 --- a/src/command/cmd/navigation.rs +++ b/src/command/cmd/navigation.rs @@ -243,6 +243,41 @@ impl Cmd for PagePrev { } } +/// Gather (cat_name, items, current_idx) for page-axis categories. +pub(super) fn page_cat_data(ctx: &CmdContext) -> Vec<(String, Vec, usize)> { + let view = ctx.model.active_view(); + let page_cats: Vec = view + .categories_on(Axis::Page) + .into_iter() + .map(String::from) + .collect(); + page_cats + .into_iter() + .filter_map(|cat| { + let items: Vec = ctx + .model + .category(&cat) + .map(|c| { + c.ordered_item_names() + .into_iter() + .map(String::from) + .collect() + }) + .unwrap_or_default(); + if items.is_empty() { + return None; + } + let current = view + .page_selection(&cat) + .map(String::from) + .or_else(|| items.first().cloned()) + .unwrap_or_default(); + let idx = items.iter().position(|i| *i == current).unwrap_or(0); + Some((cat, items, idx)) + }) + .collect() +} + #[cfg(test)] mod tests { use super::*; @@ -438,38 +473,3 @@ mod tests { ); } } - -/// Gather (cat_name, items, current_idx) for page-axis categories. -pub(super) fn page_cat_data(ctx: &CmdContext) -> Vec<(String, Vec, usize)> { - let view = ctx.model.active_view(); - let page_cats: Vec = view - .categories_on(Axis::Page) - .into_iter() - .map(String::from) - .collect(); - page_cats - .into_iter() - .filter_map(|cat| { - let items: Vec = ctx - .model - .category(&cat) - .map(|c| { - c.ordered_item_names() - .into_iter() - .map(String::from) - .collect() - }) - .unwrap_or_default(); - if items.is_empty() { - return None; - } - let current = view - .page_selection(&cat) - .map(String::from) - .or_else(|| items.first().cloned()) - .unwrap_or_default(); - let idx = items.iter().position(|i| *i == current).unwrap_or(0); - Some((cat, items, idx)) - }) - .collect() -}