From 737b177ab5d2a51eb72553827b0bbde255e2fa3c Mon Sep 17 00:00:00 2001 From: Agus Zubiaga Date: Tue, 18 Feb 2025 14:49:13 -0300 Subject: [PATCH] Switch to columnar selection by pressing `alt-shift` while mouse is down (#25096) We begin a columnar selection when we drag the mouse while holding `alt-shift`. This PR makes it possible to start the selection and then turn it into columnar by pressing `alt-shift`. Fixes #5372 Release Notes: - Support switching to columnar selection by pressing `alt-shift` while mouse is down --- crates/editor/src/editor.rs | 36 +++++++++++++++++++++++++++ crates/editor/src/element.rs | 8 +++--- crates/gpui/src/platform/keystroke.rs | 2 +- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 2c41a3f5fe0cc078382eb5b6a2d52d34217f4df4..14256b326653f9f26587ae417390debe96712c57 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -206,6 +206,14 @@ pub(crate) const SCROLL_CENTER_TOP_BOTTOM_DEBOUNCE_TIMEOUT: Duration = Duration: pub(crate) const EDIT_PREDICTION_KEY_CONTEXT: &str = "edit_prediction"; pub(crate) const EDIT_PREDICTION_CONFLICT_KEY_CONTEXT: &str = "edit_prediction_conflict"; +const COLUMNAR_SELECTION_MODIFIERS: Modifiers = Modifiers { + alt: true, + shift: true, + control: false, + platform: false, + function: false, +}; + pub fn render_parsed_markdown( element_id: impl Into, parsed: &language::ParsedMarkdown, @@ -5341,6 +5349,8 @@ impl Editor { self.update_edit_prediction_preview(&modifiers, window, cx); } + self.update_selection_mode(&modifiers, position_map, window, cx); + let mouse_position = window.mouse_position(); if !position_map.text_hitbox.is_hovered(window) { return; @@ -5355,6 +5365,32 @@ impl Editor { ) } + fn update_selection_mode( + &mut self, + modifiers: &Modifiers, + position_map: &PositionMap, + window: &mut Window, + cx: &mut Context, + ) { + if modifiers != &COLUMNAR_SELECTION_MODIFIERS || self.selections.pending.is_none() { + return; + } + + let mouse_position = window.mouse_position(); + let point_for_position = position_map.point_for_position(mouse_position); + let position = point_for_position.previous_valid; + + self.select( + SelectPhase::BeginColumnar { + position, + reset: false, + goal_column: point_for_position.exact_unclipped.column(), + }, + window, + cx, + ); + } + fn update_edit_prediction_preview( &mut self, modifiers: &Modifiers, diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index cd9d28b1a7b67e4ef40894271da546959ae495e2..b1d828564be11dce9005b9ee01ef39b4949dac8c 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -21,8 +21,9 @@ use crate::{ GutterDimensions, HalfPageDown, HalfPageUp, HandleInput, HoveredCursor, InlineCompletion, JumpData, LineDown, LineUp, OpenExcerpts, PageDown, PageUp, Point, RevertSelectedHunks, RowExt, RowRangeExt, SelectPhase, SelectedTextHighlight, Selection, SoftWrap, StickyHeaderExcerpt, - ToPoint, ToggleFold, ToggleStagedSelectedDiffHunks, CURSORS_VISIBLE_FOR, FILE_HEADER_HEIGHT, - GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED, MAX_LINE_LEN, MULTI_BUFFER_EXCERPT_HEADER_HEIGHT, + ToPoint, ToggleFold, ToggleStagedSelectedDiffHunks, COLUMNAR_SELECTION_MODIFIERS, + CURSORS_VISIBLE_FOR, FILE_HEADER_HEIGHT, GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED, MAX_LINE_LEN, + MULTI_BUFFER_EXCERPT_HEADER_HEIGHT, }; use buffer_diff::{DiffHunkSecondaryStatus, DiffHunkStatus}; use client::ParticipantIndex; @@ -515,6 +516,7 @@ impl EditorElement { if editor.hover_state.focused(window, cx) { return; } + editor.handle_modifiers_changed(event.modifiers, &position_map, window, cx); }) } @@ -594,7 +596,7 @@ impl EditorElement { let point_for_position = position_map.point_for_position(event.position); let position = point_for_position.previous_valid; - if modifiers.shift && modifiers.alt { + if modifiers == COLUMNAR_SELECTION_MODIFIERS { editor.select( SelectPhase::BeginColumnar { position, diff --git a/crates/gpui/src/platform/keystroke.rs b/crates/gpui/src/platform/keystroke.rs index f96d3d1721fa8654c48ca388c284414f767b3b4a..f2edf35a5ae6412ba1d83226a40d84d1fd774681 100644 --- a/crates/gpui/src/platform/keystroke.rs +++ b/crates/gpui/src/platform/keystroke.rs @@ -414,7 +414,7 @@ impl Modifiers { } } - /// Returns [`Modifiers`] with just control. + /// Returns [`Modifiers`] with just alt. pub fn alt() -> Modifiers { Modifiers { alt: true,