Fix crash when the length of a line is greater than 1024 chars (#11536)

张小白 created

Close #11518 

Release Notes:

- N/A

Change summary

crates/editor/src/element.rs | 22 ++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)

Detailed changes

crates/editor/src/element.rs 🔗

@@ -4495,20 +4495,26 @@ fn layout_line(
 ) -> Result<ShapedLine> {
     let mut line = snapshot.line(row);
 
-    if line.len() > MAX_LINE_LEN {
-        let mut len = MAX_LINE_LEN;
-        while !line.is_char_boundary(len) {
-            len -= 1;
-        }
+    let len = {
+        let line_len = line.len();
+        if line_len > MAX_LINE_LEN {
+            let mut len = MAX_LINE_LEN;
+            while !line.is_char_boundary(len) {
+                len -= 1;
+            }
 
-        line.truncate(len);
-    }
+            line.truncate(len);
+            len
+        } else {
+            line_len
+        }
+    };
 
     cx.text_system().shape_line(
         line.into(),
         style.text.font_size.to_pixels(cx.rem_size()),
         &[TextRun {
-            len: snapshot.line_len(row) as usize,
+            len,
             font: style.text.font(),
             color: Hsla::default(),
             background_color: None,