From edf7cba153ebcc8ce710d427435aafe25c7a6e50 Mon Sep 17 00:00:00 2001 From: temportalflux Date: Sat, 6 Jun 2026 18:17:59 -0400 Subject: [PATCH] undo SharedString being the backbone for InputState in favor of retaining mutablity of std::String. Safer now that painting doesnt need actual access to the string itself. --- crates/gpui_elements/src/input.rs | 11 ----------- crates/gpui_elements/src/input/history.rs | 9 ++++----- crates/gpui_elements/src/input/state.rs | 16 ++++++++++++---- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/crates/gpui_elements/src/input.rs b/crates/gpui_elements/src/input.rs index e4828739c5..ae67ff0e12 100644 --- a/crates/gpui_elements/src/input.rs +++ b/crates/gpui_elements/src/input.rs @@ -15,14 +15,3 @@ 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 cc7fdcd02e..4e2f36f2a8 100644 --- a/crates/gpui_elements/src/input/history.rs +++ b/crates/gpui_elements/src/input/history.rs @@ -1,10 +1,9 @@ +use gpui::NavigationDirection; use std::{ ops::Range, time::{Duration, Instant}, }; -use gpui::{NavigationDirection, SharedString}; - /// Maximum number of history entries to keep. pub const MAX_HISTORY_LEN: usize = 1000; @@ -31,7 +30,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 SharedString) -> HistoryEntry { + pub fn apply_undo(&self, content: &mut String) -> HistoryEntry { let undo_start = self.range.start; let undo_end = (self.range.start + self.new_text_len).min(content.len()); @@ -39,7 +38,7 @@ impl HistoryEntry { let removed_text = content[undo_start..undo_end].to_string(); // Replace with the old text - crate::input::replace_range(content, undo_start..undo_end, &self.old_text); + content.replace_range(undo_start..undo_end, &self.old_text); // Return reverse patch for redo HistoryEntry { @@ -53,7 +52,7 @@ impl HistoryEntry { } /// Apply this patch to redo an edit, returning the reverse patch for undo. - pub fn apply_redo(&self, content: &mut SharedString) -> HistoryEntry { + pub fn apply_redo(&self, content: &mut String) -> 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 f882dfd0b9..e3173e8dc7 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: SharedString, + content: String, pub(super) selected_range: Range, pub(super) selection_direction: NavigationDirection, pub(super) marked_range: Option>, @@ -122,7 +122,7 @@ impl InputState { let mut this = Self { entity_id: cx.entity_id(), focus_handle: cx.focus_handle(), - content: SharedString::default(), + content: String::default(), selected_range: 0..0, selection_direction: NavigationDirection::Forward, marked_range: None, @@ -163,7 +163,7 @@ impl InputState { } /// Returns the current text content. - pub fn content(&self) -> &SharedString { + pub fn content(&self) -> &String { &self.content } @@ -269,10 +269,18 @@ impl InputState { } /// Configures how long the input will wait between user-input changes to create new logs in the history for undo/redo. + /// The interval by default is 300ms (defined by `DEFAULT_GROUP_INTERVAL`). pub fn set_history_group_interval(&mut self, interval: Duration) { self.history_grouping_interval = interval; } + /// Configures how long the input will wait between user-input changes to create new logs in the history for undo/redo. + /// The interval by default is 300ms (defined by `DEFAULT_GROUP_INTERVAL`). + pub fn with_history_group_interval(mut self, interval: Duration) -> Self { + self.set_history_group_interval(interval); + self + } + /// Returns whether undo is available based on the recorded states. pub fn is_undo_available(&self) -> bool { !self.undo_stack.is_empty() @@ -796,7 +804,7 @@ impl InputState { /// Replaces the provided utf-8 character range with the provided text pub(super) fn replace_range(&mut self, range: Range, text: &str) { - crate::input::replace_range(&mut self.content, range, &text); + self.content.replace_range(range, &text); } /// Pauses cursor blinking temporarily (e.g., during typing).