From d95ea90499d08c0b0859b40915a3230546be16f3 Mon Sep 17 00:00:00 2001
From: StaffEngineer <111751109+StaffEngineer@users.noreply.github.com>
Date: Sun, 24 Sep 2023 22:38:36 +0100
Subject: [PATCH] Add set_text workaround (#53)

* Add set_text workaround
---
 src/lib.rs | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/lib.rs b/src/lib.rs
index 8bf8f81..1bacfb6 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -81,6 +81,16 @@ impl CosmicEditor {
         editor.buffer_mut().lines.clear();
         match text {
             CosmicText::OneStyle(text) => {
+                // TODO: this is a workaround
+                // if `set_text` called with text length smaller than the current buffer cosmic-text panics
+                // due to cursor being out of bounds.
+                // Should be removed once https://github.com/StaffEngineer/bevy_cosmic_edit/issues/52 fixed.
+                if text == *"" {
+                    let mut current_cursor = editor.cursor();
+                    current_cursor.line = 0;
+                    current_cursor.index = 0;
+                    editor.set_cursor(current_cursor);
+                }
                 editor.buffer_mut().set_text(
                     font_system,
                     text.as_str(),
@@ -1087,11 +1097,11 @@ fn cosmic_edit_bevy_events(
             let mut is_return = false;
             if keys.just_pressed(KeyCode::Return) {
                 is_return = true;
-                is_edit = true;
                 if (max_lines.0 == 0 || editor.0.buffer().lines.len() < max_lines.0)
                     && (max_chars.0 == 0 || editor.get_text().len() < max_chars.0)
                 {
                     // to have new line on wasm rather than E
+                    is_edit = true;
                     editor.0.action(&mut font_system.0, Action::Insert('\n'));
                 }
             }
-- 
GitLab