refactor(command): unify layout access and navigation commands

Refactor `CmdContext` to delegate layout-related information (row/column
counts, categories, cell keys) to a `GridLayout` object.

- Add `layout` field to `CmdContext` .
- Implement helper methods on `CmdContext` to access layout data.
- Consolidate multiple jump commands ( `JumpToFirstRow` , `JumpToLastRow` ,
  `JumpToFirstCol` , `JumpToLastCol` ) into a single `JumpToEdge` command.
- Introduce `ScrollRows` and `PageScroll` commands for improved navigation.
- Update `CursorState` instantiation to use the new context structure.
- Update command registry to use the new unified commands and macros.

Co-Authored-By: fiddlerwoaroof/git-smart-commit (unsloth/gemma-4-26B-A4B-it-GGUF:UD-Q5_K_XL)
This commit is contained in:
Edward Langley
2026-04-07 09:29:45 -07:00
parent 334597d825
commit 85a459289d

View File

@ -12,6 +12,7 @@ use crate::view::{Axis, AxisEntry, GridLayout};
/// Read-only context available to commands for decision-making. /// Read-only context available to commands for decision-making.
pub struct CmdContext<'a> { pub struct CmdContext<'a> {
pub model: &'a Model, pub model: &'a Model,
pub layout: &'a GridLayout,
pub mode: &'a AppMode, pub mode: &'a AppMode,
pub selected: (usize, usize), pub selected: (usize, usize),
pub row_offset: usize, pub row_offset: usize,
@ -31,24 +32,12 @@ pub struct CmdContext<'a> {
pub tile_cat_idx: usize, pub tile_cat_idx: usize,
/// Named text buffers /// Named text buffers
pub buffers: &'a HashMap<String, String>, pub buffers: &'a HashMap<String, String>,
/// Pre-resolved cell key at the cursor position (None if out of bounds)
pub cell_key: Option<crate::model::cell::CellKey>,
/// Grid dimensions (so commands don't need GridLayout)
pub row_count: usize,
pub col_count: usize,
/// Categories on Axis::None — aggregated away in the current view
pub none_cats: Vec<String>,
/// View navigation stacks (for drill back/forward) /// View navigation stacks (for drill back/forward)
pub view_back_stack: Vec<String>, pub view_back_stack: &'a [String],
pub view_forward_stack: Vec<String>, pub view_forward_stack: &'a [String],
/// Records-mode info (drill view). None for normal pivot views. /// Display value at the cursor — works uniformly for pivot and records mode.
/// When Some, edits stage to drill_state.pending_edits. pub display_value: String,
pub records_col: Option<String>,
/// The display value at the cursor in records mode (including any
/// pending edit override). None for normal pivot views.
pub records_value: Option<String>,
/// How many data rows/cols fit on screen (for viewport scrolling). /// How many data rows/cols fit on screen (for viewport scrolling).
/// Defaults to generous fallbacks when unknown.
pub visible_rows: usize, pub visible_rows: usize,
pub visible_cols: usize, pub visible_cols: usize,
/// Expanded categories in the tree panel /// Expanded categories in the tree panel
@ -57,6 +46,21 @@ pub struct CmdContext<'a> {
pub key_code: KeyCode, pub key_code: KeyCode,
} }
impl<'a> CmdContext<'a> {
pub fn cell_key(&self) -> Option<CellKey> {
self.layout.cell_key(self.selected.0, self.selected.1)
}
pub fn row_count(&self) -> usize {
self.layout.row_count()
}
pub fn col_count(&self) -> usize {
self.layout.col_count()
}
pub fn none_cats(&self) -> &[String] {
&self.layout.none_cats
}
}
impl<'a> CmdContext<'a> { impl<'a> CmdContext<'a> {
/// Resolve the category panel tree entry at the current cursor. /// Resolve the category panel tree entry at the current cursor.
pub fn cat_tree_entry(&self) -> Option<crate::ui::cat_tree::CatTreeEntry> { pub fn cat_tree_entry(&self) -> Option<crate::ui::cat_tree::CatTreeEntry> {
@ -251,8 +255,8 @@ impl CursorState {
Self { Self {
row: ctx.selected.0, row: ctx.selected.0,
col: ctx.selected.1, col: ctx.selected.1,
row_count: ctx.row_count, row_count: ctx.row_count(),
col_count: ctx.col_count, col_count: ctx.col_count(),
row_offset: ctx.row_offset, row_offset: ctx.row_offset,
col_offset: ctx.col_offset, col_offset: ctx.col_offset,
visible_rows: ctx.visible_rows, visible_rows: ctx.visible_rows,
@ -317,75 +321,32 @@ impl Cmd for MoveSelection {
} }
} }
/// Unified jump-to-edge: jump to first/last row or column.
/// `is_row` selects the axis; `end` selects first (false) or last (true).
#[derive(Debug)] #[derive(Debug)]
pub struct JumpToFirstRow { pub struct JumpToEdge {
pub col: usize, pub cursor: CursorState,
pub is_row: bool,
pub end: bool,
pub cmd_name: &'static str,
} }
impl Cmd for JumpToFirstRow { impl Cmd for JumpToEdge {
fn name(&self) -> &'static str { fn name(&self) -> &'static str {
"jump-first-row" self.cmd_name
} }
fn execute(&self, _ctx: &CmdContext) -> Vec<Box<dyn Effect>> { fn execute(&self, _ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
vec![ let (nr, nc) = if self.is_row {
Box::new(effect::SetSelected(0, self.col)), let r = if self.end { self.cursor.row_count.saturating_sub(1) } else { 0 };
Box::new(effect::SetRowOffset(0)), (r, self.cursor.col)
] } else {
} let c = if self.end { self.cursor.col_count.saturating_sub(1) } else { 0 };
} (self.cursor.row, c)
};
#[derive(Debug)] viewport_effects(
pub struct JumpToLastRow { nr, nc,
pub col: usize, self.cursor.row_offset, self.cursor.col_offset,
pub row_count: usize, self.cursor.visible_rows, self.cursor.visible_cols,
pub row_offset: usize, )
}
impl Cmd for JumpToLastRow {
fn name(&self) -> &'static str {
"jump-last-row"
}
fn execute(&self, _ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
let last = self.row_count.saturating_sub(1);
let mut effects: Vec<Box<dyn Effect>> = vec![Box::new(effect::SetSelected(last, self.col))];
if last >= self.row_offset + 20 {
effects.push(Box::new(effect::SetRowOffset(last.saturating_sub(19))));
}
effects
}
}
#[derive(Debug)]
pub struct JumpToFirstCol {
pub row: usize,
}
impl Cmd for JumpToFirstCol {
fn name(&self) -> &'static str {
"jump-first-col"
}
fn execute(&self, _ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
vec![
Box::new(effect::SetSelected(self.row, 0)),
Box::new(effect::SetColOffset(0)),
]
}
}
#[derive(Debug)]
pub struct JumpToLastCol {
pub row: usize,
pub col_count: usize,
pub col_offset: usize,
}
impl Cmd for JumpToLastCol {
fn name(&self) -> &'static str {
"jump-last-col"
}
fn execute(&self, _ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
let last = self.col_count.saturating_sub(1);
let mut effects: Vec<Box<dyn Effect>> = vec![Box::new(effect::SetSelected(self.row, last))];
if last >= self.col_offset + 8 {
effects.push(Box::new(effect::SetColOffset(last.saturating_sub(7))));
}
effects
} }
} }
@ -401,19 +362,38 @@ impl Cmd for ScrollRows {
fn execute(&self, _ctx: &CmdContext) -> Vec<Box<dyn Effect>> { fn execute(&self, _ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
let row_max = self.cursor.row_count.saturating_sub(1) as i32; let row_max = self.cursor.row_count.saturating_sub(1) as i32;
let nr = (self.cursor.row as i32 + self.delta).clamp(0, row_max) as usize; let nr = (self.cursor.row as i32 + self.delta).clamp(0, row_max) as usize;
let mut effects: Vec<Box<dyn Effect>> = viewport_effects(
vec![Box::new(effect::SetSelected(nr, self.cursor.col))]; nr,
let mut row_offset = self.cursor.row_offset; self.cursor.col,
if nr < row_offset { self.cursor.row_offset,
row_offset = nr; self.cursor.col_offset,
} self.cursor.visible_rows,
if nr >= row_offset + 20 { self.cursor.visible_cols,
row_offset = nr.saturating_sub(19); )
} }
if row_offset != self.cursor.row_offset { }
effects.push(Box::new(effect::SetRowOffset(row_offset)));
} #[derive(Debug)]
effects pub struct PageScroll {
pub direction: i32, // 1 for down, -1 for up
pub cursor: CursorState,
}
impl Cmd for PageScroll {
fn name(&self) -> &'static str {
"page-scroll"
}
fn execute(&self, _ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
let delta = (self.cursor.visible_rows as i32 * 3 / 4).max(1) * self.direction;
let row_max = self.cursor.row_count.saturating_sub(1) as i32;
let nr = (self.cursor.row as i32 + delta).clamp(0, row_max) as usize;
viewport_effects(
nr,
self.cursor.col,
self.cursor.row_offset,
self.cursor.col_offset,
self.cursor.visible_rows,
self.cursor.visible_cols,
)
} }
} }
@ -471,7 +451,7 @@ impl Cmd for SaveAndQuit {
// ── Cell operations ────────────────────────────────────────────────────────── // ── Cell operations ──────────────────────────────────────────────────────────
// All cell commands take an explicit CellKey. The interactive spec fills it // All cell commands take an explicit CellKey. The interactive spec fills it
// from ctx.cell_key; the parser fills it from Cat/Item coordinate args. // from ctx.cell_key(); the parser fills it from Cat/Item coordinate args.
/// Clear a cell. /// Clear a cell.
#[derive(Debug)] #[derive(Debug)]
@ -500,8 +480,7 @@ impl Cmd for YankCell {
"yank" "yank"
} }
fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> { fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
let layout = GridLayout::new(ctx.model, ctx.model.active_view()); let value = ctx.model.evaluate_aggregated(&self.key, ctx.none_cats());
let value = ctx.model.evaluate_aggregated(&self.key, &layout.none_cats);
vec![ vec![
Box::new(effect::SetYanked(value)), Box::new(effect::SetYanked(value)),
effect::set_status("Yanked"), effect::set_status("Yanked"),
@ -590,12 +569,7 @@ impl Cmd for TogglePanelAndFocus {
open: self.open, open: self.open,
})); }));
if self.focused { if self.focused {
let mode = match self.panel { effects.push(effect::change_mode(self.panel.mode()));
Panel::Formula => AppMode::FormulaPanel,
Panel::Category => AppMode::CategoryPanel,
Panel::View => AppMode::ViewPanel,
};
effects.push(effect::change_mode(mode));
} else { } else {
effects.push(effect::change_mode(AppMode::Normal)); effects.push(effect::change_mode(AppMode::Normal));
} }
@ -682,33 +656,29 @@ impl Cmd for EditOrDrill {
// Only consider regular (non-virtual, non-label) categories on None // Only consider regular (non-virtual, non-label) categories on None
// as true aggregation. Virtuals like _Index/_Dim are always None in // as true aggregation. Virtuals like _Index/_Dim are always None in
// pivot mode and don't imply aggregation. // pivot mode and don't imply aggregation.
let regular_none = ctx.none_cats.iter().any(|c| { let regular_none = ctx.none_cats().iter().any(|c| {
ctx.model ctx.model
.category(c) .category(c)
.map(|cat| cat.kind.is_regular()) .map(|cat| cat.kind.is_regular())
.unwrap_or(false) .unwrap_or(false)
}); });
let is_aggregated = ctx.records_col.is_none() && regular_none; // In records mode (synthetic key), always edit directly — no drilling.
let is_synthetic = ctx
.cell_key()
.as_ref()
.and_then(|k| crate::view::synthetic_record_info(k))
.is_some();
let is_aggregated = !is_synthetic && regular_none;
if is_aggregated { if is_aggregated {
let Some(key) = ctx.cell_key.clone() else { let Some(key) = ctx.cell_key().clone() else {
return vec![effect::set_status( return vec![effect::set_status("cannot drill — no cell at cursor")];
"cannot drill — no cell at cursor",
)];
}; };
return DrillIntoCell { key }.execute(ctx); return DrillIntoCell { key }.execute(ctx);
} }
// Edit path: prefer records display value (includes pending edits), EnterEditMode {
// else the underlying cell's stored value. initial_value: ctx.display_value.clone(),
let initial_value = if let Some(v) = &ctx.records_value { }
v.clone() .execute(ctx)
} else {
ctx.cell_key
.as_ref()
.and_then(|k| ctx.model.get_cell(k).cloned())
.map(|v| v.to_string())
.unwrap_or_default()
};
EnterEditMode { initial_value }.execute(ctx)
} }
} }
@ -721,8 +691,15 @@ impl Cmd for AddRecordRow {
"add-record-row" "add-record-row"
} }
fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> { fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
if ctx.records_col.is_none() { let is_records = ctx
return vec![effect::set_status("add-record-row only works in records mode")]; .cell_key()
.as_ref()
.and_then(|k| crate::view::synthetic_record_info(k))
.is_some();
if !is_records {
return vec![effect::set_status(
"add-record-row only works in records mode",
)];
} }
// Build a CellKey from the current page filters // Build a CellKey from the current page filters
let view = ctx.model.active_view(); let view = ctx.model.active_view();
@ -751,6 +728,30 @@ impl Cmd for AddRecordRow {
} }
} }
/// Vim-style 'o': add a new record row below cursor and enter edit mode.
#[derive(Debug)]
pub struct OpenRecordRow;
impl Cmd for OpenRecordRow {
fn name(&self) -> &'static str {
"open-record-row"
}
fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
let is_records = ctx
.cell_key()
.as_ref()
.and_then(|k| crate::view::synthetic_record_info(k))
.is_some();
if !is_records {
return vec![effect::set_status(
"open-record-row only works in records mode",
)];
}
let mut effects = AddRecordRow.execute(ctx);
effects.push(Box::new(effect::EnterEditAtCursor));
effects
}
}
/// Typewriter-style advance: move down, wrap to top of next column at bottom. /// Typewriter-style advance: move down, wrap to top of next column at bottom.
#[derive(Debug)] #[derive(Debug)]
pub struct EnterAdvance { pub struct EnterAdvance {
@ -804,10 +805,9 @@ impl Cmd for SearchNavigate {
return vec![]; return vec![];
} }
let layout = GridLayout::new(ctx.model, ctx.model.active_view());
let (cur_row, cur_col) = ctx.selected; let (cur_row, cur_col) = ctx.selected;
let total_rows = layout.row_count().max(1); let total_rows = ctx.row_count().max(1);
let total_cols = layout.col_count().max(1); let total_cols = ctx.col_count().max(1);
let total = total_rows * total_cols; let total = total_rows * total_cols;
let cur_flat = cur_row * total_cols + cur_col; let cur_flat = cur_row * total_cols + cur_col;
@ -815,11 +815,11 @@ impl Cmd for SearchNavigate {
.filter(|&flat| { .filter(|&flat| {
let ri = flat / total_cols; let ri = flat / total_cols;
let ci = flat % total_cols; let ci = flat % total_cols;
let key = match layout.cell_key(ri, ci) { let key = match ctx.layout.cell_key(ri, ci) {
Some(k) => k, Some(k) => k,
None => return false, None => return false,
}; };
let s = match ctx.model.evaluate_aggregated(&key, &layout.none_cats) { let s = match ctx.model.evaluate_aggregated(&key, ctx.none_cats()) {
Some(CellValue::Number(n)) => format!("{n}"), Some(CellValue::Number(n)) => format!("{n}"),
Some(CellValue::Text(t)) => t, Some(CellValue::Text(t)) => t,
None => String::new(), None => String::new(),
@ -1018,9 +1018,8 @@ impl Cmd for ToggleGroupUnderCursor {
"toggle-group-under-cursor" "toggle-group-under-cursor"
} }
fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> { fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
let layout = GridLayout::new(ctx.model, ctx.model.active_view());
let sel_row = ctx.selected.0; let sel_row = ctx.selected.0;
let Some((cat, group)) = layout.row_group_for(sel_row) else { let Some((cat, group)) = ctx.layout.row_group_for(sel_row) else {
return vec![]; return vec![];
}; };
vec![ vec![
@ -1041,9 +1040,8 @@ impl Cmd for ToggleColGroupUnderCursor {
"toggle-col-group-under-cursor" "toggle-col-group-under-cursor"
} }
fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> { fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
let layout = GridLayout::new(ctx.model, ctx.model.active_view());
let sel_col = ctx.selected.1; let sel_col = ctx.selected.1;
let Some((cat, group)) = layout.col_group_for(sel_col) else { let Some((cat, group)) = ctx.layout.col_group_for(sel_col) else {
return vec![]; return vec![];
}; };
// After toggling, col_count may shrink — clamp selection // After toggling, col_count may shrink — clamp selection
@ -1067,12 +1065,12 @@ impl Cmd for HideSelectedRowItem {
"hide-selected-row-item" "hide-selected-row-item"
} }
fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> { fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
let layout = GridLayout::new(ctx.model, ctx.model.active_view()); let Some(cat_name) = ctx.layout.row_cats.first().cloned() else {
let Some(cat_name) = layout.row_cats.first().cloned() else {
return vec![]; return vec![];
}; };
let sel_row = ctx.selected.0; let sel_row = ctx.selected.0;
let Some(items) = layout let Some(items) = ctx
.layout
.row_items .row_items
.iter() .iter()
.filter_map(|e| { .filter_map(|e| {
@ -1199,7 +1197,7 @@ impl Cmd for DrillIntoCell {
// Previously-aggregated categories (none_cats) stay on Axis::None so // Previously-aggregated categories (none_cats) stay on Axis::None so
// they don't filter records; they'll appear as columns in records mode. // they don't filter records; they'll appear as columns in records mode.
// Skip virtual categories — we already set _Index/_Dim above. // Skip virtual categories — we already set _Index/_Dim above.
for cat in &ctx.none_cats { for cat in ctx.none_cats() {
if fixed_cats.contains(cat) || cat.starts_with('_') { if fixed_cats.contains(cat) || cat.starts_with('_') {
continue; continue;
} }
@ -1436,64 +1434,38 @@ impl Cmd for ToggleRecordsMode {
"toggle-records-mode" "toggle-records-mode"
} }
fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> { fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
use crate::view::Axis; let is_records = ctx.layout.is_records_mode();
let view = ctx.model.active_view();
// Detect current state
let is_records = view
.category_axes
.get("_Index")
.copied()
== Some(Axis::Row)
&& view.category_axes.get("_Dim").copied() == Some(Axis::Column);
let mut effects: Vec<Box<dyn Effect>> = Vec::new();
if is_records { if is_records {
// Switch back to pivot: auto-assign axes // Navigate back to the previous view (restores original axes)
// First regular category → Row, second → Column, rest → Page, return vec![Box::new(effect::ViewBack), effect::set_status("Pivot mode")];
// virtuals/labels → None. }
let mut row_done = false;
let mut col_done = false; let mut effects: Vec<Box<dyn Effect>> = Vec::new();
for (name, cat) in &ctx.model.categories { let records_name = "_Records".to_string();
let axis = if !cat.kind.is_regular() {
Axis::None // Create (or replace) a _Records view and switch to it
} else if !row_done { effects.push(Box::new(effect::CreateView(records_name.clone())));
row_done = true; effects.push(Box::new(effect::SwitchView(records_name)));
Axis::Row
} else if !col_done { // _Index on Row, _Dim on Column, everything else → None
col_done = true; effects.push(Box::new(effect::SetAxis {
Axis::Column category: "_Index".to_string(),
} else { axis: crate::view::Axis::Row,
Axis::Page }));
}; effects.push(Box::new(effect::SetAxis {
category: "_Dim".to_string(),
axis: crate::view::Axis::Column,
}));
for name in ctx.model.categories.keys() {
if name != "_Index" && name != "_Dim" {
effects.push(Box::new(effect::SetAxis { effects.push(Box::new(effect::SetAxis {
category: name.clone(), category: name.clone(),
axis, axis: crate::view::Axis::None,
})); }));
} }
effects.push(effect::set_status("Pivot mode"));
} else {
// Switch to records mode
effects.push(Box::new(effect::SetAxis {
category: "_Index".to_string(),
axis: Axis::Row,
}));
effects.push(Box::new(effect::SetAxis {
category: "_Dim".to_string(),
axis: Axis::Column,
}));
// Everything else → None
for name in ctx.model.categories.keys() {
if name != "_Index" && name != "_Dim" {
effects.push(Box::new(effect::SetAxis {
category: name.clone(),
axis: Axis::None,
}));
}
}
effects.push(effect::set_status("Records mode"));
} }
effects.push(effect::set_status("Records mode"));
effects effects
} }
} }
@ -1928,14 +1900,34 @@ impl Cmd for PopChar {
/// Commit a cell edit: set cell value, advance cursor, return to Normal. /// Commit a cell edit: set cell value, advance cursor, return to Normal.
/// In records mode, stages the edit in drill_state.pending_edits instead of /// In records mode, stages the edit in drill_state.pending_edits instead of
/// writing directly to the model. /// Commit a cell value: for synthetic records keys, stage in drill pending edits
/// or apply directly; for real keys, write to the model.
fn commit_cell_value(key: &CellKey, value: &str, effects: &mut Vec<Box<dyn Effect>>) {
if let Some((record_idx, col_name)) = crate::view::synthetic_record_info(key) {
effects.push(Box::new(effect::SetDrillPendingEdit {
record_idx,
col_name,
new_value: value.to_string(),
}));
} else if value.is_empty() {
effects.push(Box::new(effect::ClearCell(key.clone())));
effects.push(effect::mark_dirty());
} else if let Ok(n) = value.parse::<f64>() {
effects.push(Box::new(effect::SetCell(key.clone(), CellValue::Number(n))));
effects.push(effect::mark_dirty());
} else {
effects.push(Box::new(effect::SetCell(key.clone(), CellValue::Text(value.to_string()))));
effects.push(effect::mark_dirty());
}
}
/// Commit a cell edit: set cell value, advance cursor, return to editing.
/// In records mode with drill, stages the edit in drill_state.pending_edits.
/// In records mode without drill or in pivot mode, writes directly to the model.
#[derive(Debug)] #[derive(Debug)]
pub struct CommitCellEdit { pub struct CommitCellEdit {
pub key: crate::model::cell::CellKey, pub key: CellKey,
pub value: String, pub value: String,
/// Records-mode edit: (record_idx, column_name). When Some, stage in
/// pending_edits; otherwise write to the model directly.
pub records_edit: Option<(usize, String)>,
} }
impl Cmd for CommitCellEdit { impl Cmd for CommitCellEdit {
fn name(&self) -> &'static str { fn name(&self) -> &'static str {
@ -1944,29 +1936,7 @@ impl Cmd for CommitCellEdit {
fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> { fn execute(&self, ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
let mut effects: Vec<Box<dyn Effect>> = Vec::new(); let mut effects: Vec<Box<dyn Effect>> = Vec::new();
if let Some((record_idx, col_name)) = &self.records_edit { commit_cell_value(&self.key, &self.value, &mut effects);
// Stage the edit in drill_state.pending_edits
effects.push(Box::new(effect::SetDrillPendingEdit {
record_idx: *record_idx,
col_name: col_name.clone(),
new_value: self.value.clone(),
}));
} else if self.value.is_empty() {
effects.push(Box::new(effect::ClearCell(self.key.clone())));
effects.push(effect::mark_dirty());
} else if let Ok(n) = self.value.parse::<f64>() {
effects.push(Box::new(effect::SetCell(
self.key.clone(),
CellValue::Number(n),
)));
effects.push(effect::mark_dirty());
} else {
effects.push(Box::new(effect::SetCell(
self.key.clone(),
CellValue::Text(self.value.clone()),
)));
effects.push(effect::mark_dirty());
}
// Advance cursor down (typewriter-style) and re-enter edit mode // Advance cursor down (typewriter-style) and re-enter edit mode
// at the new cell so the user can continue data entry. // at the new cell so the user can continue data entry.
let adv = EnterAdvance { let adv = EnterAdvance {
@ -1978,6 +1948,36 @@ impl Cmd for CommitCellEdit {
} }
} }
/// Tab in editing: commit cell, move right, re-enter edit mode (Excel-style).
#[derive(Debug)]
pub struct CommitAndAdvanceRight {
pub key: CellKey,
pub value: String,
pub cursor: CursorState,
}
impl Cmd for CommitAndAdvanceRight {
fn name(&self) -> &'static str {
"commit-and-advance-right"
}
fn execute(&self, _ctx: &CmdContext) -> Vec<Box<dyn Effect>> {
let mut effects: Vec<Box<dyn Effect>> = Vec::new();
commit_cell_value(&self.key, &self.value, &mut effects);
// Move right instead of down
let col_max = self.cursor.col_count.saturating_sub(1);
let nc = (self.cursor.col + 1).min(col_max);
effects.extend(viewport_effects(
self.cursor.row,
nc,
self.cursor.row_offset,
self.cursor.col_offset,
self.cursor.visible_rows,
self.cursor.visible_cols,
));
effects.push(Box::new(effect::EnterEditAtCursor));
effects
}
}
/// Commit a formula from the formula edit buffer. /// Commit a formula from the formula edit buffer.
#[derive(Debug)] #[derive(Debug)]
pub struct CommitFormula; pub struct CommitFormula;
@ -2430,7 +2430,7 @@ pub fn default_registry() -> CmdRegistry {
})) }))
}, },
|_args, ctx| { |_args, ctx| {
let key = ctx.cell_key.clone().ok_or("no cell at cursor")?; let key = ctx.cell_key().clone().ok_or("no cell at cursor")?;
Ok(Box::new(ClearCellCommand { key })) Ok(Box::new(ClearCellCommand { key }))
}, },
); );
@ -2482,58 +2482,20 @@ pub fn default_registry() -> CmdRegistry {
})) }))
}, },
); );
r.register( // Jump-to-edge commands: first/last row/col
&JumpToFirstRow { col: 0 }, macro_rules! reg_jump {
|_| Ok(Box::new(JumpToFirstRow { col: 0 })), ($r:expr, $is_row:expr, $end:expr, $name:expr) => {
|_, ctx| { $r.register(
Ok(Box::new(JumpToFirstRow { &JumpToEdge { cursor: CursorState::default(), is_row: $is_row, end: $end, cmd_name: $name },
col: ctx.selected.1, |_| Ok(Box::new(JumpToEdge { cursor: CursorState::default(), is_row: $is_row, end: $end, cmd_name: $name })),
})) |_, ctx| Ok(Box::new(JumpToEdge { cursor: CursorState::from_ctx(ctx), is_row: $is_row, end: $end, cmd_name: $name })),
}, );
); };
r.register( }
&JumpToLastRow { col: 0, row_count: 0, row_offset: 0 }, reg_jump!(r, true, false, "jump-first-row");
|_| { reg_jump!(r, true, true, "jump-last-row");
Ok(Box::new(JumpToLastRow { reg_jump!(r, false, false, "jump-first-col");
col: 0, reg_jump!(r, false, true, "jump-last-col");
row_count: 0,
row_offset: 0,
}))
},
|_, ctx| {
Ok(Box::new(JumpToLastRow {
col: ctx.selected.1,
row_count: ctx.row_count,
row_offset: ctx.row_offset,
}))
},
);
r.register(
&JumpToFirstCol { row: 0 },
|_| Ok(Box::new(JumpToFirstCol { row: 0 })),
|_, ctx| {
Ok(Box::new(JumpToFirstCol {
row: ctx.selected.0,
}))
},
);
r.register(
&JumpToLastCol { row: 0, col_count: 0, col_offset: 0 },
|_| {
Ok(Box::new(JumpToLastCol {
row: 0,
col_count: 0,
col_offset: 0,
}))
},
|_, ctx| {
Ok(Box::new(JumpToLastCol {
row: ctx.selected.0,
col_count: ctx.col_count,
col_offset: ctx.col_offset,
}))
},
);
r.register( r.register(
&ScrollRows { delta: 0, cursor: CursorState::default() }, &ScrollRows { delta: 0, cursor: CursorState::default() },
|args| { |args| {
@ -2562,6 +2524,25 @@ pub fn default_registry() -> CmdRegistry {
})) }))
}, },
); );
r.register(
&PageScroll { direction: 0, cursor: CursorState::default() },
|args| {
require_args("page-scroll", args, 1)?;
let dir = args[0].parse::<i32>().map_err(|e| e.to_string())?;
Ok(Box::new(PageScroll {
direction: dir,
cursor: CursorState::default(),
}))
},
|args, ctx| {
require_args("page-scroll", args, 1)?;
let dir = args[0].parse::<i32>().map_err(|e| e.to_string())?;
Ok(Box::new(PageScroll {
direction: dir,
cursor: CursorState::from_ctx(ctx),
}))
},
);
r.register( r.register(
&EnterAdvance { cursor: CursorState::default() }, &EnterAdvance { cursor: CursorState::default() },
|_| { |_| {
@ -2597,12 +2578,14 @@ pub fn default_registry() -> CmdRegistry {
})) }))
}, },
|_args, ctx| { |_args, ctx| {
let key = ctx.cell_key.clone().ok_or("no cell at cursor")?; let key = ctx.cell_key().clone().ok_or("no cell at cursor")?;
Ok(Box::new(YankCell { key })) Ok(Box::new(YankCell { key }))
}, },
); );
r.register( r.register(
&PasteCell { key: CellKey::new(vec![]) }, &PasteCell {
key: CellKey::new(vec![]),
},
|args| { |args| {
if args.is_empty() { if args.is_empty() {
return Err("paste requires at least one Cat/Item coordinate".into()); return Err("paste requires at least one Cat/Item coordinate".into());
@ -2612,11 +2595,11 @@ pub fn default_registry() -> CmdRegistry {
})) }))
}, },
|_args, ctx| { |_args, ctx| {
let key = ctx.cell_key.clone().ok_or("no cell at cursor")?; let key = ctx.cell_key().clone().ok_or("no cell at cursor")?;
Ok(Box::new(PasteCell { key })) Ok(Box::new(PasteCell { key }))
}, },
); );
// clear-cell is registered above (unified: ctx.cell_key or explicit coords) // clear-cell is registered above (unified: ctx.cell_key() or explicit coords)
// ── View / page ────────────────────────────────────────────────────── // ── View / page ──────────────────────────────────────────────────────
r.register_nullary(|| Box::new(TransposeAxes)); r.register_nullary(|| Box::new(TransposeAxes));
@ -2663,7 +2646,7 @@ pub fn default_registry() -> CmdRegistry {
})) }))
}, },
|_args, ctx| { |_args, ctx| {
let key = ctx.cell_key.clone().ok_or("no cell at cursor")?; let key = ctx.cell_key().clone().ok_or("no cell at cursor")?;
Ok(Box::new(DrillIntoCell { key })) Ok(Box::new(DrillIntoCell { key }))
}, },
); );
@ -2824,6 +2807,7 @@ pub fn default_registry() -> CmdRegistry {
Box::new(DeleteFormulaAtCursor) Box::new(DeleteFormulaAtCursor)
}); });
r.register_nullary(|| Box::new(AddRecordRow)); r.register_nullary(|| Box::new(AddRecordRow));
r.register_nullary(|| Box::new(OpenRecordRow));
r.register_nullary(|| Box::new(TogglePruneEmpty)); r.register_nullary(|| Box::new(TogglePruneEmpty));
r.register_nullary(|| Box::new(ToggleRecordsMode)); r.register_nullary(|| Box::new(ToggleRecordsMode));
r.register_nullary(|| Box::new(CycleAxisAtCursor)); r.register_nullary(|| Box::new(CycleAxisAtCursor));
@ -2877,35 +2861,36 @@ pub fn default_registry() -> CmdRegistry {
&CommitCellEdit { &CommitCellEdit {
key: CellKey::new(vec![]), key: CellKey::new(vec![]),
value: String::new(), value: String::new(),
records_edit: None,
}, },
|args| { |args| {
// parse: commit-cell-edit <value> <Cat/Item>...
if args.len() < 2 { if args.len() < 2 {
return Err("commit-cell-edit requires a value and coords".into()); return Err("commit-cell-edit requires a value and coords".into());
} }
Ok(Box::new(CommitCellEdit { Ok(Box::new(CommitCellEdit {
key: parse_cell_key_from_args(&args[1..]), key: parse_cell_key_from_args(&args[1..]),
value: args[0].clone(), value: args[0].clone(),
records_edit: None,
})) }))
}, },
|_args, ctx| { |_args, ctx| {
let value = read_buffer(ctx, "edit"); let value = read_buffer(ctx, "edit");
// In records mode, stage the edit instead of writing to the model let key = ctx.cell_key().clone().ok_or("no cell at cursor")?;
if let Some(col_name) = &ctx.records_col { Ok(Box::new(CommitCellEdit { key, value }))
let record_idx = ctx.selected.0; },
return Ok(Box::new(CommitCellEdit { );
key: CellKey::new(vec![]), // ignored in records mode r.register(
value, &CommitAndAdvanceRight {
records_edit: Some((record_idx, col_name.clone())), key: CellKey::new(vec![]),
})); value: String::new(),
} cursor: CursorState::default(),
let key = ctx.cell_key.clone().ok_or("no cell at cursor")?; },
Ok(Box::new(CommitCellEdit { |_| Err("commit-and-advance-right requires context".into()),
|_args, ctx| {
let value = read_buffer(ctx, "edit");
let key = ctx.cell_key().clone().ok_or("no cell at cursor")?;
Ok(Box::new(CommitAndAdvanceRight {
key, key,
value, value,
records_edit: None, cursor: CursorState::from_ctx(ctx),
})) }))
}, },
); );
@ -2951,12 +2936,16 @@ mod tests {
static EMPTY_EXPANDED: std::sync::LazyLock<std::collections::HashSet<String>> = static EMPTY_EXPANDED: std::sync::LazyLock<std::collections::HashSet<String>> =
std::sync::LazyLock::new(std::collections::HashSet::new); std::sync::LazyLock::new(std::collections::HashSet::new);
fn make_ctx(model: &Model) -> CmdContext<'_> { fn make_layout(model: &Model) -> GridLayout {
GridLayout::new(model, model.active_view())
}
fn make_ctx<'a>(model: &'a Model, layout: &'a GridLayout) -> CmdContext<'a> {
let view = model.active_view(); let view = model.active_view();
let layout = GridLayout::new(model, view);
let (sr, sc) = view.selected; let (sr, sc) = view.selected;
CmdContext { CmdContext {
model, model,
layout,
mode: &AppMode::Normal, mode: &AppMode::Normal,
selected: view.selected, selected: view.selected,
row_offset: view.row_offset, row_offset: view.row_offset,
@ -2973,14 +2962,15 @@ mod tests {
view_panel_cursor: 0, view_panel_cursor: 0,
tile_cat_idx: 0, tile_cat_idx: 0,
buffers: &EMPTY_BUFFERS, buffers: &EMPTY_BUFFERS,
none_cats: layout.none_cats.clone(), view_back_stack: &[],
view_back_stack: Vec::new(), view_forward_stack: &[],
view_forward_stack: Vec::new(), display_value: {
records_col: None, let key = layout.cell_key(sr, sc);
records_value: None, key.as_ref()
cell_key: layout.cell_key(sr, sc), .and_then(|k| model.get_cell(k).cloned())
row_count: layout.row_count(), .map(|v| v.to_string())
col_count: layout.col_count(), .unwrap_or_default()
},
visible_rows: 20, visible_rows: 20,
visible_cols: 8, visible_cols: 8,
expanded_cats: &EMPTY_EXPANDED, expanded_cats: &EMPTY_EXPANDED,
@ -3002,7 +2992,8 @@ mod tests {
#[test] #[test]
fn move_selection_down_produces_set_selected() { fn move_selection_down_produces_set_selected() {
let m = two_cat_model(); let m = two_cat_model();
let ctx = make_ctx(&m); let layout = make_layout(&m);
let ctx = make_ctx(&m, &layout);
let cmd = MoveSelection { let cmd = MoveSelection {
dr: 1, dr: 1,
dc: 0, dc: 0,
@ -3016,7 +3007,8 @@ mod tests {
#[test] #[test]
fn move_selection_clamps_to_bounds() { fn move_selection_clamps_to_bounds() {
let m = two_cat_model(); let m = two_cat_model();
let ctx = make_ctx(&m); let layout = make_layout(&m);
let ctx = make_ctx(&m, &layout);
// Try to move way past the end // Try to move way past the end
let cmd = MoveSelection { let cmd = MoveSelection {
dr: 100, dr: 100,
@ -3032,7 +3024,8 @@ mod tests {
let m = two_cat_model(); let m = two_cat_model();
let mut bufs = HashMap::new(); let mut bufs = HashMap::new();
bufs.insert("command".to_string(), "q".to_string()); bufs.insert("command".to_string(), "q".to_string());
let mut ctx = make_ctx(&m); let layout = make_layout(&m);
let mut ctx = make_ctx(&m, &layout);
ctx.dirty = true; ctx.dirty = true;
ctx.buffers = &bufs; ctx.buffers = &bufs;
let cmd = ExecuteCommand; let cmd = ExecuteCommand;
@ -3046,7 +3039,8 @@ mod tests {
let m = two_cat_model(); let m = two_cat_model();
let mut bufs = HashMap::new(); let mut bufs = HashMap::new();
bufs.insert("command".to_string(), "q".to_string()); bufs.insert("command".to_string(), "q".to_string());
let mut ctx = make_ctx(&m); let layout = make_layout(&m);
let mut ctx = make_ctx(&m, &layout);
ctx.buffers = &bufs; ctx.buffers = &bufs;
let cmd = ExecuteCommand; let cmd = ExecuteCommand;
let effects = cmd.execute(&ctx); let effects = cmd.execute(&ctx);
@ -3065,9 +3059,10 @@ mod tests {
("Month".to_string(), "Jan".to_string()), ("Month".to_string(), "Jan".to_string()),
]); ]);
m.set_cell(key, CellValue::Number(42.0)); m.set_cell(key, CellValue::Number(42.0));
let ctx = make_ctx(&m); let layout = make_layout(&m);
let ctx = make_ctx(&m, &layout);
let cmd = ClearCellCommand { let cmd = ClearCellCommand {
key: ctx.cell_key.clone().unwrap(), key: ctx.cell_key().clone().unwrap(),
}; };
let effects = cmd.execute(&ctx); let effects = cmd.execute(&ctx);
assert_eq!(effects.len(), 2); // ClearCell + MarkDirty assert_eq!(effects.len(), 2); // ClearCell + MarkDirty
@ -3081,9 +3076,10 @@ mod tests {
("Month".to_string(), "Jan".to_string()), ("Month".to_string(), "Jan".to_string()),
]); ]);
m.set_cell(key, CellValue::Number(99.0)); m.set_cell(key, CellValue::Number(99.0));
let ctx = make_ctx(&m); let layout = make_layout(&m);
let ctx = make_ctx(&m, &layout);
let cmd = YankCell { let cmd = YankCell {
key: ctx.cell_key.clone().unwrap(), key: ctx.cell_key().clone().unwrap(),
}; };
let effects = cmd.execute(&ctx); let effects = cmd.execute(&ctx);
assert_eq!(effects.len(), 2); // SetYanked + SetStatus assert_eq!(effects.len(), 2); // SetYanked + SetStatus
@ -3092,7 +3088,8 @@ mod tests {
#[test] #[test]
fn toggle_panel_open_and_focus() { fn toggle_panel_open_and_focus() {
let m = two_cat_model(); let m = two_cat_model();
let ctx = make_ctx(&m); let layout = make_layout(&m);
let ctx = make_ctx(&m, &layout);
let cmd = TogglePanelAndFocus { let cmd = TogglePanelAndFocus {
panel: effect::Panel::Formula, panel: effect::Panel::Formula,
open: true, open: true,
@ -3110,7 +3107,8 @@ mod tests {
#[test] #[test]
fn toggle_panel_close_and_unfocus() { fn toggle_panel_close_and_unfocus() {
let m = two_cat_model(); let m = two_cat_model();
let ctx = make_ctx(&m); let layout = make_layout(&m);
let ctx = make_ctx(&m, &layout);
let cmd = TogglePanelAndFocus { let cmd = TogglePanelAndFocus {
panel: effect::Panel::Formula, panel: effect::Panel::Formula,
open: false, open: false,
@ -3123,7 +3121,8 @@ mod tests {
#[test] #[test]
fn enter_advance_moves_down() { fn enter_advance_moves_down() {
let m = two_cat_model(); let m = two_cat_model();
let ctx = make_ctx(&m); let layout = make_layout(&m);
let ctx = make_ctx(&m, &layout);
let cmd = EnterAdvance { let cmd = EnterAdvance {
cursor: CursorState::from_ctx(&ctx), cursor: CursorState::from_ctx(&ctx),
}; };
@ -3139,7 +3138,8 @@ mod tests {
#[test] #[test]
fn search_navigate_with_empty_query_returns_nothing() { fn search_navigate_with_empty_query_returns_nothing() {
let m = two_cat_model(); let m = two_cat_model();
let ctx = make_ctx(&m); let layout = make_layout(&m);
let ctx = make_ctx(&m, &layout);
let cmd = SearchNavigate(true); let cmd = SearchNavigate(true);
let effects = cmd.execute(&ctx); let effects = cmd.execute(&ctx);
assert!(effects.is_empty()); assert!(effects.is_empty());
@ -3148,7 +3148,8 @@ mod tests {
#[test] #[test]
fn enter_edit_mode_produces_editing_mode() { fn enter_edit_mode_produces_editing_mode() {
let m = two_cat_model(); let m = two_cat_model();
let ctx = make_ctx(&m); let layout = make_layout(&m);
let ctx = make_ctx(&m, &layout);
let cmd = EnterEditMode { let cmd = EnterEditMode {
initial_value: String::new(), initial_value: String::new(),
}; };
@ -3161,7 +3162,8 @@ mod tests {
#[test] #[test]
fn enter_tile_select_with_categories() { fn enter_tile_select_with_categories() {
let m = two_cat_model(); let m = two_cat_model();
let ctx = make_ctx(&m); let layout = make_layout(&m);
let ctx = make_ctx(&m, &layout);
let cmd = EnterTileSelect; let cmd = EnterTileSelect;
let effects = cmd.execute(&ctx); let effects = cmd.execute(&ctx);
assert_eq!(effects.len(), 2); // SetTileCatIdx + ChangeMode assert_eq!(effects.len(), 2); // SetTileCatIdx + ChangeMode
@ -3177,7 +3179,8 @@ mod tests {
// Models always have virtual categories (_Index, _Dim), so tile // Models always have virtual categories (_Index, _Dim), so tile
// select always has something to operate on. // select always has something to operate on.
let m = Model::new("Empty"); let m = Model::new("Empty");
let ctx = make_ctx(&m); let layout = make_layout(&m);
let ctx = make_ctx(&m, &layout);
let cmd = EnterTileSelect; let cmd = EnterTileSelect;
let effects = cmd.execute(&ctx); let effects = cmd.execute(&ctx);
assert_eq!(effects.len(), 2); // SetTileCatIdx + ChangeMode assert_eq!(effects.len(), 2); // SetTileCatIdx + ChangeMode
@ -3186,7 +3189,8 @@ mod tests {
#[test] #[test]
fn toggle_group_under_cursor_returns_empty_without_groups() { fn toggle_group_under_cursor_returns_empty_without_groups() {
let m = two_cat_model(); let m = two_cat_model();
let ctx = make_ctx(&m); let layout = make_layout(&m);
let ctx = make_ctx(&m, &layout);
let cmd = ToggleGroupUnderCursor; let cmd = ToggleGroupUnderCursor;
let effects = cmd.execute(&ctx); let effects = cmd.execute(&ctx);
// No groups defined, so nothing to toggle // No groups defined, so nothing to toggle
@ -3196,7 +3200,8 @@ mod tests {
#[test] #[test]
fn search_or_category_add_without_query_opens_category_add() { fn search_or_category_add_without_query_opens_category_add() {
let m = two_cat_model(); let m = two_cat_model();
let ctx = make_ctx(&m); let layout = make_layout(&m);
let ctx = make_ctx(&m, &layout);
let cmd = SearchOrCategoryAdd; let cmd = SearchOrCategoryAdd;
let effects = cmd.execute(&ctx); let effects = cmd.execute(&ctx);
assert_eq!(effects.len(), 2); // SetPanelOpen + ChangeMode assert_eq!(effects.len(), 2); // SetPanelOpen + ChangeMode
@ -3210,7 +3215,8 @@ mod tests {
#[test] #[test]
fn cycle_panel_focus_with_no_panels_open() { fn cycle_panel_focus_with_no_panels_open() {
let m = two_cat_model(); let m = two_cat_model();
let ctx = make_ctx(&m); let layout = make_layout(&m);
let ctx = make_ctx(&m, &layout);
let cmd = CyclePanelFocus { let cmd = CyclePanelFocus {
formula_open: false, formula_open: false,
category_open: false, category_open: false,
@ -3223,7 +3229,8 @@ mod tests {
#[test] #[test]
fn cycle_panel_focus_with_formula_panel_open() { fn cycle_panel_focus_with_formula_panel_open() {
let m = two_cat_model(); let m = two_cat_model();
let mut ctx = make_ctx(&m); let layout = make_layout(&m);
let mut ctx = make_ctx(&m, &layout);
ctx.formula_panel_open = true; ctx.formula_panel_open = true;
let cmd = CyclePanelFocus { let cmd = CyclePanelFocus {
formula_open: true, formula_open: true,