| 1 | use crate::input::InputModeKind; |
| 2 | use gpui::{Context, Pixels, Point, Window}; |
| 3 | use sum_tree::Bias; |
| 4 | |
| 5 | use crate::input::{ |
| 6 | InputBaseState, MoveDown, MoveEnd, MoveHome, MoveLeft, MovePageDown, MovePageUp, MoveRight, |
| 7 | MoveToEnd, MoveToNextWord, MoveToPreviousWord, MoveToStart, MoveUp, RopeExt as _, |
| 8 | cursor::CursorSelection, |
| 9 | }; |
| 10 | |
| 11 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 12 | pub(crate) enum MoveDirection { |
| 13 | Up, |
| 14 | Down, |
| 15 | } |
| 16 | |
| 17 | impl<M: InputModeKind> InputBaseState<M> { |
| 18 | /// Compute the column anchor for the given `offset`. Wrap/fold-aware. |
| 19 | pub(super) fn preferred_column_for(&self, offset: usize) -> Option<(Pixels, usize)> { |
| 20 | self.preferred_column_for_with_affinity(offset, false) |
| 21 | } |
| 22 | |
| 23 | /// Like [`Self::preferred_column_for`], but resolves an offset on a soft wrap |
| 24 | /// boundary to the row the caret is drawn on. |
| 25 | pub(super) fn preferred_column_for_with_affinity( |
| 26 | &self, |
| 27 | offset: usize, |
| 28 | line_end_affinity: bool, |
| 29 | ) -> Option<(Pixels, usize)> { |
| 30 | let last_layout = self.last_layout.as_ref()?; |
| 31 | let point = self.text.offset_to_point(offset); |
| 32 | let line = last_layout.line(point.row)?; |
| 33 | let pos = line.position_for_index(point.column, last_layout, line_end_affinity)?; |
| 34 | Some((pos.x, point.column)) |
| 35 | } |
| 36 | |
| 37 | /// The line-end affinity that applies to `sel`. Only the active cursor |
| 38 | /// carries one; every other cursor sits at the start of its row. |
| 39 | pub(super) fn line_end_affinity_for(&self, sel: &CursorSelection) -> bool { |
| 40 | sel.id == self.active_selection().id && self.cursor_line_end_affinity |
| 41 | } |
| 42 | |
| 43 | /// The line-end affinity for a cursor known only by its offset. Cursors never share an |
| 44 | /// offset, so this is the active cursor's affinity when `offset` is where it sits. |
| 45 | pub(super) fn line_end_affinity_at(&self, offset: usize) -> bool { |
| 46 | offset == self.cursor() && self.cursor_line_end_affinity |
| 47 | } |
| 48 | |
| 49 | /// Called after moving the cursor. Updates the active selection's |
| 50 | /// `column_anchor` if we know where the cursor now is. |
| 51 | pub(super) fn update_preferred_column(&mut self) { |
| 52 | let anchor = |
| 53 | self.preferred_column_for_with_affinity(self.cursor(), self.cursor_line_end_affinity); |
| 54 | self.active_selection_mut().column_anchor = anchor; |
| 55 | } |
| 56 | |
| 57 | /// Move the cursor to the given offset. |
| 58 | /// |
| 59 | /// The offset is the UTF-8 offset. |
| 60 | /// |
| 61 | /// Ensure the offset use self.next_boundary or self.previous_boundary to get the correct offset. |
| 62 | pub(crate) fn move_to( |
| 63 | &mut self, |
| 64 | offset: usize, |
| 65 | direction: Option<MoveDirection>, |
| 66 | cx: &mut Context<Self>, |
| 67 | ) { |
| 68 | self.move_to_with_affinity(offset, direction, false, cx); |
| 69 | } |
| 70 | |
| 71 | /// Like [`Self::move_to`], but also carries the caret's line-end affinity. |
| 72 | /// |
| 73 | /// A soft wrap boundary is one offset shared by the end of one visual line and the start of |
| 74 | /// the next, so the offset alone cannot say where to draw the caret. Callers that resolved |
| 75 | /// the offset from a visual position -- a click, a drag, a vertical move -- already know |
| 76 | /// which of the two rows the user meant, and pass it here. Taking it in the same call as the |
| 77 | /// move is what keeps the two from drifting apart. |
| 78 | pub(crate) fn move_to_with_affinity( |
| 79 | &mut self, |
| 80 | offset: usize, |
| 81 | direction: Option<MoveDirection>, |
| 82 | line_end_affinity: bool, |
| 83 | cx: &mut Context<Self>, |
| 84 | ) { |
| 85 | self.undo_manager.break_transaction_coalescing(); |
| 86 | self.selections.remove_all_but_active(); |
| 87 | let offset = self.cursor_boundary(offset, Bias::Left); |
| 88 | self.cursor_line_end_affinity = line_end_affinity; |
| 89 | self.set_cursor_to(offset); |
| 90 | self.scroll_to(offset, direction, cx); |
| 91 | self.pause_blink_cursor(cx); |
| 92 | self.update_preferred_column(); |
| 93 | M::hide_context_menu(self, cx); |
| 94 | M::clear_inline_completion(self, cx); |
| 95 | cx.notify() |
| 96 | } |
| 97 | |
| 98 | /// Compute the target offset when moving a cursor at `offset` vertically by |
| 99 | /// `move_lines`, honoring the remembered `column_anchor`. Wrap/fold-aware. |
| 100 | /// |
| 101 | /// Returns the new offset together with the line-end affinity the caret |
| 102 | /// should carry there. |
| 103 | pub(super) fn vertical_target( |
| 104 | &self, |
| 105 | offset: usize, |
| 106 | column_anchor: Option<(Pixels, usize)>, |
| 107 | line_end_affinity: bool, |
| 108 | move_lines: isize, |
| 109 | ) -> (usize, bool) { |
| 110 | let Some(last_layout) = &self.last_layout else { |
| 111 | return (offset, line_end_affinity); |
| 112 | }; |
| 113 | |
| 114 | // Start from the row the caret is drawn on, not the row the raw offset falls in: on a |
| 115 | // soft wrap boundary those are two different rows. |
| 116 | let mut display_point = self |
| 117 | .display_map |
| 118 | .offset_to_wrap_display_point_with_affinity(offset, line_end_affinity); |
| 119 | |
| 120 | // Convert wrap row → display row (skips folded rows), move, then convert back |
| 121 | let current_display_row = self |
| 122 | .display_map |
| 123 | .wrap_row_to_display_row(display_point.row) |
| 124 | .unwrap_or_else(|| { |
| 125 | self.display_map |
| 126 | .nearest_visible_display_row(display_point.row) |
| 127 | }); |
| 128 | let max_display_row = self.display_map.display_row_count().saturating_sub(1); |
| 129 | let target_display_row = current_display_row |
| 130 | .saturating_add_signed(move_lines) |
| 131 | .min(max_display_row); |
| 132 | let target_wrap_row = self |
| 133 | .display_map |
| 134 | .display_row_to_wrap_row(target_display_row) |
| 135 | .unwrap_or(display_point.row); |
| 136 | |
| 137 | display_point.row = target_wrap_row; |
| 138 | display_point.column = 0; |
| 139 | let mut new_offset = self.display_map.wrap_display_point_to_offset(display_point); |
| 140 | |
| 141 | let column_anchor = column_anchor |
| 142 | .or_else(|| self.preferred_column_for_with_affinity(offset, line_end_affinity)); |
| 143 | let mut new_affinity = false; |
| 144 | if let Some((preferred_x, column)) = column_anchor { |
| 145 | // Get display point again to update local_row. |
| 146 | let mut next_display_point = self.display_map.offset_to_wrap_display_point(new_offset); |
| 147 | next_display_point.column = 0; |
| 148 | let next_point = self |
| 149 | .display_map |
| 150 | .wrap_display_point_to_point(next_display_point); |
| 151 | let line_start_offset = self.text.line_start_offset(next_point.row); |
| 152 | |
| 153 | // If in visible range, prefer to use position to get column. |
| 154 | if let Some(line) = last_layout.line(next_point.row) { |
| 155 | if let Some((x, line_end_affinity)) = line.closest_index_for_position( |
| 156 | Point { |
| 157 | x: preferred_x, |
| 158 | y: line.row_top(next_display_point.local_row, last_layout.line_height), |
| 159 | }, |
| 160 | last_layout, |
| 161 | ) { |
| 162 | new_offset = line_start_offset + x; |
| 163 | // Landing on a wrap boundary means the preferred column pointed past the |
| 164 | // last glyph of the target row, so the caret stays on that row. |
| 165 | new_affinity = line_end_affinity; |
| 166 | } |
| 167 | } else { |
| 168 | // Not in visible range, use column directly. |
| 169 | let max_line_len = self.text.slice_line(next_point.row).len(); |
| 170 | new_offset = line_start_offset + column.min(max_line_len); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | (new_offset, new_affinity) |
| 175 | } |
| 176 | |
| 177 | /// Extend to the document edge when there is no further visual row. Plain |
| 178 | /// movement retains its column there, but selection must still reach the |
| 179 | /// remaining text on the first or last row. |
| 180 | pub(super) fn vertical_selection_target( |
| 181 | &self, |
| 182 | offset: usize, |
| 183 | column_anchor: Option<(Pixels, usize)>, |
| 184 | line_end_affinity: bool, |
| 185 | move_lines: isize, |
| 186 | ) -> (usize, bool) { |
| 187 | let target = self.vertical_target(offset, column_anchor, line_end_affinity, move_lines); |
| 188 | if self.last_layout.is_some() { |
| 189 | let row = |offset, affinity| { |
| 190 | self.display_map |
| 191 | .offset_to_wrap_display_point_with_affinity(offset, affinity) |
| 192 | .row |
| 193 | }; |
| 194 | if row(offset, line_end_affinity) == row(target.0, target.1) { |
| 195 | return (if move_lines < 0 { 0 } else { self.text.len() }, false); |
| 196 | } |
| 197 | } |
| 198 | target |
| 199 | } |
| 200 | |
| 201 | /// Move every cursor through `f`, which maps each selection to a |
| 202 | /// `(new_offset, column_anchor, line_end_affinity)`, collapsing each to a |
| 203 | /// cursor. Overlapping cursors are merged, then the standard post-move |
| 204 | /// sequence runs. Only the active cursor's affinity is kept, see |
| 205 | /// [`Self::move_to_with_affinity`]. |
| 206 | pub(super) fn move_all_cursors( |
| 207 | &mut self, |
| 208 | f: impl Fn(&Self, &CursorSelection) -> (usize, Option<(Pixels, usize)>, bool), |
| 209 | direction: Option<MoveDirection>, |
| 210 | _window: &mut Window, |
| 211 | cx: &mut Context<Self>, |
| 212 | ) { |
| 213 | self.undo_manager.break_transaction_coalescing(); |
| 214 | let mut active_affinity = false; |
| 215 | let new_selections: Vec<CursorSelection> = self |
| 216 | .selections |
| 217 | .iter() |
| 218 | .map(|sel| { |
| 219 | let (offset, anchor, line_end_affinity) = f(self, sel); |
| 220 | if sel.id == self.active_selection().id { |
| 221 | active_affinity = line_end_affinity; |
| 222 | } |
| 223 | let mut new_sel = *sel; |
| 224 | new_sel.place_at(self.cursor_boundary(offset, Bias::Left), anchor); |
| 225 | new_sel |
| 226 | }) |
| 227 | .collect(); |
| 228 | self.selections.replace_all(new_selections); |
| 229 | self.selections.merge_overlapping(); |
| 230 | |
| 231 | self.cursor_line_end_affinity = active_affinity; |
| 232 | self.scroll_to(self.cursor(), direction, cx); |
| 233 | self.pause_blink_cursor(cx); |
| 234 | M::hide_context_menu(self, cx); |
| 235 | M::clear_inline_completion(self, cx); |
| 236 | cx.notify(); |
| 237 | } |
| 238 | |
| 239 | /// Move every cursor vertically by `move_lines`. |
| 240 | /// |
| 241 | /// When `collapse` is set, a non-empty selection first collapses to just |
| 242 | /// outside its start (up) or end (down) before moving, otherwise the cursor |
| 243 | /// offset is used. |
| 244 | fn move_vertical( |
| 245 | &mut self, |
| 246 | move_lines: isize, |
| 247 | collapse: bool, |
| 248 | window: &mut Window, |
| 249 | cx: &mut Context<Self>, |
| 250 | ) { |
| 251 | if self.is_single_line() { |
| 252 | return; |
| 253 | } |
| 254 | self.pause_blink_cursor(cx); |
| 255 | |
| 256 | let direction = if move_lines < 0 { |
| 257 | MoveDirection::Up |
| 258 | } else { |
| 259 | MoveDirection::Down |
| 260 | }; |
| 261 | |
| 262 | self.move_all_cursors( |
| 263 | move |s, sel| { |
| 264 | let (effective, anchor, affinity) = if sel.is_empty() || !collapse { |
| 265 | ( |
| 266 | sel.cursor_offset(), |
| 267 | sel.column_anchor, |
| 268 | s.line_end_affinity_for(sel), |
| 269 | ) |
| 270 | } else if move_lines < 0 { |
| 271 | let e = s.previous_boundary(sel.start.saturating_sub(1)); |
| 272 | (e, s.preferred_column_for(e), false) |
| 273 | } else { |
| 274 | let e = s.next_boundary(sel.end.saturating_sub(1)); |
| 275 | let affinity = s.line_end_affinity_at(e); |
| 276 | ( |
| 277 | e, |
| 278 | s.preferred_column_for_with_affinity(e, affinity), |
| 279 | affinity, |
| 280 | ) |
| 281 | }; |
| 282 | let anchor = |
| 283 | anchor.or_else(|| s.preferred_column_for_with_affinity(effective, affinity)); |
| 284 | let (offset, affinity) = s.vertical_target(effective, anchor, affinity, move_lines); |
| 285 | (offset, anchor, affinity) |
| 286 | }, |
| 287 | Some(direction), |
| 288 | window, |
| 289 | cx, |
| 290 | ); |
| 291 | } |
| 292 | |
| 293 | pub(super) fn left(&mut self, _: &MoveLeft, window: &mut Window, cx: &mut Context<Self>) { |
| 294 | // With a lone cursor at the very start there is nowhere to move. |
| 295 | // Propagate the keystroke so an ancestor (e.g. a navigable command |
| 296 | // palette) can act on it. This is harmless when nothing is bound there. |
| 297 | // With multiple cursors the others can still move, so only the |
| 298 | // single-cursor case propagates. |
| 299 | if self.selections.is_single() && self.active_selection().is_empty() && self.cursor() == 0 { |
| 300 | cx.propagate(); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | self.move_all_cursors( |
| 305 | |s, sel| { |
| 306 | let offset = if sel.is_empty() { |
| 307 | s.previous_boundary(sel.cursor_offset()) |
| 308 | } else { |
| 309 | sel.start |
| 310 | }; |
| 311 | (offset, s.preferred_column_for(offset), false) |
| 312 | }, |
| 313 | None, |
| 314 | window, |
| 315 | cx, |
| 316 | ); |
| 317 | } |
| 318 | |
| 319 | pub(super) fn right(&mut self, _: &MoveRight, window: &mut Window, cx: &mut Context<Self>) { |
| 320 | // Mirror `left`: a lone cursor at the end of the text has nowhere to |
| 321 | // move, so let the keystroke bubble to an ancestor. |
| 322 | if self.selections.is_single() |
| 323 | && self.active_selection().is_empty() |
| 324 | && self.cursor() == self.text.len() |
| 325 | { |
| 326 | cx.propagate(); |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | self.move_all_cursors( |
| 331 | |s, sel| { |
| 332 | let offset = if sel.is_empty() { |
| 333 | s.next_boundary(sel.cursor_offset()) |
| 334 | } else { |
| 335 | sel.end |
| 336 | }; |
| 337 | (offset, s.preferred_column_for(offset), false) |
| 338 | }, |
| 339 | None, |
| 340 | window, |
| 341 | cx, |
| 342 | ); |
| 343 | } |
| 344 | |
| 345 | pub(super) fn up(&mut self, action: &MoveUp, window: &mut Window, cx: &mut Context<Self>) { |
| 346 | if M::handle_context_menu_action(self, Box::new(action.clone()), window, cx) { |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | self.move_vertical(-1, true, window, cx); |
| 351 | } |
| 352 | |
| 353 | pub(super) fn down(&mut self, action: &MoveDown, window: &mut Window, cx: &mut Context<Self>) { |
| 354 | if M::handle_context_menu_action(self, Box::new(action.clone()), window, cx) { |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | self.move_vertical(1, true, window, cx); |
| 359 | } |
| 360 | |
| 361 | pub(super) fn page_up(&mut self, _: &MovePageUp, window: &mut Window, cx: &mut Context<Self>) { |
| 362 | if self.is_single_line() { |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | let Some(last_layout) = &self.last_layout else { |
| 367 | return; |
| 368 | }; |
| 369 | |
| 370 | let display_lines = (self.input_bounds.size.height / last_layout.line_height) as isize; |
| 371 | self.move_vertical(-display_lines, false, window, cx); |
| 372 | } |
| 373 | |
| 374 | pub(super) fn page_down( |
| 375 | &mut self, |
| 376 | _: &MovePageDown, |
| 377 | window: &mut Window, |
| 378 | cx: &mut Context<Self>, |
| 379 | ) { |
| 380 | if self.is_single_line() { |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | let Some(last_layout) = &self.last_layout else { |
| 385 | return; |
| 386 | }; |
| 387 | |
| 388 | let display_lines = (self.input_bounds.size.height / last_layout.line_height) as isize; |
| 389 | self.move_vertical(display_lines, false, window, cx); |
| 390 | } |
| 391 | |
| 392 | pub(super) fn home(&mut self, _: &MoveHome, window: &mut Window, cx: &mut Context<Self>) { |
| 393 | self.move_all_cursors( |
| 394 | |s, sel| { |
| 395 | let offset = s.start_of_line_at(sel.cursor_offset(), s.line_end_affinity_for(sel)); |
| 396 | (offset, s.preferred_column_for(offset), false) |
| 397 | }, |
| 398 | Some(MoveDirection::Up), |
| 399 | window, |
| 400 | cx, |
| 401 | ); |
| 402 | } |
| 403 | |
| 404 | pub(super) fn end(&mut self, _: &MoveEnd, window: &mut Window, cx: &mut Context<Self>) { |
| 405 | self.move_all_cursors( |
| 406 | |s, sel| { |
| 407 | let offset = s.end_of_line_at(sel.cursor_offset(), s.line_end_affinity_for(sel)); |
| 408 | // The caret belongs at the end of the visual row it is on. |
| 409 | (offset, s.preferred_column_for(offset), true) |
| 410 | }, |
| 411 | Some(MoveDirection::Down), |
| 412 | window, |
| 413 | cx, |
| 414 | ); |
| 415 | } |
| 416 | |
| 417 | pub(super) fn move_to_start( |
| 418 | &mut self, |
| 419 | _: &MoveToStart, |
| 420 | _: &mut Window, |
| 421 | cx: &mut Context<Self>, |
| 422 | ) { |
| 423 | self.move_to(0, None, cx); |
| 424 | } |
| 425 | |
| 426 | pub(super) fn move_to_end(&mut self, _: &MoveToEnd, _: &mut Window, cx: &mut Context<Self>) { |
| 427 | self.move_to(self.text.len(), None, cx); |
| 428 | } |
| 429 | |
| 430 | pub(super) fn move_to_previous_word( |
| 431 | &mut self, |
| 432 | _: &MoveToPreviousWord, |
| 433 | window: &mut Window, |
| 434 | cx: &mut Context<Self>, |
| 435 | ) { |
| 436 | self.move_all_cursors( |
| 437 | |s, sel| { |
| 438 | let offset = s.previous_start_of_word_at(sel.cursor_offset()); |
| 439 | (offset, s.preferred_column_for(offset), false) |
| 440 | }, |
| 441 | None, |
| 442 | window, |
| 443 | cx, |
| 444 | ); |
| 445 | } |
| 446 | |
| 447 | pub(super) fn move_to_next_word( |
| 448 | &mut self, |
| 449 | _: &MoveToNextWord, |
| 450 | window: &mut Window, |
| 451 | cx: &mut Context<Self>, |
| 452 | ) { |
| 453 | self.move_all_cursors( |
| 454 | |s, sel| { |
| 455 | let offset = s.next_end_of_word_at(sel.cursor_offset()); |
| 456 | (offset, s.preferred_column_for(offset), false) |
| 457 | }, |
| 458 | None, |
| 459 | window, |
| 460 | cx, |
| 461 | ); |
| 462 | } |
| 463 | } |