From 3255bb544ea104001be4ce1a116b02d4b0d053d9 Mon Sep 17 00:00:00 2001 From: temportalflux Date: Fri, 5 Jun 2026 17:55:39 -0400 Subject: [PATCH] replace InputState::content with a SharedString --- crates/gpui_elements/src/input.rs | 11 +++++++++++ crates/gpui_elements/src/input/history.rs | 8 +++++--- crates/gpui_elements/src/input/state.rs | 15 ++++++--------- .../src/input/state_input_handler.rs | 6 ++---- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/crates/gpui_elements/src/input.rs b/crates/gpui_elements/src/input.rs index ae67ff0e12..e4828739c5 100644 --- a/crates/gpui_elements/src/input.rs +++ b/crates/gpui_elements/src/input.rs @@ -15,3 +15,14 @@ pub use element::*; pub(self) use history::*; pub use layout::*; pub use state::*; + +pub(self) fn replace_range( + string: &mut gpui::SharedString, + range: std::ops::Range, + replace_with: &str, +) { + // NOTE: reallocates the SharedString bc SharedString is immutable + let mut content = string.to_string(); + content.replace_range(range, replace_with); + *string = content.into(); +} diff --git a/crates/gpui_elements/src/input/history.rs b/crates/gpui_elements/src/input/history.rs index bc4add269e..40128d21fc 100644 --- a/crates/gpui_elements/src/input/history.rs +++ b/crates/gpui_elements/src/input/history.rs @@ -3,6 +3,8 @@ use std::{ time::{Duration, Instant}, }; +use gpui::SharedString; + /// Maximum number of history entries to keep. pub const MAX_HISTORY_LEN: usize = 1000; @@ -29,7 +31,7 @@ pub struct HistoryEntry { impl HistoryEntry { /// Apply this patch to undo an edit, returning the reverse patch for redo. - pub fn apply_undo(&self, content: &mut String) -> HistoryEntry { + pub fn apply_undo(&self, content: &mut SharedString) -> HistoryEntry { let undo_start = self.range.start; let undo_end = (self.range.start + self.new_text_len).min(content.len()); @@ -37,7 +39,7 @@ impl HistoryEntry { let removed_text = content[undo_start..undo_end].to_string(); // Replace with the old text - content.replace_range(undo_start..undo_end, &self.old_text); + crate::input::replace_range(content, undo_start..undo_end, &self.old_text); // Return reverse patch for redo HistoryEntry { @@ -51,7 +53,7 @@ impl HistoryEntry { } /// Apply this patch to redo an edit, returning the reverse patch for undo. - pub fn apply_redo(&self, content: &mut String) -> HistoryEntry { + pub fn apply_redo(&self, content: &mut SharedString) -> HistoryEntry { // Redo is the same operation as undo - we're reversing the undo self.apply_undo(content) } diff --git a/crates/gpui_elements/src/input/state.rs b/crates/gpui_elements/src/input/state.rs index c7767449e4..da4a10daa5 100644 --- a/crates/gpui_elements/src/input/state.rs +++ b/crates/gpui_elements/src/input/state.rs @@ -37,7 +37,7 @@ impl EventEmitter for InputState {} pub struct InputState { entity_id: EntityId, focus_handle: FocusHandle, - content: String, + pub(super) content: SharedString, placeholder: SharedString, pub(super) selected_range: Range, pub(super) selection_reversed: bool, @@ -105,7 +105,7 @@ impl InputState { let mut this = Self { entity_id: cx.entity_id(), focus_handle: cx.focus_handle(), - content: String::new(), + content: SharedString::default(), placeholder: SharedString::default(), selected_range: 0..0, selection_reversed: false, @@ -193,14 +193,10 @@ impl InputState { } /// Returns the current text content. - pub fn content(&self) -> &str { + pub fn content(&self) -> &SharedString { &self.content } - pub(super) fn content_mut(&mut self) -> &mut String { - &mut self.content - } - pub fn get_layout(&self) -> InputLayout { self.layout } @@ -208,7 +204,8 @@ impl InputState { /// Sets the text content, resetting selection to the beginning. /// This clears the undo/redo history. pub fn set_content(&mut self, content: impl AsRef, cx: &mut Context) { - self.content = self.layout.sanitize_content(content.as_ref()).to_string(); + let content = self.layout.sanitize_content(content.as_ref()); + self.content = content.to_string().into(); self.selected_range = 0..0; self.selection_reversed = false; self.marked_range = None; @@ -388,7 +385,7 @@ impl InputState { self.cached_utf16_len = Some(cached_len - removed_utf16_len + added_utf16_len); } - self.content.replace_range(range.clone(), &text_to_insert); + crate::input::replace_range(&mut self.content, range.clone(), &text_to_insert); self.selected_range = range.start + text_to_insert.len()..range.start + text_to_insert.len(); self.marked_range.take(); diff --git a/crates/gpui_elements/src/input/state_input_handler.rs b/crates/gpui_elements/src/input/state_input_handler.rs index 2a88a42e27..a9b9e3d481 100644 --- a/crates/gpui_elements/src/input/state_input_handler.rs +++ b/crates/gpui_elements/src/input/state_input_handler.rs @@ -74,8 +74,7 @@ impl EntityInputHandler for super::InputState { self.cached_utf16_len = Some(cached_len - removed_utf16_len + added_utf16_len); } - self.content_mut() - .replace_range(range.clone(), &text_to_insert); + crate::input::replace_range(&mut self.content, range.clone(), &text_to_insert); self.selected_range = range.start + text_to_insert.len()..range.start + text_to_insert.len(); self.marked_range.take(); @@ -113,8 +112,7 @@ impl EntityInputHandler for super::InputState { self.cached_utf16_len = Some(cached_len - removed_utf16_len + added_utf16_len); } - self.content_mut() - .replace_range(range.clone(), &text_to_insert); + crate::input::replace_range(&mut self.content, range.clone(), &text_to_insert); if !text_to_insert.is_empty() { self.marked_range = Some(range.start..range.start + text_to_insert.len());