Skip to main content

euv_core/system/text_edit/
impl.rs

1use super::*;
2
3/// Range helpers for `TextSelection`.
4impl TextSelection {
5    /// Creates a collapsed caret selection at `byte_offset`.
6    ///
7    /// # Arguments
8    ///
9    /// - `usize` - Caret UTF-8 byte offset.
10    ///
11    /// # Returns
12    ///
13    /// - `Self` - Collapsed selection where `anchor == focus == byte_offset`.
14    #[must_use]
15    pub const fn collapsed(byte_offset: usize) -> Self {
16        Self {
17            anchor: byte_offset,
18            focus: byte_offset,
19        }
20    }
21
22    /// Returns the smaller (inclusive start) UTF-8 byte offset of the selection.
23    ///
24    /// # Returns
25    ///
26    /// - `usize` - Ordered start byte offset.
27    #[must_use]
28    pub fn start(&self) -> usize {
29        self.get_anchor().min(self.get_focus())
30    }
31
32    /// Returns the larger (exclusive end) UTF-8 byte offset of the selection.
33    ///
34    /// # Returns
35    ///
36    /// - `usize` - Ordered end byte offset.
37    #[must_use]
38    pub fn end(&self) -> usize {
39        self.get_anchor().max(self.get_focus())
40    }
41
42    /// Returns `true` when the selection has zero width (`anchor == focus`).
43    ///
44    /// # Returns
45    ///
46    /// - `bool` - `true` when collapsed to a caret.
47    #[must_use]
48    pub fn is_collapsed(&self) -> bool {
49        self.get_anchor() == self.get_focus()
50    }
51
52    /// Returns the selected byte length (`end - start`).
53    ///
54    /// # Returns
55    ///
56    /// - `usize` - Selected UTF-8 byte length.
57    #[must_use]
58    pub fn byte_len(&self) -> usize {
59        self.end().saturating_sub(self.start())
60    }
61
62    /// Returns a copy of this selection with both endpoints snapped to valid UTF-8 boundaries in `text`.
63    ///
64    /// # Arguments
65    ///
66    /// - `&str` - Backing UTF-8 text buffer.
67    ///
68    /// # Returns
69    ///
70    /// - `Self` - UTF-8 boundary-safe selection.
71    #[must_use]
72    pub fn clamped(&self, text: &str) -> Self {
73        Self::new(
74            clamp_utf8_boundary(text, self.get_anchor()),
75            clamp_utf8_boundary(text, self.get_focus()),
76        )
77    }
78}
79
80/// Composition update and sanitization methods for `ImePreeditState`.
81impl ImePreeditState {
82    /// Starts or updates an active IME preedit composition string, cursor range, underline clauses, and candidate anchor rectangle.
83    ///
84    /// # Arguments
85    ///
86    /// - `String` - Uncommitted UTF-8 composition string.
87    /// - `usize` - Inclusive UTF-8 cursor start offset within `composition`.
88    /// - `usize` - Exclusive UTF-8 cursor end offset within `composition`.
89    /// - `Vec<ImeUnderlineSpan>` - Clause underline spans within `composition`.
90    /// - `Rect` - Screen-space logical anchor rectangle for OS candidate popups.
91    pub fn update(
92        &mut self,
93        composition: String,
94        cursor_start_byte: usize,
95        cursor_end_byte: usize,
96        underlines: Vec<ImeUnderlineSpan>,
97        anchor_rect: Rect,
98    ) {
99        if composition.is_empty() {
100            self.clear();
101            self.set_candidate_anchor_rect_safe(anchor_rect);
102            return;
103        }
104        let clamped_start: usize = clamp_utf8_boundary(&composition, cursor_start_byte);
105        let clamped_end: usize =
106            clamp_utf8_boundary(&composition, cursor_end_byte).max(clamped_start);
107        let clean_underlines: Vec<ImeUnderlineSpan> = underlines
108            .into_iter()
109            .filter_map(|span: ImeUnderlineSpan| {
110                let start: usize = clamp_utf8_boundary(&composition, span.get_start_byte());
111                let end: usize = clamp_utf8_boundary(&composition, span.get_end_byte());
112                if end > start {
113                    Some(ImeUnderlineSpan::new(start, end, span.get_style()))
114                } else {
115                    None
116                }
117            })
118            .collect();
119        self.set_active(true);
120        self.set_composition(composition);
121        self.set_cursor_start_byte(clamped_start);
122        self.set_cursor_end_byte(clamped_end);
123        self.set_underlines(clean_underlines);
124        self.set_candidate_anchor_rect_safe(anchor_rect);
125    }
126
127    /// Updates `candidate_anchor_rect` with finite non-negative dimensions.
128    ///
129    /// # Arguments
130    ///
131    /// - `Rect` - Screen-space candidate anchor rectangle.
132    pub fn set_candidate_anchor_rect_safe(&mut self, anchor_rect: Rect) {
133        let x_val: f32 = if anchor_rect.get_x().is_finite() {
134            anchor_rect.get_x()
135        } else {
136            0.0
137        };
138        let y_val: f32 = if anchor_rect.get_y().is_finite() {
139            anchor_rect.get_y()
140        } else {
141            0.0
142        };
143        let width_val: f32 = if anchor_rect.get_width().is_finite() {
144            anchor_rect.get_width().max(0.0)
145        } else {
146            0.0
147        };
148        let height_val: f32 = if anchor_rect.get_height().is_finite() {
149            anchor_rect.get_height().max(0.0)
150        } else {
151            0.0
152        };
153        self.set_candidate_anchor_rect(Rect::new(x_val, y_val, width_val, height_val));
154    }
155
156    /// Clears the active IME composition text, cursor offsets, and underline clauses while keeping the anchor rectangle.
157    pub fn clear(&mut self) {
158        self.set_active(false);
159        self.get_mut_composition().clear();
160        self.set_cursor_start_byte(0);
161        self.set_cursor_end_byte(0);
162        self.get_mut_underlines().clear();
163    }
164
165    /// Returns `true` when an active non-empty IME composition is in progress.
166    ///
167    /// # Returns
168    ///
169    /// - `bool` - `true` when actively composing preedit text.
170    #[must_use]
171    pub fn is_composing(&self) -> bool {
172        self.get_active() && !self.get_composition().is_empty()
173    }
174}
175
176/// Default empty `TextEditHistory` configured with standard Undo limit and coalesce window.
177impl Default for TextEditHistory {
178    /// Creates an empty `TextEditHistory`.
179    ///
180    /// # Returns
181    ///
182    /// - `Self` - Empty editor history buffer.
183    fn default() -> Self {
184        Self::with_limits(DEFAULT_TEXT_HISTORY_LIMIT, DEFAULT_TEXT_COALESCE_WINDOW_MS)
185    }
186}
187
188/// Coalesced Undo/Redo, UTF-8 word navigation, selection manipulation, and IME preedit operations for `TextEditHistory`.
189impl TextEditHistory {
190    /// Creates an empty `TextEditHistory` with explicit history capacity and coalesce window.
191    ///
192    /// # Arguments
193    ///
194    /// - `usize` - Maximum number of Undo snapshots retained (clamped to `>= 1`).
195    /// - `u64` - Coalescing time window in milliseconds (`0` disables coalescing).
196    ///
197    /// # Returns
198    ///
199    /// - `Self` - Configured empty `TextEditHistory`.
200    #[must_use]
201    pub fn with_limits(max_history: usize, coalesce_window_ms: u64) -> Self {
202        Self {
203            text: String::new(),
204            selection: TextSelection::collapsed(0),
205            undo_stack: VecDeque::new(),
206            redo_stack: Vec::new(),
207            max_history: max_history.max(1),
208            coalesce_window_ms,
209            last_edit_kind: TextEditKind::Insert,
210            last_edit_timestamp_ms: 0,
211            has_coalesce_anchor: false,
212            ime_preedit: ImePreeditState::default(),
213        }
214    }
215
216    /// Creates a `TextEditHistory` initialized with `initial_text` and caret positioned at the end.
217    ///
218    /// # Arguments
219    ///
220    /// - `TextType` - Initial UTF-8 text content.
221    ///
222    /// # Returns
223    ///
224    /// - `Self` - Initialized text editor history.
225    #[must_use]
226    pub fn from_text<TextType>(initial_text: TextType) -> Self
227    where
228        TextType: Into<String>,
229    {
230        let content: String = initial_text.into();
231        let end_offset: usize = content.len();
232        let mut history: Self = Self::default();
233        history.set_text(content);
234        history.set_selection(TextSelection::collapsed(end_offset));
235        history
236    }
237
238    /// Breaks the current Undo coalescing run so the next edit always records a fresh Undo point.
239    pub fn break_coalesce(&mut self) {
240        self.set_has_coalesce_anchor(false);
241    }
242
243    /// Pushes a pre-edit Undo snapshot unless the edit qualifies to coalesce with the top Undo entry.
244    ///
245    /// # Arguments
246    ///
247    /// - `TextEditKind` - Kind of edit being applied.
248    /// - `u64` - Monotonic timestamp in milliseconds.
249    /// - `bool` - Whether this individual edit is eligible for time-window coalescing.
250    fn record_undo_checkpoint(
251        &mut self,
252        edit_kind: TextEditKind,
253        timestamp_ms: u64,
254        coalescable: bool,
255    ) {
256        self.get_mut_redo_stack().clear();
257        let within_window: bool = timestamp_ms.saturating_sub(self.get_last_edit_timestamp_ms())
258            <= self.get_coalesce_window_ms();
259        let can_coalesce: bool = coalescable
260            && self.get_coalesce_window_ms() > 0
261            && self.get_has_coalesce_anchor()
262            && self.get_last_edit_kind() == edit_kind
263            && within_window
264            && !self.get_undo_stack().is_empty();
265        if !can_coalesce {
266            let snapshot: TextEditSnapshot = TextEditSnapshot::new(
267                self.get_text().clone(),
268                self.get_selection(),
269                edit_kind,
270                timestamp_ms,
271            );
272            let limit: usize = self.get_max_history();
273            let undo_stack: &mut VecDeque<TextEditSnapshot> = self.get_mut_undo_stack();
274            while undo_stack.len() >= limit {
275                let _: Option<TextEditSnapshot> = undo_stack.pop_front();
276            }
277            undo_stack.push_back(snapshot);
278        }
279        self.set_last_edit_kind(edit_kind);
280        self.set_last_edit_timestamp_ms(timestamp_ms);
281        self.set_has_coalesce_anchor(coalescable);
282    }
283
284    /// Inserts `inserted` at the active caret or replaces the active selection, coalescing rapid non-whitespace typing.
285    ///
286    /// # Arguments
287    ///
288    /// - `&str` - UTF-8 text to insert.
289    /// - `u64` - Monotonic edit timestamp in milliseconds.
290    pub fn insert_text(&mut self, inserted: &str, timestamp_ms: u64) {
291        if inserted.is_empty() && self.get_selection().is_collapsed() {
292            return;
293        }
294        let sel: TextSelection = self.get_selection().clamped(self.get_text());
295        let is_single_word_char: bool = sel.is_collapsed()
296            && inserted.chars().count() == 1
297            && inserted.chars().all(|ch: char| !ch.is_whitespace());
298        let kind: TextEditKind = if sel.is_collapsed() {
299            TextEditKind::Insert
300        } else {
301            TextEditKind::Replace
302        };
303        self.record_undo_checkpoint(kind, timestamp_ms, is_single_word_char);
304        let start: usize = sel.start();
305        let end: usize = sel.end();
306        self.get_mut_text().replace_range(start..end, inserted);
307        let next_caret: usize = start.saturating_add(inserted.len());
308        self.set_selection(TextSelection::collapsed(next_caret));
309    }
310
311    /// Replaces the current selection (or inserts at caret) as an uncoalesced Undo step.
312    ///
313    /// # Arguments
314    ///
315    /// - `&str` - Replacement UTF-8 text.
316    /// - `u64` - Monotonic edit timestamp in milliseconds.
317    pub fn replace_selection(&mut self, replacement: &str, timestamp_ms: u64) {
318        let sel: TextSelection = self.get_selection().clamped(self.get_text());
319        self.record_undo_checkpoint(TextEditKind::Replace, timestamp_ms, false);
320        let start: usize = sel.start();
321        let end: usize = sel.end();
322        self.get_mut_text().replace_range(start..end, replacement);
323        let next_caret: usize = start.saturating_add(replacement.len());
324        self.set_selection(TextSelection::collapsed(next_caret));
325    }
326
327    /// Deletes the active selection or the single UTF-8 character preceding the caret (Backspace).
328    ///
329    /// # Arguments
330    ///
331    /// - `u64` - Monotonic edit timestamp in milliseconds.
332    ///
333    /// # Returns
334    ///
335    /// - `bool` - `true` when the text buffer changed.
336    pub fn delete_backward(&mut self, timestamp_ms: u64) -> bool {
337        let sel: TextSelection = self.get_selection().clamped(self.get_text());
338        if !sel.is_collapsed() {
339            self.record_undo_checkpoint(TextEditKind::DeleteBackward, timestamp_ms, false);
340            let start: usize = sel.start();
341            let end: usize = sel.end();
342            self.get_mut_text().replace_range(start..end, "");
343            self.set_selection(TextSelection::collapsed(start));
344            return true;
345        }
346        let caret: usize = sel.get_focus();
347        if caret == 0 {
348            return false;
349        }
350        let prev_offset: usize = prev_char_boundary(self.get_text(), caret);
351        self.record_undo_checkpoint(TextEditKind::DeleteBackward, timestamp_ms, true);
352        self.get_mut_text().replace_range(prev_offset..caret, "");
353        self.set_selection(TextSelection::collapsed(prev_offset));
354        true
355    }
356
357    /// Deletes the active selection or the single UTF-8 character following the caret (Forward Delete).
358    ///
359    /// # Arguments
360    ///
361    /// - `u64` - Monotonic edit timestamp in milliseconds.
362    ///
363    /// # Returns
364    ///
365    /// - `bool` - `true` when the text buffer changed.
366    pub fn delete_forward(&mut self, timestamp_ms: u64) -> bool {
367        let sel: TextSelection = self.get_selection().clamped(self.get_text());
368        if !sel.is_collapsed() {
369            self.record_undo_checkpoint(TextEditKind::DeleteForward, timestamp_ms, false);
370            let start: usize = sel.start();
371            let end: usize = sel.end();
372            self.get_mut_text().replace_range(start..end, "");
373            self.set_selection(TextSelection::collapsed(start));
374            return true;
375        }
376        let caret: usize = sel.get_focus();
377        if caret >= self.get_text().len() {
378            return false;
379        }
380        let next_offset: usize = next_char_boundary(self.get_text(), caret);
381        self.record_undo_checkpoint(TextEditKind::DeleteForward, timestamp_ms, true);
382        self.get_mut_text().replace_range(caret..next_offset, "");
383        self.set_selection(TextSelection::collapsed(caret));
384        true
385    }
386
387    /// Deletes the active selection or the word preceding the caret (Option/Ctrl+Backspace).
388    ///
389    /// # Arguments
390    ///
391    /// - `u64` - Monotonic edit timestamp in milliseconds.
392    ///
393    /// # Returns
394    ///
395    /// - `bool` - `true` when at least one byte was deleted.
396    pub fn delete_word_backward(&mut self, timestamp_ms: u64) -> bool {
397        let sel: TextSelection = self.get_selection().clamped(self.get_text());
398        if !sel.is_collapsed() {
399            return self.delete_backward(timestamp_ms);
400        }
401        let caret: usize = sel.get_focus();
402        let word_start: usize = prev_word_boundary(self.get_text(), caret);
403        if word_start == caret {
404            return false;
405        }
406        self.record_undo_checkpoint(TextEditKind::DeleteBackward, timestamp_ms, false);
407        self.get_mut_text().replace_range(word_start..caret, "");
408        self.set_selection(TextSelection::collapsed(word_start));
409        true
410    }
411
412    /// Deletes the active selection or the word following the caret (Option/Ctrl+Delete).
413    ///
414    /// # Arguments
415    ///
416    /// - `u64` - Monotonic edit timestamp in milliseconds.
417    ///
418    /// # Returns
419    ///
420    /// - `bool` - `true` when at least one byte was deleted.
421    pub fn delete_word_forward(&mut self, timestamp_ms: u64) -> bool {
422        let sel: TextSelection = self.get_selection().clamped(self.get_text());
423        if !sel.is_collapsed() {
424            return self.delete_forward(timestamp_ms);
425        }
426        let caret: usize = sel.get_focus();
427        let word_end: usize = next_word_boundary(self.get_text(), caret);
428        if word_end == caret {
429            return false;
430        }
431        self.record_undo_checkpoint(TextEditKind::DeleteForward, timestamp_ms, false);
432        self.get_mut_text().replace_range(caret..word_end, "");
433        self.set_selection(TextSelection::collapsed(caret));
434        true
435    }
436
437    /// Returns the currently selected UTF-8 slice (empty when the selection is collapsed).
438    ///
439    /// # Returns
440    ///
441    /// - `&str` - Borrowed selected text slice.
442    #[must_use]
443    pub fn selected_text(&self) -> &str {
444        let sel: TextSelection = self.get_selection().clamped(self.get_text());
445        &self.get_text()[sel.start()..sel.end()]
446    }
447
448    /// Selects the entire committed text buffer (`0..text.len()`).
449    pub fn select_all(&mut self) {
450        let end: usize = self.get_text().len();
451        self.set_selection(TextSelection::new(0, end));
452        self.break_coalesce();
453    }
454
455    /// Moves the caret to `byte_offset` (clamped to UTF-8 boundaries), optionally extending the selection anchor.
456    ///
457    /// # Arguments
458    ///
459    /// - `usize` - Target UTF-8 byte offset.
460    /// - `bool` - When `true`, preserves the existing anchor to extend or shrink the selection.
461    pub fn set_cursor(&mut self, byte_offset: usize, extend_selection: bool) {
462        let clamped: usize = clamp_utf8_boundary(self.get_text(), byte_offset);
463        let anchor: usize = if extend_selection {
464            clamp_utf8_boundary(self.get_text(), self.get_selection().get_anchor())
465        } else {
466            clamped
467        };
468        self.set_selection(TextSelection::new(anchor, clamped));
469        self.break_coalesce();
470    }
471
472    /// Moves the caret one UTF-8 character left, or collapses to selection start when not extending.
473    ///
474    /// # Arguments
475    ///
476    /// - `bool` - When `true`, extends the active selection.
477    pub fn move_left(&mut self, extend_selection: bool) {
478        let sel: TextSelection = self.get_selection().clamped(self.get_text());
479        if !extend_selection && !sel.is_collapsed() {
480            self.set_selection(TextSelection::collapsed(sel.start()));
481            self.break_coalesce();
482            return;
483        }
484        let target: usize = prev_char_boundary(self.get_text(), sel.get_focus());
485        self.set_cursor(target, extend_selection);
486    }
487
488    /// Moves the caret one UTF-8 character right, or collapses to selection end when not extending.
489    ///
490    /// # Arguments
491    ///
492    /// - `bool` - When `true`, extends the active selection.
493    pub fn move_right(&mut self, extend_selection: bool) {
494        let sel: TextSelection = self.get_selection().clamped(self.get_text());
495        if !extend_selection && !sel.is_collapsed() {
496            self.set_selection(TextSelection::collapsed(sel.end()));
497            self.break_coalesce();
498            return;
499        }
500        let target: usize = next_char_boundary(self.get_text(), sel.get_focus());
501        self.set_cursor(target, extend_selection);
502    }
503
504    /// Moves the caret one word boundary left, optionally extending the selection.
505    ///
506    /// # Arguments
507    ///
508    /// - `bool` - When `true`, extends the active selection.
509    pub fn move_word_left(&mut self, extend_selection: bool) {
510        let sel: TextSelection = self.get_selection().clamped(self.get_text());
511        let target: usize = prev_word_boundary(self.get_text(), sel.get_focus());
512        self.set_cursor(target, extend_selection);
513    }
514
515    /// Moves the caret one word boundary right, optionally extending the selection.
516    ///
517    /// # Arguments
518    ///
519    /// - `bool` - When `true`, extends the active selection.
520    pub fn move_word_right(&mut self, extend_selection: bool) {
521        let sel: TextSelection = self.get_selection().clamped(self.get_text());
522        let target: usize = next_word_boundary(self.get_text(), sel.get_focus());
523        self.set_cursor(target, extend_selection);
524    }
525
526    /// Restores the most recent Undo snapshot and pushes the current buffer state onto the Redo stack.
527    ///
528    /// # Returns
529    ///
530    /// - `bool` - `true` when an Undo step was applied.
531    pub fn undo(&mut self) -> bool {
532        let Some(previous) = self.get_mut_undo_stack().pop_back() else {
533            return false;
534        };
535        let current_snapshot: TextEditSnapshot = TextEditSnapshot::new(
536            self.get_text().clone(),
537            self.get_selection(),
538            previous.get_edit_kind(),
539            previous.get_timestamp_ms(),
540        );
541        self.get_mut_redo_stack().push(current_snapshot);
542        let restored_sel: TextSelection = previous.get_selection().clamped(previous.get_text());
543        self.set_text(previous.get_text().clone());
544        self.set_selection(restored_sel);
545        self.break_coalesce();
546        true
547    }
548
549    /// Re-applies the most recently undone snapshot from the Redo stack.
550    ///
551    /// # Returns
552    ///
553    /// - `bool` - `true` when a Redo step was applied.
554    pub fn redo(&mut self) -> bool {
555        let Some(next_state) = self.get_mut_redo_stack().pop() else {
556            return false;
557        };
558        let current_snapshot: TextEditSnapshot = TextEditSnapshot::new(
559            self.get_text().clone(),
560            self.get_selection(),
561            next_state.get_edit_kind(),
562            next_state.get_timestamp_ms(),
563        );
564        let limit: usize = self.get_max_history();
565        let undo_stack: &mut VecDeque<TextEditSnapshot> = self.get_mut_undo_stack();
566        while undo_stack.len() >= limit {
567            let _: Option<TextEditSnapshot> = undo_stack.pop_front();
568        }
569        undo_stack.push_back(current_snapshot);
570        let restored_sel: TextSelection = next_state.get_selection().clamped(next_state.get_text());
571        self.set_text(next_state.get_text().clone());
572        self.set_selection(restored_sel);
573        self.break_coalesce();
574        true
575    }
576
577    /// Returns `true` when at least one Undo step is available.
578    ///
579    /// # Returns
580    ///
581    /// - `bool` - `true` if `undo()` will restore a prior state.
582    #[must_use]
583    pub fn can_undo(&self) -> bool {
584        !self.get_undo_stack().is_empty()
585    }
586
587    /// Returns `true` when at least one Redo step is available.
588    ///
589    /// # Returns
590    ///
591    /// - `bool` - `true` if `redo()` will re-apply an undone state.
592    #[must_use]
593    pub fn can_redo(&self) -> bool {
594        !self.get_redo_stack().is_empty()
595    }
596
597    /// Updates the attached OS IME preedit composition state without mutating the committed text buffer.
598    ///
599    /// # Arguments
600    ///
601    /// - `String` - Uncommitted IME composition string.
602    /// - `usize` - Inclusive UTF-8 cursor start inside `composition`.
603    /// - `usize` - Exclusive UTF-8 cursor end inside `composition`.
604    /// - `Vec<ImeUnderlineSpan>` - Clause underline spans.
605    /// - `Rect` - Screen-space candidate window anchor rectangle.
606    pub fn update_ime_preedit(
607        &mut self,
608        composition: String,
609        cursor_start_byte: usize,
610        cursor_end_byte: usize,
611        underlines: Vec<ImeUnderlineSpan>,
612        anchor_rect: Rect,
613    ) {
614        self.get_mut_ime_preedit().update(
615            composition,
616            cursor_start_byte,
617            cursor_end_byte,
618            underlines,
619            anchor_rect,
620        );
621    }
622
623    /// Clears active IME preedit state and commits `committed` as a discrete non-coalesced Undo step.
624    ///
625    /// # Arguments
626    ///
627    /// - `&str` - Final UTF-8 string committed by the OS IME.
628    /// - `u64` - Monotonic commit timestamp in milliseconds.
629    pub fn commit_ime_preedit(&mut self, committed: &str, timestamp_ms: u64) {
630        self.get_mut_ime_preedit().clear();
631        if committed.is_empty() && self.get_selection().is_collapsed() {
632            return;
633        }
634        let sel: TextSelection = self.get_selection().clamped(self.get_text());
635        self.record_undo_checkpoint(TextEditKind::ImeCommit, timestamp_ms, false);
636        let start: usize = sel.start();
637        let end: usize = sel.end();
638        self.get_mut_text().replace_range(start..end, committed);
639        let next_caret: usize = start.saturating_add(committed.len());
640        self.set_selection(TextSelection::collapsed(next_caret));
641    }
642
643    /// Cancels any active IME preedit session without modifying committed text or Undo history.
644    pub fn cancel_ime_preedit(&mut self) {
645        self.get_mut_ime_preedit().clear();
646    }
647
648    /// Produces the full display string with any active IME preedit composition spliced at the current selection range.
649    ///
650    /// # Returns
651    ///
652    /// - `String` - Renderable text containing uncommitted IME composition inline.
653    #[must_use]
654    pub fn composed_display_text(&self) -> String {
655        if !self.get_ime_preedit().is_composing() {
656            return self.get_text().clone();
657        }
658        let sel: TextSelection = self.get_selection().clamped(self.get_text());
659        let composition: &str = self.get_ime_preedit().get_composition();
660        let mut combined: String =
661            String::with_capacity(self.get_text().len().saturating_add(composition.len()));
662        combined.push_str(&self.get_text()[..sel.start()]);
663        combined.push_str(composition);
664        combined.push_str(&self.get_text()[sel.end()..]);
665        combined
666    }
667}