Skip to content

Commit 7570588

Browse files
okt4vclaude
andcommitted
fix: three correctness bugs in input state management
- SetCell sheet: 0 → active_sheet: cells edited on Sheet 2+ were silently written to Sheet 1, causing data loss in multi-sheet workbooks. handle_insert now receives workbook.active_sheet from handle_key and passes it through to all three SetCell sites (Esc / Enter / Tab commit). - key_buffer + key_buffer_count not cleared on Esc: typing a prefix key (g, d, y, @, …) then pressing Esc left stale state in key_buffer/key_buffer_count so the next keypress was wrongly matched as the second key of the abandoned sequence. - count_buffer leak through multi-key handler: digits typed between the first and second key of a two-key sequence were never consumed, leaving a phantom count that affected the next Normal-mode action. Added count_buffer.clear() immediately after key_buffer_count is taken in the multi-key dispatcher. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1e0ba7c commit 7570588

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

crates/asat-input/src/lib.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ impl InputState {
872872

873873
match self.mode.clone() {
874874
Mode::Normal => self.handle_normal(key, workbook),
875-
Mode::Insert { replace } => self.handle_insert(key, replace),
875+
Mode::Insert { replace } => self.handle_insert(key, replace, workbook.active_sheet),
876876
Mode::Visual { .. } => self.handle_visual(key, false),
877877
Mode::VisualLine => self.handle_visual(key, true),
878878
Mode::Command => self.handle_command(key),
@@ -927,6 +927,7 @@ impl InputState {
927927
let _first_ctrl = self.key_buffer[0].modifiers.contains(KeyModifiers::CONTROL);
928928
self.key_buffer.clear();
929929
let n = std::mem::replace(&mut self.key_buffer_count, 1);
930+
self.count_buffer.clear(); // drop any digits typed between the two keys
930931

931932
return match (first, key.code) {
932933
// gg / gt / gT / gw
@@ -1377,6 +1378,8 @@ impl InputState {
13771378

13781379
KeyCode::Esc => {
13791380
self.count_buffer.clear();
1381+
self.key_buffer.clear();
1382+
self.key_buffer_count = 1;
13801383
vec![AppAction::ClearSearch]
13811384
}
13821385

@@ -1603,7 +1606,12 @@ impl InputState {
16031606

16041607
// ── Insert mode ───────────────────────────────────────────────────────────
16051608

1606-
fn handle_insert(&mut self, key: KeyEvent, _replace: bool) -> Vec<AppAction> {
1609+
fn handle_insert(
1610+
&mut self,
1611+
key: KeyEvent,
1612+
_replace: bool,
1613+
active_sheet: usize,
1614+
) -> Vec<AppAction> {
16071615
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
16081616

16091617
// Record keystrokes for `.` repeat
@@ -1708,7 +1716,7 @@ impl InputState {
17081716
self.ci_suffix.clear();
17091717
vec![
17101718
AppAction::SetCell {
1711-
sheet: 0,
1719+
sheet: active_sheet,
17121720
row,
17131721
col,
17141722
value,
@@ -1731,7 +1739,7 @@ impl InputState {
17311739
self.ci_suffix.clear();
17321740
vec![
17331741
AppAction::SetCell {
1734-
sheet: 0,
1742+
sheet: active_sheet,
17351743
row,
17361744
col,
17371745
value,
@@ -1758,7 +1766,7 @@ impl InputState {
17581766
self.ci_suffix.clear();
17591767
vec![
17601768
AppAction::SetCell {
1761-
sheet: 0,
1769+
sheet: active_sheet,
17621770
row,
17631771
col,
17641772
value,

0 commit comments

Comments
 (0)