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.

This commit is contained in:
temportalflux
2026-07-11 09:31:07 -04:00
parent c37593343a
commit edf7cba153
3 changed files with 16 additions and 20 deletions
-11
View File
@@ -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<usize>,
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();
}
+4 -5
View File
@@ -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)
}
+12 -4
View File
@@ -37,7 +37,7 @@ impl EventEmitter<InputStateEvent> for InputState {}
pub struct InputState {
entity_id: EntityId,
focus_handle: FocusHandle,
content: SharedString,
content: String,
pub(super) selected_range: Range<usize>,
pub(super) selection_direction: NavigationDirection,
pub(super) marked_range: Option<Range<usize>>,
@@ -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<usize>, 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).