replace InputState::content with a SharedString
This commit is contained in:
@@ -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<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();
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ impl EventEmitter<InputStateEvent> for InputState {}
|
||||
pub struct InputState {
|
||||
entity_id: EntityId,
|
||||
focus_handle: FocusHandle,
|
||||
content: String,
|
||||
pub(super) content: SharedString,
|
||||
placeholder: SharedString,
|
||||
pub(super) selected_range: Range<usize>,
|
||||
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<str>, cx: &mut Context<Self>) {
|
||||
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();
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user