input: Grouping input history to avoid IME placeholder. (#1375)

This commit is contained in:
Jason Lee 2025-10-14 21:27:23 +08:00 committed by GitHub
parent e56c7a39bf
commit 889382cba1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 4 deletions

View file

@ -27,6 +27,7 @@ pub struct History<I: HistoryItem> {
pub(crate) ignore: bool,
max_undo: usize,
group_interval: Option<Duration>,
grouping: bool,
unique: bool,
}
@ -43,6 +44,7 @@ where
version: 0,
max_undo: 1000,
group_interval: None,
grouping: false,
unique: false,
}
}
@ -66,10 +68,20 @@ where
self
}
/// Start grouping changes, this will prevent the version from being incremented until `end_grouping` is called.
pub fn start_grouping(&mut self) {
self.grouping = true;
}
/// End grouping changes, this will allow the version to be incremented again.
pub fn end_grouping(&mut self) {
self.grouping = false;
}
/// Increment the version number if the last change was made more than `GROUP_INTERVAL` milliseconds ago.
fn inc_version(&mut self) -> usize {
let t = Instant::now();
if Some(self.last_changed_at.elapsed()) > self.group_interval {
if !self.grouping && Some(self.last_changed_at.elapsed()) > self.group_interval {
self.version += 1;
}

View file

@ -1579,7 +1579,6 @@ impl InputState {
}
let old_text = text.slice(range.clone()).to_string();
let new_range = range.start..range.start + new_text.len();
self.history
@ -2150,6 +2149,7 @@ impl EntityInputHandler for InputState {
}
self.push_history(&old_text, &range, &new_text);
self.history.end_grouping();
if let Some(diagnostics) = self.mode.diagnostics_mut() {
diagnostics.reset(&self.text)
}
@ -2205,7 +2205,6 @@ impl EntityInputHandler for InputState {
}
}
self.push_history(&old_text, &range, new_text);
if let Some(diagnostics) = self.mode.diagnostics_mut() {
diagnostics.reset(&self.text)
}
@ -2228,7 +2227,8 @@ impl EntityInputHandler for InputState {
.into();
}
self.mode.update_auto_grow(&self.text_wrapper);
cx.emit(InputEvent::Change);
self.history.start_grouping();
self.push_history(&old_text, &range, new_text);
cx.notify();
}