Skip to content

Commit

Permalink
Cherry-pick char_index_for_point fix to v0.172.x (#24286)
Browse files Browse the repository at this point in the history
Manual cherry-pick of #23989 

Release Notes:

- Fixed a bug where Zed could crash with certain input sources on macOS

Co-authored-by: Mikayla Maki <mikayla@zed.dev>
Co-authored-by: Louis Brunner <louis.brunner.fr@gmail.com>
Co-authored-by: ben <ben@zed.dev>
  • Loading branch information
4 people authored Feb 5, 2025
1 parent 3dd6dfb commit 43ccb51
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 120 deletions.
32 changes: 26 additions & 6 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ pub use editor_settings::{
CurrentLineHighlight, EditorSettings, ScrollBeyondLastLine, SearchSettings, ShowScrollbar,
};
pub use editor_settings_controls::*;
use element::LineWithInvisibles;
pub use element::{
CursorLayout, EditorElement, HighlightedRange, HighlightedRangeLine, PointForPosition,
};
use element::{LineWithInvisibles, PositionMap};
use futures::{future, FutureExt};
use fuzzy::StringMatchCandidate;
use zed_predict_onboarding::ZedPredictModal;
Expand Down Expand Up @@ -723,6 +723,7 @@ pub struct Editor {
>,
>,
last_bounds: Option<Bounds<Pixels>>,
last_position_map: Option<Rc<PositionMap>>,
expect_bounds_change: Option<Bounds<Pixels>>,
tasks: BTreeMap<(BufferId, BufferRow), RunnableTasks>,
tasks_update_task: Option<Task<()>>,
Expand Down Expand Up @@ -1382,6 +1383,7 @@ impl Editor {
gutter_hovered: false,
pixel_position_of_newest_cursor: None,
last_bounds: None,
last_position_map: None,
expect_bounds_change: None,
gutter_dimensions: GutterDimensions::default(),
style: None,
Expand Down Expand Up @@ -14095,7 +14097,7 @@ impl Editor {
.and_then(|item| item.to_any().downcast_ref::<T>())
}

fn character_size(&self, window: &mut Window) -> gpui::Point<Pixels> {
fn character_size(&self, window: &mut Window) -> gpui::Size<Pixels> {
let text_layout_details = self.text_layout_details(window);
let style = &text_layout_details.editor_style;
let font_id = window.text_system().resolve_font(&style.text.font());
Expand All @@ -14109,7 +14111,7 @@ impl Editor {
.size
.width;

gpui::Point::new(em_width, line_height)
gpui::Size::new(em_width, line_height)
}
}

Expand Down Expand Up @@ -15610,9 +15612,9 @@ impl EntityInputHandler for Editor {
cx: &mut Context<Self>,
) -> Option<gpui::Bounds<Pixels>> {
let text_layout_details = self.text_layout_details(window);
let gpui::Point {
x: em_width,
y: line_height,
let gpui::Size {
width: em_width,
height: line_height,
} = self.character_size(window);

let snapshot = self.snapshot(window, cx);
Expand All @@ -15630,6 +15632,24 @@ impl EntityInputHandler for Editor {
size: size(em_width, line_height),
})
}

fn character_index_for_point(
&mut self,
point: gpui::Point<Pixels>,
_window: &mut Window,
_cx: &mut Context<Self>,
) -> Option<usize> {
let position_map = self.last_position_map.as_ref()?;
if !position_map.text_hitbox.contains(&point) {
return None;
}
let display_point = position_map.point_for_position(point).previous_valid;
let anchor = position_map
.snapshot
.display_point_to_anchor(display_point, Bias::Left);
let utf16_offset = anchor.to_offset_utf16(&position_map.snapshot.buffer_snapshot);
Some(utf16_offset.0)
}
}

trait SelectionExt {
Expand Down
Loading

0 comments on commit 43ccb51

Please sign in to comment.