diff --git a/crates/engine/src/cmd/edit/selection.rs b/crates/engine/src/cmd/edit/selection.rs index 8115f6390..f171c65a8 100644 --- a/crates/engine/src/cmd/edit/selection.rs +++ b/crates/engine/src/cmd/edit/selection.rs @@ -66,7 +66,14 @@ pub(super) fn selection_move(s: &mut Session, p: &Json) -> Result { // Extension moves the far corner, not the active cell. let from = if extend { sel.moving_end() } else { sel.active }; // Skip hidden rows/columns and step over merged areas. - let mut to = if jmp { + let mut to = if jmp && extend && (dr == 0) != (dc == 0) { + // Excel: Ctrl+Shift+arrow searches the active cell's column (or row) from the moving + // edge, so a header row selected with Ctrl+Shift+Right extends down to the end of the + // active column's data. + let probe = if dr != 0 { CellRef::new(from.row, sel.active.col) } else { CellRef::new(sel.active.row, from.col) }; + let edge = jump(sheet, probe, dr.signum(), dc.signum()); + if dr != 0 { CellRef::new(edge.row, from.col) } else { CellRef::new(from.row, edge.col) } + } else if jmp { jump(sheet, from, dr.signum(), dc.signum()) } else { let mut t = from.offset_clamped(dr, dc); diff --git a/crates/engine/tests/it/ctrl_shift_arrow_active_line.rs b/crates/engine/tests/it/ctrl_shift_arrow_active_line.rs new file mode 100644 index 000000000..d296d50bb --- /dev/null +++ b/crates/engine/tests/it/ctrl_shift_arrow_active_line.rs @@ -0,0 +1,56 @@ +//! Ctrl/Cmd+Shift+arrow searches the active cell's column (or row) from the moving edge (#3484). + +use gridcraft_engine::Session; +use serde_json::json; + +fn table() -> Session { + // Headers in A2:K2, a key column in A3:A13, nothing else filled. + let mut s = Session::new(); + s.new_workbook(); + let header: Vec = (1..=11).collect(); + s.execute("range.setValues", json!({"range": "A2:K2", "values": [header]})).unwrap(); + let keys: Vec> = (1..=11).map(|i| vec![i]).collect(); + s.execute("range.setValues", json!({"range": "A3:A13", "values": keys})).unwrap(); + s +} + +fn ctrl_shift(s: &mut Session, dr: i64, dc: i64) -> String { + s.execute("selection.move", json!({"dr": dr, "dc": dc, "extend": true, "jump": true})).unwrap(); + s.doc().unwrap().selection.a1() +} + +#[test] +fn ctrl_shift_down_after_ctrl_shift_right_follows_the_active_column() { + let mut s = table(); + s.execute("selection.set", json!({"cell": "A2"})).unwrap(); + assert_eq!(ctrl_shift(&mut s, 0, 1), "A2:K2"); + assert_eq!(ctrl_shift(&mut s, 1, 0), "A2:K13", "column A's data ends on row 13; column K's ends on row 2"); + // From the end of column A's data the next press runs to the sheet's last row. + assert_eq!(ctrl_shift(&mut s, 1, 0), "A2:K1048576"); +} + +#[test] +fn ctrl_shift_right_after_ctrl_shift_down_follows_the_active_row() { + let mut s = table(); + s.execute("selection.set", json!({"cell": "A2"})).unwrap(); + assert_eq!(ctrl_shift(&mut s, 1, 0), "A2:A13"); + assert_eq!(ctrl_shift(&mut s, 0, 1), "A2:K13", "row 2's data ends in column K; row 13's ends in column A"); +} + +#[test] +fn ctrl_shift_down_after_ctrl_shift_left_follows_the_active_column() { + // The active cell is on the right: column K, empty below the header, decides, not column A. + let mut s = table(); + s.execute("selection.set", json!({"cell": "K2"})).unwrap(); + assert_eq!(ctrl_shift(&mut s, 0, -1), "A2:K2"); + assert_eq!(ctrl_shift(&mut s, 1, 0), "A2:K1048576"); +} + +#[test] +fn ctrl_shift_right_after_ctrl_shift_up_follows_the_active_row() { + // The active cell is at the bottom: row 13, empty right of the key, decides, not row 2. + let mut s = table(); + s.execute("selection.set", json!({"cell": "A13"})).unwrap(); + assert_eq!(ctrl_shift(&mut s, -1, 0), "A2:A13"); + assert_eq!(ctrl_shift(&mut s, 0, 1), "2:13", "row 13's data ends in column A, so the rows extend to the last column"); +} diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index a9cd7cbfe..0ba7c0db8 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -7,6 +7,7 @@ mod chart_workbook_names; mod command_sequence_fuzz; mod cross_sheet_cut; mod csv_export_budget; +mod ctrl_shift_arrow_active_line; mod custom_filter_blanks; mod custom_view_freeze; mod dynamicmetadata;