From 682712f132ff0187a4ff46548fec30f0f396e698 Mon Sep 17 00:00:00 2001
From: Antonio Scandurra <me@as-cii.com>
Date: Fri, 24 Nov 2023 18:32:48 +0100
Subject: [PATCH] Account for previous line lengths when returning index

---
 crates/gpui2/src/elements/text.rs | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/crates/gpui2/src/elements/text.rs b/crates/gpui2/src/elements/text.rs
index c81d4ff0ed..a0715b81a9 100644
--- a/crates/gpui2/src/elements/text.rs
+++ b/crates/gpui2/src/elements/text.rs
@@ -244,13 +244,17 @@ impl TextState {
 
         let line_height = element_state.line_height;
         let mut line_origin = bounds.origin;
+        let mut line_start_ix = 0;
         for line in &element_state.lines {
             let line_bottom = line_origin.y + line.size(line_height).height;
             if position.y > line_bottom {
                 line_origin.y = line_bottom;
+                line_start_ix += line.len() + 1;
             } else {
                 let position_within_line = position - line_origin;
-                return line.index_for_position(position_within_line, line_height);
+                let index_within_line =
+                    line.index_for_position(position_within_line, line_height)?;
+                return Some(line_start_ix + index_within_line);
             }
         }