Skip to content

Commit 8d95e9b

Browse files
fix: keep copy mode active during projection updates (#4281)
* fix: keep copy mode active during projection updates refs #3812 * fix: preserve linewise selections during projection gaps refs #3812 --------- Co-authored-by: Ogulcan Celik <[email protected]>
1 parent a28bed2 commit 8d95e9b

2 files changed

Lines changed: 102 additions & 16 deletions

File tree

‎src/client/shell/copy_mode.rs‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -504,13 +504,10 @@ impl ClientShellState {
504504
}
505505

506506
fn move_copy_cursor(&mut self, row_delta: i16, col_delta: i16, outcome: &mut ClientShellInput) {
507-
let Some(hit) = self.copy_hit() else {
508-
self.exit_copy_mode(false, outcome);
509-
return;
510-
};
511507
let Some(copy_mode) = self.copy_mode.as_mut() else {
512508
return;
513509
};
510+
let (width, height) = copy_mode.geometry;
514511
if col_delta < 0 {
515512
copy_mode.cursor.col = copy_mode
516513
.cursor
@@ -521,11 +518,11 @@ impl ClientShellState {
521518
.cursor
522519
.col
523520
.saturating_add(col_delta as u16)
524-
.min(hit.inner_rect.width.saturating_sub(1));
521+
.min(width.saturating_sub(1));
525522
}
526523
let total_rows = copy_mode
527524
.max_offset_from_bottom
528-
.saturating_add(hit.inner_rect.height as usize)
525+
.saturating_add(height as usize)
529526
.max(1);
530527
if row_delta < 0 {
531528
copy_mode.cursor.row = copy_mode
@@ -610,16 +607,13 @@ impl ClientShellState {
610607
}
611608

612609
fn reveal_copy_cursor(&mut self, outcome: &mut ClientShellInput, reserve_mode_bar_row: bool) {
613-
let Some(hit) = self.copy_hit() else {
614-
return;
615-
};
616610
let request = self.copy_mode.as_mut().and_then(|copy_mode| {
617611
let current_top = copy_mode
618612
.max_offset_from_bottom
619613
.saturating_sub(copy_mode.offset_from_bottom) as u32;
620-
let max_cursor_row = hit
621-
.inner_rect
622-
.height
614+
let max_cursor_row = copy_mode
615+
.geometry
616+
.1
623617
.saturating_sub(if reserve_mode_bar_row { 2 } else { 1 });
624618
let bottom = current_top.saturating_add(u32::from(max_cursor_row));
625619
let desired_top = if copy_mode.cursor.row < current_top {
@@ -647,12 +641,11 @@ impl ClientShellState {
647641
}
648642

649643
fn begin_copy_selection(&mut self, linewise: bool) {
650-
let end_col = self
651-
.copy_hit()
652-
.map_or(0, |hit| hit.inner_rect.width.saturating_sub(1));
644+
let width = self.copy_hit().map(|hit| hit.inner_rect.width);
653645
let Some(copy_mode) = self.copy_mode.as_mut() else {
654646
return;
655647
};
648+
let end_col = width.unwrap_or(copy_mode.geometry.0).saturating_sub(1);
656649
if linewise {
657650
copy_mode.selection = Some(ClientCopySelection::Linewise {
658651
anchor_row: copy_mode.cursor.row,
@@ -695,7 +688,8 @@ impl ClientShellState {
695688
anchor_row,
696689
copy_mode.cursor.row,
697690
self.copy_hit()
698-
.map_or(0, |hit| hit.inner_rect.width.saturating_sub(1)),
691+
.map_or(copy_mode.geometry.0, |hit| hit.inner_rect.width)
692+
.saturating_sub(1),
699693
)
700694
}
701695
});

‎src/client/shell/tests/copy.rs‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,3 +2216,95 @@ fn word_selection_result_survives_focus_snapshot_lag() {
22162216
.as_ref()
22172217
.is_some_and(crate::selection::Selection::is_visible));
22182218
}
2219+
2220+
#[test]
2221+
fn copy_mode_repeat_during_projection_gap_stays_active() {
2222+
for selection_before_gap in [None, Some(true), Some(false)] {
2223+
let mut state = ClientShellState::new(ClientShellConfig::from_config(&Config::default()));
2224+
state.set_snapshot(Box::new(snapshot()));
2225+
let mut pane_surface = surface();
2226+
pane_surface.panes[0].scroll = Some(crate::protocol::PaneSurfaceScrollMetrics {
2227+
offset_from_bottom: 0,
2228+
max_offset_from_bottom: 20,
2229+
viewport_rows: 2,
2230+
});
2231+
state.set_pane_surface(pane_surface);
2232+
state.compose(106, 20).expect("composed frame");
2233+
let mut enter = ClientShellInput::default();
2234+
state.record_binding(
2235+
crate::input::KeybindMatch::Action(crate::input::KeybindAction::CopyMode),
2236+
&mut enter,
2237+
);
2238+
if selection_before_gap == Some(true) {
2239+
state.handle_input_bytes(b"V");
2240+
}
2241+
state.handle_raw_events(vec![RawInputEvent::Key(crate::input::TerminalKey::new(
2242+
KeyCode::Char('k'),
2243+
KeyModifiers::empty(),
2244+
))]);
2245+
2246+
let end_col = state.copy_mode.as_ref().expect("copy mode").geometry.0 - 1;
2247+
let mut next = snapshot();
2248+
next.revision += 1;
2249+
state.set_snapshot(Box::new(next));
2250+
assert!(state.hits.panes.is_empty());
2251+
assert_eq!(state.mode, ClientShellMode::Copy);
2252+
if selection_before_gap == Some(false) {
2253+
state.handle_input_bytes(b"V");
2254+
assert_eq!(
2255+
state
2256+
.selection
2257+
.as_ref()
2258+
.expect("linewise selection")
2259+
.ordered_cells(),
2260+
((20, 0), (20, end_col))
2261+
);
2262+
}
2263+
2264+
let kind = if selection_before_gap == Some(false) {
2265+
crossterm::event::KeyEventKind::Press
2266+
} else {
2267+
crossterm::event::KeyEventKind::Repeat
2268+
};
2269+
let moved = state.handle_raw_events(vec![RawInputEvent::Key(
2270+
crate::input::TerminalKey::new(KeyCode::Char('k'), KeyModifiers::empty())
2271+
.with_kind(kind),
2272+
)]);
2273+
assert!(moved.actions.iter().any(|action| matches!(
2274+
action,
2275+
ClientShellAction::Endpoint { request, .. }
2276+
if matches!(&request.method, crate::api::schema::Method::PaneScroll(params)
2277+
if params.pane_id == "pane_1" && params.offset_from_bottom == 1)
2278+
)));
2279+
if selection_before_gap.is_some() {
2280+
assert_eq!(
2281+
state
2282+
.selection
2283+
.as_ref()
2284+
.expect("linewise selection")
2285+
.ordered_cells(),
2286+
(
2287+
(19, 0),
2288+
(
2289+
if selection_before_gap == Some(true) {
2290+
21
2291+
} else {
2292+
20
2293+
},
2294+
end_col
2295+
)
2296+
)
2297+
);
2298+
}
2299+
2300+
assert_eq!(state.mode, ClientShellMode::Copy);
2301+
assert!(state.copy_mode.is_some());
2302+
assert_eq!(
2303+
state
2304+
.copy_mode
2305+
.as_ref()
2306+
.map(|copy_mode| copy_mode.cursor.row),
2307+
Some(19)
2308+
);
2309+
}
2310+
}

0 commit comments

Comments
 (0)