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)
This commit is contained in:
@ -222,22 +222,23 @@ impl Cmd for CommitFormula {
|
|||||||
}
|
}
|
||||||
fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
|
fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
|
||||||
let buf = ctx.buffers.get("formula").cloned().unwrap_or_default();
|
let buf = ctx.buffers.get("formula").cloned().unwrap_or_default();
|
||||||
let mut effects: Vec<Box<dyn Effect>> = Vec::new();
|
|
||||||
// Default formula target to _Measure (the virtual measure category).
|
// Default formula target to _Measure (the virtual measure category).
|
||||||
// _Measure dynamically includes all formula targets.
|
// _Measure dynamically includes all formula targets.
|
||||||
effects.push(Box::new(effect::AddFormula {
|
vec![
|
||||||
|
Box::new(effect::AddFormula {
|
||||||
raw: buf,
|
raw: buf,
|
||||||
target_category: "_Measure".to_string(),
|
target_category: "_Measure".to_string(),
|
||||||
}));
|
}),
|
||||||
effects.push(effect::mark_dirty());
|
effect::mark_dirty(),
|
||||||
effects.push(effect::set_status("Formula added"));
|
effect::set_status("Formula added"),
|
||||||
effects.push(effect::change_mode(AppMode::FormulaPanel));
|
effect::change_mode(AppMode::FormulaPanel),
|
||||||
effects
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shared helper: read a buffer, trim it, and if non-empty, produce add + dirty
|
/// Shared helper: read a buffer, trim it, and if non-empty, produce add + dirty
|
||||||
/// + status effects. If empty, return to CategoryPanel.
|
/// + status effects. If empty, return to CategoryPanel.
|
||||||
|
///
|
||||||
/// Buffer clearing is handled by the keymap (Enter → [commit, clear-buffer]).
|
/// Buffer clearing is handled by the keymap (Enter → [commit, clear-buffer]).
|
||||||
fn commit_add_from_buffer(
|
fn commit_add_from_buffer(
|
||||||
ctx: &CmdContext,
|
ctx: &CmdContext,
|
||||||
|
|||||||
@ -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<String>, usize)> {
|
||||||
|
let view = ctx.model.active_view();
|
||||||
|
let page_cats: Vec<String> = view
|
||||||
|
.categories_on(Axis::Page)
|
||||||
|
.into_iter()
|
||||||
|
.map(String::from)
|
||||||
|
.collect();
|
||||||
|
page_cats
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|cat| {
|
||||||
|
let items: Vec<String> = 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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
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<String>, usize)> {
|
|
||||||
let view = ctx.model.active_view();
|
|
||||||
let page_cats: Vec<String> = view
|
|
||||||
.categories_on(Axis::Page)
|
|
||||||
.into_iter()
|
|
||||||
.map(String::from)
|
|
||||||
.collect();
|
|
||||||
page_cats
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|cat| {
|
|
||||||
let items: Vec<String> = 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()
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user